Exemple #1
0
def executeCmd(text):
    if type(text) is not unicode:
        text = codecs.decode(text, encoding)
    for (regex, pattern) in operator:
        match = re.search(regex, text)
        while match:
            cmd = pattern
            opt = match.groups()
            for i in range(len(opt)):
                cmd = cmd.replace(u"%" + u(i+1), opt[i])
            text = text[:match.start()] + u"`" + cmd + u"`"+ text[match.end():]
            match = re.search(regex, text)

    result = u""
    m = re.search(bq, text)
    while text and m:
        cmd  = m.group(1)
        head = textOut(text[:m.start()])
        text = text[m.end():]
        try:
            r, rest = parseLine(cmd, _inner, [], True, comment)
            if rest: raise SyntaxError(cmd)
        except SyntaxError:
            if included:
                raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in executing command: " + cmd.strip())
            else:
                raise SyntaxError(u"in " + u(line) + u": syntax error in executing command: " + cmd.strip())
        inner = _finish(r)
        result += head + inner
        m = re.search(bq, text)
    result += textOut(text)

    return result
Exemple #2
0
def go(t, rule):    
    return parseLine( t,
                      rule,
                      resultSoFar = [], 
                      skipWS = True,
                      skipComments = None,
                      packrat = False)
Exemple #3
0
def parse(s):
    ast = []
    # print "parsing: >>>%s<<<" % s
    if len(s) == 0:
        return ast
    ast, remaining = pyPEG.parseLine(s, mp._start(), ast, False)
    assert remaining == '', "oops remaining is %s" % remaining
    return ast
Exemple #4
0
def check(txt, expect_unparsed, expect_ast=[]):
    print '\n\n\n'
    ast = []
    ast, unparsed = pyPEG.parseLine(txt, _start, ast, False)
    eq_(unparsed, expect_unparsed)
    eq_(ast, expect_ast)
    print "unparsed=", unparsed
    pprint(ast)
def check(txt, expect_unparsed, expect_ast = []):
    print '\n\n\n'
    ast = []
    ast, unparsed = pyPEG.parseLine(txt, _start, ast, False)
    eq_(unparsed, expect_unparsed)
    eq_(ast, expect_ast)
    print "unparsed=", unparsed
    pprint(ast)
Exemple #6
0
def compiles( string="", rule=None):
    """ Returns True if string compiles under rule, False otherwise. """
    _ast = None
    try:
        _ast = pyPEG.parseLine(string, rule, [], False, COMMENT, False)
    except:
        return False

    return True
    def interpret(self, cosys, trace, specindex, color = False):

        if not color:
            self.CS = ""
            self.CE = ""
            self.CR = ""
            self.CB = ""
            self.CG = ""
            self.CY = ""
        else:
            self.CS = '\033['
            self.CE = '\033[1;m'
            self.CR = self.CS + '1;31m'
            self.CB = self.CS + '1;94m'
            self.CG = self.CS + '1;32m'
            self.CY = self.CS + '1;33m'

        self.cosys = cosys
        self.sys = cosys.sys

        (ast, rest) = parseLine(trace, RESULT(), [], True, packrat=True)

        if rest != "":
            LDEBUG(ast)
            LERROR( "Error al interpretar las trazas. No se pudo " \
                      + "interpretar lo que sigue:\n"  + rest)

        specrepr = cosys.compiled.props[specindex]['comm']
        specname = cosys.compiled.props[specindex]['name']
        for sp in ast:
            if sp.__name__ == "TRUESPEC":
                print self.CG + "|+| - Specification " + self.CE + self.CY \
                    + str(specname) + "\n    - " + specrepr + self.CE + self.CG\
                    + "\n    - is true" + self.CE

            elif sp.__name__ == "FALSESPEC":
                print self.CR + "|-| - Specification " + self.CE + self.CY \
                    + str(specname)  + "\n    - " + specrepr\
                    + self.CE + self.CR  + "\n    - is false" + self.CE \
                    + "\n    - as demonstrated by the following execution"\
                    + " sequence:\n"
                self.tab.i()
                self.interpret_trace(sp.what[1])
                self.tab.d()

            elif sp.__name__ == "WARNING":
                warning = self.interpret_warning(sp)
                if warning != "":
                    self.tprint(self.CR \
                          + "<<<<  WARNING FOR NEXT TEST CASE: >>>>\n"\
                          + warning + self.CE)
            elif sp.__name__ == "NUSMVHEADER":
                pass # we don't do nothing with the header 
            else:
                raise TypeError(sp.__name__)
