Class: PEG::Grammar

Inherits:
Sequence show all
Defined in:
lib/peg.rb

Instance Attribute Summary

Attributes inherited from Rule

#children

Instance Method Summary collapse

Methods inherited from Sequence

#_inspect

Methods inherited from Rule

#inspect, #name, #parse, #result

Methods inherited from ValueObject

#==

Constructor Details

#initialize(source) ⇒ Grammar

Returns a new instance of Grammar.



267
268
269
270
# File 'lib/peg.rb', line 267

def initialize(source)
  @_nodes = peg_grammar.parse(source)
  @children = [ReferenceResolver.new(grammar).resolve]
end

Instance Method Details

#grammarObject



276
277
278
# File 'lib/peg.rb', line 276

def grammar
  GrammarGenerator.visit(@_nodes)
end

#match(source) ⇒ Object



272
273
274
# File 'lib/peg.rb', line 272

def match(source)
  @children[0].match(source)
end

#peg_grammarObject



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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/peg.rb', line 280

def peg_grammar
  end_of_line = Or.new(
                  Literal.new("\r\n"),
                  Literal.new("\n"),
                  Literal.new("\r"),
                )
  space = Or.new(Literal.new(" "), Literal.new("\t"), end_of_line)
  comment = Sequence.new(
              Literal.new('#'),
              ZeroOrMore.new(
                Sequence.new(Not.new(end_of_line), Regex.new('.')),
              ),
              end_of_line,
            )
  spacing = ZeroOrMore.new(Or.new(space, comment))

  and_ = Sequence.new(Literal.new('&'), spacing)
  not_ = Sequence.new(Literal.new('!'), spacing)
  slash = Sequence.new(Literal.new('/'), spacing)
  left_arrow = Sequence.new(Literal.new('<-'), spacing)
  question = Sequence.new(Literal.new('?'), spacing)
  star = Sequence.new(Literal.new('*'), spacing)
  plus = Sequence.new(Literal.new('+'), spacing)
  open = Sequence.new(Literal.new('('), spacing)
  close = Sequence.new(Literal.new(')'), spacing)
  dot = Sequence.new(Literal.new('.'), spacing).name('dot')

  # HACK these three rules are simplified
  literal = Sequence.new(
              Or.new(Regex.new("'.*?'"), Regex.new('".*?"')),
              spacing
            ).name('literal')
  class_ = Sequence.new(Regex.new('\[.*?\]'), spacing).name('class')
  identifier = Sequence.new(
                 Regex.new('[A-Za-z0-9_]+').name('identifier__regex'),
                 spacing
               ).name('identifier')

  primary = Or.new(
              Sequence.new(
                identifier,
                Not.new(left_arrow)
              ).name('primary__sequence'),
              Sequence.new(
                open,
                'EXPRESSION',  # paceholder for future substitution
                close
              ).name('primary__parens'),
              literal,
              class_,
              dot,
            ).name('primary')
  suffix = Sequence.new(
             primary,
             Optional.new(
               Or.new(question, star, plus)
             ).name('suffix__optional'),
           ).name('suffix')
  prefix = Sequence.new(
             Optional.new(
               Or.new(and_, not_)
             ).name('prefix__optional'),
             suffix
           ).name('prefix')
  sequence = ZeroOrMore.new(prefix).name('sequence')
  expression = Sequence.new(
                 sequence,
                 ZeroOrMore.new(
                   Sequence.new(
                     slash,
                     sequence
                   ).name('expression__sequence')
                 ).name('expression__zeroormore')
               ).name('expression')
  if primary.children[1].children[1] != 'EXPRESSION'
    raise 'Invalid PEG grammar'
  else
    primary.children[1].children[1] = expression
  end
  definition = Sequence.new(
                 identifier,
                 left_arrow,
                 expression
               ).name('definition')
  # In the original PEG paper `grammar` is specified as:
  #     grammar <- spacing definition+ end_of_file
  # but we skip `end_of_file` allowing the grammar to
  # match just a part of source in order to know where
  # the syntax error occured.
  grammar = Sequence.new(
              spacing,
              OneOrMore.new(definition).name('grammar__oneormore')
            ).name('grammar')

  grammar
end