summaryrefslogtreecommitdiff
path: root/test/racc/assets/liquor.y
blob: 8045a072a425b897557bd969726ef0f83d324266 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# Copyright (c) 2012-2013 Peter Zotov  <whitequark@whitequark.org>
#              2012 Yaroslav Markin  <yaroslav@markin.net>
#              2012 Nate Gadgibalaev  <nat@xnsv.ru>
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class Liquor::Parser
  token comma dot endtag ident integer keyword lblock lblock2 lbracket
        linterp lparen op_div op_eq op_gt op_geq op_lt op_leq op_minus
        op_mod op_mul op_neq op_not op_plus pipe plaintext rblock
        rbracket rinterp rparen string tag_ident

  prechigh
    left dot
    nonassoc op_uminus op_not
    left op_mul op_div op_mod
    left op_plus op_minus
    left op_eq op_neq op_lt op_leq op_gt op_geq
    left op_and
    left op_or
  preclow

  expect 15

  start block

rule
  block: /* empty */
      { result = [] }
    | plaintext block
      { result = [ val[0], *val[1] ] }
    | interp block
      { result = [ val[0], *val[1] ] }
    | tag block
      { result = [ val[0], *val[1] ] }

  interp:
      linterp expr rinterp
      { result = [ :interp, retag(val), val[1] ] }
    | linterp filter_chain rinterp
      { result = [ :interp, retag(val), val[1] ] }

  primary_expr:
      ident
    | lparen expr rparen
      { result = [ val[1][0], retag(val), *val[1][2..-1] ] }

  expr:
      integer
    | string
    | tuple
    | ident function_args
      { result = [ :call,   retag(val), val[0], val[1] ] }
    | expr lbracket expr rbracket
      { result = [ :index,  retag(val), val[0], val[2] ] }
    | expr dot ident function_args
      { result = [ :external, retag(val), val[0], val[2], val[3] ] }
    | expr dot ident
      { result = [ :external, retag(val), val[0], val[2], nil ] }
    | op_minus expr =op_uminus
      { result = [ :uminus, retag(val), val[1] ] }
    | op_not expr
      { result = [ :not, retag(val), val[1] ] }
    | expr op_mul expr
      { result = [ :mul, retag(val), val[0], val[2] ] }
    | expr op_div expr
      { result = [ :div, retag(val), val[0], val[2] ] }
    | expr op_mod expr
      { result = [ :mod, retag(val), val[0], val[2] ] }
    | expr op_plus expr
      { result = [ :plus, retag(val), val[0], val[2] ] }
    | expr op_minus expr
      { result = [ :minus, retag(val), val[0], val[2] ] }
    | expr op_eq expr
      { result = [ :eq, retag(val), val[0], val[2] ] }
    | expr op_neq expr
      { result = [ :neq, retag(val), val[0], val[2] ] }
    | expr op_lt expr
      { result = [ :lt, retag(val), val[0], val[2] ] }
    | expr op_leq expr
      { result = [ :leq, retag(val), val[0], val[2] ] }
    | expr op_gt expr
      { result = [ :gt, retag(val), val[0], val[2] ] }
    | expr op_geq expr
      { result = [ :geq, retag(val), val[0], val[2] ] }
    | expr op_and expr
      { result = [ :and, retag(val), val[0], val[2] ] }
    | expr op_or expr
      { result = [ :or, retag(val), val[0], val[2] ] }
    | primary_expr

  tuple:
      lbracket tuple_content rbracket
      { result = [ :tuple, retag(val), val[1].compact ] }

  tuple_content:
      expr comma tuple_content
      { result = [ val[0], *val[2] ] }
    | expr
      { result = [ val[0] ] }
    | /* empty */
      { result = [ ] }

  function_args:
      lparen function_args_inside rparen
      { result = [ :args, retag(val), *val[1] ] }

  function_args_inside:
      expr function_keywords
      { result = [ val[0], val[1][2] ] }
    | function_keywords
      { result = [ nil,    val[0][2] ] }

  function_keywords:
      keyword expr function_keywords
      { name = val[0][2].to_sym
        tail = val[2][2]
        loc  = retag([ val[0], val[1] ])

        if tail.include? name
          @errors << SyntaxError.new("duplicate keyword argument `#{val[0][2]}'",
              tail[name][1])
        end

        hash = {
          name => [ val[1][0], loc, *val[1][2..-1] ]
        }.merge(tail)

        result = [ :keywords, retag([ loc, val[2] ]), hash ]
      }
    | /* empty */
      { result = [ :keywords, nil, {} ] }

  filter_chain:
      expr pipe filter_chain_cont
      { result = [ val[0], *val[2] ].
            reduce { |tree, node| node[3][2] = tree; node }
      }

  filter_chain_cont:
      filter_call pipe filter_chain_cont
      { result = [ val[0], *val[2] ] }
    | filter_call
      { result = [ val[0] ] }

  filter_call:
      ident function_keywords
      { ident_loc = val[0][1]
        empty_args_loc = { line:  ident_loc[:line],
                           start: ident_loc[:end] + 1,
                           end:   ident_loc[:end] + 1, }
        result = [ :call, val[0][1], val[0],
                   [ :args, val[1][1] || empty_args_loc, nil, val[1][2] ] ]
      }

  tag:
      lblock ident expr tag_first_cont
      { result = [ :tag, retag(val), val[1], val[2], *reduce_tag_args(val[3][2]) ] }
    | lblock ident tag_first_cont
      { result = [ :tag, retag(val), val[1], nil,    *reduce_tag_args(val[2][2]) ] }

  # Racc cannot do lookahead across rules. I had to add states
  # explicitly to avoid S/R conflicts. You are not expected to
  # understand this.

  tag_first_cont:
      rblock
      { result = [ :cont,  retag(val), [] ] }
    | keyword tag_first_cont2
      { result = [ :cont,  retag(val), [ val[0], *val[1][2] ] ] }

  tag_first_cont2:
      rblock block lblock2 tag_next_cont
      { result = [ :cont2, val[0][1],  [ [:block, val[0][1], val[1] ], *val[3] ] ] }
    | expr tag_first_cont
      { result = [ :cont2, retag(val), [ val[0], *val[1][2] ] ] }

  tag_next_cont:
      endtag rblock
      { result = [] }
    | keyword tag_next_cont2
      { result = [ val[0], *val[1] ] }

  tag_next_cont2:
      rblock block lblock2 tag_next_cont
      { result = [ [:block, val[0][1], val[1] ], *val[3] ] }
    | expr keyword tag_next_cont3
      { result = [ val[0], val[1], *val[2] ] }

  tag_next_cont3:
      rblock block lblock2 tag_next_cont
      { result = [ [:block, val[0][1], val[1] ], *val[3] ] }
    | expr tag_next_cont
      { result = [ val[0], *val[1] ] }

