Skip to content

Commit

Permalink
fixed column info
Browse files Browse the repository at this point in the history
  • Loading branch information
corno committed Feb 5, 2020
1 parent 04071a3 commit 18a9368
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
54 changes: 37 additions & 17 deletions src/CParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function assertUnreachable<RT>(_x: never): RT {
export type Options = {
trim?: boolean
normalize?: boolean
tab?: number
}

export enum GlobalStateType {
Expand Down Expand Up @@ -217,7 +218,7 @@ type Unicode = {
export class CParser {

private bufferCheckPosition = MAX_BUFFER_LENGTH
private codeOfCurrentCharacter = 0
private curChar = 0
closed = false
readonly opt: Options
public state: GlobalState = [GlobalStateType.OTHER, {
Expand Down Expand Up @@ -333,25 +334,44 @@ export class CParser {
public writeImp(chunk: string): void {

//initialize
let currentChunkIndex = -1 //start at the position just before the first character

//start at the position just before the first character
//because we are going to call next() once at the beginning
let currentChunkIndex = -1
let curChar: number = 0


const next = () => {

currentChunkIndex++

curChar = chunk.charCodeAt(currentChunkIndex)
this.position++
this.codeOfCurrentCharacter = curChar

if (curChar === WhitespaceChar.lineFeed) {
this.line++
this.column = 0
} else this.column++
this.curChar = curChar

if (DEBUG) console.log(currentChunkIndex, curChar, String.fromCharCode(curChar), getStateDescription(this.state), this.position, this.line, this.column)
if (!isNaN(curChar)) {
this.position++
switch (curChar) {
case WhitespaceChar.lineFeed:
this.line++
this.column = 0
break
case WhitespaceChar.carriageReturn:
break
case WhitespaceChar.tab:
const tab = (this.opt.tab) ? this.opt.tab : 4
this.column += tab
break
default:
this.column++
}
}
}
next()
while (true) {
if (isNaN(curChar)) {
return
}
if (DEBUG) console.log(currentChunkIndex, curChar, String.fromCharCode(curChar), getStateDescription(this.state))

const st = this.state

Expand All @@ -368,7 +388,7 @@ export class CParser {
if (isNaN(curChar)) {
return
}
if (DEBUG) console.log(currentChunkIndex, curChar, String.fromCharCode(curChar), 'number loop')
//if (DEBUG) console.log(currentChunkIndex, curChar, String.fromCharCode(curChar), 'number loop')

//first check if we are breaking out of a number. Can only be done by checking the character that comes directly after the number
if (curChar !== NumberChar.period
Expand Down Expand Up @@ -428,7 +448,7 @@ export class CParser {
}

STRING_BIGLOOP: while (true) {
if (DEBUG) console.log(currentChunkIndex, curChar, String.fromCharCode(curChar), 'string loop', $.slashed, $.textNode)
//if (DEBUG) console.log(currentChunkIndex, curChar, String.fromCharCode(curChar), 'string loop', $.slashed, $.textNode)

if (isNaN(curChar)) {
//end of the chunk
Expand Down Expand Up @@ -457,8 +477,8 @@ export class CParser {
else if (curChar === StringChar.u) {
// \uxxxx. meh!
$.unicode = {
charactersLeft: 4,
foundCharacters: ""
charactersLeft: 4,
foundCharacters: ""
}
}
else {
Expand Down Expand Up @@ -697,8 +717,8 @@ export class CParser {
er += `
Line: ${this.line}
Column: ${this.column}
Char: '${String.fromCharCode(this.codeOfCurrentCharacter)}'
Char#: ${this.codeOfCurrentCharacter}`
Char: '${String.fromCharCode(this.curChar)}'
Char#: ${this.curChar}`
const error = new Error(er)
this.state = [GlobalStateType.ERROR, { error: error }]
this.onerror.signal(error)
Expand All @@ -714,7 +734,7 @@ export class CParser {
return this
}

this.codeOfCurrentCharacter = 0
this.curChar = 0
this.state = [GlobalStateType.OTHER, { state: OtherState.EXPECTING_ROOTVALUE }]
this.closed = true
this.onend.signal()
Expand Down
27 changes: 14 additions & 13 deletions test/bass_clarinet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ const docs: {
just_a_string: {
text: '"a string"',
events: [
["value", "a string"],
["value", "a string", 1, 10],
["end", undefined],
["ready", undefined],
],
},
empty_array: {
text: '[]',
events: [
["openarray", undefined],
["closearray", undefined],
["openarray", undefined, 1, 1],
["closearray", undefined, 1, 2],
["end", undefined],
["ready", undefined]
]
Expand Down Expand Up @@ -271,7 +271,7 @@ const docs: {
["value", 'bar and all"'],
["key", "bar"],
["value", 'its "nice"'],
["closeobject", undefined, 1, 53],
["closeobject", undefined, 1, 47],
["end", undefined],
["ready", undefined]]
},
Expand All @@ -289,9 +289,9 @@ const docs: {
string_invalid_escape: {
text: '["and you can\'t escape thi\s"]',
events: [
["openarray", undefined],
["value", 'and you can\'t escape this'],
["closearray", undefined, 1, 31],
["openarray", undefined, 1, 1],
["value", 'and you can\'t escape this', 1, 28],
["closearray", undefined, 1, 29],
["end", undefined],
["ready", undefined]]
},
Expand Down Expand Up @@ -645,12 +645,12 @@ const docs: {
events: [
["openarray", undefined, 1, 1],
["openarray", undefined, 1, 2],
["value", 1, 1, 3],
["value", 2, 1, 5],
["value", 3, 1, 7],
["value", 1, 1, 4],
["value", 2, 1, 6],
["value", 3, 1, 8],
["closearray", undefined, 1, 8],
["openarray", undefined, 1, 10],
["value", 4, 1, 11],
["value", 4, 1, 12],
['error', undefined]]
},
incomplete_json_terminates_ending_in_comma: {
Expand Down Expand Up @@ -788,8 +788,9 @@ function doTest(doc_chunks: string[], expectedEvents: EventDefinition[]) {
//assert1(currentExpectedEvent[1] === value.message, '[' + currentExpectedEventIndex + '] value: [' + currentExpectedEvent[1] + '] got: [' + value + ']');
}
if (currentExpectedEvent[3] !== undefined) {
// assert(currentExpectedEvent[2] === parser.line, `expected linenumber ${currentExpectedEvent[2]} but found ${parser.line}`)
// assert(currentExpectedEvent[3] === parser.column, `expected column ${currentExpectedEvent[3]} but found ${parser.column}`)
//check line numbers
assert.ok(currentExpectedEvent[2] === parser.line, `expected linenumber ${currentExpectedEvent[2]} but found ${parser.line}`)
assert.ok(currentExpectedEvent[3] === parser.column, `expected column ${currentExpectedEvent[3]} but found ${parser.column}`)
}
}
};
Expand Down

0 comments on commit 18a9368

Please sign in to comment.