Quantcast
Channel: Recent Gists from assertchris
Viewing all articles
Browse latest Browse all 30

gist:9bc8fa85d694be22004e

$
0
0
gistfile1.txt
class Lexer {
get patterns() {
return {
"whitespace": "\\s+",
"type": "int",
"assign": "=",
"identity": "[a-z]+",
"value": "[0-9]+"
};
}
analyse(code = "") {
let tokens = [];
while(true) {
let length = code.length;
for (let key in this.patterns) {
let pattern = new RegExp(
"^(" + this.patterns[key] + ")"
);
let matches = code.match(pattern);
if (matches) {
tokens.push([
key, matches[1]
]);
code = code.substring(matches[1].length);
}
}
if (length == code.length) {
break;
}
}
return {
tokens,
code,
};
}
}
let lexer = new Lexer();
let result = lexer.analyse("int minutes = 90");
console.log(result);

Viewing all articles
Browse latest Browse all 30

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>