-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparser.y
168 lines (136 loc) · 2.99 KB
/
parser.y
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
##
# Parser used for bootstrapping ruby-ll's own LL(1) parser.
#
class LL::Bootstrap::Parser
token T_RUBY T_NAME T_TERMINALS T_INNER T_HEADER T_IDENT T_EQUALS T_COLON T_PIPE
token T_EPSILON T_SEMICOLON
options no_result_var
rule
grammar
: elements { s(:grammar, val[0]) }
| /* none */ { s(:grammar) }
;
elements
: elements element { val[0] << val[1] }
| element { val }
;
element
: directive
| rule
;
directive
: name
| terminals
| inner
| header
;
# %name directives
name
: T_NAME ident T_SEMICOLON
{
s(:name, [val[1]], :source_line => val[0].source_line)
}
| T_NAME ident name_ns T_SEMICOLON
{
s(:name, [val[1], *val[2]], :source_line => val[0].source_line)
}
;
name_ns
: T_COLON T_COLON ident { [val[2]] }
| T_COLON T_COLON ident name_ns { [val[2]] + val[3] }
;
# %terminals directive
terminals
: T_TERMINALS idents T_SEMICOLON
{
s(:terminals, val[1], :source_line => val[0].source_line)
}
;
# Code directives
inner
: T_INNER ruby { s(:inner, [val[1]], :source_line => val[0].source_line) }
;
header
: T_HEADER ruby { s(:header, [val[1]], :source_line => val[0].source_line) }
;
# Generic identifiers
idents
: ident { val }
| idents ident { val[0] << val[1] }
;
ident
: T_IDENT { s(:ident, [val[0].value], :source_line => val[0].source_line) }
;
# Identifiers
idents_or_epsilon
: idents
{
s(:steps, val[0], :source_line => val[0][0].source_line)
}
| epsilon
{
s(:steps, [val[0]], :source_line => val[0].source_line)
}
;
epsilon
: T_EPSILON { s(:epsilon, [], :source_line => val[0].source_line) }
;
# Rules
branch
: idents_or_epsilon
{
s(:branch, [val[0]], :source_line => val[0].source_line)
}
| idents_or_epsilon ruby
{
s(:branch, [val[0], val[1]], :source_line => val[0].source_line)
}
;
branches
: branch { val }
| branch T_PIPE branches { [val[0]] + val[2] }
;
rule
: ident T_EQUALS branches T_SEMICOLON
{
s(:rule, [val[0], *val[2]], :source_line => val[0].source_line)
}
;
# Ruby code blocks
ruby
: T_RUBY { s(:ruby, [val[0].value], :source_line => val[0].source_line) }
end
---- inner
##
# @see [LL::Lexer#initialize]
#
def initialize(*args)
@lexer = Lexer.new(*args)
end
##
# Yields the next token from the lexer.
#
# @yieldparam [Array]
#
def yield_next_token
@lexer.advance do |token|
yield [token.type, token]
end
yield [false, false]
end
##
# @see [LL::AST::Node#initialize]
# @return [LL::AST::Node]
#
def s(*args)
return AST::Node.new(*args)
end
##
# Parses the input and returns the corresponding AST.
#
# @return [LL::AST::Node]
#
def parse
return yyparse(self, :yield_next_token)
end
# vim: set ft=racc: