]> granicus.if.org Git - flex/commitdiff
Adjust yylineno properly when rewinding trailing contexts.
authorTill Varoquaux <till.varoquaux@gmail.com>
Tue, 2 Jul 2013 21:18:13 +0000 (17:18 -0400)
committerWill Estes <westes575@gmail.com>
Wed, 3 Jul 2013 00:26:45 +0000 (20:26 -0400)
configure.ac
flex.skl
nfa.c
tests/Makefile.am
tests/test-lineno-trailing/.gitignore [new file with mode: 0644]
tests/test-lineno-trailing/Makefile.am [new file with mode: 0644]
tests/test-lineno-trailing/scanner.l [new file with mode: 0644]
tests/test-lineno-trailing/test.input [new file with mode: 0644]

index f0a4fda87735b73c2e3ea3b37b3ead62f38cff95..e82f72f834f24a6cd7d271fceef0b54db3023b35 100644 (file)
@@ -146,6 +146,7 @@ tests/test-string-r/Makefile
 tests/test-yyextra/Makefile
 tests/test-alloc-extra/Makefile
 tests/test-lineno-nr/Makefile
+tests/test-lineno-trailing/Makefile
 tests/test-lineno-r/Makefile
 tests/test-linedir-r/Makefile
 tests/test-debug-r/Makefile
index aebf2ae67dcb9b33de6026881061034793f4f216..f878ff0a86185436e660f5424a239f04d4c2d7a0 100644 (file)
--- a/flex.skl
+++ b/flex.skl
@@ -503,9 +503,17 @@ m4_ifdef( [[M4_YY_NOT_IN_HEADER]],
                     if ( yytext[yyl] == '\n' )\
                         --yylineno;\
             }while(0)
+    #define YY_LINENO_REWIND_TO(dst) \
+            do {\
+                const char *p;\
+                for ( p = yy_cp-1; p >= (dst); --p)\
+                    if ( *p == '\n' )\
+                        --yylineno;\
+            }while(0)
     ]],
     [[
     #define YY_LESS_LINENO(n)
+    #define YY_LINENO_REWIND_TO(ptr)
     ]])
 ]])
 
