Skip to content

Commit

Permalink
error message now contains line and column
Browse files Browse the repository at this point in the history
  • Loading branch information
lm committed Feb 26, 2012
1 parent 45ff1fd commit c0c6035
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
16 changes: 12 additions & 4 deletions Json/Json.st
@@ -1,5 +1,5 @@
Object subclass: Json [
| stream |
| stream line linePosition |

<category: 'json'>
<comment: 'This class reads and writes JSON format data - strings, numbers, boolean, nil, arrays and dictionaries. See http://json.org.'>
Expand Down Expand Up @@ -64,6 +64,8 @@ Object subclass: Json [
<category: 'initialising'>
stream := anObject.
line := 1.
linePosition := 0.
]
stream [
Expand Down Expand Up @@ -147,8 +149,10 @@ Object subclass: Json [
| char |
[(char := stream peek) notNil and: [char isSeparator]]
whileTrue: [stream next].
[(char := stream peek) notNil and: [char isSeparator]] whileTrue: [
stream next = Character nl ifTrue: [
line := line + 1.
linePosition := stream position]].
]
nextDictionary [
Expand Down Expand Up @@ -271,6 +275,10 @@ Object subclass: Json [
<category: 'error raising'>
JsonSyntaxError signal:
'Expected %1 but %2 found' % {aString. currrentString ifNil: ['end of input']}
'Expected %1 but %2 found on line %3 column %4' %
{aString.
currrentString ifNil: ['end of input'].
line.
stream position - linePosition}
]
]
22 changes: 11 additions & 11 deletions tests/JsonTest.st
Expand Up @@ -116,17 +116,17 @@ TestCase subclass: JsonTest [

testErrorMessages [
self
parseJson: '[' error: 'Expected string, object, array, true, false, null or digit but end of input found';
parseJson: '{' error: 'Expected string but end of input found';
parseJson: '{""' error: 'Expected : but end of input found';
parseJson: 't' error: 'Expected true but t found';
parseJson: 'f' error: 'Expected false but f found';
parseJson: 'n' error: 'Expected null but n found';
parseJson: '"s' error: 'Expected character but end of input found';
parseJson: '"', Character nl asString, '"' error: 'Expected printable character but control character found';
parseJson: '"\uA' error: 'Expected 4 hex digits but A found';
parseJson: '"\x' error: 'Expected ", \, /, b, n, f, r, t, u but x found';
parseJson: '0 1' error: 'Expected end of input but 1 found'.
parseJson: '[' error: 'Expected string, object, array, true, false, null or digit but end of input found on line 1 column 1';
parseJson: '{' error: 'Expected string but end of input found on line 1 column 1';
parseJson: '{""' error: 'Expected : but end of input found on line 1 column 3';
parseJson: 't' error: 'Expected true but t found on line 1 column 1';
parseJson: 'f' error: 'Expected false but f found on line 1 column 1';
parseJson: 'n' error: 'Expected null but n found on line 1 column 1';
parseJson: '"s' error: 'Expected character but end of input found on line 1 column 2';
parseJson: '"', Character nl asString, '"' error: 'Expected printable character but control character found on line 1 column 2';
parseJson: '"\uA' error: 'Expected 4 hex digits but A found on line 1 column 4';
parseJson: '"\x' error: 'Expected ", \, /, b, n, f, r, t, u but x found on line 1 column 3';
parseJson: '0 1' error: 'Expected end of input but 1 found on line 1 column 2'.
]

parseJson: aString error: aMessageString [
Expand Down

0 comments on commit c0c6035

Please sign in to comment.