summaryrefslogtreecommitdiff
path: root/scss/expression.parsley
blob: 8fc35bffac348f6ccef4030a2aecfcd45871c0aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Character classes
### NOTE: These definitions are implemented in Python as an optimization,
### because lots of | has the most parsley overhead.  The Python
### implementations are intended to match the behavior of the rules below.
#_space = ' ' | '\r' | '\n' | '\f' | '\t'
#ws = < _space+ >
#ows = < _space* >
#hex = DIGIT | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
#letterish = letter | '_' | '-'

escape = '\\' (
        '\n' -> ''
        # TODO this is supposed to replace junk with FFFD
        | <hex{1,6}>:cp ws? -> unichr(int(cp, 16))
        # TODO i think this should be...  more specific?  ascii + valid unicode?
        # see also: rx.rb
        | anything
    )


### Tokens
identifier = < letterish (letterish | digit)* >
number = < digit+ ('.' digit*)? | '.' digit+ >
variable = < '$' identifier >


### Components
expression = comma_list

comma_list = spaced_list:head (
        ows ',' ows
        spaced_list:tail -> tail
    )*:tails -> ListLiteral([head] + tails) if tails else head

spaced_list = single_expression:head (
        ows
        single_expression:tail -> tail
    )*:tails -> ListLiteral([head] + tails, comma=False) if tails else head


single_expression = or_test  ^(single expression)

or_test = and_test:head (
        'o' 'r'
        and_test:tail -> tail
    )*:tails -> AnyOp(*[head] + tails) if tails else head

and_test = not_test:head (
        'a' 'n' 'd'
        not_test:tail -> tail
    )*:tails -> AllOp(*[head] + tails) if tails else head

not_test = comparison | ( 'n' 'o' 't' not_test:node -> NotOp(node) )

comparison = add_expr:node (
        ows (
              '<' '='   !(operator.le):op
            | '>' '='   !(operator.ge):op
            | '<'       !(operator.lt):op
            | '>'       !(operator.gt):op
            | '=' '='   !(operator.eq):op
            | '!' '='   !(operator.ne):op
        ) ows
        add_expr:operand
        !(BinaryOp(op, node, operand)):node
    )* -> node

add_expr = mult_expr:node (
        ows (
              '+'  !(operator.add):op
            | '-'  !(operator.sub):op
        ) ows
        mult_expr:operand
        !(BinaryOp(op, node, operand)):node
    )* -> node

mult_expr = unary_expr:node (
        ows (
              '*'  !(operator.mul):op
            | '/'  !(operator.truediv):op
        ) ows
        unary_expr:operand
        !(BinaryOp(op, node, operand)):node
    )* -> node

unary_expr = (
        '-' unary_expr:node -> UnaryOp(operator.neg, node)
        | '+' unary_expr:node -> UnaryOp(operator.pos, node)
        | atom
    )

atom = (
        # Parenthesized expression
        '(' comma_list:node ')' -> Parentheses(node)

        # Old xCSS-style parenthesized expression
        # TODO kill this off
        | '[' comma_list:node ']' -> Parentheses(node)

        # Map literal
        | map

        # URL literal
        | 'u' 'r' 'l' '(' inside_url:s ')' -> FunctionLiteral('url', s)

        # Function call
        | identifier:name '(' argspec:args ')' -> CallOp(name, args)

        # Bareword
        | < '!'? identifier >:word -> Literal(parse_bareword(word))

        # Number
        | number:number < '%' | letter+ >?:unit -> Literal(Number(float(number), unit=unit))

        # String
        | string

        # Color literal
        # DEVIATION: Sass doesn't support alpha in hex literals
        | '#' (
            <hex{2}>:red <hex{2}>:green <hex{2}>:blue
            <hex{2}>?:alpha
                -> Literal(Color.from_rgb(
                    int(red, 16) / 255.,
                    int(green, 16) / 255.,
                    int(blue, 16) / 255.,
                    int(alpha or "ff", 16) / 255.,
                    original_literal="#" + red + green + blue + (alpha or '')))
            | hex:red hex:green hex:blue hex?:alpha
                -> Literal(Color.from_rgb(
                    int(red, 16) / 15.,
                    int(green, 16) / 15.,
                    int(blue, 16) / 15.,
                    int(alpha or "f", 16) / 15.,
                    original_literal="#" + red + green + blue + (alpha or '')))
        )

        # Variable
        | variable:name -> Variable(name)
    )
    ^(single value)


### Map literals
map = '(' ows
    ( map_pair:pair ows ',' ows -> pair )*:pairs
    ( map_pair:pair ows !(pairs.append(pair)) )?
    ')'
    -> MapLiteral(pairs)

map_pair =
    single_expression:key
    ows ':' ows
    spaced_list:value
    -> (key, value)


### Strings, literals, and interpolation
# TODO I'm not entirely sure any of these character classes are correct
# TODO ruby sass appears to preserve escapes

inside_url = (
        interpolation:node -> node
        | variable:name -> Variable(name)
        | (
            escape
            | '#' ~'{'
            | anything:ch ?(ord(ch) > 32 and ch not in ' !"#$\'()') -> ch
        )+:s -> Literal(String(''.join(s), quotes=None))
        | string_part(')'):s -> Literal(String(s, quotes=None))
    )*:nodes
    -> Interpolation(nodes, quotes=None)

string = (
        '"' string_contents('"' '"'):node '"' -> node
        | '\'' string_contents('\'' '\''):node '\'' -> node
    )

interpolation = '#' '{' expression:node '}' -> node

string_contents :end :quotes =
    (
        interpolation:node -> node
        | string_part(end):s -> Literal(String(s, quotes=quotes))
    )*:nodes
    -> Interpolation(nodes, quotes=quotes)

string_part :end = <(
        '#' ~'{'
        | anything:ch ?(ch not in ('#', end))
    )+>


### Function definitions and arguments
goal_argspec = argspec

argspec = (
        argspec_item:node
        ows ',' ows
        -> node
    )*:nodes
    ( argspec_item:tail ows !(nodes.append(tail)) )?
    -> ArgspecLiteral(nodes)


argspec_item =
    ows
    (
        ( variable:name ows ':' ows -> Variable(name) )?:name
        spaced_list:value
        -> (name, value)
    )