~corey.bryant/ubuntu/wily/python-pyscss/thedac

« back to all changes in this revision

Viewing changes to scss/src/grammar/grammar.g

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-06-26 12:10:36 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140626121036-3dv13zn5zptk9fpx
Tags: 1.2.0.post3-1
* Team upload.
* New upstream release (Closes: #738776).
* Added a debian/gbp.conf.
* Added missing ${python:Depends}
* Added Python 3 support.
* Removed duplicate debhelper build-depends.
* Cannonical VCS URLs.
* Standards-Version: is now 3.9.5.
* Added a watch file.
* override dh helpers which the package doesn't use.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# python yapps2.py grammar.g grammar.py
2
 
 
3
 
 
4
 
_units = ['em', 'ex', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'deg', 'rad'
5
 
          'grad', 'ms', 's', 'hz', 'khz', '%']
6
 
_inv = lambda s: s
7
 
ParserValue = lambda s: s
8
 
NumberValue = lambda s: float(s)
9
 
StringValue = lambda s: s
10
 
QuotedStringValue = lambda s: s
11
 
BooleanValue = lambda s: bool(s)
12
 
ColorValue = lambda s: s
13
 
class ListValue():
14
 
    def __init__(self, v):
15
 
        if isinstance(v, self.__class__):
16
 
            self.v = v
17
 
        else:
18
 
            self.v = {0: v}
19
 
    def first(self):
20
 
        return self.v[0]
21
 
    def __len__(self):
22
 
        return len(self.v)
23
 
 
24
 
 
25
 
def _reorder_list(lst):
26
 
    return dict((i if isinstance(k, int) else k, v) for i, (k, v) in enumerate(sorted(lst.items())))
27
 
 
28
 
 
29
 
def interpolate(v, R):
30
 
    return v
31
 
 
32
 
 
33
 
def call(fn, args, R, function=True):
34
 
    print 'call: ', fn, args
35
 
    return args
36
 
 
37
 
################################################################################
38
 
#'(?<!\\s)(?:' + '|'.join(_units) + ')(?![-\\w])'
39
 
## Grammar compiled using Yapps:
40
 
%%
41
 
parser Calculator:
42
 
    ignore: "[ \r\t\n]+"
43
 
    token COMMA: ","
44
 
    token LPAR: "\\(|\\["
45
 
    token RPAR: "\\)|\\]"
46
 
    token END: "$"
47
 
    token MUL: "[*]"
48
 
    token DIV: "/"
49
 
    token ADD: "[+]"
50
 
    token SUB: "-\s"
51
 
    token SIGN: "-(?![a-zA-Z_])"
52
 
    token AND: "(?<![-\w])and(?![-\w])"
53
 
    token OR: "(?<![-\w])or(?![-\w])"
54
 
    token NOT: "(?<![-\w])not(?![-\w])"
55
 
    token NE: "!="
56
 
    token INV: "!"
57
 
    token EQ: "=="
58
 
    token LE: "<="
59
 
    token GE: ">="
60
 
    token LT: "<"
61
 
    token GT: ">"
62
 
    token STR: "'[^']*'"
63
 
    token QSTR: '"[^"]*"'
64
 
    token UNITS: "(?<!\s)(?:px|cm|mm|hz|%)(?![-\w])"
65
 
    token NUM: "(?:\d+(?:\.\d*)?|\.\d+)"
66
 
    token BOOL: "(?<![-\w])(?:true|false)(?![-\w])"
67
 
    token COLOR: "#(?:[a-fA-F0-9]{6}|[a-fA-F0-9]{3})(?![a-fA-F0-9])"
68
 
    token VAR: "\$[-a-zA-Z0-9_]+"
69
 
    token FNCT: "[-a-zA-Z_][-a-zA-Z0-9_]*(?=\()"
70
 
    token ID: "[-a-zA-Z_][-a-zA-Z0-9_]*"
71
 
    rule goal<<R>>:         expr_lst<<R>>                   {{ v = expr_lst.first() if len(expr_lst) == 1 else expr_lst }}
72
 
                              END                           {{ return v }}
73
 
    rule expr<<R>>:         and_test<<R>>                   {{ v = and_test }}
74
 
                              (
75
 
                                  OR and_test<<R>>          {{ v = and_test if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) else (v or and_test) }}
76
 
                              )*                            {{ return v }}
77
 
    rule and_test<<R>>:     not_test<<R>>                   {{ v = not_test }}
78
 
                              (
79
 
                                  AND not_test<<R>>         {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) else (v and not_test) }}
80
 
                              )*                            {{ return v }}
81
 
    rule not_test<<R>>:     comparison<<R>>                 {{ return comparison }}
82
 
                              |
83
 
                              (
84
 
                                  NOT not_test<<R>>         {{ v = 'undefined' if isinstance(not_test, basestring) and (not_test == 'undefined' or not_test.startswith('$')) else (not not_test) }}
85
 
                                  |
86
 
                                  INV not_test<<R>>         {{ v = 'undefined' if isinstance(not_test, basestring) and (not_test == 'undefined' or not_test.startswith('$')) else _inv('!', not_test) }}
87
 
                              )+                            {{ return v }}
88
 
    rule comparison<<R>>:   a_expr<<R>>                     {{ v = a_expr }}
89
 
                              (
90
 
                                  LT a_expr<<R>>            {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(a_expr, basestring) and (a_expr == 'undefined' or a_expr.startswith('$')) else (v < a_expr) }}
91
 
                                  |
92
 
                                  GT a_expr<<R>>            {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(a_expr, basestring) and (a_expr == 'undefined' or a_expr.startswith('$')) else (v > a_expr) }}
93
 
                                  |
94
 
                                  LE a_expr<<R>>            {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(a_expr, basestring) and (a_expr == 'undefined' or a_expr.startswith('$')) else (v <= a_expr) }}
