Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/snippets.js

Issue 29761597: Issue 6538, 6781 - Implement script parsing for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Move parseScript to top level Created April 26, 2018, 2:27 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/snippets.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/snippets.js
===================================================================
--- a/lib/snippets.js
+++ b/lib/snippets.js
@@ -74,8 +74,64 @@
result.push(filter);
}
}
return result;
}
};
exports.Snippets = Snippets;
+
+/**
+ * Parses a script and returns a list of all its commands and their arguments
+ * @param {string} script
+ * @return {Array.<string[]>}
+ */
+function parseScript(script)
+{
+ let tree = [];
+
+ let escape = false;
+ let literal = false;
+
+ let call = [];
+ let argument = "";
+
+ for (let character of [...script.trim(), ";"])
+ {
+ 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.parseScript = parseScript;
« no previous file with comments | « no previous file | test/snippets.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld