Class: Aspen::CustomGrammar::Parser
Instance Attribute Summary
#position, #tokens
Instance Method Summary
collapse
#expect, #first, #initialize, #last, #need, #next_token, parse, parse_code, #peek
Instance Method Details
#parse ⇒ Object
expression = { segment } segment = BARE || capture_segment capture_segment = OPEN_PARENS, type, VAR_NAME, CLOSE_PARENS type = { node CONTENT } || numeric | float | integer | string
#parse_bare_segment ⇒ Object
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_segment ⇒ Object
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_expression ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/aspen/custom_grammar/parser.rb', line 14
def parse_expression
segments = []
while segment = parse_segment
segments << segment
break if tokens[position].nil?
end
segments
end
|
#parse_segment ⇒ Object
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
|