summary refs log tree commit diff stats
path: root/abdl
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2020-02-03 17:59:46 -0300
committerSoniEx2 <endermoneymod@gmail.com>2020-02-03 17:59:56 -0300
commitc067e3258864d5c3bcaf03beee3ff6744cd03fae (patch)
treebe4afe68c46ee28d84ab0082b3c4f01d6bba02db /abdl
parente9f667fdb1cf4176d9bd62e7fbbecedb60cde008 (diff)
Fix things up feature/predicates
Diffstat (limited to 'abdl')
-rw-r--r--abdl/__init__.py3
-rw-r--r--abdl/_parser.py31
2 files changed, 21 insertions, 13 deletions
diff --git a/abdl/__init__.py b/abdl/__init__.py
index d751187..2c6b26a 100644
--- a/abdl/__init__.py
+++ b/abdl/__init__.py
@@ -137,6 +137,9 @@ from abdl import _parser
 from abdl import _vm
 from abdl import exceptions
 
+# backwards compatibility TODO: remove in 3.0.0
+from abdl.exceptions import ValidationError, DeprecationError, PatternError
+
 class Pattern:
     """A compiled pattern object.
 
diff --git a/abdl/_parser.py b/abdl/_parser.py
index c39a45e..3e179a2 100644
--- a/abdl/_parser.py
+++ b/abdl/_parser.py
@@ -16,7 +16,7 @@
 
 import pyparsing
 
-import abdl.exceptions
+from abdl import exceptions
 from abdl import _vm
 
 def _build_syntax():
@@ -27,14 +27,21 @@ def _build_syntax():
 
     skippable = Optional("?", default="")
 
-    str_literal = (Combine(Suppress("'")
-            + (Suppress("%") + ("'" | "%") | Literal("%") + (CharsNotIn("") | StringEnd()).setParseAction(abdl.exceptions.PatternError._str_escape) | CharsNotIn("%'"))[...]
-            + (Suppress("'") | StringEnd().setParseAction(abdl.exceptions.PatternError._str_end))) + skippable)
+    escape_char = Literal("%")
+    str_token = Literal("'")
+    re_token = Literal("/")
+
+    unexpected_token = CharsNotIn("", exact=1).setParseAction(exceptions.PatternError._unexpected_tok)
+    unexpected_end = StringEnd().setParseAction(exceptions.PatternError._unexpected_tok)
+
+    str_literal = (Combine(Suppress(str_token)
+            + (Suppress(escape_char) + (str_token | escape_char) | escape_char + CharsNotIn("", exact=1).setParseAction(exceptions.PatternError._str_escape) | CharsNotIn("%'"))[...]
+            + (Suppress(str_token) | StringEnd().setParseAction(exceptions.PatternError._str_end))) + skippable)
     str_literal.setParseAction(lambda toks: [_vm.StringKey(toks)])
 
-    re_literal = (Combine(Suppress("/")
-            + (Suppress("%") + ("/" | "%") | Literal("%") + (CharsNotIn("") | StringEnd()).setParseAction(abdl.exceptions.PatternError._re_escape) | CharsNotIn("%/"))[...]
-            + (Suppress("/") | StringEnd().setParseAction(abdl.exceptions.PatternError._re_end))) + skippable)
+    re_literal = (Combine(Suppress(re_token)
+            + (Suppress(escape_char) + (re_token | escape_char) | escape_char + CharsNotIn("", exact=1).setParseAction(exceptions.PatternError._re_escape) | CharsNotIn("%/"))[...]
+            + (Suppress(re_token) | StringEnd().setParseAction(exceptions.PatternError._re_end))) + skippable)
     re_literal.setParseAction(lambda toks: [_vm.RegexKey(toks)])
 
     arrow = Literal("->")
@@ -46,25 +53,23 @@ def _build_syntax():
     parameter = (Suppress("$") + skippable + identifier)
     parameter.setParseAction(lambda toks: [_vm.Param(toks)])
 
-    type_ = (Suppress(":") + skippable + Optional(Suppress("$")) + identifier)
+    type_ = (Suppress(":") + skippable + Suppress("$") + identifier)
     type_.setParseAction(lambda toks: [_vm.ApplyPredicate(toks)])
 
     # support for objects-as-keys
-    keysubtree = (Suppress("[")
-            + Group(type_[...] + subtree)
-            + (Suppress("]") | (CharsNotIn("") | StringEnd()).setParseAction(abdl.exceptions.PatternError._unexpected_tok)) + skippable)
+    keysubtree = (Suppress("[") + Group(type_[...] + subtree) + (Suppress("]") | unexpected_token | unexpected_end) + skippable)
     keysubtree.setParseAction(lambda toks: [_vm.KeySubtree(toks)])
 
     # represents key matching - switches from "key" to "value"
     tag = (identifier + Optional(parameter | str_literal | re_literal | keysubtree) | parameter | str_literal | re_literal | keysubtree) + type_[...] + Empty().setParseAction(lambda: [_vm.End()])
 
     # multiple value matching
-    valuesubtree = (Suppress("(") + Group(subtree) + (Suppress(")") | CharsNotIn("").setParseAction(abdl.exceptions.PatternError._unexpected_tok) | StringEnd().setParseAction(abdl.exceptions.PatternError._unexpected_tok)) + Optional("?", default=""))
+    valuesubtree = (Suppress("(") + Group(subtree) + (Suppress(")") | unexpected_token | unexpected_end) + Optional("?", default=""))
     valuesubtree.setParseAction(lambda toks: [_vm.ValueSubtree(toks)])
 
     # arrow and tag, value subtree
     subtree <<= (arrow + tag)[...] + (valuesubtree + Empty().setParseAction(lambda: [_vm.End()]))[...]
 
-    return ((subtree | CharsNotIn("").setParseAction(abdl.exceptions.PatternError._unexpected_tok)) + StringEnd()).parseWithTabs()
+    return ((subtree | unexpected_token) + StringEnd()).parseWithTabs()
 
 BUILT_SYNTAX = _build_syntax()