Files
Linux.js---javascript-OS/rootfs overrides/usr/lib/sheebr/lexer.js
T
2022-02-08 13:35:49 +01:00

63 lines
1.0 KiB
JavaScript

//@ts-check
const { isAlpha } = require('./utils')
class Lexer{
/**
* @param { string } source
*/
constructor(source){
/** @type { string[] } */
this.tokens = [];
this.current = 0;
this.start = 0;
this.source = source
}
scanTokens(){
while(this.current < this.source.length){
this.start = this.current
this.scanToken()
}
return this.tokens;
}
scanToken(){
if(this.source[this.current] == "\""){
this.string()
}
else{
let content = ""
while(this.source[this.current] != ' ' && this.source[this.current] != '\n' && this.current < this.source.length ){
content += this.source[this.current];
this.current++;
}
this.tokens.push(content)
}
this.current++;
}
string(){
this.current++;
let content = ""
while(this.source[this.current] != "\"" && this.current < this.source.length){
content += this.source[this.current];
this.current++;
}
this.tokens.push(content)
}
//addToken(tokenType, value=""){
// this.tokens.push(Token(tokenType, value))
//}
}
module.exports = {
Lexer
}