]> granicus.if.org Git - re2c/commitdiff
- Improve error messages
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Sun, 1 Jan 2006 13:42:11 +0000 (13:42 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Sun, 1 Jan 2006 13:42:11 +0000 (13:42 +0000)
18 files changed:
actions.cc
bootstrap/scanner.cc
scanner.h
scanner.re
substr.cc
substr.h
test/error4.c [new file with mode: 0755]
test/error4.re [new file with mode: 0755]
test/error5.c [new file with mode: 0755]
test/error5.re [new file with mode: 0755]
test/error6.c [new file with mode: 0755]
test/error6.re [new file with mode: 0755]
test/error7.c [new file with mode: 0755]
test/error7.re [new file with mode: 0755]
test/error8.c [new file with mode: 0755]
test/error8.re [new file with mode: 0755]
test/error9.c [new file with mode: 0755]
test/error9.re [new file with mode: 0755]

index 22c8b994053e6440eacd46d593e6e723e41df62e..7ab2ad1f6e0a63496c81cf1fbf6b14c927177bb9 100644 (file)
@@ -515,6 +515,9 @@ RegExp *expr(Scanner &);
 
 uint Scanner::unescape(SubStr &s) const
 {
+       static const char * hex = "0123456789abcdef";
+       static const char * oct = "01234567";
+
        s.len--;
        uint c;
 
@@ -537,13 +540,18 @@ uint Scanner::unescape(SubStr &s) const
 
                case 'x':
                {
-                       static const char * hex = "0123456789abcdef";
-                       const char *p1, *p2;
+                       if (s.len < 2)
+                       {
+                               fatal(s.ofs()+s.len, "Illegal hexadecimal character code, two hexadecimal digits are required");
+                               return ~0;
+                       }
+                       
+                       const char *p1 = strchr(hex, tolower(s.str[0]));
+                       const char *p2 = strchr(hex, tolower(s.str[1]));
 
-                       if (s.len < 2 || !(p1 = strchr(hex, tolower(s.str[0]))) 
-                                     || !(p2 = strchr(hex, tolower(s.str[1]))))
+                       if (!p1 || !p2)
                        {
-                               fatal("Illegal hexadecimal character code");
+                               fatal(s.ofs()+(p1?1:0), "Illegal hexadecimal character code");
                                return ~0;
                        }
                        else
@@ -560,15 +568,20 @@ uint Scanner::unescape(SubStr &s) const
 
                case 'X':
                {
-                       static const char * hex = "0123456789abcdef";
-                       const char *p1, *p2, *p3, *p4;
+                       if (s.len < 4)
+                       {
+                               fatal(s.ofs()+s.len, "Illegal hexadecimal character code, four hexadecimal digits are required");
+                               return ~0;
+                       }
+                       
+                       const char *p1 = strchr(hex, tolower(s.str[0]));
+                       const char *p2 = strchr(hex, tolower(s.str[1]));
+                       const char *p3 = strchr(hex, tolower(s.str[2]));
+                       const char *p4 = strchr(hex, tolower(s.str[3]));
 
-                       if (s.len < 4 || !(p1 = strchr(hex, tolower(s.str[0]))) 
-                                     || !(p2 = strchr(hex, tolower(s.str[1])))
-                                     || !(p3 = strchr(hex, tolower(s.str[2])))
-                                     || !(p4 = strchr(hex, tolower(s.str[3]))))
+                       if (!p1 || !p2 || !p3 || !p4)
                        {
-                               fatal("Illegal hexadecimal character code");
+                               fatal(s.ofs()+(p1?1:0)+(p2?1:0)+(p3?1:0), "Illegal hexadecimal character code");
                                return ~0;
                        }
                        else
@@ -583,30 +596,40 @@ uint Scanner::unescape(SubStr &s) const
        
                                if (v >= nRealChars)
                                {
-                                       fatal("Illegal hexadecimal character code");
+                                       fatal(s.ofs(), "Illegal hexadecimal character code, out of range");
                                }
        
                                return v;
                        }
                }
 
-               case '0':
-               case '1':
-               case '2':
-               case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                {
-                       static const char * oct = "01234567";
-                       const char *p0, *p1, *p2;
+                       fatal(s.ofs()-1, "Illegal octal character code, first digit must be 0 thru 3");
+                       return ~0;
+               }
+
+               case '0':
+               case '1':
+               case '2':
+               case '3':
+               {
+                       if (s.len < 2)
+                       {
+                               fatal(s.ofs()+s.len, "Illegal octal character code, three octal digits are required");
+                               return ~0;
+                       }
+
+                       const char *p0 = strchr(oct, c);
+                       const char *p1 = strchr(oct, s.str[0]);
+                       const char *p2 = strchr(oct, s.str[1]);
 
-                       if (s.len < 2 || !(p0 = strchr(oct, c)) || c > '3'
-                                     || !(p1 = strchr(oct, s.str[0])) 
-                                     || !(p2 = strchr(oct, s.str[1])))
+                       if (!p0 || !p1 || !p2)
                        {
-                               fatal("Illegal octal character code");
+                               fatal(s.ofs()+(p1?1:0), "Illegal octal character code");
                                return ~0;
                        }
                        else
index 4a77627d154b899426ff9aa0c52d1c8c34679bef..f8c9d8a9d166c89772697ecd75e5ccd5b83163ed 100644 (file)
@@ -1,4 +1,4 @@
-/* Generated by re2c 0.10.0.dev on Sun Jan  1 01:08:57 2006 */
+/* Generated by re2c 0.10.0.dev on Sun Jan  1 14:19:20 2006 */
 #line 1 "scanner.re"
 /* $Id$ */
 #include <stdlib.h>
@@ -1296,13 +1296,18 @@ yy177:
 
 }
 
-void Scanner::fatal(const char *msg) const
+void Scanner::fatal(uint ofs, const char *msg) const
 {
-    std::cerr << "line " << tline << ", column " << (tchar + 1) << ": "
+    std::cerr << "line " << tline << ", column " << (tchar + ofs + 1) << ": "
                << msg << std::endl;
     exit(1);
 }
 
+void Scanner::fatal(const char *msg) const
+{
+    fatal(0, msg);
+}
+
 void Scanner::config(const Str* cfg, int num)
 {
        if (cfg->to_string() == "indent")
index 4074f2667e00d7f58ec41c2ee7c75b29e0327c8b..f951a108913d8ae3cd92b1ba4868836e640f8810 100644 (file)
--- a/scanner.h
+++ b/scanner.h
@@ -28,6 +28,7 @@ public:
        int echo(std::ostream&);
        int scan();
        void fatal(const char*) const;
+       void fatal(uint, const char*) const;
        void config(const Str*, const Str *);
        void config(const Str*, int);
        SubStr token() const;
index e3c438b26caa0b5b7c54449ffe60af0d206560ef..abb9d59213f9012ff4982464cfed7fbb521415b9 100644 (file)
@@ -304,13 +304,18 @@ value:
 */
 }
 
-void Scanner::fatal(char *msg) const
+void Scanner::fatal(uint ofs, const char *msg) const
 {
-    std::cerr << "line " << tline << ", column " << (tchar + 1) << ": "
+    std::cerr << "line " << tline << ", column " << (tchar + ofs + 1) << ": "
                << msg << std::endl;
     exit(1);
 }
 
+void Scanner::fatal(const char *msg) const
+{
+    fatal(0, msg);
+}
+
 void Scanner::config(const Str* cfg, int num)
 {
        if (cfg->to_string() == "indent")
index a891dcf4e8914b4173eb212309ffb455519a5cd9..67c5cacec685dcd30448952ec967e1fdf39701ea 100644 (file)
--- a/substr.cc
+++ b/substr.cc
@@ -32,18 +32,21 @@ bool operator==(const SubStr &s1, const SubStr &s2)
        return (bool) (s1.len == s2.len && memcmp(s1.str, s2.str, s1.len) == 0);
 }
 
-Str::Str(const SubStr& s) : SubStr(strndup(s.str, s.len), s.len)
+Str::Str(const SubStr& s)
+       : SubStr(strndup(s.str, s.len), s.len)
 {
        ;
 }
 
-Str::Str(Str& s) : SubStr(s.str, s.len)
+Str::Str(Str& s)
+       : SubStr(s.str, s.len)
 {
        s.str = NULL;
        s.len = 0;
 }
 
-Str::Str() : SubStr((char*) NULL, 0)
+Str::Str()
+       : SubStr((char*) NULL, 0)
 {
        ;
 }
@@ -59,4 +62,3 @@ Str::~Str()
 }
 
 } // end namespace re2c
