tokeniser should handle negative and decimal place numbers

This commit is contained in:
Kurt Hutten IrevDev
2022-11-26 20:38:07 +11:00
parent 149633a5cb
commit 83b8333694
2 changed files with 50 additions and 5 deletions

View File

@ -1,4 +1,6 @@
const NUMBER = /^[0-9]+/
// regular expression for number that includes a decimal point or starts with a minus sign
const NUMBER = /^-?\d+(\.\d+)?/
const WHITESPACE = /\s+/
const WORD = /^[a-zA-Z_][a-zA-Z0-9_]*/
// regex that captures everything between two non escaped quotes and the quotes aren't captured in the match
@ -58,9 +60,6 @@ const makeToken = (
const returnTokenAtIndex = (str: string, startIndex: number): Token | null => {
const strFromIndex = str.slice(startIndex)
if (isOperator(strFromIndex)) {
return makeToken('operator', matchFirst(strFromIndex, OPERATOR), startIndex)
}
if (isString(strFromIndex)) {
return makeToken('string', matchFirst(strFromIndex, STRING), startIndex)
}
@ -82,6 +81,9 @@ const returnTokenAtIndex = (str: string, startIndex: number): Token | null => {
if (isNumber(strFromIndex)) {
return makeToken('number', matchFirst(strFromIndex, NUMBER), startIndex)
}
if (isOperator(strFromIndex)) {
return makeToken('operator', matchFirst(strFromIndex, OPERATOR), startIndex)
}
if (isWord(strFromIndex)) {
return makeToken('word', matchFirst(strFromIndex, WORD), startIndex)
}