summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Worth <cworth@cworth.org>2010-06-01 12:18:43 -0700
committerCarl Worth <cworth@cworth.org>2010-06-01 12:18:43 -0700
commit2571415d1a7eec72db33cd521ca48fe755c43f9c (patch)
tree7eac576396e73cd06dcd8d0f7abe102e0b7f4821
parenta771a40e2257657cbdae0eb97a7bb8733db76b91 (diff)
Implement comment handling in the lexer (with test).
We support both single-line (//) and multi-line (/* ... */) comments and add a test for this, (trying to stress the rules just a bit by embedding one comment delimiter into a comment delimited with the other style, etc.). To keep the test suite passing we do now discard any output lines from glcpp that consist only of spacing, (in addition to blank lines as previously). We also discard any initial whitespace from gcc output. In neither case should the absence or presence of this whitespace affect correctness.
-rw-r--r--glcpp-lex.l11
-rw-r--r--tests/063-comments.c15
-rwxr-xr-xtests/glcpp-test4
3 files changed, 28 insertions, 2 deletions
diff --git a/glcpp-lex.l b/glcpp-lex.l
index a51d9e1..0954ab7 100644
--- a/glcpp-lex.l
+++ b/glcpp-lex.l
@@ -47,6 +47,17 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
%%
+ /* Single-line comments */
+"//"[^\n]+\n {
+ return NEWLINE;
+}
+
+ /* Multi-line comments */
+[/][*]([^*]*[*]+[^/])*[^*]*[*]*[/] {
+ if (yyextra->space_tokens)
+ return SPACE;
+}
+
{HASH}if/.*\n {
yyextra->lexing_if = 1;
yyextra->space_tokens = 0;
diff --git a/tests/063-comments.c b/tests/063-comments.c
new file mode 100644
index 0000000..4cda522
--- /dev/null
+++ b/tests/063-comments.c
@@ -0,0 +1,15 @@
+/* this is a comment */
+// so is this
+// */
+f = g/**//h;
+/*//*/l();
+m = n//**/o
++ p;
+/* this
+comment spans
+multiple lines and
+contains *** stars
+and slashes / *** /
+and other stuff.
+****/
+more code here
diff --git a/tests/glcpp-test b/tests/glcpp-test
index ba398af..2411033 100755
--- a/tests/glcpp-test
+++ b/tests/glcpp-test
@@ -3,8 +3,8 @@
for test in *.c; do
echo "Testing $test"
../glcpp < $test > $test.glcpp
- grep -v '^$' < $test.glcpp > $test.out || true
+ grep -v '^ *$' < $test.glcpp > $test.out || true
gcc -E $test -o $test.gcc
- grep -v '^#' < $test.gcc | grep -v '^$' > $test.expected || true
+ grep -v '^#' < $test.gcc | grep -v '^$' | sed -r -e 's/^ +/ /' > $test.expected || true
diff -u $test.expected $test.out
done