95
 
                                  |
96
 
                                  GE a_expr<<R>>            {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(a_expr, basestring) and (a_expr == 'undefined' or a_expr.startswith('$')) else (v >= a_expr) }}
97
 
                                  |
98
 
                                  EQ a_expr<<R>>            {{ v = (None if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) else v) == (None if isinstance(a_expr, basestring) and (a_expr == 'undefined' or a_expr.startswith('$')) else a_expr) }}
99
 
                                  |
100
 
                                  NE a_expr<<R>>            {{ v = (None if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) else v) != (None if isinstance(a_expr, basestring) and (a_expr == 'undefined' or a_expr.startswith('$')) else a_expr) }}
101
 
                              )*                            {{ return v }}
102
 
    rule a_expr<<R>>:       m_expr<<R>>                     {{ v = m_expr }}
103
 
                              (
104
 
                                  ADD m_expr<<R>>           {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(m_expr, basestring) and (m_expr == 'undefined' or m_expr.startswith('$')) else (v + m_expr) }}
105
 
                                  |
106
 
                                  SUB m_expr<<R>>           {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(m_expr, basestring) and (m_expr == 'undefined' or m_expr.startswith('$')) else (v - m_expr) }}
107
 
                              )*                            {{ return v }}
108
 
    rule m_expr<<R>>:       u_expr<<R>>                     {{ v = u_expr }}
109
 
                              (
110
 
                                  MUL u_expr<<R>>           {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(u_expr, basestring) and (u_expr == 'undefined' or u_expr.startswith('$')) else (v * u_expr) }}
111
 
                                  |
112
 
                                  DIV u_expr<<R>>           {{ v = 'undefined' if isinstance(v, basestring) and (v == 'undefined' or v.startswith('$')) or isinstance(u_expr, basestring) and (u_expr == 'undefined' or u_expr.startswith('$')) else (v / u_expr) }}
113
 
                              )*                            {{ return v }}
114
 
    rule u_expr<<R>>:       SIGN u_expr<<R>>                {{ return 'undefined' if isinstance(u_expr, basestring) and (u_expr == 'undefined' or u_expr.startswith('$')) else _inv('-', u_expr) }}
115
 
                              |
116
 
                              ADD u_expr<<R>>               {{ return 'undefined' if isinstance(u_expr, basestring) and (u_expr == 'undefined' or u_expr.startswith('$')) else u_expr }}
117
 
                              |
118
 
                              atom<<R>>                     {{ v = atom }}
119
 
                              [
120
 
                                  UNITS                     {{ v = call(UNITS, ListValue(ParserValue({0: v, 1: UNITS})), R, False) }}
121
 
                              ]                             {{ return v }}
122
 
    rule atom<<R>>:         LPAR expr_lst<<R>> RPAR         {{ return expr_lst.first() if len(expr_lst) == 1 else expr_lst }}
123
 
                              |
124
 
                              ID                            {{ return ID }}
125
 
                              |
126
 
                              FNCT                          {{ v = None }}
127
 
                              LPAR [
128
 
                                  expr_lst<<R>>             {{ v = expr_lst }}
129
 
                              ] RPAR                        {{ return call(FNCT, v, R) }}
130
 
                              |
131
 
                              NUM                           {{ return NumberValue(ParserValue(NUM)) }}
132
 
                              |
133
 
                              STR                           {{ return StringValue(ParserValue(STR)) }}
134
 
                              |
135
 
                              QSTR                          {{ return QuotedStringValue(ParserValue(QSTR)) }}
136
 
                              |
137
 
                              BOOL                          {{ return BooleanValue(ParserValue(BOOL)) }}
138
 
                              |
139
 
                              COLOR                         {{ return ColorValue(ParserValue(COLOR)) }}
140
 
                              |
141
 
                              VAR                           {{ return interpolate(VAR, R) }}
142
 
    rule expr_lst<<R>>:                                     {{ n = None }}
143
 
                              [
144
 
                                  VAR [
145
 
                                      ":"                   {{ n = VAR }}
146
 
                                  ]                         {{ else: self._rewind() }}
147
 
                              ]
148
 
                              expr_slst<<R>>                {{ v = {n or 0: expr_slst} }}
149
 
                              (                             {{ n = None }}
150
 
                                  COMMA                     {{ v['_'] = COMMA }}
151
 
                                  [
152
 
                                      VAR [
153
 
                                          ":"               {{ n = VAR }}
154
 
                                      ]                     {{ else: self._rewind() }}
155
 
                                  ]
156
 
                                  expr_slst<<R>>            {{ v[n or len(v)] = expr_slst }}
157
 
                              )*                            {{ return ListValue(ParserValue(v)) }}
158
 
    rule expr_slst<<R>>:    expr<<R>>                       {{ v = {0: expr} }}
159
 
                              (
160
 
                                  expr<<R>>                 {{ v[len(v)] = expr }}
161
 
                              )*                            {{ return ListValue(ParserValue(v)) if len(v) > 1 else v[0] }}
162
 
%%
163
 
    expr_lst_rsts_ = None
164
 
 
165
 
### Grammar ends.
166
 
################################################################################
167
 
 
168
 
P = Calculator(CalculatorScanner())
169
 
 
170
 
 
171
 
def parse(rule, text, *args):
172
 
    P.reset(text)
173
 
    return wrap_error_reporter(P, rule, *args)
174
 
 
175
 
 
176
 
if __name__ == '__main__':
177
 
    while True:
178
 
        try:
179
 
            s = raw_input('>>> ')
180
 
        except EOFError:
181
 
            break
182
 
        if not s.strip():
183
 
            break
184
 
        print parse('goal', s, None)
185
 
    print 'Bye.'