diff --git a/nfa.c b/nfa.c
index dbd15578b77ceb9d8de82a33baa04ca8f15f88c3..26b162ce6f045be41e7830b42c8f7a489fd1cad9 100644 (file)
--- a/nfa.c
+++ b/nfa.c
@@ -257,12 +257,23 @@ void    finish_rule (mach, variable_trail_rule, headcnt, trailcnt,
                                ("*yy_cp = YY_G(yy_hold_char); /* undo effects of setting up yytext */\n");
 
                        if (headcnt > 0) {
+                               if (rule_has_nl[num_rules]) {
+                                       snprintf (action_text, sizeof(action_text),
+                                               "YY_LINENO_REWIND_TO(%s + %d);\n", scanner_bp, headcnt);
+                                       add_action (action_text);
+                               }
                                snprintf (action_text, sizeof(action_text), "%s = %s + %d;\n",
                                         scanner_cp, scanner_bp, headcnt);
                                add_action (action_text);
                        }
 
                        else {
+                               if (rule_has_nl[num_rules]) {
+                                       snprintf (action_text, sizeof(action_text),
+                                                "YY_LINENO_REWIND_TO(yy_cp - %d);\n", trailcnt);
+                                       add_action (action_text);
+                               }
+
                                snprintf (action_text, sizeof(action_text), "%s -= %d;\n",
                                         scanner_cp, trailcnt);
                                add_action (action_text);
index 33c20bdd731e1a5a4bdb972832c147e4224694d0..25d8b0c73ae756d23a83fba819b7f6c95db7284f 100644 (file)
@@ -53,6 +53,7 @@ DIST_SUBDIRS = \
        test-debug-r \
        test-lineno-r \
        test-lineno-nr \
+       test-lineno-trailing \
        test-linedir-r \
        TEMPLATE \
        test-top \
@@ -102,6 +103,7 @@ SUBDIRS = \
        test-debug-r \
        test-lineno-r \
        test-lineno-nr \
+       test-lineno-trailing \
        test-linedir-r \
        test-array-nr \
        test-array-r \
diff --git a/tests/test-lineno-trailing/.gitignore b/tests/test-lineno-trailing/.gitignore
new file mode 100644 (file)
index 0000000..84b3fa9
--- /dev/null
@@ -0,0 +1,8 @@
+Makefile
+Makefile.in
+parser.c
+parser.h
+scanner.c
+test-lineno-trailing
+OUTPUT
+.deps
diff --git a/tests/test-lineno-trailing/Makefile.am b/tests/test-lineno-trailing/Makefile.am
new file mode 100644 (file)
index 0000000..bb303c3
--- /dev/null
@@ -0,0 +1,45 @@
+# This file is part of flex.
+
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+
+# Neither the name of the University nor the names of its contributors
+# may be used to endorse or promote products derived from this software
+# without specific prior written permission.
+
+# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE.
+
+
+FLEX = $(top_builddir)/flex
+
+EXTRA_DIST = scanner.l test.input
+CLEANFILES = scanner.c $(testname)$(EXEEXT) OUTPUT $(OBJS)
+OBJS = scanner.o
+
+AM_CPPFLAGS = -I$(srcdir) -I$(top_srcdir) -I$(top_builddir)
+
+testname = test-lineno-trailing
+
+scanner.c: $(srcdir)/scanner.l
+       $(FLEX) $(LFLAGS) $<
+
+
+$(testname)$(EXEEXT): $(OBJS)
+       $(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(OBJS) $(LOADLIBES)
+
+test: $(testname)$(EXEEXT)
+       test `./$(testname)$(EXEEXT)   < $(srcdir)/test.input` -eq \
+            `./$(testname)$(EXEEXT) 1 < $(srcdir)/test.input` || exit 1
+
+.c.o:
+       $(CC) -c -o $@ $(AM_CPPFLAGS) $(CPPFLAGS) $(CFLAGS) $<
diff --git a/tests/test-lineno-trailing/scanner.l b/tests/test-lineno-trailing/scanner.l
new file mode 100644 (file)
index 0000000..20d3c8a
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * This file is part of flex.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ * Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
+%{
+/* A template scanner file to build "scanner.c".
+   Run as:
+          test-lineno-trailing    # report flex's yylineno
+          test-lineno-trailing 1  # report count_newlines(stdin)
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "config.h"
+
+%}
+
+%option 8bit outfile="scanner.c" prefix="test"
+%option nounput nomain noyywrap yylineno
+%option warn
+
+WORD [[:alpha:]]+
+
+%%
+    /* The goal here is to test the yylineno in the context of trailing-contexts.
+       Using rules that have newlines in look-ahead.
+    */
+"Fixed_trailing:"/[\n]"test"[\n] {}
+"Var_trailing:"{WORD}/[\n] {}
+"Var_prefix_and_trailing:"{WORD}":"/(\n{WORD})* {}
+\n                     {}
+.                      {}
+<<EOF>>  { printf("%d\n", yylineno);
+           yyterminate();
+         }
+
+%%
+
+/* returns number of '\n' characters in input, plus one.
+   This is what flex does, essentially. */
+   
+static int
+count_newlines (FILE* in)
+{
+    int n=1,c;
+    while ((c=fgetc(in)) != EOF)
+        if( c == '\n')
+            n++;
+    return n;
+}
+
+int main ( int, char**);
+
+int
+main ( argc,  argv )
+    int argc;
+    char** argv;
+{
+    if( argc > 1 )
+        printf("%d\n", count_newlines(stdin));
+
+    else{
+        yyin = stdin;
+        yyout = stdout;
+        yylex();
+    }
+    return 0;
+}
diff --git a/tests/test-lineno-trailing/test.input b/tests/test-lineno-trailing/test.input
new file mode 100644 (file)
index 0000000..201164d
--- /dev/null
@@ -0,0 +1,13 @@
+We are testing rules with trailing contexts containing newlines (see scanner.l):
+
+Fixed_trailing:
+test
+
+Var_trailing:word
+test
+
+Var_prefix_and_trailing:word:
+more
+text
+comes
+here