I am writing a lexer for a programming language.
I have managed to detect all characters but spaces (not general whitespaces, just spaces).
I’ve tried just using letter == " " and letter === " ".
I’ve also looked at some SO question that recommend using some Regex, but that also didn’t work.
Relevant code snippet (this is part of a bigger Lexer class):
generateOutput(input) {
const wordList = input.split(' ');
const keyword = wordList[0];
const identifier = wordList[1];
const output = [
"[KEYWORD] " + keyword.toUpperCase(),
"[IDENTIFIER] " + identifier
]
for (let i = 2; i < wordList.length; i++) {
const word = wordList[i];
const words = word.split("");
words.forEach((letter) => {
if (upperCase.includes(letter)) {
output.push("[KEY] U_" + letter)
} else if (lowerCase.includes(letter)) {
output.push("[KEY] L_" + letter)
} else if (symbols.includes(letter)) {
output.push("[SYMBOL] " + letter)
} else if (letter === " ") {
output.push("[KEY] SPACE")
}
})
}
return output
}
>Solution :
You used input.split(' '), like I said in my comment. That deleted all of the spaces.
var a = "this is a sentence"
console.log( a.split( " " ) );
//["this","is","a","sentence"] <- contains absolutely no spaces