---- inner
  attr_reader :errors, :ast

  def initialize(tags={})
    super()

    @errors = []
    @ast    = nil
    @tags   = tags
  end

  def success?
    @errors.empty?
  end

  def parse(string, name='(code)')
    @errors.clear
    @name = name
    @ast  = nil

    begin
      @stream = Lexer.lex(string, @name, @tags)
      @ast = do_parse
    rescue Liquor::SyntaxError => e
      @errors << e
    end

    success?
  end

  def next_token
    tok = @stream.shift
    [ tok[0], tok ] if tok
  end

  TOKEN_NAME_MAP = {
    :comma    => ',',
    :dot      => '.',
    :lblock   => '{%',
    :rblock   => '%}',
    :linterp  => '{{',
    :rinterp  => '}}',
    :lbracket => '[',
    :rbracket => ']',
    :lparen   => '(',
    :rparen   => ')',
    :pipe     => '|',
    :op_not   => '!',
    :op_mul   => '*',
    :op_div   => '/',
    :op_mod   => '%',
    :op_plus  => '+',
    :op_minus => '-',
    :op_eq    => '==',
    :op_neq   => '!=',
    :op_lt    => '<',
    :op_leq   => '<=',
    :op_gt    => '>',
    :op_geq   => '>=',
    :keyword  => 'keyword argument name',
    :kwarg    => 'keyword argument',
    :ident    => 'identifier',
  }

  def on_error(error_token_id, error_token, value_stack)
    if token_to_str(error_token_id) == "$end"
      raise Liquor::SyntaxError.new("unexpected end of program", {
        file: @name
      })
    else
      type, (loc, value) = error_token
      type = TOKEN_NAME_MAP[type] || type

      raise Liquor::SyntaxError.new("unexpected token `#{type}'", loc)
    end
  end

  def retag(nodes)
    loc = nodes.map { |node| node[1] }.compact
    first, *, last = loc
    return first if last.nil?

    {
      file:  first[:file],
      line:  first[:line],
      start: first[:start],
      end:    last[:end],
    }
  end

  def reduce_tag_args(list)
    list.each_slice(2).reduce([]) { |args, (k, v)|
      if v[0] == :block
        args << [ :blockarg, retag([ k, v ]), k, v[2] || [] ]
      else
        args << [ :kwarg,    retag([ k, v ]), k, v          ]
      end
    }
  end