1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# This file is part of A Boneless Datastructure Language
# Copyright (C) 2020 Soni L.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import pyparsing
import abdl.exceptions
from abdl import _vm
def _build_syntax():
# pylint: disable=protected-access
from pyparsing import Suppress, Literal, Forward, CharsNotIn, StringEnd, Combine, Optional, Group, Word, srange, Empty
subtree = Forward()
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)
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.setParseAction(lambda toks: [_vm.RegexKey(toks)])
arrow = Literal("->")
arrow.setParseAction(lambda: [_vm.Arrow()])
identifier = Word(srange("[A-Za-z_]"), srange("[A-Za-z0-9_]"))
identifier.setParseAction(lambda toks: [_vm.Ident(toks)])
parameter = (Suppress("$") + skippable + identifier)
parameter.setParseAction(lambda toks: [_vm.Param(toks)])
type_ = (Suppress(":") + skippable + Optional(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.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.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()
BUILT_SYNTAX = _build_syntax()
|