-
index bbc78a559e848cdc12cfa248b3404796cdbfe43e..4393f9c80e467877158e1f4c3b5ad924c2fa72c2 100644 (file)
--- a/substr.h
+++ b/substr.h
@@ -13,6 +13,7 @@ class SubStr
 {
 public:
        const char * str;
+       const char * const org;
        uint         len;
 
 public:
@@ -23,10 +24,9 @@ public:
        SubStr(const SubStr&);
        virtual ~SubStr();
        void out(std::ostream&) const;
-       std::string to_string() const
-       {
-               return std::string(str, len);
-       }
+       std::string to_string() const;
+       uint ofs() const;
+
 #ifdef PEDANTIC
 protected:
        SubStr& operator = (const SubStr& oth);
@@ -54,28 +54,38 @@ inline std::ostream& operator<<(std::ostream& o, const SubStr* s)
 }
 
 inline SubStr::SubStr(const uchar *s, uint l)
-               : str((char*) s), len(l)
+               : str((char*)s), org((char*)s), len(l)
 { }
 
 inline SubStr::SubStr(const char *s, uint l)
-               : str(s), len(l)
+               : str(s), org(s), len(l)
 { }
 
 inline SubStr::SubStr(const char *s)
-               : str(s), len(strlen(s))
+               : str(s), org(s), len(strlen(s))
 { }
 
 inline SubStr::SubStr(const SubStr &s)
