Skip to content

Commit

Permalink
fix: Incremental lexing (#132)
Browse files Browse the repository at this point in the history
* refactor: Tokenizer Class

* Update test
  • Loading branch information
nzakas authored Oct 4, 2024
1 parent b07acb3 commit cbf2cae
Show file tree
Hide file tree
Showing 9 changed files with 605 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
},
"range": [
0,
74
75
]
}
],
"range": [
0,
75
]
}
}
14 changes: 14 additions & 0 deletions js/src/char-code-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ export class CharCodeReader {
return this.#text.charCodeAt(this.#offset + 1);
}

/**
* Determines if the next character code in the text matches a specific character code.
* @param {(number) => boolean} fn A function to call on the next character.
* @returns {boolean} True if the next character code matches, false if not.
*/
match(fn) {
if (fn(this.peek())) {
this.next();
return true;
}

return false;
}

/**
* Returns the last character code read.
* @returns {number} The last character code read.
Expand Down
15 changes: 15 additions & 0 deletions js/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ export class UnexpectedChar extends ErrorWithLocation {
}
}

/**
* Error thrown when an unexpected identifier is found during tokenizing.
*/
export class UnexpectedIdentifier extends ErrorWithLocation {

/**
* Creates a new instance.
* @param {string} unexpected The character that was found.
* @param {Location} loc The location information for the found character.
*/
constructor(unexpected, loc) {
super(`Unexpected identifier '${ unexpected }' found.`, loc);
}
}

/**
* Error thrown when an unexpected token is found during parsing.
*/
Expand Down
Loading

0 comments on commit cbf2cae

Please sign in to comment.