Class: Aspen::CustomGrammar::Parser

Inherits:
AbstractParser show all
Defined in:
lib/aspen/custom_grammar/parser.rb

Instance Attribute Summary

Attributes inherited from AbstractParser

#position, #tokens

Instance Method Summary collapse

Methods inherited from AbstractParser

#expect, #first, #initialize, #last, #need, #next_token, parse, parse_code, #peek

Constructor Details

This class inherits a constructor from Aspen::AbstractParser

Instance Method Details

#parseObject

expression = { segment } segment = BARE || capture_segment capture_segment = OPEN_PARENS, type, VAR_NAME, CLOSE_PARENS type = { node CONTENT } || numeric | float | integer | string



10
11
12
# File 'lib/aspen/custom_grammar/parser.rb', line 10

def parse
  Aspen::CustomGrammar::AST::Nodes::Expression.new(parse_expression)
end

#parse_bare_segmentObject



31
32
33
34
35
# File 'lib/aspen/custom_grammar/parser.rb', line 31

def parse_bare_segment
  if content = expect(:BARE)
    Aspen::CustomGrammar::AST::Nodes::Bare.new(content.first.last)
  end
end

#parse_capture_segmentObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aspen/custom_grammar/parser.rb', line 37

def parse_capture_segment
  if (_, type, var_name, _ = expect(:OPEN_PARENS, :TYPE, :VAR_NAME, :CLOSE_PARENS))
    type_name, label = type.last

    Aspen::CustomGrammar::AST::Nodes::CaptureSegment.new(
      type:     type_name.to_sym,
      var_name: var_name.last,
      label:    label
    )
  end
end

#parse_expressionObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aspen/custom_grammar/parser.rb', line 14

def parse_expression
  segments = []

  # Make sure this returns on empty
  while segment = parse_segment
    segments << segment
    break if tokens[position].nil?
  end

  segments
end

#parse_segmentObject

Raises:



26
27
28
29
# File 'lib/aspen/custom_grammar/parser.rb', line 26

def parse_segment
  return parse_bare_segment || parse_capture_segment
  raise Aspen::ParseError, "Didn't match expected tokens, got\n\t#{upcoming.inspect}"
end