Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic line number and character support to exceptions #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/hjson/ast/nodes/any.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Any < Node

parser do
if punctuator?
fail SyntaxError, 'Any value does not expected %p' % char
fail SyntaxError.new(self, ('Any value does not expect %p' % char))
end
end
parser do
Expand Down
2 changes: 1 addition & 1 deletion lib/hjson/ast/nodes/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Array < Node
end
end
end
parser { fail EndOfInputError }
parser { fail EndOfInputError.new(self) }
end
end
end
8 changes: 4 additions & 4 deletions lib/hjson/ast/nodes/keyname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ class Keyname < Node
parser do
until eos?
if colon?
fail SyntaxError if payload.empty?
fail SyntaxError.new(self) if payload.empty?
if positive?(space) && space != payload.length
buffer.pos = start + space
fail SyntaxError
fail SyntaxError.new(self)
end
halt(payload)
elsif char <= ' '
fail SyntaxError if eos?
fail SyntaxError.new(self) if eos?
self.space = payload.length if negative?(space)
elsif punctuator?
fail SyntaxError
fail SyntaxError.new(self)
else
payload << char
end
Expand Down
5 changes: 4 additions & 1 deletion lib/hjson/ast/nodes/multiline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class Multiline < Node

parser do
loop do
fail SyntaxError unless char
unless char
fail SyntaxError.new(self)
end

if single_quote?
self.triple += 1
read
Expand Down
2 changes: 1 addition & 1 deletion lib/hjson/ast/nodes/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Object < Node
if without_braces?
halt(payload)
else
fail EndOfInputError
fail EndOfInputError.new(self)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/hjson/ast/nodes/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class String < Node

declare :payload, ''

parser { fail SyntaxError unless double_quote? }
parser { fail SyntaxError.new(self) unless double_quote? }
parser do
while read
halt(read && payload) if double_quote?
Expand Down Expand Up @@ -48,7 +48,7 @@ def read_escapee
def read_unicode
uffff = 4.times.reduce(0) do |i, _|
read
fail SyntaxError unless hex?
fail SyntaxError.new(self) unless hex?
hex = char.to_i(16)
break if hex.infinite?
i * 16 + hex
Expand Down
10 changes: 8 additions & 2 deletions lib/hjson/ast/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def initialize(buffer, **options)
assign_declared_vars!
end

def current_line_number
string[0..pos].count("\n") + 1
end

private

def assign_declared_vars!
Expand All @@ -62,8 +66,10 @@ def assign_declared_vars!

def validate(expected)
current = current_char
fail SyntaxError,
"Expected %p instead of %p" % [expected, current] if expected && expected != current

if expected && expected != current
fail SyntaxError.new(self, ("Expected %p instead of %p" % [expected, current]))
end
end

def halt(data = nil)
Expand Down
10 changes: 9 additions & 1 deletion lib/hjson/errors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module Hjson
Error = Class.new(StandardError)
class Error < StandardError
def initialize(parser, message = "")
message.prepend("L#{parser.current_line_number}:#{parser.charpos} ")
message.strip!

super(message)
end
end

SyntaxError = Class.new(Error)
EndOfInputError = Class.new(Error)
end