bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] scanner: reintroduce unput for missing end tokens


From: Theophile Ranquet
Subject: [PATCH] scanner: reintroduce unput for missing end tokens
Date: Wed, 12 Dec 2012 11:32:52 +0100

* src/scan-gram.l (unexpected_end): Here.
* tests/input.at: Adjust for new order of error reports.
---
 src/scan-gram.l | 111 +++++++++++++++++---------------------------------------
 tests/input.at  |  12 +++---
 2 files changed, 40 insertions(+), 83 deletions(-)

diff --git a/src/scan-gram.l b/src/scan-gram.l
index e6f42ea..a764bb9 100644
--- a/src/scan-gram.l
+++ b/src/scan-gram.l
@@ -519,23 +519,15 @@ eqopt    ([[:space:]]*=)?
 
 <SC_ESCAPED_STRING>
 {
-  "\""|"\n" {
-    if (yytext[0] == '\n')
-      unexpected_newline (token_start, "\"");
-    STRING_FINISH;
-    loc->start = token_start;
-    val->chars = last_string;
-    BEGIN INITIAL;
-    return STRING;
-  }
-  <<EOF>> {
-    unexpected_eof (token_start, "\"");
+  "\"" {
     STRING_FINISH;
     loc->start = token_start;
     val->chars = last_string;
     BEGIN INITIAL;
     return STRING;
   }
+  <<EOF>>   unexpected_eof (token_start, "\"");
+  "\n"      unexpected_newline (token_start, "\"");
 }
 
   /*----------------------------------------------------------.
@@ -545,49 +537,27 @@ eqopt    ([[:space:]]*=)?
 
 <SC_ESCAPED_CHARACTER>
 {
-  "'"|"\n" {
-    STRING_FINISH;
-    loc->start = token_start;
-    val->character = last_string[0];
-    {
-      /* FIXME: Eventually, make these errors.  */
-      if (last_string[0] == '\0')
-        {
-          complain (loc, Wother, _("empty character literal"));
-          /* '\0' seems dangerous even if we are about to complain.  */
-          val->character = '\'';
-        }
-      else if (last_string[1] != '\0')
-        complain (loc, Wother,
-                  _("extra characters in character literal"));
-    }
-    if (yytext[0] == '\n')
-      unexpected_newline (token_start, "'");
-    STRING_FREE;
-    BEGIN INITIAL;
-    return CHAR;
-  }
-  <<EOF>> {
+  "'" {
     STRING_FINISH;
     loc->start = token_start;
     val->character = last_string[0];
+
+    /* FIXME: Eventually, make these errors.  */
+    if (last_string[0] == '\0')
     {
-      /* FIXME: Eventually, make these errors.  */
-      if (last_string[0] == '\0')
-        {
-          complain (loc, Wother, _("empty character literal"));
-          /* '\0' seems dangerous even if we are about to complain.  */
-          val->character = '\'';
-        }
-      else if (last_string[1] != '\0')
-        complain (loc, Wother,
-                  _("extra characters in character literal"));
+      complain (loc, Wother, _("empty character literal"));
+      /* '\0' seems dangerous even if we are about to complain.  */
+      val->character = '\'';
     }
-    unexpected_eof (token_start, "'");
+    else if (last_string[1] != '\0')
+      complain (loc, Wother,
+                _("extra characters in character literal"));
     STRING_FREE;
     BEGIN INITIAL;
     return CHAR;
   }
+  "\n"      unexpected_newline (token_start, "'");
+  <<EOF>>   unexpected_eof (token_start, "'");
 }
 
   /*-----------------------------------------------------------.
@@ -614,15 +584,7 @@ eqopt    ([[:space:]]*=)?
   [^<>]+ STRING_GROW;
   "<"+   STRING_GROW; nesting += yyleng;
 
-  <<EOF>> {
-    unexpected_eof (token_start, ">");
-    STRING_FINISH;
-    loc->start = token_start;
-    val->uniqstr = uniqstr_new (last_string);
-    STRING_FREE;
-    BEGIN INITIAL;
-    return TAG;
-  }
+  <<EOF>>   unexpected_eof (token_start, ">");
 }
 
   /*----------------------------.
@@ -693,15 +655,15 @@ eqopt    ([[:space:]]*=)?
 <SC_CHARACTER>
 {
   "'"           STRING_GROW; BEGIN context_state;
-  \n            unexpected_newline (token_start, "'"); BEGIN context_state;
-  <<EOF>>       unexpected_eof (token_start, "'"); BEGIN context_state;
+  \n            unexpected_newline (token_start, "'");
+  <<EOF>>       unexpected_eof (token_start, "'");
 }
 
 <SC_STRING>
 {
   "\""          STRING_GROW; BEGIN context_state;
-  \n            unexpected_newline (token_start, "\""); BEGIN context_state;
-  <<EOF>>       unexpected_eof (token_start, "\""); BEGIN context_state;
+  \n            unexpected_newline (token_start, "\"");
+  <<EOF>>       unexpected_eof (token_start, "\"");
 }
 
 
@@ -752,15 +714,7 @@ eqopt    ([[:space:]]*=)?
      (as '<' '<%').  */
   "<"{splice}"<"  STRING_GROW;
 
