diff options
Diffstat (limited to 'abdl/_parser.py')
-rw-r--r-- | abdl/_parser.py | 31 |
1 files changed, 18 insertions, 13 deletions
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() |