]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
parser: use control_verb parser inline
authorJustin Viiret <justin.viiret@intel.com>
Thu, 2 Feb 2017 04:49:26 +0000 (15:49 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 26 Apr 2017 04:58:43 +0000 (14:58 +1000)
src/parser/Parser.rl
src/parser/control_verbs.h
src/parser/control_verbs.rl
src/parser/parse_error.cpp
src/parser/parse_error.h
unit/hyperscan/bad_patterns.txt

index 913eaa0e065369fbe5683ec3cd3153fbcfac1c8f..ea8e88a95b6c6a2c0946a30c880850a4bd1da75b 100644 (file)
@@ -560,13 +560,13 @@ unichar readUtf8CodePoint4c(const char *s) {
             throw LocatedParseError("(*UCP) must be at start of "
                                     "expression, encountered");
         };
-        'UTF16)' => {
-            throw LocatedParseError("(*UTF16) not supported");
-        };
-        'UTF32)' => {
-            throw LocatedParseError("(*UTF32) not supported");
-        };
-        any => {
+        # Use the control verb mini-parser to report an error for this
+        # unsupported/unknown verb.
+        [^)]+ ')' => {
+            ParseMode temp_mode;
+            assert(ts - 2 >= ptr); // parser needs the '(*' at the start too.
+            read_control_verbs(ts - 2, te, (ts - 2 - ptr), temp_mode);
+            assert(0); // Should have thrown a parse error.
             throw LocatedParseError("Unknown control verb");
         };
     *|;
@@ -1838,7 +1838,7 @@ unique_ptr<Component> parse(const char *ptr, ParseMode &globalMode) {
 
     // First, read the control verbs, set any global mode flags and move the
     // ptr forward.
-    p = read_control_verbs(p, pe, globalMode);
+    p = read_control_verbs(p, pe, 0, globalMode);
 
     const char *eof = pe;
     int cs;
index 9cf5b11696154a0adf1d93ebb9cef4230001a9a6..58934ec2ced3ce69b6195a4e959f13d847370e10 100644 (file)
 #ifndef CONTROL_VERBS_H
 #define CONTROL_VERBS_H
 
+#include "ue2common.h"
+
 namespace ue2 {
 
 struct ParseMode;
 
-const char *read_control_verbs(const char *ptr, const char *end,
+const char *read_control_verbs(const char *ptr, const char *end, size_t start,
                                ParseMode &mode);
 
 } // namespace ue2
index 7eb9b86c1cbec8dfd8b5bb22c0a969f83f028253..1d3e33a9aacda2db073281f9fa39031debbfc87b 100644 (file)
@@ -43,7 +43,7 @@ using namespace std;
 
 namespace ue2 {
 
-const char *read_control_verbs(const char *ptr, const char *end,
+const char *read_control_verbs(const char *ptr, const char *end, size_t start,
                                ParseMode &mode) {
     const char *p = ptr;
     const char *pe = end;
@@ -108,7 +108,7 @@ const char *read_control_verbs(const char *ptr, const char *end,
         %% write exec;
     } catch (LocatedParseError &error) {
         if (ts >= ptr && ts <= pe) {
-            error.locate(ts - ptr);
+            error.locate(ts - ptr + start);
         } else {
             error.locate(0);
         }
index 6245adb9f79b26767d44ce3f22abdb8210e513e1..e7f60b2645c445a418ae7de737ab92c737ed46c5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2017, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -44,9 +44,13 @@ ParseError::~ParseError() {}
 LocatedParseError::~LocatedParseError() {}
 
 void LocatedParseError::locate(size_t offset) {
+    if (finalized) {
+        return;
+    }
     std::ostringstream str;
     str << reason << " at index " << offset << ".";
     reason = str.str();
+    finalized = true;
 }
 
 }
index e727991dbedb7ed3a00c4f9e4e05ea49d16c2c2b..4556ed5e048704297031b41567568f3568529691 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2017, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -30,8 +30,8 @@
  * \brief Parse/Compile exceptions.
  */
 
-#ifndef PARSE_ERROR_H_A02047D1AA16C9
-#define PARSE_ERROR_H_A02047D1AA16C9
+#ifndef PARSE_ERROR_H
+#define PARSE_ERROR_H
 
 #include "util/compile_error.h"
 
@@ -44,22 +44,24 @@ class ParseError : public CompileError {
 public:
     // Note: 'why' should describe why the error occurred and end with a
     // full stop, but no line break.
-    explicit ParseError(const std::string &why) : CompileError(why) {}
+    explicit ParseError(std::string why) : CompileError(std::move(why)) {}
 
     ~ParseError() override;
 };
 
 class LocatedParseError : public ParseError {
 public:
-    explicit LocatedParseError(const std::string &why) : ParseError(".") {
-        reason = why; // don't use ParseError ctor
+    explicit LocatedParseError(std::string why) : ParseError(".") {
+        reason = std::move(why); // don't use ParseError ctor
     }
 
     ~LocatedParseError() override;
 
     void locate(size_t offset);
+private:
+    bool finalized = false; //!< true when locate() has been called.
 };
 
 } // namespace ue2
 
-#endif /* PARSE_ERROR_H_A02047D1AA16C9 */
+#endif /* PARSE_ERROR_H */
index 52287ec07a1a57424a386db96eb2b6622116e99a..d4de452aaf11696e24fb9877bce0eac015d2ce02 100644 (file)
@@ -92,7 +92,7 @@
 93:/a\o{777}/ #Value in \o{...} sequence is too large at index 1.
 94:/(*UTF16)foo/ #Unsupported control verb (*UTF16) at index 0.
 95:/(*BSR_UNICODE)abc/ #Unsupported control verb (*BSR_UNICODE) at index 0.
-96:/a+(*SKIP)b/ #Unknown control verb at index 4.
+96:/a+(*SKIP)b/ #Unknown control verb (*SKIP) at index 2.
 97:/foo(*/ #Invalid repeat at index 4.
 98:/[:\]:]/ #POSIX named classes are only supported inside a class at index 0.
 99:/[[:[:]/ #Invalid POSIX named class at index 1.