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