Blame view

fsabuilder/morfeuszbuilder/segrules/preprocessor.py 3.69 KB
Michał Lenart authored
1
# -*- coding:utf-8 -*-
Michał Lenart authored
2
3
4
5
6
7
8
'''
Created on 23 sty 2014

@author: mlenart
'''
import re
from pyparsing import *
Michał Lenart authored
9
from morfeuszbuilder.utils import exceptions
Michał Lenart authored
10
from pyparseString import pyparseString
Michał Lenart authored
11
Michał Lenart authored
12
identifier = Word(alphas, bodyChars=alphanums+u'_>*+{},')
Michał Lenart authored
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
define = Keyword('#define').suppress() + identifier + Optional(Suppress('(') + identifier + Suppress(')')) + restOfLine + LineEnd() + StringEnd()
ifdef = Keyword('#ifdef').suppress() + identifier + LineEnd() + StringEnd()
endif = Keyword('#endif').suppress() + LineEnd() + StringEnd()

class NonArgDefine(object):

    def __init__(self, name, val):
        self.name = name
        self.val = val

    def hasArg(self):
        return False

class ArgDefine(object):

    def __init__(self, name, arg, val):
        self.name = name
        self.arg = arg
        self.val = val

    def hasArg(self):
        return True

    def __str__(self):
        return '%s(%s) %s' % (self.name, self.arg, self.val)

def _tryToSubstituteArgDefine(s, t, defines):
    defineName = t[0]
    substituteValue = t[1]
    if defineName in defines and defines[defineName].hasArg():
        define = defines[defineName]
        return re.sub(r'\b%s\b' % define.arg, substituteValue, define.val)
    elif defineName in defines:
        return '%s ( %s )' % (defines[defineName].val, substituteValue)
    else:
        return ' '.join(t)

def _tryToSubstituteNonArgDefine(s, t, defines):
    defineName = t[0]

    if defineName in defines and not defines[defineName].hasArg():
        return defines[defineName].val
    else:
        return defineName
Michał Lenart authored
58
def _processLine(lineNum, line, defines, filename):
Michał Lenart authored
59
60
61
62
63
    if line.strip():

        rule = Forward()
        defineInstance = Forward()
        localId = identifier.copy()
Michał Lenart authored
64
        weakLiteral = CaselessLiteral('!weak')
Michał Lenart authored
65
Michał Lenart authored
66
        rule << OneOrMore(defineInstance ^ localId ^ Word('*|+?>') ^ (Literal('(') + rule + Literal(')')) ^ weakLiteral)
Michał Lenart authored
67
68
69
70
71
        defineInstance << localId + Suppress('(') + rule + Suppress(')')

        rule.setParseAction(lambda s, l, t: ' '.join(t))
        defineInstance.setParseAction(lambda s, l, t: _tryToSubstituteArgDefine(s, t, defines))
        localId.setParseAction(lambda s, l, t: _tryToSubstituteNonArgDefine(s, t, defines))
Michał Lenart authored
72
        return pyparseString(rule, lineNum, line, filename)[0]
Michał Lenart authored
73
74
75
    else:
        return line
Michał Lenart authored
76
def preprocess(inputLines, defs, filename):
Michał Lenart authored
77
78
    defines = {}
    ifdefsStack = []
Michał Lenart authored
79
    for lineNum, line in inputLines:
Michał Lenart authored
80
        if line.startswith('#define'):
Michał Lenart authored
81
            parsedDefine = list(pyparseString(define, lineNum, line, filename))
Michał Lenart authored
82
83
84
85
86
87
88
            if len(parsedDefine) == 2:
                name, val = parsedDefine
                defines[name] = NonArgDefine(name, val)
            else:
                name, arg, val = parsedDefine
                localDefines = defines.copy()
                localDefines[arg] = NonArgDefine(arg, arg)
Michał Lenart authored
89
                val = _processLine(lineNum, val, localDefines, filename)
Michał Lenart authored
90
                defines[name] = ArgDefine(name, arg, val)
Michał Lenart authored
91
        elif line.startswith('#ifdef'):
Michał Lenart authored
92
            name = pyparseString(ifdef, lineNum, line, filename)[0]
Michał Lenart authored
93
            ifdefsStack.append((name, True))
Michał Lenart authored
94
        elif line.startswith('#else'):
Michał Lenart authored
95
96
            name, isActive = ifdefsStack[-1]
            assert isActive
Michał Lenart authored
97
            ifdefsStack[-1] = (name, False)
Michał Lenart authored
98
#             ifdefsStack.pop()
Michał Lenart authored
99
        elif line.startswith('#endif'):
Michał Lenart authored
100
            ifdefsStack.pop()
Michał Lenart authored
101
102
        elif line.startswith('#'):
            yield lineNum, line
Michał Lenart authored
103
        elif len(ifdefsStack) == 0 or all(map(lambda (name, isActive): (name in defs and isActive) or (name not in defs and not isActive), ifdefsStack)):
Michał Lenart authored
104
            yield lineNum, _processLine(lineNum, line, defines, filename)
Michał Lenart authored
105