yml2.py
author Volker Birk <vb@pep-project.org>
Mon, 04 Nov 2019 11:38:34 +0100
changeset 40 432ab62b2537
parent 31 d3dddb80d1f5
permissions -rw-r--r--
date
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 28
diff changeset
     1
# YML 2.6.0 language definition
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     2
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     3
# written by VB.
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     4
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     5
import re
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     6
from pyPEG import keyword, _and, _not
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     7
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     8
# pyPEG:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     9
#
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    10
#   basestring:     terminal symbol (characters)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    11
#   keyword:        terminal symbol (keyword)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    12
#   matchobj:       terminal symbols (regex, use for scanning symbols)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    13
#   function:       named non-terminal symbol, recursive definition
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    14
#                   if you don't want naming in output, precede name with an underscore
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    15
#   tuple:          production sequence
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    16
#   integer:        count in production sequence:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    17
#                    0: following element is optional
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    18
#                   -1: following element can be omitted or repeated endless
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    19
#                   -2: following element is required and can be repeated endless
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    20
#   list:           options, choose one of them
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
#   _not:           next element in production sequence is matched only if this would not
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    22
#   _and:           next element in production sequence is matched only if this would, too
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    23
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
newSyntax = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    26
def oldSyntax():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    27
    global newSyntax
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
    newSyntax = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
def _if(cond, val):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
    if cond:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
        return val
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    33
    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    34
        return ()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    35
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    36
def listing(x):     return x, -1, (",", x)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    37
r = re.compile
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    38
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    39
comment = [r(r"//.*"), r(r"/\*.*?\*/", re.S)]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    40
_symbol = r"(?=\D)\w(\w|:)*"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    41
symbol = r(_symbol, re.U)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    42
pointer = r(r"\*" + _symbol, re.U)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    43
ppointer = r(r"\*\*" + _symbol, re.U)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    44
macro = r(r"\%" + _symbol, re.U)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    45
reference = r(r"\&" + _symbol, re.U)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    46
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 28
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]'''
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 28
diff changeset
    48
NameChar = NameStartChar + r'''|-|\.|[0-9]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]'''
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    49
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 28
diff changeset
    50
_xmlSymbol = "(" + NameStartChar + ")(" + NameChar + ")*"
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    51
xmlSymbol = r(_xmlSymbol)
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 28
diff changeset
    52
aliasSymbol = r(r"-|(" + _xmlSymbol + r")")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    53
28
e9a51b1d5587 adding hex literals
Volker Birk <vb@pep-project.org>
parents: 23
diff changeset
    54
literal = [r(r'""".*?"""', re.S), r(r"'''.*?'''", re.S), r(r"""0x[a-f0-9]+|-?\d+\.\d*|-?\.\d+|-?\d+|".*?"|'.*?'""")]
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    55
filename = [("'", r(r"[^']*"), "'"), ('"', r(r'[^"]*'), '"'), r(r"[^\s;]+")]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    56
ws = r(r"\s+", re.U)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    57
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    58
def pyExp():        return "!", r(r"(!=|\\!|[^!])+"), "!"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    59
value = [literal, pyExp]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    60
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    61
def tagQuote():     return r(r"\].*|\<.*?\>")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    62
def lineQuote():    return r(r"\|.*")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    63
def quote():        return [r(r"\d*>.*"), (literal, 0, [";", "."])]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    64
def parm():         return [([xmlSymbol, pyExp, pointer, macro], "=", [value, pointer, symbol]), value, pointer]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    65
def parm_eq():      return [xmlSymbol, pyExp, pointer, macro], "=", [value, pointer, symbol]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    66
parm_eq.__name__ = "parm"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    67
_func = [symbol, ppointer, pointer, reference], _if(newSyntax, (-1, ("[", listing(parm), "]"))), 0, ("(", listing(parm), ")"), 0, listing(parm), -1, parm_eq
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    68
def pythonCall():   return keyword("python"), _func, [";", "."]
20
c936066cff62 keywords as values in decl parameters
Volker Birk <vb@pep.foundation>
parents: 14
diff changeset
    69
def declParm():     return [pointer, macro, xmlSymbol], 0, ("=", [literal, symbol])
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    70
def alias():        return keyword("alias"), aliasSymbol
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    71
def descend():      return r(r"[+@*]" + _symbol, re.U)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    72
def base():         return keyword("is"), symbol
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    73
def shape():        return symbol
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
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
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    75
def python():       return [r(r"!!.*?!!", re.S), r(r"!.*")]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    76
def operator():     return 0, keyword("define"), keyword("operator"), literal, keyword("as"), r(r".*")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    77
def constant():     return 0, keyword("define"), [pointer, symbol], "=", literal, 0, [";", "."]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    78
def in_ns():        return keyword("in"), xmlSymbol, [_decl, ("{", -2, _decl, "}")]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    79
_decl = keyword("decl"), listing(decl), [";", "."]
23
f2e8837da4e9 allowing || in line quotes
Volker Birk <vb@pep.foundation>
parents: 20
diff changeset
    80
def textsection():  return r(r'(\|\|(\>*)(.*?)\|\|(\>*))\s*$', re.S | re.M)
14
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    81
def textsectionu(): return r(r'(\>\>.*?\>\>)', re.S)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    82
def include():      return keyword("include"), 0, reverse, 0, [ktext, kxml], filename, 0, [";", "."]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    83
def func():         return _func, 0, content
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    84
def funclist():     return listing(func)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    85
_cmd = funclist, 0, [";", "."]
14
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
    86
_inner = [include, textsection, textsectionu, pythonCall, _cmd, quote, lineQuote, tagQuote, pyExp]
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    87
_cc = "{", -1, _inner, "}"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    88
def content_plain(): return [ (_l, 0, _p, 0, _b, 0, _cc), (_p, 0, _b, 0, _cc), (_b, 0, _cc), _cc ]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    89
content_plain.__name__ = "content"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    90
def func_plain():   return _func, 0, content_plain
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    91
func_plain.__name__ = "func"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    92
def flist_plain():  return -2, func_plain
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    93
flist_plain.__name__ = "funclist"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    94
def xbase():        return flist_plain
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    95
def generic():      return flist_plain
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    96
def subscript():    return flist_plain
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    97
def parentheses():  return "(", 0, funclist, ")"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    98
def fparm():        return flist_plain
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    99
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   100
_l = _if(newSyntax, ("<", listing(generic), ">"))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   101
_p = _if(not newSyntax, parentheses), _if(newSyntax, ("(", 0, listing(fparm), ")"))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   102
_b = (":", listing(xbase))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   103
_c = [_inner, _cc]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   104
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   105
def content():      return [ (_l, 0, _p, 0, _b, 0, _c), (_p, 0, _b, 0, _c), (_b, 0, _c), _c ]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   106
def reverse():      return keyword("reverse")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   107
def ktext():        return keyword("text")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   108
def kxml():         return keyword("xml")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   109
def ymlCStyle():    return -1, [_decl, in_ns, include, python, operator, constant, tagQuote, lineQuote, quote, _cmd]