diff --git a/Resources/OSL Ast.js b/Resources/OSL Ast.js index c60a710c..dafa9526 100644 --- a/Resources/OSL Ast.js +++ b/Resources/OSL Ast.js @@ -40,6 +40,7 @@ function tokenise(CODE) { let letter = 0; let depth = ""; let brackets = 0; + let b_depth = 0; let out = []; let split = []; let escaped = false; @@ -47,22 +48,22 @@ function tokenise(CODE) { while (letter < len) { depth = CODE[letter]; + if (brackets === 0 && !escaped) { + if (depth === "[" || depth === "{") b_depth ++ + if (depth === "]" || depth === "}") b_depth -- + } if (depth === '"' && !escaped) { brackets = 1 - brackets; out.push('"'); - } else if (depth === '\\') { + } else if (depth === '\\' && !escaped) { escaped = !escaped; out.push("\\"); } else { out.push(depth); escaped = false; } - if (brackets === 0) { - if (depth === "[" || depth === "{") b_depth ++ - if (depth === "]" || depth === "}") b_depth -- - } letter++; - + if (brackets === 0 && CODE[letter] === " " && b_depth === 0) { split.push(out.join("")); out = []; @@ -88,6 +89,11 @@ function tokenise(CODE) { class OSLComp { + + constructor() { + this.regex = /"[^"]+"|{[^}]+}|\[[^\]]+\]|[^."(]*\((?:(?:"[^"]+")*[^.]+)*|\d[\d.]+\d|[^." ]+/g; + } + evalToken(cur) { if ((cur[0] === "{" && cur[cur.length - 1] === "}") || (cur[0] === "[" && cur[cur.length - 1] === "]")) { try { @@ -172,7 +178,7 @@ function tokenise(CODE) { return { type: "log", data: cur } } else if (["|", "&", "<<", ">>", "^^"].indexOf(cur) !== -1) { return { type: "bit", data: cur } - } else if (cur.indexOf(".") !== -1) { + } else if ((cur.split("(",1)[0] ?? cur).indexOf(".") !== -1) { let method = cur.match(this.regex) for (let i = 0; i < method.length; i++) { method[i] = this.evalToken(method[i]) @@ -194,7 +200,6 @@ function tokenise(CODE) { let ast = [] let tokens = autoTokenise(CODE) - console.log(tokens) for (let i = 0; i < tokens.length; i++) { const cur = tokens[i] ast.push(this.evalToken(cur)) @@ -232,5 +237,5 @@ function tokenise(CODE) { let comp = new OSLComp() console.log(JSON.stringify(comp.generateAST({ - CODE: "wow = [var * 10]" + CODE: "jn input_name.matchregex(\"/[^.]+\\.[^.]+/gm\") 10" }), null, 4))