Exemple #8
0
 def solveMath(self, formula):
     assert isinstance(formula, str)
     _result = 0
     p, rest = parseLine(formula, SUM, resultSoFar=[])
     if rest:
         raise NotMath(formula)
     try:
         _result = self.solveSum(p[0])
     except NoName as e:
         raise Exception("Can't solve " + formula + " because: " + str(e))
     return _result
def test_expand():
    examples =[('-I${prefix}/sr`voom`c', 
                [(u'includeflag', ['FOO', '/sr', 'EXPvoomPXE', 'c'])], 
                dict(prefix='FOO')),
               ]

    for txt, expected, ctx in examples:
        # print txt, expected, ctx
        ast,unparsed = pyPEG.parseLine(txt, _start, [], False)
        result = traverse(ast, ctx, 'dollar_brace_var', expand_dollar_vars)
        result = traverse(result, ctx, 'backtick', expand_backticks)
        # print "result=", result
        eq_(result, expected)
Exemple #10
0
def test_expand():
    examples = [
        ('-I${prefix}/sr`voom`c', [
            (u'includeflag', ['FOO', '/sr', 'EXPvoomPXE', 'c'])
        ], dict(prefix='FOO')),
    ]

    for txt, expected, ctx in examples:
        # print txt, expected, ctx
        ast, unparsed = pyPEG.parseLine(txt, _start, [], False)
        result = traverse(ast, ctx, 'dollar_brace_var', expand_dollar_vars)
        result = traverse(result, ctx, 'backtick', expand_backticks)
        # print "result=", result
        eq_(result, expected)
Exemple #11
0
    def replaceDefs(self, path):
        """ Make a sintactic replacement of definitions in file at path.
            @ input path: path to the file
            @ warning: this method will actualy modify the file if needed
        """
        for d,v in self.defs.iteritems():
            print d, v
        print "................................."
        # First replace inside definitions #TODO use visited flags in algorithm
        for _d in self.adj:
            self.defRepl(_d)

        for d,v in self.defs.iteritems():
            print d, v


        # Replace everywhere except when we have a comment, a definition or 
        # a string
        prog1 = re.compile(r"[\s]*(/\*.*?\*/)?[\s]*DEFINE[^\r\n]*")
        prog2 = re.compile(r"//.*")
        _s = "" # how the new file will look like
        _f = open(path, "r")

        for _l in _f:
            if prog1.match(_l):
                x,y = pyPEG.parseLine(str(_l),DEFINITION,[])
                print _l
                print x
                x = x[0].what
                assert y == ""
                _s += "DEFINE " + Utils.ss(x[0]) + " " + self.defs[Utils.ss(x[0])]
            if not (prog1.match(_l) or prog2.match(_l)):
                for _d in self.defs.iteritems():
                    _l = re.subn( u'\\b'+unicode(_d[0])+u'\\b'
                                , unicode(_d[1])
                                , str(_l))[0]
                _s += _l
        debugCURRENT(_s)
        _f = open(path+".aux", 'w')
        _f.write(_s)
        _f.close()
Exemple #12
0
				hours_str = value[1][1]
			else:
				hours_str = value[0][1]
		elif name == "minutes":
			minutes_str = value[0][1]
		elif name == "sign":
			if value[0] == "voor":
				sign = -1

	minutes = 0

	if len(hours_str) > 0:
		minutes = string_to_int(hours_str) * 60

	if half:
		minutes -= 30
		
	if len(minutes_str) > 0:
		minutes += sign * string_to_int(minutes_str)

	hours = minutes // 60
	minutes = minutes - (60 * hours)
	
	today = datetime.date.today() + datetime.timedelta(days=1)
	return datetime.datetime.combine(today, datetime.time(hours, minutes))

for time_str in times:	
	ast = parseLine(textline=time_str, pattern=time(), resultSoFar=[])	
	print time_str, " => ", to_time(ast)

