backend.py
author Volker Birk <vb@pep.foundation>
Tue, 20 Nov 2018 22:35:38 +0100
changeset 24 1cb8f7566a07
parent 22 3a2bd70c01df
child 25 cb4a7f8b230d
child 29 6a8a7951d8e6
permissions -rw-r--r--
Added tag 2.5.9 for changeset 8138b86a9196
20
c936066cff62 keywords as values in decl parameters
Volker Birk <vb@pep.foundation>
parents: 16
diff changeset
     1
# 2.5.8 backend
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, codecs
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     6
import fileinput
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     7
import sys, traceback, exceptions, os
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     8
from xml.sax.saxutils import escape, quoteattr
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     9
from copy import deepcopy
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    10
from glob import glob
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    11
from pyPEG import code, parse, parseLine, u, Symbol
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    12
from yml2 import ymlCStyle, comment, _inner
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    13
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    14
ymlFunc, pointers, pythonFunc = {}, {}, {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    15
in_ns = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    16
operator = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    17
included = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    18
includePath = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    19
emitlinenumbers = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    20
encoding = "utf-8"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    22
first = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    23
enable_tracing = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
ymlFunc["decl"] = "#error"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    26
ymlFunc["define"] = "#error"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    27
ymlFunc["operator"] = "#error"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
def clearAll():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
    global ymlFunc, pointers, pythonFunc, in_ns, operator, included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
    ymlFunc, pointers, pythonFunc = {}, {}, {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
    in_ns = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    33
    operator = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    34
    included = u""
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
lq = re.compile(r"\|(\>*)(.*)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    37
sq = re.compile(r"(\d*)\>(.*)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    38
ts = re.compile(r'(\|\|(?P<inds>\>*)\s*\n(?P<text1>.*?)\n(?P<base>\s*)\|\|)|("""(?P<text2>.*?)""")|(\>\>(?P<text3>.*?)\>\>)', re.S)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    39
tq = re.compile(r"(\]|\<)\s*(.*)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    40
bq = re.compile(r"\`(.*?)\`", re.S)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    41
bqq = re.compile(r"\s*\`\`(.*)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    42
all = re.compile(r".*", re.S)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    43
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    44
line = 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    45
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    46
def pointer(name):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    47
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    48
        return u(pointers[name[1:]])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    49
    except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    50
        if name == "*_trace_info":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    51
            return u'""'
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    52
        if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    53
            raise LookupError(u"in " + included + u":" + u(line) + u": pointer " + name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    54
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    55
            raise LookupError(u"in " + u(line) + u": pointer " + name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    56
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    57
def evalPython(expr):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    58
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    59
        result = eval(u(expr), pythonFunc)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    60
        if type(result) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    61
            return codecs.decode(result, encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    62
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    63
            return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    64
    except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    65
        name, parm, tb = sys.exc_info()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    66
        msg = u"in python expression: " + u(parm)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    67
        if name is exceptions.SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    68
            tbl = traceback.format_exception(name, parm, tb)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    69
            msg += u"\n" + tbl[-3] + tbl[-2]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    70
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    71
            msg += u": " + expr + u"\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    72
        if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    73
            raise name(u"in " + included + u":" + u(line) + u": " + msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    74
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    75
            raise name(u"in " + u(line) + u": " + msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    76
    
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    77
def execPython(script):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    78
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    79
        if type(script) is unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    80
            exec script in pythonFunc
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    81
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    82
            exec codecs.decode(script, encoding) in pythonFunc
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    83
    except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    84
        name, parm, tb = sys.exc_info()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    85
        msg = u"in python script: " + u(parm)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    86
        if name is exceptions.SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    87
            tbl = traceback.format_exception(name, parm, tb)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    88
            msg += u"\n" + tbl[-3] + tbl[-2]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    89
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    90
            msg += u": " + expr + u"\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    91
        if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    92
            raise name(u"in " + included + u":" + u(line) + u": " + msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    93
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    94
            raise name(u"in " + u(line) + u": " + msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    95
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    96
def textOut(text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    97
    if not text:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    98
        return u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    99
    if type(text) is not unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   100
        text = codecs.decode(text, encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   101
    text = text.replace(r'\"', r'\\"')
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   102
    text = u'u"""' + text.replace('"', r'\"') + u'"""'
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   103
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   104
        textFunc = ymlFunc["text"]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   105
        parms = ['text', ('parm', [text])]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   106
        c, result = textFunc(parms)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   107
        if c:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   108
            if type(textFunc.alias) is unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   109
                result += u"</" + textFunc.alias + u">"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   110
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   111
                result += u"</" + codecs.decode(textFunc.alias, encoding) + u">"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   112
        return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   113
    except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   114
        return escape(eval(text))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   115
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   116
def strRepl(text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   117
    if not text:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   118
        return u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   119
    if type(text) is not unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   120
        text = codecs.decode(text, encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   121
    text = text.replace(r'\"', r'\\"')
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   122
    text = u'u"""' + text.replace('"', r'\"') + u'"""'
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   123
    if type(text) is unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   124
        return escape(eval(text))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   125
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   126
def applyMacros(macros, text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   127
    result = text
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   128
    for key, value in macros.iteritems():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   129
        result = result.replace(key, value)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   130
    return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   131
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   132
class YF:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   133
    def __init__(self, name):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   134
        self.name = name
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   135
        self.parms = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   136
        self.descends = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   137
        self.values = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   138
        self.content = None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   139
        self.pointers = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   140
        self.macros = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   141
        if in_ns:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   142
            self.alias = in_ns + u":" + name.replace("_", "-")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   143
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   144
            self.alias = name.replace("_", "-")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   145
        pythonFunc["yml_" + name] = self
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   146
        if emitlinenumbers:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   147
            self.values["yml:declared"] = u(line)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   148
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   149
    def copy(self, newName):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   150
        yf = YF(newName)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   151
        yf.parms.extend(self.parms)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   152
        yf.descends.extend(self.descends)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   153
        yf.values = self.values.copy()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   154
        yf.content = self.content
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   155
        yf.pointers = self.pointers.copy()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   156
        yf.macros = self.macros.copy()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   157
        yf.alias = self.alias
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   158
        return yf
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   159
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   160
    def patch(self, second):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   161
        self.parms.extend(second.parms)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   162
        self.descends.extend(second.descends)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   163
        self.values.update(second.values)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   164
        if second.content:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   165
            self.content = second.content
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   166
        self.pointers.update(second.pointers)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   167
        self.macros.update(second.macros)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   168
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   169
    def __call__(self, called_with, hasContent = False, avoidTag = False):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   170
        global pointers
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   171
        parms = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   172
        vals = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   173
        if self.pointers:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   174
            pointers.update(self.pointers)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   175
   
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   176
        for data in called_with:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   177
            if type(data) is tuple or type(data) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   178
                if data[0] == "parm":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   179
                    l = data[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   180
                    parm = l[0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   181
                    if parm[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   182
                        parm = pointer(parm)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   183
                    if len(l) == 1:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   184
                        if type(parm) is tuple or type(parm) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   185
                            if parm[0] == "pyExp":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   186
                                val = evalPython(parm[1][0])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   187
                                parms.append(val)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   188
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   189
                            parms.append(evalPython((parm)))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   190
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   191
                        if type(parm) is tuple or type(parm) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   192
                            if parm[0] == "pyExp":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   193
                                parm = evalPython(parm[1][0])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   194
                        val = l[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   195
                        if type(val) is tuple or type(val) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   196
                            if val[0] == "pyExp":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   197
                                val = evalPython(val[1][0])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   198
                        if val[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   199
                            val = pointer(val)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   200
                        if u(val)[0] == '"' or u(val)[0] == "'":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   201
                            vals[parm] = evalPython(u(val))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   202
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   203
                            vals[parm] = u(val)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   204
                elif data[0] == "content":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   205
                    hasContent = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   206
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   207
        if enable_tracing:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   208
            text = u(parms) + u", " + u(vals)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   209
            pointers["_trace_info"] = u'"' + u(line) + u": " + u(self.name) + u" " + text.replace(u'"', u'#') + u'"'
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   210
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   211
        if emitlinenumbers:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   212
            global first
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   213
            if first:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   214
                vals["xmlns:yml"] = u"http://fdik.org/yml"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   215
                first = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   216
            vals["yml:called"] = u(line)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   217
        return self.xml(parms, vals, hasContent, avoidTag)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   218
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   219
    def addParm(self, parm):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   220
        if parm[0] == "%":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   221
            for i in range(len(self.parms)):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   222
                if self.parms[i][0] != "%":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   223
                    self.parms.insert(i, parm)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   224
                    return
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   225
        self.parms.append(parm)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   226
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   227
    def addDescend(self, desc):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   228
        if desc[0] == "+" or desc[0] == "@":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   229
            self.descends.append(desc[1:])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   230
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   231
            self.descends.append(desc)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   232
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   233
    def addValue(self, parm, value):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   234
        if type(value) is str or type(value) is unicode:
20
c936066cff62 keywords as values in decl parameters
Volker Birk <vb@pep.foundation>
parents: 16
diff changeset
   235
            if value[0] != "'" and value[0] != '"':
22
Volker Birk <vb@pep.foundation>
parents: 20
diff changeset
   236
                self.values[parm] = u(value)
Volker Birk <vb@pep.foundation>
parents: 20
diff changeset
   237
            else:
Volker Birk <vb@pep.foundation>
parents: 20
diff changeset
   238
                self.values[parm] = u(evalPython(value))
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   239
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   240
            self.values[parm] = u(evalPython(u(value)))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   241
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   242
    def xml(self, callParms, callValues, hasContent, avoidTag = False):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   243
        global pointers
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   244
        extraContent = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   245
        if self.content:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   246
            hasContent = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   247
        resultParms = self.values.copy()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   248
        macros = self.macros.copy()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   249
        toDelete = resultParms.keys()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   250
        for key in toDelete:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   251
            if key[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   252
                del resultParms[key]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   253
        for key, value in callValues.iteritems():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   254
            if key[0] == "%":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   255
                macros[key] = value
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   256
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   257
                resultParms[key] = value
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   258
        i = 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   259
        for cp in callParms:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   260
            if i < len(self.parms):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   261
                if self.parms[i][0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   262
                    cp = u(cp)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   263
                    if "'" in cp:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   264
                        pointers[self.parms[i][1:]] = u'"' + cp + u'"'
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   265
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   266
                        pointers[self.parms[i][1:]] = u"'" + cp + u"'"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   267
                elif self.parms[i][0] == "%":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   268
                    macros[self.parms[i]] = u(cp)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   269
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   270
                    resultParms[self.parms[i]] = cp
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   271
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   272
                extraContent += u(cp)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   273
                hasContent = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   274
            i += 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   275
        result = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   276
        for p, v in resultParms.iteritems():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   277
            if p[0] == "'" or p[0] == '"':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   278
                p = eval(p)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   279
            result += u" "+ p + u"=" + quoteattr(applyMacros(macros, u(v)))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   280
        if hasContent:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   281
            if avoidTag:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   282
                return True, strRepl(extraContent)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   283
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   284
                return True, u"<" + self.alias + result + u">" + strRepl(extraContent)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   285
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   286
            if avoidTag:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   287
                return False, u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   288
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   289
                return False, u"<" + self.alias + result + u"/>"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   290
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   291
def replaceContent(tree, subtree):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   292
    n = 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   293
    while n < len(tree):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   294
        obj = tree[n]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   295
        if obj[0] == "func":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   296
            l = obj[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   297
            if l[0] == "content":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   298
                d = 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   299
                if subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   300
                    for el in subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   301
                        tree.insert(n+d, el)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   302
                        d += 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   303
                del tree[n]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   304
                n += d
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   305
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   306
                try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   307
                    if l[-1][0] == "content":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   308
                        replaceContent(l[-1][1], subtree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   309
                except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   310
        elif obj[0] == "funclist":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   311
            replaceContent(obj[1], subtree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   312
        n += 1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   313
    return tree
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   314
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   315
def executeCmd(text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   316
    if type(text) is not unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   317
        text = codecs.decode(text, encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   318
    for (regex, pattern) in operator:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   319
        match = re.search(regex, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   320
        while match:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   321
            cmd = pattern
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   322
            opt = match.groups()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   323
            for i in range(len(opt)):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   324
                cmd = cmd.replace(u"%" + u(i+1), opt[i])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   325
            text = text[:match.start()] + u"`" + cmd + u"`"+ text[match.end():]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   326
            match = re.search(regex, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   327
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   328
    result = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   329
    m = re.search(bq, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   330
    while text and m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   331
        cmd  = m.group(1)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   332
        head = textOut(text[:m.start()])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   333
        text = text[m.end():]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   334
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   335
            r, rest = parseLine(cmd, _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   336
            if rest: raise SyntaxError(cmd)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   337
        except SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   338
            if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   339
                raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in executing command: " + cmd.strip())
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   340
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   341
                raise SyntaxError(u"in " + u(line) + u": syntax error in executing command: " + cmd.strip())
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   342
        inner = _finish(r)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   343
        result += head + inner
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   344
        m = re.search(bq, text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   345
    result += textOut(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   346
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   347
    return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   348
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   349
def codegen(obj):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   350
    global in_ns, pointers, line, included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   351
    ctype = obj[0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   352
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   353
    if type(obj) is code:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   354
        return obj
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   355
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   356
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   357
        if ctype.line: line = ctype.line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   358
    except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   359
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   360
    if ctype == "empty":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   361
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   362
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   363
    if ctype == "in_ns":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   364
        in_ns = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   365
        subtree = obj[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   366
        for sel in subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   367
            codegen(sel)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   368
        in_ns = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   369
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   370
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   371
    elif ctype == "decl":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   372
        name = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   373
        for data in obj[1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   374
            if type(data) is unicode or type(data) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   375
                name = data
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   376
                try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   377
                    yf = ymlFunc[name]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   378
                    yf.alias
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   379
                except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   380
                    ymlFunc[name] = YF(name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   381
                    yf = ymlFunc[name]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   382
                    if in_ns:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   383
                        yf.alias = in_ns + u":" + name
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   384
                        if not enable_tracing:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   385
                            if in_ns == "xsl" and (name == "debug" or name=="assert" or name[:7]=="_trace_"):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   386
                                yf.alias = "-"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   387
                                yf.addParm("skip1")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   388
                                yf.addParm("skip2")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   389
                                break
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   390
            elif type(data) is tuple or type(data) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   391
                if data[0] == "base":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   392
                    base = data[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   393
                    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   394
                        yf = ymlFunc[name] = ymlFunc[base].copy(name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   395
                    except KeyError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   396
                        if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   397
                            raise KeyError(u"in " + included + u":" + u(line) + u": " + base + u" as base for " + name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   398
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   399
                            raise KeyError(u"in " + u(line) + u": " + base + u" as base for " + name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   400
                elif data[0] == "shape":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   401
                    shape = ymlFunc[data[1]]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   402
                    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   403
                        yf = ymlFunc[name]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   404
                        yf.patch(shape)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   405
                    except KeyError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   406
                        if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   407
                            raise KeyError(u"in " + included + u":" + u(line) + u": " + base + u" as shape for " + name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   408
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   409
                            raise KeyError(u"in " + u(line) + u": " + base + u" as shape for " + name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   410
                elif data[0] == "descend":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   411
                    yf.addDescend(data[1])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   412
                elif data[0] == "declParm":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   413
                    l = data[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   414
                    parmName = l[0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   415
                    if len(l)==1:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   416
                        yf.addParm(parmName)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   417
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   418
                        value = l[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   419
                        if parmName[0] != "%":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   420
                            yf.addValue(parmName, value)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   421
                        if parmName[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   422
                            yf.pointers[parmName[1:]] = value
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   423
                            yf.addParm(parmName)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   424
                        elif parmName[0] == "%":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   425
                            if type(value) is unicode or type(value) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   426
                                yf.macros[parmName] = u(evalPython(value))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   427
                            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   428
                                yf.macros[parmName] = u(evalPython(u(value)))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   429
                            yf.addParm(parmName)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   430
                elif data[0] == "alias":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   431
                    if in_ns:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   432
                        yf.alias = in_ns + u":" + data[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   433
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   434
                        yf.alias = data[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   435
                elif data[0] == "content":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   436
                    yf.content = data[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   437
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   438
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   439
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   440
    elif ctype == "funclist":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   441
        result = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   442
        for f in obj[1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   443
            result += codegen(f)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   444
        return code(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   445
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   446
    elif ctype == "parentheses":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   447
        if len(obj[1]):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   448
            return codegen(('func', ['_parentheses', ('content', [obj[1][0]])]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   449
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   450
            return u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   451
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   452
    elif ctype == "fparm":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   453
        if len(obj[1]):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   454
            return codegen(('func', ['_parm', ('content', [obj[1][0]])]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   455
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   456
            return u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   457
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   458
    elif ctype == "generic":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   459
        return codegen(('func', ['_generic', ('content', [obj[1][0]])]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   460
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   461
    elif ctype == "xbase":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   462
        return codegen(('func', ['_base', ('content', [obj[1][0]])]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   463
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   464
    elif ctype == "func":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   465
        avoidTag = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   466
        name = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   467
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   468
        if name == "decl":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   469
            if ymlFunc[name] == "#error":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   470
                if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   471
                    raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in decl statement")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   472
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   473
                    raise SyntaxError(u"in " + u(line) + u": syntax error in decl statement")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   474
        if name == "define" or name == "operator":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   475
            if ymlFunc[name] == "#error":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   476
                if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   477
                    raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in define statement")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   478
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   479
                    raise SyntaxError(u"in " + u(line) + u": syntax error in define statement")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   480
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   481
        if name[0] == "&":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   482
            avoidTag = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   483
            name = name[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   484
        hasContent = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   485
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   486
        if len(name) > 2:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   487
            if name[0:2] == "**":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   488
                return code(eval('u'+pointer(name[1:])))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   489
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   490
        if name[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   491
            name = eval(pointer(name))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   492
            if name[0] == "&":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   493
                avoidTag = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   494
                name = name[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   495
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   496
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   497
            ymlFunc[name]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   498
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   499
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   500
                ymlFunc["_"]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   501
                return codegen(('func', ['_', ('content', [('funclist', [obj])])]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   502
            except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   503
                ymlFunc[name] = YF(name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   504
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   505
        if ymlFunc[name].alias == "-": avoidTag = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   506
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   507
        to_add = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   508
        if len(ymlFunc[name].descends):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   509
            if obj[1][-1][0] != 'content':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   510
                if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   511
                    raise KeyError(u"in " + included + u":" + u(line) + u": " + name + u" has descending attributes, but no descendants are following")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   512
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   513
                    raise KeyError(u"in " + u(line) + u": " + name + u" has descending attributes, but no descendants are following")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   514
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   515
            def first_func(obj):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   516
                if type(obj) is tuple or type(obj) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   517
                    if obj[0] == 'func':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   518
                        return obj
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   519
                    elif obj[0] == 'funclist':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   520
                        return first_func(obj[1])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   521
                    elif obj[0] == 'content':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   522
                        return first_func(obj[1])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   523
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   524
                        return None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   525
                elif type(obj) == list:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   526
                    for e in obj:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   527
                        f = first_func(e)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   528
                        if f: return f
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   529
                    return None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   530
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   531
            def copy_without_first_func(o, found = False):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   532
                c = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   533
                for obj in o:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   534
                    if found:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   535
                        c.append(obj)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   536
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   537
                        if obj[0] == 'func':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   538
                            if obj[1][-1][0] == 'content':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   539
                                c.extend( obj[1][-1][1] )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   540
                            found = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   541
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   542
                            c.append( ( obj[0], copy_without_first_func(obj[1], False ) ) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   543
                return c
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   544
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   545
            def get_parms(obj):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   546
                result = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   547
                for e in obj[1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   548
                    if type(e) is tuple or type(e) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   549
                        if e[0] == "parm":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   550
                            result.append( e )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   551
                return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   552
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   553
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   554
                add_params = get_parms(obj)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   555
                for e in obj[1][-1][1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   556
                    c = e[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   557
                    for dname in ymlFunc[name].descends:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   558
                        f, c = first_func(c), copy_without_first_func(c)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   559
                        if dname[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   560
                            pointers[dname[1:]] = "'" + f[1][0] + "'"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   561
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   562
                            add_params.append( ('parm', [dname, u"'" + f[1][0] + u"'"]) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   563
                        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   564
                            add_params.extend( get_parms(f) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   565
                        except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   566
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   567
                    new_things = [ e[1][0] ]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   568
                    new_things.extend( add_params )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   569
                    new_things.append( ('content', c) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   570
                    
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   571
                    to_add.append( ('func', new_things ) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   572
            except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   573
                if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   574
                    raise KeyError(u"in " + included + u":" + u(line) + u": " + name + u" has descending attributes, and too less descendants are following")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   575
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   576
                    raise KeyError(u"in " + u(line) + u": " + name + u" has descending attributes, and too less descendants are following")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   577
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   578
        if not to_add:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   579
            to_add = ( obj, )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   580
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   581
        complete = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   582
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   583
        for obj in to_add:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   584
            subtree = None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   585
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   586
                if obj[1][-1][0] == "content":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   587
                    subtree = obj[1][-1][1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   588
            except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   589
     
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   590
            if ymlFunc[name].content:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   591
                hasContent = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   592
                treetemplate = deepcopy(ymlFunc[name].content)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   593
                subtree = replaceContent(treetemplate, subtree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   594
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   595
            if subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   596
                hasContent = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   597
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   598
            hasContent, result = ymlFunc[name](obj[1], hasContent, avoidTag)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   599
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   600
            if subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   601
                for sel in subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   602
                    result += codegen(sel)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   603
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   604
            if hasContent and not(avoidTag):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   605
                result += u"</" + ymlFunc[name].alias + u">"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   606
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   607
            complete += result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   608
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   609
        return code(complete)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   610
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   611
    elif ctype == "textsection":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   612
        result = u''
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   613
        ll = obj[1].splitlines()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   614
        space = len(ll[-1]) - 2
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   615
        for l in ll[1:-1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   616
            m = re.match(bqq, l)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   617
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   618
                cmd = m.group(1)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   619
                try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   620
                    r, x = parseLine(cmd, _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   621
                    if x: raise SyntaxError(cmd)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   622
                    result += _finish(r)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   623
                except SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   624
                    if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   625
                        raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in executing command: " + cmd.strip())
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   626
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   627
                        raise SyntaxError(u"in " + u(line) + u": syntax error in executing command: " + cmd.strip())
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   628
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   629
                result += codegen(Symbol(u'lineQuote', u'| ' + l[space:]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   630
        return code(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   631
14
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   632
    elif ctype == "textsectionu":
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   633
        result = u''
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   634
        ll = obj[1].splitlines()
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   635
        space = len(ll[-1]) - 2
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   636
        for l in ll[1:-1]:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   637
            m = re.match(bqq, l)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   638
            if m:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   639
                cmd = m.group(1)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   640
                try:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   641
                    r, x = parseLine(cmd, _inner, [], True, comment)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   642
                    if x: raise SyntaxError(cmd)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   643
                    result += _finish(r)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   644
                except SyntaxError:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   645
                    if included:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   646
                        raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in executing command: " + cmd.strip())
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   647
                    else:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   648
                        raise SyntaxError(u"in " + u(line) + u": syntax error in executing command: " + cmd.strip())
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   649
            else:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   650
                if result != u'': result += u' '
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   651
                result += codegen(Symbol(u'quote', [u'> ' + l[space:]]))
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   652
        return code(result)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   653
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   654
    elif ctype == "lineQuote" or ctype == "quote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   655
        m, text, base, inds = None, u"", 0, 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   656
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   657
        if ctype == "lineQuote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   658
            text = obj[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   659
            m = lq.match(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   660
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   661
                inds = len(m.group(1))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   662
                text = m.group(2)[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   663
            else: inds = 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   664
        elif ctype == "quote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   665
            inds = -1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   666
            text = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   667
            m = sq.match(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   668
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   669
                if m.group(1):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   670
                    inds = int(m.group(1))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   671
                text = m.group(2)[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   672
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   673
                if type(text) is unicode or type(text) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   674
                    text = u(evalPython(text))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   675
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   676
        ind = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   677
        if inds > -1:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   678
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   679
                cmd = evalPython(u"indent(" + u(inds) + u")")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   680
                result, rest = parseLine(u(cmd), _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   681
                if rest:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   682
                    raise SyntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   683
                ind = _finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   684
            except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   685
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   686
        if ctype == "lineQuote": text += u"\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   687
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   688
        hasTextFunc = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   689
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   690
            ymlFunc["text"]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   691
            hasTextFunc = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   692
        except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   693
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   694
        text = executeCmd(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   695
        return code(ind + text) 
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   696
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   697
    elif ctype == "tagQuote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   698
        m = tq.match(obj[1])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   699
        if m.group(1) == "<":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   700
            return code(u"<" + m.group(2))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   701
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   702
            return code(m.group(2))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   703
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   704
    elif ctype == "operator":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   705
        operator.append((re.compile(evalPython(obj[1][0])), obj[1][1]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   706
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   707
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   708
    elif ctype == "constant":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   709
        name = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   710
        if name[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   711
            name = name[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   712
        value = obj[1][1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   713
        pointers[name] = value
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   714
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   715
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   716
    elif ctype == "include":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   717
        reverse = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   718
        ktext, kxml = False, False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   719
        for arg in obj[1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   720
            if type(arg) is tuple or type(arg) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   721
                if arg[0] == "reverse":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   722
                    reverse = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   723
                elif arg[0] == "ktext":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   724
                    ktext = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   725
                elif arg[0] == "kxml":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   726
                    kxml = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   727
            elif type(arg) is unicode or type(arg) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   728
                filemask = arg
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   729
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   730
        if filemask[0] == '/' or filemask[0] == '.':
16
da8160da8d3e glob() does not sort any more - adding sorted() function
Volker Birk <vb@pep.foundation>
parents: 15
diff changeset
   731
            files = sorted(glob(filemask))
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   732
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   733
            files = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   734
            for directory in includePath:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   735
                path = os.path.join(directory, filemask)
16
da8160da8d3e glob() does not sort any more - adding sorted() function
Volker Birk <vb@pep.foundation>
parents: 15
diff changeset
   736
                files.extend(sorted(glob(path)))
da8160da8d3e glob() does not sort any more - adding sorted() function
Volker Birk <vb@pep.foundation>
parents: 15
diff changeset
   737
da8160da8d3e glob() does not sort any more - adding sorted() function
Volker Birk <vb@pep.foundation>
parents: 15
diff changeset
   738
        if files and reverse:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   739
            files = files[-1::-1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   740
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   741
        if not(files):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   742
            if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   743
                raise IOError(u"in " + included + ":" + u(line) + u": include file(s) '" + filemask + u"' not found")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   744
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   745
                raise IOError(u"in " + u(line) + u": include file(s) '" + filemask + u"' not found")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   746
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   747
        includeFile = fileinput.input(files, mode="rU", openhook=fileinput.hook_encoded(encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   748
        _included = included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   749
        if ktext or kxml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   750
            text = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   751
            for line in includeFile:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   752
                included = includeFile.filename()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   753
                if kxml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   754
                    if (not line[:6] == '<?xml ') and (not line[:6] == '<?XML '):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   755
                        text += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   756
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   757
                    text += executeCmd(line)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   758
            included = _included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   759
            return code(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   760
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   761
            result = parse(ymlCStyle(), includeFile, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   762
            included = u(filemask)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   763
            x = _finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   764
            included = _included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   765
            return code(x)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   766
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   767
    elif ctype == "pyExp":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   768
        exp = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   769
        cmd = evalPython(exp)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   770
        result, rest = parseLine(u(cmd), _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   771
        if rest:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   772
            raise SyntaxError(cmd)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   773
        return code(_finish(result))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   774
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   775
    elif ctype == "pythonCall":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   776
        parms = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   777
        data = obj[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   778
        for p in data:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   779
            if type(p) is unicode or type(p) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   780
                name = p
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   781
            elif type(p) is tuple or type(p) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   782
                ptype = p[0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   783
                if ptype == "parm":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   784
                    if p[1][0][0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   785
                        parms.append(pointer(p[1][0]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   786
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   787
                        parms.append(p[1][0])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   788
        if len(parms) == 0:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   789
            exp = name + u"()"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   790
        elif len(parms) == 1:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   791
            exp = name + u"(" + u(parms[0]) + u")"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   792
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   793
            exp = name + u(tuple(parms))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   794
        cmd = evalPython(exp)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   795
        result, rest = parseLine(u(cmd), _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   796
        if rest:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   797
            raise SyntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   798
        return code(_finish(result))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   799
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   800
    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   801
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   802
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   803
def _finish(tree):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   804
    result = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   805
    python = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   806
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   807
    for el in tree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   808
        if el[0] == "python":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   809
            if el[1][0][:2] == "!!":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   810
                python += el[1][0][2:-2]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   811
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   812
                python += el[1][0][1:] + u"\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   813
            continue
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   814
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   815
            if python:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   816
                execPython(python)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   817
                python = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   818
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   819
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   820
            result += codegen(el)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   821
        except RuntimeError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   822
            if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   823
                raise RuntimeError(u"in " + included + u":" + u(line))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   824
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   825
                raise RuntimeError(u"in " + u(line))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   826
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   827
    if python:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   828
        execPython(python)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   829
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   830
    return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   831
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   832
def finish(tree):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   833
    global first
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   834
    first = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   835
    return _finish(tree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   836
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   837
ymlFunc["_parentheses"] = YF("parms")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   838
ymlFunc["_parm"] = YF("parm")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   839
ymlFunc["_generic"] = YF("generic")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   840
ymlFunc["_base"] = YF("base")