Skip to content

Commit 22c3d85

Browse files
author
Yorick Peterse
committed
Include lines/columns in parser error messages.
1 parent 3ccdac1 commit 22c3d85

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/ll/parser.rb

+14
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ def s(*args)
135135
return AST::Node.new(*args)
136136
end
137137

138+
##
139+
# @see [LL::Driver#parser_error]
140+
#
141+
def parser_error(stack_type, stack_value, token_type, token_value)
142+
message = parser_error_message(stack_type, stack_value, token_type)
143+
144+
if token_value.is_a?(LL::Token)
145+
sl = token_value.source_line
146+
message += " (line #{sl.line}, column #{sl.column})"
147+
end
148+
149+
raise ParserError, message
150+
end
151+
138152
def _rule_0(val)
139153
s(:grammar, val[0])
140154
end

lib/ll/parser.rll

+14
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,18 @@ rule
160160
def s(*args)
161161
return AST::Node.new(*args)
162162
end
163+
164+
##
165+
# @see [LL::Driver#parser_error]
166+
#
167+
def parser_error(stack_type, stack_value, token_type, token_value)
168+
message = parser_error_message(stack_type, stack_value, token_type)
169+
170+
if token_value.is_a?(LL::Token)
171+
sl = token_value.source_line
172+
message += " (line #{sl.line}, column #{sl.column})"
173+
end
174+
175+
raise ParserError, message
176+
end
163177
}

spec/ll/parser_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
block.should raise_error(
3636
LL::ParserError,
37-
'Unexpected T_IDENT, expected T_COLON instead'
37+
'Unexpected T_IDENT, expected T_COLON instead (line 1, column 11)'
3838
)
3939
end
4040
end

0 commit comments

Comments
 (0)