Exemple #13
0
def codegen(obj):
    global in_ns, pointers, line, included
    ctype = obj[0]

    if type(obj) is code:
        return obj

    try:
        if ctype.line: line = ctype.line
    except: pass

    if ctype == "empty":
        return code(u"")

    if ctype == "in_ns":
        in_ns = obj[1][0]
        subtree = obj[1]
        for sel in subtree:
            codegen(sel)
        in_ns = u""
        return code(u"")

    elif ctype == "decl":
        name = u""
        for data in obj[1]:
            if type(data) is unicode or type(data) is str:
                name = data
                try:
                    yf = ymlFunc[name]
                    yf.alias
                except:
                    ymlFunc[name] = YF(name)
                    yf = ymlFunc[name]
                    if in_ns:
                        yf.alias = in_ns + u":" + name
                        if not enable_tracing:
                            if in_ns == "xsl" and (name == "debug" or name=="assert" or name[:7]=="_trace_"):
                                yf.alias = "-"
                                yf.addParm("skip1")
                                yf.addParm("skip2")
                                break
            elif type(data) is tuple or type(data) is Symbol:
                if data[0] == "base":
                    base = data[1][0]
                    try:
                        yf = ymlFunc[name] = ymlFunc[base].copy(name)
                    except KeyError:
                        if included:
                            raise KeyError(u"in " + included + u":" + u(line) + u": " + base + u" as base for " + name)
                        else:
                            raise KeyError(u"in " + u(line) + u": " + base + u" as base for " + name)
                elif data[0] == "shape":
                    shape = ymlFunc[data[1]]
                    try:
                        yf = ymlFunc[name]
                        yf.patch(shape)
                    except KeyError:
                        if included:
                            raise KeyError(u"in " + included + u":" + u(line) + u": " + base + u" as shape for " + name)
                        else:
                            raise KeyError(u"in " + u(line) + u": " + base + u" as shape for " + name)
                elif data[0] == "descend":
                    yf.addDescend(data[1])
                elif data[0] == "declParm":
                    l = data[1]
                    parmName = l[0]
                    if len(l)==1:
                        yf.addParm(parmName)
                    else:
                        value = l[1]
                        if parmName[0] != "%":
                            yf.addValue(parmName, value)
                        if parmName[0] == "*":
                            yf.pointers[parmName[1:]] = value
                            yf.addParm(parmName)
                        elif parmName[0] == "%":
                            if type(value) is unicode or type(value) is str:
                                yf.macros[parmName] = u(evalPython(value))
                            else:
                                yf.macros[parmName] = u(evalPython(u(value)))
                            yf.addParm(parmName)
                elif data[0] == "alias":
                    if in_ns:
                        yf.alias = in_ns + u":" + data[1][0]
                    else:
                        yf.alias = data[1][0]
                elif data[0] == "content":
                    yf.content = data[1]

        return code(u"")

    elif ctype == "funclist":
        result = u""
        for f in obj[1]:
            result += codegen(f)
        return code(result)

    elif ctype == "parentheses":
        if len(obj[1]):
            return codegen(('func', ['_parentheses', ('content', [obj[1][0]])]))
        else:
            return u""

    elif ctype == "fparm":
        if len(obj[1]):
            return codegen(('func', ['_parm', ('content', [obj[1][0]])]))
        else:
            return u""

    elif ctype == "generic":
        return codegen(('func', ['_generic', ('content', [obj[1][0]])]))

    elif ctype == "xbase":
        return codegen(('func', ['_base', ('content', [obj[1][0]])]))

    elif ctype == "func":
        avoidTag = False
        name = obj[1][0]

        if name == "decl":
            if ymlFunc[name] == "#error":
                if included:
                    raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in decl statement")
                else:
                    raise SyntaxError(u"in " + u(line) + u": syntax error in decl statement")
        if name == "define" or name == "operator":
            if ymlFunc[name] == "#error":
                if included:
                    raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in define statement")
                else:
                    raise SyntaxError(u"in " + u(line) + u": syntax error in define statement")

        if name[0] == "&":
            avoidTag = True
            name = name[1:]
        hasContent = False

        if len(name) > 2:
            if name[0:2] == "**":
                return code(eval('u'+pointer(name[1:])))

        if name[0] == "*":
            name = eval(pointer(name))
            if name[0] == "&":
                avoidTag = True
                name = name[1:]

        try:
            ymlFunc[name]
        except:
            try:
                ymlFunc["_"]
                return codegen(('func', ['_', ('content', [('funclist', [obj])])]))
            except:
                ymlFunc[name] = YF(name)
        
        if ymlFunc[name].alias == "-": avoidTag = True

        to_add = []
        if len(ymlFunc[name].descends):
            if obj[1][-1][0] != 'content':
                if included:
                    raise KeyError(u"in " + included + u":" + u(line) + u": " + name + u" has descending attributes, but no descendants are following")
                else:
                    raise KeyError(u"in " + u(line) + u": " + name + u" has descending attributes, but no descendants are following")

            def first_func(obj):
                if type(obj) is tuple or type(obj) is Symbol:
                    if obj[0] == 'func':
                        return obj
                    elif obj[0] == 'funclist':
                        return first_func(obj[1])
                    elif obj[0] == 'content':
                        return first_func(obj[1])
                    else:
                        return None
                elif type(obj) == list:
                    for e in obj:
                        f = first_func(e)
                        if f: return f
                    return None

            def copy_without_first_func(o, found = False):
                c = []
                for obj in o:
                    if found:
                        c.append(obj)
                    else:
                        if obj[0] == 'func':
                            if obj[1][-1][0] == 'content':
                                c.extend( obj[1][-1][1] )
                            found = True
                        else:
                            c.append( ( obj[0], copy_without_first_func(obj[1], False ) ) )
                return c

            def get_parms(obj):
                result = []
                for e in obj[1]:
                    if type(e) is tuple or type(e) is Symbol:
                        if e[0] == "parm":
                            result.append( e )
                return result

            try:
                add_params = get_parms(obj)
                for e in obj[1][-1][1]:
                    c = e[1]
                    for dname in ymlFunc[name].descends:
                        f, c = first_func(c), copy_without_first_func(c)
                        if dname[0] == "*":
                            pointers[dname[1:]] = "'" + f[1][0] + "'"
                        else:
                            add_params.append( ('parm', [dname, u"'" + f[1][0] + u"'"]) )
                        try:
                            add_params.extend( get_parms(f) )
                        except: pass

                    new_things = [ e[1][0] ]
                    new_things.extend( add_params )
                    new_things.append( ('content', c) )
                    
                    to_add.append( ('func', new_things ) )
            except:
                if included:
                    raise KeyError(u"in " + included + u":" + u(line) + u": " + name + u" has descending attributes, and too less descendants are following")
                else:
                    raise KeyError(u"in " + u(line) + u": " + name + u" has descending attributes, and too less descendants are following")

        if not to_add:
            to_add = ( obj, )

        complete = u""

        for obj in to_add:
            subtree = None
            try:
                if obj[1][-1][0] == "content":
                    subtree = obj[1][-1][1]
            except: pass
     
            if ymlFunc[name].content:
                hasContent = True
                treetemplate = deepcopy(ymlFunc[name].content)
                subtree = replaceContent(treetemplate, subtree)

            if subtree:
                hasContent = True

            hasContent, result = ymlFunc[name](obj[1], hasContent, avoidTag)

            if subtree:
                for sel in subtree:
                    result += codegen(sel)

            if hasContent and not(avoidTag):
                result += u"</" + ymlFunc[name].alias + u">"

            complete += result

        return code(complete)

    elif ctype == "textsection":
        result = u''
        ll = obj[1].splitlines()
        space = len(ll[-1]) - 2
        for l in ll[1:-1]:
            m = re.match(bqq, l)
            if m:
                cmd = m.group(1)
                try:
                    r, x = parseLine(cmd, _inner, [], True, comment)
                    if x: raise SyntaxError(cmd)
                    result += _finish(r)
                except SyntaxError:
                    if included:
                        raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in executing command: " + cmd.strip())
                    else:
                        raise SyntaxError(u"in " + u(line) + u": syntax error in executing command: " + cmd.strip())
            else:
                result += codegen(Symbol(u'lineQuote', u'| ' + l[space:]))
        return code(result)

    elif ctype == "textsectionu":
        result = u''
        ll = obj[1].splitlines()
        space = len(ll[-1]) - 2
        for l in ll[1:-1]:
            m = re.match(bqq, l)
            if m:
                cmd = m.group(1)
                try:
                    r, x = parseLine(cmd, _inner, [], True, comment)
                    if x: raise SyntaxError(cmd)
                    result += _finish(r)
                except SyntaxError:
                    if included:
                        raise SyntaxError(u"in " + included + u":" + u(line) + u": syntax error in executing command: " + cmd.strip())
                    else:
                        raise SyntaxError(u"in " + u(line) + u": syntax error in executing command: " + cmd.strip())
            else:
                if result != u'': result += u' '
                result += codegen(Symbol(u'quote', [u'> ' + l[space:]]))
        return code(result)

    elif ctype == "lineQuote" or ctype == "quote":
        m, text, base, inds = None, u"", 0, 0

        if ctype == "lineQuote":
            text = obj[1]
            m = lq.match(text)
            if m:
                inds = len(m.group(1))
                text = m.group(2)[1:]
            else: inds = 0
        elif ctype == "quote":
            inds = -1
            text = obj[1][0]
            m = sq.match(text)
            if m:
                if m.group(1):
                    inds = int(m.group(1))
                text = m.group(2)[1:]
            else:
                if type(text) is unicode or type(text) is str:
                    text = u(evalPython(text))

        ind = u""
        if inds > -1:
            try:
                cmd = evalPython(u"indent(" + u(inds) + u")")
                result, rest = parseLine(u(cmd), _inner, [], True, comment)
                if rest:
                    raise SyntaxError()
                ind = _finish(result)
            except: pass
        
        if ctype == "lineQuote": text += u"\n"

        hasTextFunc = False
        try:
            ymlFunc["text"]
            hasTextFunc = True
        except: pass

        text = executeCmd(text)
        return code(ind + text) 

    elif ctype == "tagQuote":
        m = tq.match(obj[1])
        if m.group(1) == "<":
            return code(u"<" + m.group(2))
        else:
            return code(m.group(2))

    elif ctype == "operator":
        operator.append((re.compile(evalPython(obj[1][0])), obj[1][1]))
        return code(u"")

    elif ctype == "constant":
        name = obj[1][0]
        if name[0] == "*":
            name = name[1:]
        value = obj[1][1]
        pointers[name] = value
        return code(u"")

    elif ctype == "include":
        reverse = False
        ktext, kxml = False, False
        for arg in obj[1]:
            if type(arg) is tuple or type(arg) is Symbol:
                if arg[0] == "reverse":
                    reverse = True
                elif arg[0] == "ktext":
                    ktext = True
                elif arg[0] == "kxml":
                    kxml = True
            elif type(arg) is unicode or type(arg) is str:
                filemask = arg

        if filemask[0] == '/' or filemask[0] == '.':
            files = sorted(glob(filemask))
        else:
            files = []
            for directory in includePath:
                path = os.path.join(directory, filemask)
                files.extend(sorted(glob(path)))

        if files and reverse:
            files = files[-1::-1]

        if not(files):
            if included:
                raise IOError(u"in " + included + ":" + u(line) + u": include file(s) '" + filemask + u"' not found")
            else:
                raise IOError(u"in " + u(line) + u": include file(s) '" + filemask + u"' not found")

        includeFile = fileinput.input(files, mode="rU", openhook=fileinput.hook_encoded(encoding))
        _included = included
        if ktext or kxml:
            text = u""
            for line in includeFile:
                included = includeFile.filename()
                if kxml:
                    if (not line[:6] == '<?xml ') and (not line[:6] == '<?XML '):
                        text += line
                else:
                    text += executeCmd(line)
            included = _included
            return code(text)
        else:
            result = parse(ymlCStyle(), includeFile, True, comment)
            included = u(filemask)
            x = _finish(result)
            included = _included
            return code(x)

    elif ctype == "pyExp":
        exp = obj[1][0]
        cmd = evalPython(exp)
        result, rest = parseLine(u(cmd), _inner, [], True, comment)
        if rest:
            raise SyntaxError(cmd)
        return code(_finish(result))

    elif ctype == "pythonCall":
        parms = []
        data = obj[1]
        for p in data:
            if type(p) is unicode or type(p) is str:
                name = p
            elif type(p) is tuple or type(p) is Symbol:
                ptype = p[0]
                if ptype == "parm":
                    if p[1][0][0] == "*":
                        parms.append(pointer(p[1][0]))
                    else:
                        parms.append(p[1][0])
        if len(parms) == 0:
            exp = name + u"()"
        elif len(parms) == 1:
            exp = name + u"(" + u(parms[0]) + u")"
        else:
            exp = name + u(tuple(parms))
        cmd = evalPython(exp)
        result, rest = parseLine(u(cmd), _inner, [], True, comment)
        if rest:
            raise SyntaxError()
        return code(_finish(result))

    else:
        return code(u"")
Exemple #14
0
def getTrueExpresion():
    string = u'True'
    ast = pyPEG.parseLine(string, EXPRESION,[],True,COMMENT)
    return ast[0][0]
Exemple #15
0
def parse(path):
    rslt = parseLine(path, pattern=jsonpath, resultSoFar = [], skipWS = True, skipComments = None, packrat = True)[0]
    return cst2json(rslt[0])
Exemple #16
0
def parse_time(time_str):
	ast = parseLine(textline=time_str, pattern=time(), resultSoFar=[])	
	return to_time(ast)