-               : str(s.str), len(s.len)
+               : str(s.str), org(s.str), len(s.len)
 { }
 
 inline SubStr::~SubStr()
 { }
 
+inline std::string SubStr::to_string() const
+{
+       return std::string(str, len);
+}
+
+inline uint SubStr::ofs() const
+{
+       return str - org;
+}
+
 #ifdef PEDANTIC
 inline SubStr& SubStr::operator = (const SubStr& oth)
 {
-       str = oth.str;
+       new(this) SubStr(oth);
        return *this;
 }
 #endif
diff --git a/test/error4.c b/test/error4.c
new file mode 100755 (executable)
index 0000000..b9c46b8
--- /dev/null
@@ -0,0 +1,3 @@
+line 2, column 5: Illegal hexadecimal character code, two hexadecimal digits are required
+/* Generated by re2c */
+#line 1 "error4.re"
diff --git a/test/error4.re b/test/error4.re
new file mode 100755 (executable)
index 0000000..f136506
--- /dev/null
@@ -0,0 +1,3 @@
+/*!re2c
+[\x0] {}
+*/
diff --git a/test/error5.c b/test/error5.c
new file mode 100755 (executable)
index 0000000..6bcdc5a
--- /dev/null
@@ -0,0 +1,3 @@
+line 2, column 5: Illegal hexadecimal character code
+/* Generated by re2c */
+#line 1 "error5.re"
diff --git a/test/error5.re b/test/error5.re
new file mode 100755 (executable)
index 0000000..b63f5f9
--- /dev/null
@@ -0,0 +1,3 @@
+/*!re2c
+[\x0Z] {}
+*/
diff --git a/test/error6.c b/test/error6.c
new file mode 100755 (executable)
index 0000000..943d1a4
--- /dev/null
@@ -0,0 +1,3 @@
+line 2, column 7: Illegal hexadecimal character code, four hexadecimal digits are required
+/* Generated by re2c */
+#line 1 "error6.re"
diff --git a/test/error6.re b/test/error6.re
new file mode 100755 (executable)
index 0000000..1c588ec
--- /dev/null
@@ -0,0 +1,3 @@
+/*!re2c
+[\X123] {}
+*/
diff --git a/test/error7.c b/test/error7.c
new file mode 100755 (executable)
index 0000000..6456b1f
--- /dev/null
@@ -0,0 +1,3 @@
+line 2, column 3: Illegal octal character code, first digit must be 0 thru 3
+/* Generated by re2c */
+#line 1 "error7.re"
diff --git a/test/error7.re b/test/error7.re
new file mode 100755 (executable)
index 0000000..6ebb711
--- /dev/null
@@ -0,0 +1,3 @@
+/*!re2c
+[\400] {}
+*/
diff --git a/test/error8.c b/test/error8.c
new file mode 100755 (executable)
index 0000000..c862ec0
--- /dev/null
@@ -0,0 +1,3 @@
+line 2, column 4: Illegal octal character code
+/* Generated by re2c */
+#line 1 "error8.re"
diff --git a/test/error8.re b/test/error8.re
new file mode 100755 (executable)
index 0000000..b007922
--- /dev/null
@@ -0,0 +1,3 @@
+/*!re2c
+[\090] {}
+*/
diff --git a/test/error9.c b/test/error9.c
new file mode 100755 (executable)
index 0000000..24b5c6c
--- /dev/null
@@ -0,0 +1,3 @@
+line 2, column 5: Illegal octal character code
+/* Generated by re2c */
+#line 1 "error9.re"
diff --git a/test/error9.re b/test/error9.re
new file mode 100755 (executable)
index 0000000..d0d1f0c
--- /dev/null
@@ -0,0 +1,3 @@
+/*!re2c
+[\009] {}
+*/