backend.py
author Volker Birk <vb@pep-project.org>
Fri, 15 Feb 2019 10:45:50 +0100
changeset 29 6a8a7951d8e6
parent 22 3a2bd70c01df
child 31 d3dddb80d1f5
permissions -rw-r--r--
feature: default function is used as a template
29
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
     1
# 2.5.10 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
29
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
     9
from copy import copy, deepcopy
0
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]
29
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   498
        except KeyError:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   499
            try:
29
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   500
                if ymlFunc["_"].alias != u"-":
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   501
                    return codegen(('func', ['_', ('content', [('funclist', [obj])])]))
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   502
                else:
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   503
                    ymlFunc[name] = copy(ymlFunc["_"])
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   504
                    ymlFunc[name].alias = name.replace("_", "-")
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   505
                    return codegen(obj)
6a8a7951d8e6 feature: default function is used as a template
Volker Birk <vb@pep-project.org>
parents: 22
diff changeset
   506
            except KeyError:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   507
                ymlFunc[name] = YF(name)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   508
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   509
        if ymlFunc[name].alias == "-": avoidTag = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   510
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   511
        to_add = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   512
        if len(ymlFunc[name].descends):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   513
            if obj[1][-1][0] != 'content':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   514
                if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   515
                    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
   516
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   517
                    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
   518
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   519
            def first_func(obj):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   520
                if type(obj) is tuple or type(obj) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   521
                    if obj[0] == 'func':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   522
                        return obj
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   523
                    elif obj[0] == 'funclist':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   524
                        return first_func(obj[1])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   525
                    elif obj[0] == 'content':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   526
                        return first_func(obj[1])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   527
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   528
                        return None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   529
                elif type(obj) == list:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   530
                    for e in obj:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   531
                        f = first_func(e)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   532
                        if f: return f
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   533
                    return None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   534
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   535
            def copy_without_first_func(o, found = False):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   536
                c = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   537
                for obj in o:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   538
                    if found:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   539
                        c.append(obj)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   540
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   541
                        if obj[0] == 'func':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   542
                            if obj[1][-1][0] == 'content':
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   543
                                c.extend( obj[1][-1][1] )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   544
                            found = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   545
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   546
                            c.append( ( obj[0], copy_without_first_func(obj[1], False ) ) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   547
                return c
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   548
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   549
            def get_parms(obj):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   550
                result = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   551
                for e in obj[1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   552
                    if type(e) is tuple or type(e) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   553
                        if e[0] == "parm":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   554
                            result.append( e )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   555
                return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   556
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   557
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   558
                add_params = get_parms(obj)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   559
                for e in obj[1][-1][1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   560
                    c = e[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   561
                    for dname in ymlFunc[name].descends:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   562
                        f, c = first_func(c), copy_without_first_func(c)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   563
                        if dname[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   564
                            pointers[dname[1:]] = "'" + f[1][0] + "'"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   565
                        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   566
                            add_params.append( ('parm', [dname, u"'" + f[1][0] + u"'"]) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   567
                        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   568
                            add_params.extend( get_parms(f) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   569
                        except: pass
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
                    new_things = [ e[1][0] ]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   572
                    new_things.extend( add_params )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   573
                    new_things.append( ('content', c) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   574
                    
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   575
                    to_add.append( ('func', new_things ) )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   576
            except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   577
                if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   578
                    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
   579
                else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   580
                    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
   581
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   582
        if not to_add:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   583
            to_add = ( obj, )
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   584
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   585
        complete = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   586
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   587
        for obj in to_add:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   588
            subtree = None
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   589
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   590
                if obj[1][-1][0] == "content":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   591
                    subtree = obj[1][-1][1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   592
            except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   593
     
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   594
            if ymlFunc[name].content:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   595
                hasContent = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   596
                treetemplate = deepcopy(ymlFunc[name].content)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   597
                subtree = replaceContent(treetemplate, subtree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   598
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   599
            if subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   600
                hasContent = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   601
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   602
            hasContent, result = ymlFunc[name](obj[1], hasContent, avoidTag)
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 subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   605
                for sel in subtree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   606
                    result += codegen(sel)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   607
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   608
            if hasContent and not(avoidTag):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   609
                result += u"</" + ymlFunc[name].alias + u">"
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
            complete += result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   612
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   613
        return code(complete)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   614
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   615
    elif ctype == "textsection":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   616
        result = u''
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   617
        ll = obj[1].splitlines()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   618
        space = len(ll[-1]) - 2
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   619
        for l in ll[1:-1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   620
            m = re.match(bqq, l)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   621
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   622
                cmd = m.group(1)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   623
                try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   624
                    r, x = parseLine(cmd, _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   625
                    if x: raise SyntaxError(cmd)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   626
                    result += _finish(r)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   627
                except SyntaxError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   628
                    if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   629
                        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
   630
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   631
                        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
   632
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   633
                result += codegen(Symbol(u'lineQuote', u'| ' + l[space:]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   634
        return code(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   635
14
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   636
    elif ctype == "textsectionu":
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   637
        result = u''
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   638
        ll = obj[1].splitlines()
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   639
        space = len(ll[-1]) - 2
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   640
        for l in ll[1:-1]:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   641
            m = re.match(bqq, l)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   642
            if m:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   643
                cmd = m.group(1)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   644
                try:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   645
                    r, x = parseLine(cmd, _inner, [], True, comment)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   646
                    if x: raise SyntaxError(cmd)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   647
                    result += _finish(r)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   648
                except SyntaxError:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   649
                    if included:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   650
                        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
   651
                    else:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   652
                        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
   653
            else:
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   654
                if result != u'': result += u' '
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   655
                result += codegen(Symbol(u'quote', [u'> ' + l[space:]]))
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   656
        return code(result)
d937e9d7c4fe Version 2.5.6
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
   657
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   658
    elif ctype == "lineQuote" or ctype == "quote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   659
        m, text, base, inds = None, u"", 0, 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   660
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   661
        if ctype == "lineQuote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   662
            text = obj[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   663
            m = lq.match(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   664
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   665
                inds = len(m.group(1))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   666
                text = m.group(2)[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   667
            else: inds = 0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   668
        elif ctype == "quote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   669
            inds = -1
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   670
            text = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   671
            m = sq.match(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   672
            if m:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   673
                if m.group(1):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   674
                    inds = int(m.group(1))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   675
                text = m.group(2)[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   676
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   677
                if type(text) is unicode or type(text) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   678
                    text = u(evalPython(text))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   679
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   680
        ind = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   681
        if inds > -1:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   682
            try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   683
                cmd = evalPython(u"indent(" + u(inds) + u")")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   684
                result, rest = parseLine(u(cmd), _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   685
                if rest:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   686
                    raise SyntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   687
                ind = _finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   688
            except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   689
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   690
        if ctype == "lineQuote": text += u"\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   691
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   692
        hasTextFunc = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   693
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   694
            ymlFunc["text"]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   695
            hasTextFunc = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   696
        except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   697
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   698
        text = executeCmd(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   699
        return code(ind + text) 
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   700
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   701
    elif ctype == "tagQuote":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   702
        m = tq.match(obj[1])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   703
        if m.group(1) == "<":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   704
            return code(u"<" + m.group(2))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   705
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   706
            return code(m.group(2))
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 == "operator":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   709
        operator.append((re.compile(evalPython(obj[1][0])), obj[1][1]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   710
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   711
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   712
    elif ctype == "constant":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   713
        name = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   714
        if name[0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   715
            name = name[1:]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   716
        value = obj[1][1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   717
        pointers[name] = value
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   718
        return code(u"")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   719
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   720
    elif ctype == "include":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   721
        reverse = False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   722
        ktext, kxml = False, False
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   723
        for arg in obj[1]:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   724
            if type(arg) is tuple or type(arg) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   725
                if arg[0] == "reverse":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   726
                    reverse = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   727
                elif arg[0] == "ktext":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   728
                    ktext = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   729
                elif arg[0] == "kxml":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   730
                    kxml = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   731
            elif type(arg) is unicode or type(arg) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   732
                filemask = arg
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   733
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   734
        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
   735
            files = sorted(glob(filemask))
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   736
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   737
            files = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   738
            for directory in includePath:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   739
                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
   740
                files.extend(sorted(glob(path)))
da8160da8d3e glob() does not sort any more - adding sorted() function
Volker Birk <vb@pep.foundation>
parents: 15
diff changeset
   741
da8160da8d3e glob() does not sort any more - adding sorted() function
Volker Birk <vb@pep.foundation>
parents: 15
diff changeset
   742
        if files and reverse:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   743
            files = files[-1::-1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   744
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   745
        if not(files):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   746
            if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   747
                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
   748
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   749
                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
   750
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   751
        includeFile = fileinput.input(files, mode="rU", openhook=fileinput.hook_encoded(encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   752
        _included = included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   753
        if ktext or kxml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   754
            text = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   755
            for line in includeFile:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   756
                included = includeFile.filename()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   757
                if kxml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   758
                    if (not line[:6] == '<?xml ') and (not line[:6] == '<?XML '):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   759
                        text += line
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
                    text += executeCmd(line)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   762
            included = _included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   763
            return code(text)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   764
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   765
            result = parse(ymlCStyle(), includeFile, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   766
            included = u(filemask)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   767
            x = _finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   768
            included = _included
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   769
            return code(x)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   770
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   771
    elif ctype == "pyExp":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   772
        exp = obj[1][0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   773
        cmd = evalPython(exp)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   774
        result, rest = parseLine(u(cmd), _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   775
        if rest:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   776
            raise SyntaxError(cmd)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   777
        return code(_finish(result))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   778
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   779
    elif ctype == "pythonCall":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   780
        parms = []
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   781
        data = obj[1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   782
        for p in data:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   783
            if type(p) is unicode or type(p) is str:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   784
                name = p
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   785
            elif type(p) is tuple or type(p) is Symbol:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   786
                ptype = p[0]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   787
                if ptype == "parm":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   788
                    if p[1][0][0] == "*":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   789
                        parms.append(pointer(p[1][0]))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   790
                    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   791
                        parms.append(p[1][0])
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   792
        if len(parms) == 0:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   793
            exp = name + u"()"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   794
        elif len(parms) == 1:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   795
            exp = name + u"(" + u(parms[0]) + u")"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   796
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   797
            exp = name + u(tuple(parms))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   798
        cmd = evalPython(exp)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   799
        result, rest = parseLine(u(cmd), _inner, [], True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   800
        if rest:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   801
            raise SyntaxError()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   802
        return code(_finish(result))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   803
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   804
    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   805
        return code(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
def _finish(tree):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   808
    result = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   809
    python = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   810
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   811
    for el in tree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   812
        if el[0] == "python":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   813
            if el[1][0][:2] == "!!":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   814
                python += el[1][0][2:-2]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   815
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   816
                python += el[1][0][1:] + u"\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   817
            continue
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   818
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   819
            if python:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   820
                execPython(python)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   821
                python = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   822
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   823
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   824
            result += codegen(el)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   825
        except RuntimeError:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   826
            if included:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   827
                raise RuntimeError(u"in " + included + u":" + u(line))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   828
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   829
                raise RuntimeError(u"in " + u(line))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   830
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   831
    if python:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   832
        execPython(python)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   833
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   834
    return result
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   835
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   836
def finish(tree):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   837
    global first
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   838
    first = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   839
    return _finish(tree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   840
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   841
ymlFunc["_parentheses"] = YF("parms")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   842
ymlFunc["_parm"] = YF("parm")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   843
ymlFunc["_generic"] = YF("generic")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   844
ymlFunc["_base"] = YF("base")