-  <<EOF>> {
-    int token = (YY_START == SC_BRACED_CODE) ? BRACED_CODE : BRACED_PREDICATE;
-    unexpected_eof (code_start, "}");
-    STRING_FINISH;
-    loc->start = code_start;
-    val->code = last_string;
-    BEGIN INITIAL;
-    return token;
-  }
+  <<EOF>>   unexpected_eof (code_start, "}");
 }
 
 <SC_BRACED_CODE>
@@ -811,14 +765,7 @@ eqopt    ([[:space:]]*=)?
     return PROLOGUE;
   }
 
-  <<EOF>> {
-    unexpected_eof (code_start, "%}");
-    STRING_FINISH;
-    loc->start = code_start;
-    val->chars = last_string;
-    BEGIN INITIAL;
-    return PROLOGUE;
-  }
+  <<EOF>>   unexpected_eof (code_start, "%}");
 }
 
 
@@ -1001,8 +948,8 @@ handle_syncline (char *args, location loc)
 
 /*----------------------------------------------------------------.
 | For a token or comment starting at START, report message MSGID, |
-| which should say that an end marker was found before            |
-| the expected TOKEN_END.                                         |
+| which should say that an end marker was found before the        |
+| expected TOKEN_END. Then, pretend that TOKEN_END was found.     |
 `----------------------------------------------------------------*/
 
 static void
@@ -1011,6 +958,15 @@ unexpected_end (boundary start, char const *msgid, char 
const *token_end)
   location loc;
   loc.start = start;
   loc.end = scanner_cursor;
+  size_t i = strlen (token_end);
+
+/* Adjust scanner cursor so that any later message does not count
+     the characters about to be inserted.  */
+  scanner_cursor.column -= i;
+
+  while (i != 0)
+    unput (token_end[--i]);
+
   token_end = quote (token_end);
   // Instead of '\'', display "'".
   if (STREQ (token_end, "'\\''"))
@@ -1022,6 +978,7 @@ unexpected_end (boundary start, char const *msgid, char 
const *token_end)
 /*------------------------------------------------------------------------.
 | Report an unexpected EOF in a token or comment starting at START.       |
 | An end of file was encountered and the expected TOKEN_END was missing.  |
+| After reporting the problem, pretend that TOKEN_END was found.          |
 `------------------------------------------------------------------------*/
 
 static void
diff --git a/tests/input.at b/tests/input.at
index 5e5b1e8..70ad41b 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -1480,10 +1480,10 @@ AT_CHECK([[$PERL -e "print 'start: \'';" >> empty.y || 
exit 77]])
 
 AT_BISON_CHECK([empty.y], [1], [],
 [[empty.y:2.8-9: warning: empty character literal [-Wother]
-empty.y:3.8-4.0: warning: empty character literal [-Wother]
 empty.y:3.8-4.0: error: missing "'" at end of line
-empty.y:4.8: warning: empty character literal [-Wother]
+empty.y:3.8-4.0: warning: empty character literal [-Wother]
 empty.y:4.8: error: missing "'" at end of file
+empty.y:4.8: warning: empty character literal [-Wother]
 ]])
 
 AT_DATA([two.y],
@@ -1495,10 +1495,10 @@ AT_CHECK([[$PERL -e "print 'start: \'ab';" >> two.y || 
exit 77]])
 
 AT_BISON_CHECK([two.y], [1], [],
 [[two.y:2.8-11: warning: extra characters in character literal [-Wother]
-two.y:3.8-4.0: warning: extra characters in character literal [-Wother]
 two.y:3.8-4.0: error: missing "'" at end of line
-two.y:4.8-10: warning: extra characters in character literal [-Wother]
+two.y:3.8-4.0: warning: extra characters in character literal [-Wother]
 two.y:4.8-10: error: missing "'" at end of file
+two.y:4.8-10: warning: extra characters in character literal [-Wother]
 ]])
 
 AT_DATA([three.y],
@@ -1510,10 +1510,10 @@ AT_CHECK([[$PERL -e "print 'start: \'abc';" >> three.y 
|| exit 77]])
 
 AT_BISON_CHECK([three.y], [1], [],
 [[three.y:2.8-12: warning: extra characters in character literal [-Wother]
-three.y:3.8-4.0: warning: extra characters in character literal [-Wother]
 three.y:3.8-4.0: error: missing "'" at end of line
-three.y:4.8-11: warning: extra characters in character literal [-Wother]
+three.y:3.8-4.0: warning: extra characters in character literal [-Wother]
 three.y:4.8-11: error: missing "'" at end of file
+three.y:4.8-11: warning: extra characters in character literal [-Wother]
 ]])
 
 AT_CLEANUP
-- 
1.8.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]