Skip to content

Commit

Permalink
Include lines/columns in parser error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorick Peterse committed Feb 15, 2015
1 parent 3ccdac1 commit 22c3d85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/ll/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ 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

def _rule_0(val)
s(:grammar, val[0])
end
Expand Down
14 changes: 14 additions & 0 deletions lib/ll/parser.rll
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,18 @@ rule
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
}
2 changes: 1 addition & 1 deletion spec/ll/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

block.should raise_error(
LL::ParserError,
'Unexpected T_IDENT, expected T_COLON instead'
'Unexpected T_IDENT, expected T_COLON instead (line 1, column 11)'
)
end
end
Expand Down

0 comments on commit 22c3d85

Please sign in to comment.