I developed a lexical analyzer function which gets a string and separate the items in string in an array like this :
const lexer = (str) =>
str
.split(" ")
.map((s) => s.trim())
.filter((s) => s.length);
console.log(lexer("John Doe")) // outputs ["John" , "Doe"]
Now I want to develop a lexical analyzer with javascript to analyze types , something like this :
if (foo) {
bar();
}
and return the output like this :
[
{
lexeme: 'if',
type: 'keyword',
position: {
row: 0,
col: 0
}
},
{
lexeme: '(',
type: 'open_paran',
position: {
row: 0,
col: 3
}
},
{
lexeme: 'foo',
type: 'identifier',
position: {
row: 0,
col: 4
}
},
...
]
How can I develop a lexical analyzer with javascript to identify types ?
Thanks in advance .