author | Hartmut Goebel <h.goebel@crazy-compilers.com> |
Thu, 27 Oct 2016 12:51:47 +0200 | |
changeset 42 | 700f4d003349 |
parent 41 | 98a53c3282c3 |
child 65 | d659b8c2ed22 |
child 72 | e52ee17bca47 |
permissions | -rw-r--r-- |
41
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
1 |
# YML 2.6.0 language definition |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
2 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
3 |
# written by VB. |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
4 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
5 |
import re |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
6 |
from .pyPEG import keyword, _and, _not |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
7 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
8 |
# pyPEG: |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
9 |
# |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
10 |
# basestring: terminal symbol (characters) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
11 |
# keyword: terminal symbol (keyword) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
12 |
# matchobj: terminal symbols (regex, use for scanning symbols) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
13 |
# function: named non-terminal symbol, recursive definition |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
14 |
# if you don't want naming in output, precede name with an underscore |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
15 |
# tuple: production sequence |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
16 |
# integer: count in production sequence: |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
17 |
# 0: following element is optional |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
18 |
# -1: following element can be omitted or repeated endless |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
19 |
# -2: following element is required and can be repeated endless |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
20 |
# list: options, choose one of them |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
21 |
# _not: next element in production sequence is matched only if this would not |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
22 |
# _and: next element in production sequence is matched only if this would, too |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
23 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
24 |
newSyntax = True |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
25 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
26 |
def oldSyntax(): |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
27 |
global newSyntax |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
28 |
newSyntax = False |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
29 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
30 |
def _if(cond, val): |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
31 |
if cond: |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
32 |
return val |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
33 |
else: |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
34 |
return () |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
35 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
36 |
def listing(x): return x, -1, (",", x) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
37 |
r = re.compile |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
38 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
39 |
comment = [r(r"//.*"), r(r"/\*.*?\*/", re.S)] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
40 |
_symbol = r"(?=\D)\w(\w|:)*" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
41 |
symbol = r(_symbol, re.U) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
42 |
pointer = r(r"\*" + _symbol, re.U) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
43 |
ppointer = r(r"\*\*" + _symbol, re.U) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
44 |
macro = r(r"\%" + _symbol, re.U) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
45 |
reference = r(r"\&" + _symbol, re.U) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
46 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
47 |
NameStartChar = r''':|[A-Z]|_|[a-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]''' |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
48 |
NameChar = NameStartChar + r'''|-|\.|[0-9]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]''' |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
49 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
50 |
_xmlSymbol = "(" + NameStartChar + ")(" + NameChar + ")*" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
51 |
xmlSymbol = r(_xmlSymbol) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
52 |
aliasSymbol = r(r"-|(" + _xmlSymbol + r")") |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
53 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
54 |
literal = [r(r'""".*?"""', re.S), r(r"'''.*?'''", re.S), r(r"""0x[a-f0-9]+|-?\d+\.\d*|-?\.\d+|-?\d+|".*?"|'.*?'""")] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
55 |
filename = [("'", r(r"[^']*"), "'"), ('"', r(r'[^"]*'), '"'), r(r"[^\s;]+")] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
56 |
ws = r(r"\s+", re.U) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
57 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
58 |
def pyExp(): return "!", r(r"(!=|\\!|[^!])+"), "!" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
59 |
value = [literal, pyExp] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
60 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
61 |
def tagQuote(): return r(r"\].*|\<.*?\>") |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
62 |
def lineQuote(): return r(r"\|.*") |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
63 |
def quote(): return [r(r"\d*>.*"), (literal, 0, [";", "."])] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
64 |
def parm(): return [([xmlSymbol, pyExp, pointer, macro], "=", [value, pointer, symbol]), value, pointer] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
65 |
def parm_eq(): return [xmlSymbol, pyExp, pointer, macro], "=", [value, pointer, symbol] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
66 |
parm_eq.__name__ = "parm" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
67 |
_func = [symbol, ppointer, pointer, reference], _if(newSyntax, (-1, ("[", listing(parm), "]"))), 0, ("(", listing(parm), ")"), 0, listing(parm), -1, parm_eq |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
68 |
def pythonCall(): return keyword("python"), _func, [";", "."] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
69 |
def declParm(): return [pointer, macro, xmlSymbol], 0, ("=", [literal, symbol]) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
70 |
def alias(): return keyword("alias"), aliasSymbol |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
71 |
def descend(): return r(r"[+@*]" + _symbol, re.U) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
72 |
def base(): return keyword("is"), symbol |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
73 |
def shape(): return symbol |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
74 |
def decl(): return symbol, 0, base, 0, ("<", listing(shape), ">"), -1, descend, _if(newSyntax, (-1, ("[", 0, listing(declParm), "]"))), 0, ("(", 0, listing(declParm), ")"), 0, alias, 0, content |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
75 |
def python(): return [r(r"!!.*?!!", re.S), r(r"!.*")] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
76 |
def operator(): return 0, keyword("define"), keyword("operator"), literal, keyword("as"), r(r".*") |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
77 |
def constant(): return 0, keyword("define"), [pointer, symbol], "=", literal, 0, [";", "."] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
78 |
def in_ns(): return keyword("in"), xmlSymbol, [_decl, ("{", -2, _decl, "}")] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
79 |
_decl = keyword("decl"), listing(decl), [";", "."] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
80 |
def textsection(): return r(r'(\|\|(\>*)(.*?)\|\|(\>*))\s*$', re.S | re.M) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
81 |
def textsectionu(): return r(r'(\>\>.*?\>\>)', re.S) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
82 |
def include(): return keyword("include"), 0, reverse, 0, [ktext, kxml], filename, 0, [";", "."] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
83 |
def func(): return _func, 0, content |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
84 |
def funclist(): return listing(func) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
85 |
_cmd = funclist, 0, [";", "."] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
86 |
_inner = [include, textsection, textsectionu, pythonCall, _cmd, quote, lineQuote, tagQuote, pyExp] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
87 |
_cc = "{", -1, _inner, "}" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
88 |
def content_plain(): return [ (_l, 0, _p, 0, _b, 0, _cc), (_p, 0, _b, 0, _cc), (_b, 0, _cc), _cc ] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
89 |
content_plain.__name__ = "content" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
90 |
def func_plain(): return _func, 0, content_plain |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
91 |
func_plain.__name__ = "func" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
92 |
def flist_plain(): return -2, func_plain |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
93 |
flist_plain.__name__ = "funclist" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
94 |
def xbase(): return flist_plain |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
95 |
def generic(): return flist_plain |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
96 |
def subscript(): return flist_plain |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
97 |
def parentheses(): return "(", 0, funclist, ")" |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
98 |
def fparm(): return flist_plain |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
99 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
100 |
_l = _if(newSyntax, ("<", listing(generic), ">")) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
101 |
_p = _if(not newSyntax, parentheses), _if(newSyntax, ("(", 0, listing(fparm), ")")) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
102 |
_b = (":", listing(xbase)) |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
103 |
_c = [_inner, _cc] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
104 |
|
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
105 |
def content(): return [ (_l, 0, _p, 0, _b, 0, _c), (_p, 0, _b, 0, _c), (_b, 0, _c), _c ] |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
106 |
def reverse(): return keyword("reverse") |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
107 |
def ktext(): return keyword("text") |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
108 |
def kxml(): return keyword("xml") |
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
diff
changeset
|
109 |
def ymlCStyle(): return -1, [_decl, in_ns, include, python, operator, constant, tagQuote, lineQuote, quote, _cmd] |