mirror of
https://github.com/Pomax/BezierInfo-2.git
synced 2025-08-25 17:42:46 +02:00
meh lexer
This commit is contained in:
91
docs/js/custom-element/lib/lexer.js
Normal file
91
docs/js/custom-element/lib/lexer.js
Normal file
@@ -0,0 +1,91 @@
|
||||
function splitSymbols(v) {
|
||||
if (v.match(/\w/)) return v;
|
||||
return v.split(``);
|
||||
}
|
||||
|
||||
class Lexer {
|
||||
constructor(code) {
|
||||
this.scope = 0;
|
||||
this.pos = 0;
|
||||
this.tokens = code.split(/\b/).map(splitSymbols).flat();
|
||||
this.scopes = [];
|
||||
console.log(this.tokens);
|
||||
}
|
||||
|
||||
parse() {
|
||||
while (this.pos < this.tokens.length) {
|
||||
let token = this.tokens[this.pos++];
|
||||
|
||||
if ([`const`, `let`, "var"].includes(token)) {
|
||||
this.parseVariable(token);
|
||||
}
|
||||
|
||||
// ...
|
||||
else if ([`'`, `"`, "`"].includes(token)) {
|
||||
this.parseString(token);
|
||||
}
|
||||
|
||||
// ...
|
||||
else if (token === `(`) {
|
||||
let functor,
|
||||
i = 2;
|
||||
do {
|
||||
functor = this.tokens[this.pos - i++];
|
||||
} while (functor.match(/\s+/));
|
||||
|
||||
// TODO: maths is fun?
|
||||
|
||||
console.log(`[${this.scope}]: ${functor}(...`);
|
||||
} else if (token === `)`) {
|
||||
}
|
||||
|
||||
// ...
|
||||
else if (token === `{`) {
|
||||
this.scopes[this.pos] = ++this.scope;
|
||||
}
|
||||
|
||||
// ...
|
||||
else if (token === `}`) {
|
||||
this.scopes[this.pos] = --this.scope;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(this.scopes);
|
||||
}
|
||||
|
||||
parseVariable(type) {
|
||||
let name;
|
||||
do {
|
||||
name = this.tokens[this.pos++];
|
||||
} while (name.match(/\s+/));
|
||||
console.log(`[${this.scope}]: ${type} ${name}`);
|
||||
}
|
||||
|
||||
parseString(symbol) {
|
||||
// we technically don't really care about the contents
|
||||
// of strings, as they don't introduce new variables
|
||||
// or functions that we need to care about.
|
||||
let token;
|
||||
let buffer = [symbol];
|
||||
let blen = 1;
|
||||
do {
|
||||
token = this.tokens[this.pos++];
|
||||
buffer.push(token);
|
||||
blen++;
|
||||
} while (
|
||||
token !== symbol &&
|
||||
buffer[blen - 2] !== `\\` &&
|
||||
this.pos < this.tokens.length
|
||||
);
|
||||
// buffer = buffer.join(``);
|
||||
// if (symbol === "`") {
|
||||
// this.parseTemplateString(buffer);
|
||||
// }
|
||||
}
|
||||
|
||||
// parseTemplateString(buffer) {
|
||||
// // console.log(buffer);
|
||||
// }
|
||||
}
|
||||
|
||||
export { Lexer };
|
@@ -1,9 +1,8 @@
|
||||
import { GraphicsAPI } from "../api/graphics-api.js";
|
||||
|
||||
export default function performCodeSurgery(code) {
|
||||
// 0. strip out block comments and whitespace
|
||||
// 0. strip out superfluous whitespace
|
||||
|
||||
code = code.replace(/\\\*[\w\s\r\n]+?\*\\/, ``);
|
||||
code = code.replace(/\r?\n(\r?\n)+/, `\n`);
|
||||
|
||||
// 1. ensure that anything that needs to run by first calling its super function, does so.
|
||||
|
@@ -3,6 +3,10 @@
|
||||
* We're going to regexp our way to flawed victory here.
|
||||
*/
|
||||
export default function splitCodeSections(code) {
|
||||
// removs comments and superfluous white space.
|
||||
code = code.replace(/\\\*[\w\s\r\n]+?\*\\/, ``);
|
||||
code = code.replace(/\r?\n(\r?\n)+/, `\n`);
|
||||
|
||||
const re = /\b[\w\W][^\s]*?\([^)]*\)[\r\n\s]*{/;
|
||||
const cuts = [];
|
||||
for (let result = code.match(re); result; result = code.match(re)) {
|
||||
|
Reference in New Issue
Block a user