-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.rll
188 lines (153 loc) · 3.23 KB
/
parser.rll
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
%name LL::Parser;
%terminals T_RUBY T_NAME T_TERMINALS T_INNER T_HEADER T_IDENT T_EQUALS T_COLON;
%terminals T_PIPE T_EPSILON T_SEMICOLON T_STAR T_PLUS T_QUESTION;
%terminals T_LPAREN T_RPAREN;
grammar
= elements { s(:grammar, val[0]) }
;
elements
= element*
;
element
= rule
| name
| terminals
| inner
| header
;
# %name directives
#
# This handles the parsing of %name directives, which can either contain a
# single name (e.g. "Foo") or a Ruby namespace (e.g. "Foo::Bar").
name
= T_NAME ident namespace_part* T_SEMICOLON
{
s(:name, [val[1], *val[2]], :source_line => val[0].source_line)
}
;
namespace_part
= T_COLON T_COLON ident { val[2] }
;
# Generic Directives
terminals
= T_TERMINALS idents T_SEMICOLON
{
s(:terminals, val[1], :source_line => val[0].source_line)
}
;
# Code Directives
#
# These are directives that can be used to specify inline Ruby code (e.g. %inner
# and %header).
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+
;
ident
= T_IDENT
{
s(:ident, [val[0].value], :source_line => val[0].source_line)
}
;
ident_with_operator
# X and X+
= ident operator?
{
val[1] ? s(val[1][0], [val[0]], :source_line => val[1][1]) : val[0]
}
# (X Y Z)+
| T_LPAREN idents T_RPAREN operator
{
s(val[3][0], val[1], :source_line => val[0].source_line)
}
;
operator
= T_PLUS { [:plus, val[0].source_line] }
| T_STAR { [:star, val[0].source_line] }
| T_QUESTION { [:question, val[0].source_line] }
;
idents_or_epsilon
= ident_with_operator+
{
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 ruby?
{
steps = [val[0]]
steps << val[1] if val[1]
s(:branch, steps, :source_line => val[0].source_line)
}
;
ruby
= T_RUBY { s(:ruby, [val[0].value], :source_line => val[0].source_line) }
;
branches
= branch another_branch* { [val[0], *val[1]] }
;
another_branch
= T_PIPE branch { val[1] }
;
rule
= ident T_EQUALS branches T_SEMICOLON
{
s(:rule, [val[0], *val[2]], :source_line => val[0].source_line)
}
;
%inner
{
##
# @see [LL::Lexer#initialize]
#
def initialize(*args)
@lexer = Lexer.new(*args)
end
##
# @yieldparam [Symbol] type
# @yieldparam [String] value
#
def each_token
@lexer.advance do |token|
yield [token.type, token]
end
yield [-1, -1]
end
##
# @see [LL::AST::Node#initialize]
#
def s(*args)
return AST::Node.new(*args)
end
##
# @see [LL::Driver#parser_error]
#
def parser_error(stack_type, stack_value, token_type, token_value)
message = parser_error_message(stack_type, stack_value, token_type)
if token_value.is_a?(LL::Token)
sl = token_value.source_line
message += " (line #{sl.line}, column #{sl.column})"
end
raise ParserError, message
end
}