summaryrefslogtreecommitdiffstats
path: root/node_modules/fastparse
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/fastparse')
-rw-r--r--node_modules/fastparse/LICENSE7
-rw-r--r--node_modules/fastparse/README.md118
-rw-r--r--node_modules/fastparse/lib/Parser.js108
-rw-r--r--node_modules/fastparse/package.json66
4 files changed, 299 insertions, 0 deletions
diff --git a/node_modules/fastparse/LICENSE b/node_modules/fastparse/LICENSE
new file mode 100644
index 0000000..481b17d
--- /dev/null
+++ b/node_modules/fastparse/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2018 Tobias Koppers
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/node_modules/fastparse/README.md b/node_modules/fastparse/README.md
new file mode 100644
index 0000000..ee33c3b
--- /dev/null
+++ b/node_modules/fastparse/README.md
@@ -0,0 +1,118 @@
+# fastparse
+
+A very simple and stupid parser, based on a statemachine and regular expressions.
+
+It's not intended for complex languages. It's intended to easily write a simple parser for a simple language.
+
+
+
+## Usage
+
+Pass a description of statemachine to the constructor. The description must be in this form:
+
+``` javascript
+new Parser(description)
+
+description is {
+ // The key is the name of the state
+ // The value is an object containing possible transitions
+ "state-name": {
+ // The key is a regular expression
+ // If the regular expression matches the transition is executed
+ // The value can be "true", a other state name or a function
+
+ "a": true,
+ // true will make the parser stay in the current state
+
+ "b": "other-state-name",
+ // a string will make the parser transit to a new state
+
+ "[cde]": function(match, index, matchLength) {
+ // "match" will be the matched string
+ // "index" will be the position in the complete string
+ // "matchLength" will be "match.length"
+
+ // "this" will be the "context" passed to the "parse" method"
+
+ // A new state name (string) can be returned
+ return "other-state-name";
+ },
+
+ "([0-9]+)(\\.[0-9]+)?": function(match, first, second, index, matchLength) {
+ // groups can be used in the regular expression
+ // they will match to arguments "first", "second"
+ },
+
+ // the parser stops when it cannot match the string anymore
+
+ // order of keys is the order in which regular expressions are matched
+ // if the javascript runtime preserves the order of keys in an object
+ // (this is not standardized, but it's a de-facto standard)
+ }
+}
+```
+
+The statemachine is compiled down to a single regular expression per state. So basically the parsing work is delegated to the (native) regular expression logic of the javascript runtime.
+
+
+``` javascript
+Parser.prototype.parse(initialState: String, parsedString: String, context: Object)
+```
+
+`initialState`: state where the parser starts to parse.
+
+`parsedString`: the string which should be parsed.
+
+`context`: an object which can be used to save state and results. Available as `this` in transition functions.
+
+returns `context`
+
+
+
+
+## Example
+
+``` javascript
+var Parser = require("fastparse");
+
+// A simple parser that extracts @licence ... from comments in a JS file
+var parser = new Parser({
+ // The "source" state
+ "source": {
+ // matches comment start
+ "/\\*": "comment",
+ "//": "linecomment",
+
+ // this would be necessary for a complex language like JS
+ // but omitted here for simplicity
+ // "\"": "string1",
+ // "\'": "string2",
+ // "\/": "regexp"
+
+ },
+ // The "comment" state
+ "comment": {
+ "\\*/": "source",
+ "@licen[cs]e\\s((?:[^*\n]|\\*+[^*/\n])*)": function(match, licenseText) {
+ this.licences.push(licenseText.trim());
+ }
+ },
+ // The "linecomment" state
+ "linecomment": {
+ "\n": "source",
+ "@licen[cs]e\\s(.*)": function(match, licenseText) {
+ this.licences.push(licenseText.trim());
+ }
+ }
+});
+
+var licences = parser.parse("source", sourceCode, { licences: [] }).licences;
+
+console.log(licences);
+```
+
+
+
+## License
+
+MIT (http://www.opensource.org/licenses/mit-license.php)
diff --git a/node_modules/fastparse/lib/Parser.js b/node_modules/fastparse/lib/Parser.js
new file mode 100644
index 0000000..7f47c58
--- /dev/null
+++ b/node_modules/fastparse/lib/Parser.js
@@ -0,0 +1,108 @@
+/*
+ MIT License http://www.opensource.org/licenses/mit-license.php
+ Author Tobias Koppers @sokra
+*/
+
+function ignoreFunction() {}
+
+function createReturningFunction(value) {
+ return function() {
+ return value;
+ };
+}
+
+function Parser(states) {
+ this.states = this.compileStates(states);
+}
+
+Parser.prototype.compileStates = function(states) {
+ var result = {};
+ Object.keys(states).forEach(function(name) {
+ result[name] = this.compileState(states[name], states);
+ }, this);
+ return result;
+};
+
+Parser.prototype.compileState = function(state, states) {
+ var regExps = [];
+ function iterator(str, value) {
+ regExps.push({
+ groups: Parser.getGroupCount(str),
+ regExp: str,
+ value: value
+ });
+ }
+ function processState(statePart) {
+ if(Array.isArray(statePart)) {
+ statePart.forEach(processState);
+ } else if(typeof statePart === "object") {
+ Object.keys(statePart).forEach(function(key) {
+ iterator(key, statePart[key]);
+ });
+ } else if(typeof statePart === "string") {
+ processState(states[statePart]);
+ } else {
+ throw new Error("Unexpected 'state' format");
+ }
+ }
+ processState(state);
+ var total = regExps.map(function(r) {
+ return "(" + r.regExp + ")";
+ }).join("|");
+ var actions = [];
+ var pos = 1;
+ regExps.forEach(function(r) {
+ var fn;
+ if(typeof r.value === "function") {
+ fn = r.value;
+ } else if(typeof r.value === "string") {
+ fn = createReturningFunction(r.value);
+ } else {
+ fn = ignoreFunction;
+ }
+ actions.push({
+ name: r.regExp,
+ fn: fn,
+ pos: pos,
+ pos2: pos + r.groups + 1
+ });
+ pos += r.groups + 1;
+ });
+ return {
+ regExp: new RegExp(total, "g"),
+ actions: actions
+ };
+};
+
+Parser.getGroupCount = function(regExpStr) {
+ return new RegExp("(" + regExpStr + ")|^$").exec("").length - 2;
+};
+
+Parser.prototype.parse = function(initialState, string, context) {
+ context = context || {};
+ var currentState = initialState;
+ var currentIndex = 0;
+ for(;;) {
+ var state = this.states[currentState];
+ var regExp = state.regExp;
+ regExp.lastIndex = currentIndex;
+ var match = regExp.exec(string);
+ if(!match) return context;
+ var actions = state.actions;
+ currentIndex = state.regExp.lastIndex;
+ for(var i = 0; i < actions.length; i++) {
+ var action = actions[i];
+ if(match[action.pos]) {
+ var ret = action.fn.apply(context, Array.prototype.slice.call(match, action.pos, action.pos2).concat([state.regExp.lastIndex - match[0].length, match[0].length]));
+ if(ret) {
+ if(!(ret in this.states))
+ throw new Error("State '" + ret + "' doesn't exist");
+ currentState = ret;
+ }
+ break;
+ }
+ }
+ }
+};
+
+module.exports = Parser;
diff --git a/node_modules/fastparse/package.json b/node_modules/fastparse/package.json
new file mode 100644
index 0000000..950ad45
--- /dev/null
+++ b/node_modules/fastparse/package.json
@@ -0,0 +1,66 @@
+{
+ "_from": "fastparse@^1.1.2",
+ "_id": "fastparse@1.1.2",
+ "_inBundle": false,
+ "_integrity": "sha512-483XLLxTVIwWK3QTrMGRqUfUpoOs/0hbQrl2oz4J0pAcm3A3bu84wxTFqGqkJzewCLdME38xJLJAxBABfQT8sQ==",
+ "_location": "/fastparse",
+ "_phantomChildren": {},
+ "_requested": {
+ "type": "range",
+ "registry": true,
+ "raw": "fastparse@^1.1.2",
+ "name": "fastparse",
+ "escapedName": "fastparse",
+ "rawSpec": "^1.1.2",
+ "saveSpec": null,
+ "fetchSpec": "^1.1.2"
+ },
+ "_requiredBy": [
+ "/css-selector-tokenizer"
+ ],
+ "_resolved": "https://registry.npmjs.org/fastparse/-/fastparse-1.1.2.tgz",
+ "_shasum": "91728c5a5942eced8531283c79441ee4122c35a9",
+ "_spec": "fastparse@^1.1.2",
+ "_where": "/home/pruss/Dev/3-minute-website/node_modules/css-selector-tokenizer",
+ "author": {
+ "name": "Tobias Koppers @sokra"
+ },
+ "bugs": {
+ "url": "https://github.com/webpack/fastparse/issues"
+ },
+ "bundleDependencies": false,
+ "deprecated": false,
+ "description": "A very simple and stupid parser, based on a statemachine and regular expressions.",
+ "devDependencies": {
+ "coveralls": "^2.11.2",
+ "eslint": "^0.21.2",
+ "istanbul": "^0.3.14",
+ "mocha": "^2.2.5",
+ "should": "^6.0.3"
+ },
+ "files": [
+ "lib"
+ ],
+ "homepage": "https://github.com/webpack/fastparse",
+ "keywords": [
+ "parser",
+ "regexp"
+ ],
+ "license": "MIT",
+ "main": "lib/Parser.js",
+ "name": "fastparse",
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/webpack/fastparse.git"
+ },
+ "scripts": {
+ "cover": "istanbul cover node_modules/mocha/bin/_mocha",
+ "lint": "eslint lib",
+ "precover": "npm run lint",
+ "pretest": "npm run lint",
+ "publish-patch": "mocha && npm version patch && git push && git push --tags && npm publish",
+ "test": "mocha",
+ "travis": "npm run cover -- --report lcovonly"
+ },
+ "version": "1.1.2"
+}