Permalink
Browse files

Fixed parsing problem with comments following preprocessor declarations

  • Loading branch information...
1 parent c0f1d23 commit 4253134bdc3b7b7d6f0fa1b77de8a162bc246134 @vsedach committed Aug 19, 2012
View
@@ -25,6 +25,7 @@
#:include-libc-file
;; runtime
+ #:load-c-file
#:run-c-program
))
View
@@ -63,10 +63,6 @@
(decf *line-number*))
(unread-char c %in))
-(defun c-read-line ()
- (incf *line-number*)
- (read-line %in))
-
(defmacro loop-reading (&body body)
`(loop with c do (setf c (c-read-char))
,@body))
@@ -79,20 +75,29 @@
finally (when c (c-unread-char c)))
string-buffer))
+(defun %maybe-read-comment ()
+ (case (peek-char nil %in)
+ (#\/ (incf *line-number*)
+ (read-line %in))
+ (#\* (slurp-while (let ((previous-char (code-char 0)))
+ (lambda (c)
+ (prog1 (not (and (char= previous-char #\*)
+ (char= c #\/)))
+ (setf previous-char c)))))
+ (c-read-char))))
+
+(defun read-c-comment (%in slash)
+ (declare (ignore slash))
+ (%maybe-read-comment)
+ (values))
+
(defun next-char (&optional (eof-error? t))
"Returns the next character, skipping over whitespace and comments"
(loop-reading
while (case c
- ((nil) (when eof-error?
- (read-error "Unexpected end of file")))
- (#\/ (case (peek-char nil %in)
- (#\/ (c-read-line))
- (#\* (slurp-while (let ((previous-char (code-char 0)))
- (lambda (c)
- (prog1 (not (and (char= previous-char #\*)
- (char= c #\/)))
- (setf previous-char c)))))
- (c-read-char))))
+ ((nil) (when eof-error?
+ (read-error "Unexpected end of file")))
+ (#\/ (%maybe-read-comment))
((#\Space #\Newline #\Tab) t))
finally (return c)))
@@ -182,6 +187,20 @@
(defvar preprocessor-if-stack ())
+(defun pp-read-line ()
+ (let (comment-follows?)
+ (prog1
+ (slurp-while (lambda (c)
+ (case c
+ (#\Newline)
+ (#\/ (if (find (peek-char nil %in nil nil) "/*")
+ (progn (setf comment-follows? t) nil)
+ t))
+ (t t))))
+ (c-read-char)
+ (when comment-follows?
+ (%maybe-read-comment)))))
+
(defmacro lookup-define ()
`(gethash (read-c-identifier (next-char)) *preprocessor-defines*))
@@ -190,7 +209,7 @@
(defun preprocessor-skip-branch ()
(let ((if-nest-depth 1))
- (loop for line = (c-read-line) do
+ (loop for line = (pp-read-line) do
(cond ((starts-with? line "#if")
(incf if-nest-depth))
((and (starts-with? line "#endif")
@@ -200,7 +219,7 @@
((and (starts-with? line "#elif")
(= 1 if-nest-depth))
(case (car preprocessor-if-stack)
- (if (when (preprocessor-test (c-read-line))
+ (if (when (preprocessor-test (pp-read-line))
(setf (car preprocessor-if-stack) 'elif)
(return)))
(elif nil)
@@ -255,21 +274,21 @@
(setf (lookup-define)
(if (eql #\( (peek-char t %in))
(let ((args (c-read-delimited-strings t))
- (template (string-trim '(#\Space #\Tab) (c-read-line))))
+ (template (string-trim '(#\Space #\Tab) (pp-read-line))))
(lambda (substitutions)
(if args
(fill-in-template args template substitutions)
template)))
- (c-read-line))))
+ (pp-read-line))))
(vacietis.c:undef
(remhash (read-c-identifier (next-char)) *preprocessor-defines*)
- (c-read-line))
+ (pp-read-line))
(vacietis.c:include
(let* ((delimiter
(case (next-char)
(#\" #\") (#\< #\>)
(otherwise (read-error "Error reading include path: ~A"
- (c-read-line)))))
+ (pp-read-line)))))
(include-file
(slurp-while (lambda (c) (char/= c delimiter)))))
(next-char)
@@ -282,7 +301,7 @@
(include-libc-file include-file))))
(vacietis.c:if
(push 'if preprocessor-if-stack)
- (unless (preprocessor-test (c-read-line))
+ (unless (preprocessor-test (pp-read-line))
(preprocessor-skip-branch)))
(vacietis.c:ifdef
(push 'if preprocessor-if-stack)
@@ -306,7 +325,7 @@
(preprocessor-skip-branch)
(read-error "Misplaced #elif")))
(otherwise ;; line, pragma, error ignored for now
- (c-read-line))))
+ (pp-read-line))))
nil)
;;; infix
@@ -635,6 +654,8 @@
(:macro-char #\# 'read-c-macro nil)
+ (:macro-char #\/ 'read-c-comment nil)
+
;; numbers (should this be here?)
,@(loop for i from 0 upto 9
collect `(:macro-char ,(digit-char i) 'read-c-toplevel nil))
View
@@ -3,3 +3,8 @@
(defun strerror (errnum)
(aref vacietis.libc.errno.h::errno-strings errnum))
+
+;; (eval-when (:compile-toplevel :load-toplevel)
+;; (load-c-file (merge-pathnames "string.c" (directory-namestring
+;; (or *load-truename*
+;; *compile-file-truename*)))))
View
@@ -16,4 +16,6 @@
(program-test if-then-else2 :return-code 5)
+(program-test different-comment-styles :return-code 9)
+
;; (program-test kr-echo :input "foobar" :output "foobar")
@@ -0,0 +1,10 @@
+// Here we have some comments
+/* another comment type
+
+ */
+
+#define FOOBAR 8 /* defines something */
+#define OTHER 1 // something else
+
+/* to finish up */
+// nah
@@ -0,0 +1,11 @@
+/* This file has comments */
+// This file has many comments
+
+#include "foobar.h" // lots of includes
+
+int main(void) { // main
+ /* note */ return FOOBAR + OTHER; /* doing stuff */
+ // !!! important
+}
+
+/* end of file */
View
@@ -39,7 +39,7 @@
(unwind-protect
(progn
(let ((*package* test-package))
- (vacietis.reader::load-c-file
+ (load-c-file
(merge-pathnames
(format nil "programs/~(~A~)/main.c" ',name)
(directory-namestring #.*compile-file-truename*))))

0 comments on commit 4253134

Please sign in to comment.