Index: lib/snippets.js |
=================================================================== |
--- a/lib/snippets.js |
+++ b/lib/snippets.js |
@@ -70,12 +70,66 @@ |
let filter = Filter.fromText(text); |
if (filter.isActiveOnDomain(domain) && |
!ElemHide.getException(filter, domain)) |
{ |
result.push(filter); |
} |
} |
return result; |
+ }, |
+ |
+ /** |
+ * Parses a script and returns a list of all its commands and their arguments |
+ * @param {string} script |
+ * @return {Array.<string[]>} |
Manish Jethani
2018/04/26 13:25:17
I had to use Array.<string[]> instead of string[][
|
+ */ |
+ parseScript(script) |
+ { |
+ let tree = []; |
+ |
+ let escape = false; |
+ let literal = false; |
+ |
+ let call = []; |
+ let argument = ""; |
+ |
+ for (let character of [...script.trim(), ";"]) |
Manish Jethani
2018/04/26 13:25:17
The parsing here is as per the specification in Tr
|
+ { |
+ if (escape) |
+ { |
+ escape = false; |
+ |
+ argument += character; |
+ } |
+ else if (character == "\\") |
+ { |
+ escape = true; |
+ } |
+ else if (character == "'") |
+ { |
+ literal = !literal; |
+ } |
+ else if (literal || character != ";" && !/\s/u.test(character)) |
+ { |
+ argument += character; |
+ } |
+ else |
+ { |
+ if (argument) |
+ { |
+ call.push(argument); |
+ argument = ""; |
+ } |
+ |
+ if (character == ";" && call.length > 0) |
+ { |
+ tree.push(call); |
+ call = []; |
+ } |
+ } |
+ } |
+ |
+ return tree; |
} |
}; |
exports.Snippets = Snippets; |