From 0c9d487048afc539b6d09ce2d615f4570f68d40d Mon Sep 17 00:00:00 2001 From: Run1t Date: Sun, 11 Nov 2018 16:33:54 +0100 Subject: [PATCH] copy parser and remove esquery from dependencies --- package-lock.json | 11 +- package.json | 4 +- src/index.ts | 18 +- src/matchers/index.ts | 5 +- src/matchers/scope.ts | 11 + src/parse.ts | 4 +- src/parser/parser.js | 2945 +++++++++++++++++++++++++++++++++++++++++ test/project.spec.ts | 4 +- tsconfig.json | 2 +- 9 files changed, 2977 insertions(+), 27 deletions(-) create mode 100644 src/matchers/scope.ts create mode 100644 src/parser/parser.js diff --git a/package-lock.json b/package-lock.json index 6f7675b..99191a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2209,14 +2209,6 @@ "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", "dev": true }, - "esquery": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.0.1.tgz", - "integrity": "sha512-SmiyZ5zIWH9VM+SRUReLS5Q8a7GxtRdxEBVZpm98rJM7Sb+A9DVCndXfkeFUd3byderg+EbDkfnevfCwynWaNA==", - "requires": { - "estraverse": "^4.0.0" - } - }, "esrecurse": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/esrecurse/-/esrecurse-4.2.1.tgz", @@ -2229,7 +2221,8 @@ "estraverse": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-4.2.0.tgz", - "integrity": "sha1-De4/7TH81GlhjOc0IJn8GvoL2xM=" + "integrity": "sha1-De4/7TH81GlhjOc0IJn8GvoL2xM=", + "dev": true }, "esutils": { "version": "2.0.2", diff --git a/package.json b/package.json index c5062ec..abc7ee8 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,7 @@ "test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand", "prepublishOnly": "npm run build" }, - "dependencies": { - "esquery": "^1.0.1" - }, + "dependencies": {}, "peerDependencies": { "typescript": "^3" }, diff --git a/src/index.ts b/src/index.ts index fab172f..926954f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,13 @@ // Dependencies: -import { createAST } from './ast'; -import { map } from './map'; -import { match } from './match'; -import { parse } from './parse'; -import { project } from './project'; -import { query } from './query'; -import { replace } from './replace'; -import { syntaxKindName } from './syntax-kind'; -import { TSQueryApi } from './tsquery-types'; +import { createAST } from "./ast"; +import { map } from "./map"; +import { match } from "./match"; +import { parse } from "./parse"; +import { project } from "./project"; +import { query } from "./query"; +import { replace } from "./replace"; +import { syntaxKindName } from "./syntax-kind"; +import { TSQueryApi } from "./tsquery-types"; const api = query; api.ast = createAST; diff --git a/src/matchers/index.ts b/src/matchers/index.ts index c490ff6..69cddc0 100644 --- a/src/matchers/index.ts +++ b/src/matchers/index.ts @@ -12,9 +12,11 @@ import { identifier } from './identifier'; import { matches } from './matches'; import { not } from './not'; import { nthChild, nthLastChild } from './nth-child'; +import { scope } from './scope'; import { adjacent, sibling } from './sibling'; import { wildcard } from './wildcard'; + export const MATCHERS: TSQueryMatchers = { adjacent, attribute, @@ -30,5 +32,6 @@ export const MATCHERS: TSQueryMatchers = { matches: matches('some'), not, sibling, - wildcard + wildcard, + scope }; diff --git a/src/matchers/scope.ts b/src/matchers/scope.ts new file mode 100644 index 0000000..d8a26f0 --- /dev/null +++ b/src/matchers/scope.ts @@ -0,0 +1,11 @@ +// Dependencies: +import { Node } from 'typescript'; +import { findMatches } from '../match'; +import { TSQuerySelectorNode } from '../tsquery-types'; + +export function scope (node: Node, selector: TSQuerySelectorNode, ancestry: Array): boolean { + if (findMatches(node, selector.right, ancestry)) { + return findMatches(ancestry[0], selector.left, ancestry.slice(1)); + } + return false; +} diff --git a/src/parse.ts b/src/parse.ts index f71982a..f509ff7 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,5 +1,5 @@ // Dependencies: -import * as esquery from 'esquery'; +import * as queryParser from './parser/parser.js'; import { SyntaxKind } from 'typescript'; import { TSQuerySelectorNode } from './tsquery-types'; @@ -7,7 +7,7 @@ import { TSQuerySelectorNode } from './tsquery-types'; const IDENTIFIER_QUERY = 'identifier'; export function parse (selector: string): TSQuerySelectorNode { - return validateParse(esquery.parse(selector)); + return validateParse(queryParser.parse(selector)); } function validateParse (selector: TSQuerySelectorNode): TSQuerySelectorNode { diff --git a/src/parser/parser.js b/src/parser/parser.js new file mode 100644 index 0000000..09edfa1 --- /dev/null +++ b/src/parser/parser.js @@ -0,0 +1,2945 @@ +var result = (function(){ + /* + * Generated by PEG.js 0.7.0. + * + * http://pegjs.majda.cz/ + */ + + function quote(s) { + /* + * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a + * string literal except for the closing quote character, backslash, + * carriage return, line separator, paragraph separator, and line feed. + * Any character may appear in the form of an escape sequence. + * + * For portability, we also escape escape all control and non-ASCII + * characters. Note that "\0" and "\v" escape sequences are not used + * because JSHint does not like the first and IE the second. + */ + return '"' + s + .replace(/\\/g, '\\\\') // backslash + .replace(/"/g, '\\"') // closing quote character + .replace(/\x08/g, '\\b') // backspace + .replace(/\t/g, '\\t') // horizontal tab + .replace(/\n/g, '\\n') // line feed + .replace(/\f/g, '\\f') // form feed + .replace(/\r/g, '\\r') // carriage return + .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape) + + '"'; + } + + var result = { + /* + * Parses the input with a generated parser. If the parsing is successfull, + * returns a value explicitly or implicitly specified by the grammar from + * which the parser was generated (see |PEG.buildParser|). If the parsing is + * unsuccessful, throws |PEG.parser.SyntaxError| describing the error. + */ + parse: function(input, startRule) { + var parseFunctions = { + "start": parse_start, + "_": parse__, + "identifierName": parse_identifierName, + "binaryOp": parse_binaryOp, + "selectors": parse_selectors, + "relativeSelectors": parse_relativeSelectors, + "selector": parse_selector, + "relativeSelector": parse_relativeSelector, + "sequence": parse_sequence, + "atom": parse_atom, + "wildcard": parse_wildcard, + "identifier": parse_identifier, + "attr": parse_attr, + "attrOps": parse_attrOps, + "attrEqOps": parse_attrEqOps, + "attrName": parse_attrName, + "attrValue": parse_attrValue, + "string": parse_string, + "number": parse_number, + "path": parse_path, + "type": parse_type, + "regex": parse_regex, + "field": parse_field, + "negation": parse_negation, + "matches": parse_matches, + "has": parse_has, + "firstChild": parse_firstChild, + "lastChild": parse_lastChild, + "nthChild": parse_nthChild, + "nthLastChild": parse_nthLastChild, + "class": parse_class, + "root": parse_root, + "scope": parse_scope + }; + + if (startRule !== undefined) { + if (parseFunctions[startRule] === undefined) { + throw new Error("Invalid rule name: " + quote(startRule) + "."); + } + } else { + startRule = "start"; + } + + var pos = 0; + var reportFailures = 0; + var rightmostFailuresPos = 0; + var rightmostFailuresExpected = []; + var cache = {}; + + function padLeft(input, padding, length) { + var result = input; + + var padLength = length - input.length; + for (var i = 0; i < padLength; i++) { + result = padding + result; + } + + return result; + } + + function escape(ch) { + var charCode = ch.charCodeAt(0); + var escapeChar; + var length; + + if (charCode <= 0xFF) { + escapeChar = 'x'; + length = 2; + } else { + escapeChar = 'u'; + length = 4; + } + + return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length); + } + + function matchFailed(failure) { + if (pos < rightmostFailuresPos) { + return; + } + + if (pos > rightmostFailuresPos) { + rightmostFailuresPos = pos; + rightmostFailuresExpected = []; + } + + rightmostFailuresExpected.push(failure); + } + + function parse_start() { + var cacheKey = "start@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + result0 = parse__(); + if (result0 !== null) { + result1 = parse_selectors(); + if (result1 !== null) { + result2 = parse__(); + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, ss) { return ss.length === 1 ? ss[0] : { type: 'matches', selectors: ss }; })(pos0, result0[1]); + } + if (result0 === null) { + pos = pos0; + } + if (result0 === null) { + pos0 = pos; + result0 = parse__(); + if (result0 !== null) { + result0 = (function(offset) { return void 0; })(pos0); + } + if (result0 === null) { + pos = pos0; + } + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse__() { + var cacheKey = "_@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1; + + result0 = []; + if (input.charCodeAt(pos) === 32) { + result1 = " "; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\" \""); + } + } + while (result1 !== null) { + result0.push(result1); + if (input.charCodeAt(pos) === 32) { + result1 = " "; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\" \""); + } + } + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_identifierName() { + var cacheKey = "identifierName@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1; + var pos0; + + pos0 = pos; + if (/^[^ [\],():#!=><~+.]/.test(input.charAt(pos))) { + result1 = input.charAt(pos); + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("[^ [\\],():#!=><~+.]"); + } + } + if (result1 !== null) { + result0 = []; + while (result1 !== null) { + result0.push(result1); + if (/^[^ [\],():#!=><~+.]/.test(input.charAt(pos))) { + result1 = input.charAt(pos); + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("[^ [\\],():#!=><~+.]"); + } + } + } + } else { + result0 = null; + } + if (result0 !== null) { + result0 = (function(offset, i) { return i.join(''); })(pos0, result0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_binaryOp() { + var cacheKey = "binaryOp@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + result0 = parse__(); + if (result0 !== null) { + if (input.charCodeAt(pos) === 62) { + result1 = ">"; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\">\""); + } + } + if (result1 !== null) { + result2 = parse__(); + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset) { return 'child'; })(pos0); + } + if (result0 === null) { + pos = pos0; + } + if (result0 === null) { + pos0 = pos; + pos1 = pos; + result0 = parse__(); + if (result0 !== null) { + if (input.charCodeAt(pos) === 126) { + result1 = "~"; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"~\""); + } + } + if (result1 !== null) { + result2 = parse__(); + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset) { return 'sibling'; })(pos0); + } + if (result0 === null) { + pos = pos0; + } + if (result0 === null) { + pos0 = pos; + pos1 = pos; + result0 = parse__(); + if (result0 !== null) { + if (input.charCodeAt(pos) === 43) { + result1 = "+"; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"+\""); + } + } + if (result1 !== null) { + result2 = parse__(); + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset) { return 'adjacent'; })(pos0); + } + if (result0 === null) { + pos = pos0; + } + if (result0 === null) { + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 32) { + result0 = " "; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\" \""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset) { return 'descendant'; })(pos0); + } + if (result0 === null) { + pos = pos0; + } + } + } + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_selectors() { + var cacheKey = "selectors@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4, result5; + var pos0, pos1, pos2; + + pos0 = pos; + pos1 = pos; + result0 = parse_selector(); + if (result0 !== null) { + result1 = []; + pos2 = pos; + result2 = parse__(); + if (result2 !== null) { + if (input.charCodeAt(pos) === 44) { + result3 = ","; + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("\",\""); + } + } + if (result3 !== null) { + result4 = parse__(); + if (result4 !== null) { + result5 = parse_selector(); + if (result5 !== null) { + result2 = [result2, result3, result4, result5]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + while (result2 !== null) { + result1.push(result2); + pos2 = pos; + result2 = parse__(); + if (result2 !== null) { + if (input.charCodeAt(pos) === 44) { + result3 = ","; + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("\",\""); + } + } + if (result3 !== null) { + result4 = parse__(); + if (result4 !== null) { + result5 = parse_selector(); + if (result5 !== null) { + result2 = [result2, result3, result4, result5]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, s, ss) { + return [s].concat(ss.map(function (s) { return s[3]; })); + })(pos0, result0[0], result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_relativeSelectors() { + var cacheKey = "relativeSelectors@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4, result5; + var pos0, pos1, pos2; + + pos0 = pos; + pos1 = pos; + result0 = parse_relativeSelector(); + if (result0 !== null) { + result1 = []; + pos2 = pos; + result2 = parse__(); + if (result2 !== null) { + if (input.charCodeAt(pos) === 44) { + result3 = ","; + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("\",\""); + } + } + if (result3 !== null) { + result4 = parse__(); + if (result4 !== null) { + result5 = parse_relativeSelector(); + if (result5 !== null) { + result2 = [result2, result3, result4, result5]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + while (result2 !== null) { + result1.push(result2); + pos2 = pos; + result2 = parse__(); + if (result2 !== null) { + if (input.charCodeAt(pos) === 44) { + result3 = ","; + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("\",\""); + } + } + if (result3 !== null) { + result4 = parse__(); + if (result4 !== null) { + result5 = parse_relativeSelector(); + if (result5 !== null) { + result2 = [result2, result3, result4, result5]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, s, ss) { + return [s].concat(ss.map(function (s) { return s[3]; })); + })(pos0, result0[0], result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_selector() { + var cacheKey = "selector@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3; + var pos0, pos1, pos2; + + pos0 = pos; + pos1 = pos; + result0 = parse_sequence(); + if (result0 !== null) { + result1 = []; + pos2 = pos; + result2 = parse_binaryOp(); + if (result2 !== null) { + result3 = parse_sequence(); + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + while (result2 !== null) { + result1.push(result2); + pos2 = pos; + result2 = parse_binaryOp(); + if (result2 !== null) { + result3 = parse_sequence(); + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, a, ops) { + return ops.reduce(function (memo, rhs) { + return { type: rhs[0], left: memo, right: rhs[1] }; + }, a); + })(pos0, result0[0], result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_relativeSelector() { + var cacheKey = "relativeSelector@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3; + var pos0, pos1, pos2; + + pos0 = pos; + pos1 = pos; + result0 = parse_sequence(); + result0 = result0 !== null ? result0 : ""; + if (result0 !== null) { + result1 = []; + pos2 = pos; + result2 = parse_binaryOp(); + if (result2 !== null) { + result3 = parse_sequence(); + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + while (result2 !== null) { + result1.push(result2); + pos2 = pos; + result2 = parse_binaryOp(); + if (result2 !== null) { + result3 = parse_sequence(); + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos2; + } + } else { + result2 = null; + pos = pos2; + } + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, a, ops) { + return ops.reduce(function (memo, rhs) { + return { type: rhs[0], left: memo, right: rhs[1] }; + }, + !a || a.type === 'scope' + ? { type: 'scope' } + : { type: 'descendant', left: { type: 'scope' }, right: a }); + })(pos0, result0[0], result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_sequence() { + var cacheKey = "sequence@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 33) { + result0 = "!"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"!\""); + } + } + result0 = result0 !== null ? result0 : ""; + if (result0 !== null) { + result2 = parse_atom(); + if (result2 !== null) { + result1 = []; + while (result2 !== null) { + result1.push(result2); + result2 = parse_atom(); + } + } else { + result1 = null; + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, subject, as) { + var b = as.length === 1 ? as[0] : { type: 'compound', selectors: as }; + if(subject) b.subject = true; + return b; + })(pos0, result0[0], result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_atom() { + var cacheKey = "atom@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0; + + result0 = parse_wildcard(); + if (result0 === null) { + result0 = parse_identifier(); + if (result0 === null) { + result0 = parse_attr(); + if (result0 === null) { + result0 = parse_field(); + if (result0 === null) { + result0 = parse_negation(); + if (result0 === null) { + result0 = parse_matches(); + if (result0 === null) { + result0 = parse_has(); + if (result0 === null) { + result0 = parse_firstChild(); + if (result0 === null) { + result0 = parse_lastChild(); + if (result0 === null) { + result0 = parse_nthChild(); + if (result0 === null) { + result0 = parse_nthLastChild(); + if (result0 === null) { + result0 = parse_class(); + if (result0 === null) { + result0 = parse_root(); + if (result0 === null) { + result0 = parse_scope(); + } + } + } + } + } + } + } + } + } + } + } + } + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_wildcard() { + var cacheKey = "wildcard@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0; + var pos0; + + pos0 = pos; + if (input.charCodeAt(pos) === 42) { + result0 = "*"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"*\""); + } + } + if (result0 !== null) { + result0 = (function(offset, a) { return { type: 'wildcard', value: a }; })(pos0, result0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_identifier() { + var cacheKey = "identifier@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 35) { + result0 = "#"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"#\""); + } + } + result0 = result0 !== null ? result0 : ""; + if (result0 !== null) { + result1 = parse_identifierName(); + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, i) { return { type: 'identifier', value: i }; })(pos0, result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_attr() { + var cacheKey = "attr@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 91) { + result0 = "["; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"[\""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + result2 = parse_attrValue(); + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + if (input.charCodeAt(pos) === 93) { + result4 = "]"; + pos++; + } else { + result4 = null; + if (reportFailures === 0) { + matchFailed("\"]\""); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, v) { return v; })(pos0, result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_attrOps() { + var cacheKey = "attrOps@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (/^[><]/.test(input.charAt(pos))) { + result0 = input.charAt(pos); + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("[><]"); + } + } + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_attrEqOps() { + var cacheKey = "attrEqOps@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 33) { + result0 = "!"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"!\""); + } + } + result0 = result0 !== null ? result0 : ""; + if (result0 !== null) { + if (input.charCodeAt(pos) === 61) { + result1 = "="; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"=\""); + } + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, a) { return a + '='; })(pos0, result0[0]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_attrName() { + var cacheKey = "attrName@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1; + var pos0; + + pos0 = pos; + result1 = parse_identifierName(); + if (result1 === null) { + if (input.charCodeAt(pos) === 46) { + result1 = "."; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\".\""); + } + } + } + if (result1 !== null) { + result0 = []; + while (result1 !== null) { + result0.push(result1); + result1 = parse_identifierName(); + if (result1 === null) { + if (input.charCodeAt(pos) === 46) { + result1 = "."; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\".\""); + } + } + } + } + } else { + result0 = null; + } + if (result0 !== null) { + result0 = (function(offset, i) { return i.join(''); })(pos0, result0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_attrValue() { + var cacheKey = "attrValue@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + result0 = parse_attrName(); + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + result2 = parse_attrEqOps(); + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + result4 = parse_type(); + if (result4 === null) { + result4 = parse_regex(); + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, name, op, value) { + return { type: 'attribute', name: name, operator: op, value: value }; + })(pos0, result0[0], result0[2], result0[4]); + } + if (result0 === null) { + pos = pos0; + } + if (result0 === null) { + pos0 = pos; + pos1 = pos; + result0 = parse_attrName(); + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + result2 = parse_attrOps(); + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + result4 = parse_string(); + if (result4 === null) { + result4 = parse_number(); + if (result4 === null) { + result4 = parse_path(); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, name, op, value) { + return { type: 'attribute', name: name, operator: op, value: value }; + })(pos0, result0[0], result0[2], result0[4]); + } + if (result0 === null) { + pos = pos0; + } + if (result0 === null) { + pos0 = pos; + result0 = parse_attrName(); + if (result0 !== null) { + result0 = (function(offset, name) { return { type: 'attribute', name: name }; })(pos0, result0); + } + if (result0 === null) { + pos = pos0; + } + } + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_string() { + var cacheKey = "string@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3; + var pos0, pos1, pos2, pos3; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 34) { + result0 = "\""; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"\\\"\""); + } + } + if (result0 !== null) { + result1 = []; + if (/^[^\\"]/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[^\\\\\"]"); + } + } + if (result2 === null) { + pos2 = pos; + pos3 = pos; + if (input.charCodeAt(pos) === 92) { + result2 = "\\"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\"\\\\\""); + } + } + if (result2 !== null) { + if (input.length > pos) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("any character"); + } + } + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos3; + } + } else { + result2 = null; + pos = pos3; + } + if (result2 !== null) { + result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]); + } + if (result2 === null) { + pos = pos2; + } + } + while (result2 !== null) { + result1.push(result2); + if (/^[^\\"]/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[^\\\\\"]"); + } + } + if (result2 === null) { + pos2 = pos; + pos3 = pos; + if (input.charCodeAt(pos) === 92) { + result2 = "\\"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\"\\\\\""); + } + } + if (result2 !== null) { + if (input.length > pos) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("any character"); + } + } + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos3; + } + } else { + result2 = null; + pos = pos3; + } + if (result2 !== null) { + result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]); + } + if (result2 === null) { + pos = pos2; + } + } + } + if (result1 !== null) { + if (input.charCodeAt(pos) === 34) { + result2 = "\""; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\"\\\"\""); + } + } + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, d) { + return { type: 'literal', value: strUnescape(d.join('')) }; + })(pos0, result0[1]); + } + if (result0 === null) { + pos = pos0; + } + if (result0 === null) { + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 39) { + result0 = "'"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"'\""); + } + } + if (result0 !== null) { + result1 = []; + if (/^[^\\']/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[^\\\\']"); + } + } + if (result2 === null) { + pos2 = pos; + pos3 = pos; + if (input.charCodeAt(pos) === 92) { + result2 = "\\"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\"\\\\\""); + } + } + if (result2 !== null) { + if (input.length > pos) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("any character"); + } + } + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos3; + } + } else { + result2 = null; + pos = pos3; + } + if (result2 !== null) { + result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]); + } + if (result2 === null) { + pos = pos2; + } + } + while (result2 !== null) { + result1.push(result2); + if (/^[^\\']/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[^\\\\']"); + } + } + if (result2 === null) { + pos2 = pos; + pos3 = pos; + if (input.charCodeAt(pos) === 92) { + result2 = "\\"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\"\\\\\""); + } + } + if (result2 !== null) { + if (input.length > pos) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("any character"); + } + } + if (result3 !== null) { + result2 = [result2, result3]; + } else { + result2 = null; + pos = pos3; + } + } else { + result2 = null; + pos = pos3; + } + if (result2 !== null) { + result2 = (function(offset, a, b) { return a + b; })(pos2, result2[0], result2[1]); + } + if (result2 === null) { + pos = pos2; + } + } + } + if (result1 !== null) { + if (input.charCodeAt(pos) === 39) { + result2 = "'"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\"'\""); + } + } + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, d) { + return { type: 'literal', value: strUnescape(d.join('')) }; + })(pos0, result0[1]); + } + if (result0 === null) { + pos = pos0; + } + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_number() { + var cacheKey = "number@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2; + var pos0, pos1, pos2; + + pos0 = pos; + pos1 = pos; + pos2 = pos; + result0 = []; + if (/^[0-9]/.test(input.charAt(pos))) { + result1 = input.charAt(pos); + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + while (result1 !== null) { + result0.push(result1); + if (/^[0-9]/.test(input.charAt(pos))) { + result1 = input.charAt(pos); + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + } + if (result0 !== null) { + if (input.charCodeAt(pos) === 46) { + result1 = "."; + pos++; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\".\""); + } + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos2; + } + } else { + result0 = null; + pos = pos2; + } + result0 = result0 !== null ? result0 : ""; + if (result0 !== null) { + if (/^[0-9]/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + if (result2 !== null) { + result1 = []; + while (result2 !== null) { + result1.push(result2); + if (/^[0-9]/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + } + } else { + result1 = null; + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, a, b) { + return { type: 'literal', value: parseFloat((a ? a.join('') : '') + b.join('')) }; + })(pos0, result0[0], result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_path() { + var cacheKey = "path@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0; + var pos0; + + pos0 = pos; + result0 = parse_identifierName(); + if (result0 !== null) { + result0 = (function(offset, i) { return { type: 'literal', value: i }; })(pos0, result0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_type() { + var cacheKey = "type@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.substr(pos, 5) === "type(") { + result0 = "type("; + pos += 5; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"type(\""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + if (/^[^ )]/.test(input.charAt(pos))) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("[^ )]"); + } + } + if (result3 !== null) { + result2 = []; + while (result3 !== null) { + result2.push(result3); + if (/^[^ )]/.test(input.charAt(pos))) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("[^ )]"); + } + } + } + } else { + result2 = null; + } + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + if (input.charCodeAt(pos) === 41) { + result4 = ")"; + pos++; + } else { + result4 = null; + if (reportFailures === 0) { + matchFailed("\")\""); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, t) { return { type: 'type', value: t.join('') }; })(pos0, result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_regex() { + var cacheKey = "regex@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 47) { + result0 = "/"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\"/\""); + } + } + if (result0 !== null) { + if (/^[^\/]/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[^\\/]"); + } + } + if (result2 !== null) { + result1 = []; + while (result2 !== null) { + result1.push(result2); + if (/^[^\/]/.test(input.charAt(pos))) { + result2 = input.charAt(pos); + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("[^\\/]"); + } + } + } + } else { + result1 = null; + } + if (result1 !== null) { + if (input.charCodeAt(pos) === 47) { + result2 = "/"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\"/\""); + } + } + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, d) { return { type: 'regexp', value: new RegExp(d.join('')) }; })(pos0, result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_field() { + var cacheKey = "field@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1, pos2; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 46) { + result0 = "."; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\".\""); + } + } + if (result0 !== null) { + result1 = parse_identifierName(); + if (result1 !== null) { + result2 = []; + pos2 = pos; + if (input.charCodeAt(pos) === 46) { + result3 = "."; + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("\".\""); + } + } + if (result3 !== null) { + result4 = parse_identifierName(); + if (result4 !== null) { + result3 = [result3, result4]; + } else { + result3 = null; + pos = pos2; + } + } else { + result3 = null; + pos = pos2; + } + while (result3 !== null) { + result2.push(result3); + pos2 = pos; + if (input.charCodeAt(pos) === 46) { + result3 = "."; + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("\".\""); + } + } + if (result3 !== null) { + result4 = parse_identifierName(); + if (result4 !== null) { + result3 = [result3, result4]; + } else { + result3 = null; + pos = pos2; + } + } else { + result3 = null; + pos = pos2; + } + } + if (result2 !== null) { + result0 = [result0, result1, result2]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, i, is) { + return { type: 'field', name: is.reduce(function(memo, p){ return memo + p[0] + p[1]; }, i)}; + })(pos0, result0[1], result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_negation() { + var cacheKey = "negation@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.substr(pos, 5) === ":not(") { + result0 = ":not("; + pos += 5; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":not(\""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + result2 = parse_selectors(); + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + if (input.charCodeAt(pos) === 41) { + result4 = ")"; + pos++; + } else { + result4 = null; + if (reportFailures === 0) { + matchFailed("\")\""); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, ss) { return { type: 'not', selectors: ss }; })(pos0, result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_matches() { + var cacheKey = "matches@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.substr(pos, 9) === ":matches(") { + result0 = ":matches("; + pos += 9; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":matches(\""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + result2 = parse_selectors(); + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + if (input.charCodeAt(pos) === 41) { + result4 = ")"; + pos++; + } else { + result4 = null; + if (reportFailures === 0) { + matchFailed("\")\""); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, ss) { return { type: 'matches', selectors: ss }; })(pos0, result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_has() { + var cacheKey = "has@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.substr(pos, 5) === ":has(") { + result0 = ":has("; + pos += 5; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":has(\""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + result2 = parse_relativeSelectors(); + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + if (input.charCodeAt(pos) === 41) { + result4 = ")"; + pos++; + } else { + result4 = null; + if (reportFailures === 0) { + matchFailed("\")\""); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, ss) { return { type: 'has', selectors: ss }; })(pos0, result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_firstChild() { + var cacheKey = "firstChild@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0; + var pos0; + + pos0 = pos; + if (input.substr(pos, 12) === ":first-child") { + result0 = ":first-child"; + pos += 12; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":first-child\""); + } + } + if (result0 !== null) { + result0 = (function(offset) { return nth(1); })(pos0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_lastChild() { + var cacheKey = "lastChild@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0; + var pos0; + + pos0 = pos; + if (input.substr(pos, 11) === ":last-child") { + result0 = ":last-child"; + pos += 11; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":last-child\""); + } + } + if (result0 !== null) { + result0 = (function(offset) { return nthLast(1); })(pos0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_nthChild() { + var cacheKey = "nthChild@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.substr(pos, 11) === ":nth-child(") { + result0 = ":nth-child("; + pos += 11; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":nth-child(\""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + if (/^[0-9]/.test(input.charAt(pos))) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + if (result3 !== null) { + result2 = []; + while (result3 !== null) { + result2.push(result3); + if (/^[0-9]/.test(input.charAt(pos))) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + } + } else { + result2 = null; + } + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + if (input.charCodeAt(pos) === 41) { + result4 = ")"; + pos++; + } else { + result4 = null; + if (reportFailures === 0) { + matchFailed("\")\""); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, n) { return nth(parseInt(n.join(''), 10)); })(pos0, result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_nthLastChild() { + var cacheKey = "nthLastChild@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1, result2, result3, result4; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.substr(pos, 16) === ":nth-last-child(") { + result0 = ":nth-last-child("; + pos += 16; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":nth-last-child(\""); + } + } + if (result0 !== null) { + result1 = parse__(); + if (result1 !== null) { + if (/^[0-9]/.test(input.charAt(pos))) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + if (result3 !== null) { + result2 = []; + while (result3 !== null) { + result2.push(result3); + if (/^[0-9]/.test(input.charAt(pos))) { + result3 = input.charAt(pos); + pos++; + } else { + result3 = null; + if (reportFailures === 0) { + matchFailed("[0-9]"); + } + } + } + } else { + result2 = null; + } + if (result2 !== null) { + result3 = parse__(); + if (result3 !== null) { + if (input.charCodeAt(pos) === 41) { + result4 = ")"; + pos++; + } else { + result4 = null; + if (reportFailures === 0) { + matchFailed("\")\""); + } + } + if (result4 !== null) { + result0 = [result0, result1, result2, result3, result4]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, n) { return nthLast(parseInt(n.join(''), 10)); })(pos0, result0[2]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_class() { + var cacheKey = "class@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0, result1; + var pos0, pos1; + + pos0 = pos; + pos1 = pos; + if (input.charCodeAt(pos) === 58) { + result0 = ":"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":\""); + } + } + if (result0 !== null) { + if (input.substr(pos, 9).toLowerCase() === "statement") { + result1 = input.substr(pos, 9); + pos += 9; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"statement\""); + } + } + if (result1 === null) { + if (input.substr(pos, 10).toLowerCase() === "expression") { + result1 = input.substr(pos, 10); + pos += 10; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"expression\""); + } + } + if (result1 === null) { + if (input.substr(pos, 11).toLowerCase() === "declaration") { + result1 = input.substr(pos, 11); + pos += 11; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"declaration\""); + } + } + if (result1 === null) { + if (input.substr(pos, 8).toLowerCase() === "function") { + result1 = input.substr(pos, 8); + pos += 8; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"function\""); + } + } + if (result1 === null) { + if (input.substr(pos, 7).toLowerCase() === "pattern") { + result1 = input.substr(pos, 7); + pos += 7; + } else { + result1 = null; + if (reportFailures === 0) { + matchFailed("\"pattern\""); + } + } + } + } + } + } + if (result1 !== null) { + result0 = [result0, result1]; + } else { + result0 = null; + pos = pos1; + } + } else { + result0 = null; + pos = pos1; + } + if (result0 !== null) { + result0 = (function(offset, c) { + return { type: 'class', name: c }; + })(pos0, result0[1]); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_root() { + var cacheKey = "root@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0; + var pos0; + + pos0 = pos; + if (input.substr(pos, 5) === ":root") { + result0 = ":root"; + pos += 5; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":root\""); + } + } + if (result0 !== null) { + result0 = (function(offset) { return { type: 'root' }; })(pos0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + function parse_scope() { + var cacheKey = "scope@" + pos; + var cachedResult = cache[cacheKey]; + if (cachedResult) { + pos = cachedResult.nextPos; + return cachedResult.result; + } + + var result0; + var pos0; + + pos0 = pos; + if (input.substr(pos, 6) === ":scope") { + result0 = ":scope"; + pos += 6; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\":scope\""); + } + } + if (result0 !== null) { + result0 = (function(offset) { return { type: 'scope' }; })(pos0); + } + if (result0 === null) { + pos = pos0; + } + + cache[cacheKey] = { + nextPos: pos, + result: result0 + }; + return result0; + } + + + function cleanupExpected(expected) { + expected.sort(); + + var lastExpected = null; + var cleanExpected = []; + for (var i = 0; i < expected.length; i++) { + if (expected[i] !== lastExpected) { + cleanExpected.push(expected[i]); + lastExpected = expected[i]; + } + } + return cleanExpected; + } + + function computeErrorPosition() { + /* + * The first idea was to use |String.split| to break the input up to the + * error position along newlines and derive the line and column from + * there. However IE's |split| implementation is so broken that it was + * enough to prevent it. + */ + + var line = 1; + var column = 1; + var seenCR = false; + + for (var i = 0; i < Math.max(pos, rightmostFailuresPos); i++) { + var ch = input.charAt(i); + if (ch === "\n") { + if (!seenCR) { line++; } + column = 1; + seenCR = false; + } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") { + line++; + column = 1; + seenCR = true; + } else { + column++; + seenCR = false; + } + } + + return { line: line, column: column }; + } + + + function nth(n) { return { type: 'nth-child', index: { type: 'literal', value: n } }; } + function nthLast(n) { return { type: 'nth-last-child', index: { type: 'literal', value: n } }; } + function strUnescape(s) { + return s.replace(/\\(.)/g, function(match, ch) { + switch(ch) { + case 'a': return '\a'; + case 'b': return '\b'; + case 'f': return '\f'; + case 'n': return '\n'; + case 'r': return '\r'; + case 't': return '\t'; + case 'v': return '\v'; + default: return ch; + } + }); + } + + + var result = parseFunctions[startRule](); + + /* + * The parser is now in one of the following three states: + * + * 1. The parser successfully parsed the whole input. + * + * - |result !== null| + * - |pos === input.length| + * - |rightmostFailuresExpected| may or may not contain something + * + * 2. The parser successfully parsed only a part of the input. + * + * - |result !== null| + * - |pos < input.length| + * - |rightmostFailuresExpected| may or may not contain something + * + * 3. The parser did not successfully parse any part of the input. + * + * - |result === null| + * - |pos === 0| + * - |rightmostFailuresExpected| contains at least one failure + * + * All code following this comment (including called functions) must + * handle these states. + */ + if (result === null || pos !== input.length) { + var offset = Math.max(pos, rightmostFailuresPos); + var found = offset < input.length ? input.charAt(offset) : null; + var errorPosition = computeErrorPosition(); + + throw new this.SyntaxError( + cleanupExpected(rightmostFailuresExpected), + found, + offset, + errorPosition.line, + errorPosition.column + ); + } + + return result; + }, + + /* Returns the parser source code. */ + toSource: function() { return this._source; } + }; + + /* Thrown when a parser encounters a syntax error. */ + + result.SyntaxError = function(expected, found, offset, line, column) { + function buildMessage(expected, found) { + var expectedHumanized, foundHumanized; + + switch (expected.length) { + case 0: + expectedHumanized = "end of input"; + break; + case 1: + expectedHumanized = expected[0]; + break; + default: + expectedHumanized = expected.slice(0, expected.length - 1).join(", ") + + " or " + + expected[expected.length - 1]; + } + + foundHumanized = found ? quote(found) : "end of input"; + + return "Expected " + expectedHumanized + " but " + foundHumanized + " found."; + } + + this.name = "SyntaxError"; + this.expected = expected; + this.found = found; + this.message = buildMessage(expected, found); + this.offset = offset; + this.line = line; + this.column = column; + }; + + result.SyntaxError.prototype = Error.prototype; + + return result; + })(); + if (typeof define === "function" && define.amd) { define(function(){ return result; }); } else if (typeof module !== "undefined" && module.exports) { module.exports = result; } else { this.esquery = result; } \ No newline at end of file diff --git a/test/project.spec.ts b/test/project.spec.ts index 8c32d0d..4185287 100644 --- a/test/project.spec.ts +++ b/test/project.spec.ts @@ -9,13 +9,13 @@ describe('tsquery:', () => { it('should process a tsconfig.json file', () => { const files = tsquery.project('./tsconfig.json'); - expect(files.length).to.equal(82); + expect(files.length).to.equal(87); }); it('should find a tsconfig.json file in a director', () => { const files = tsquery.project('./'); - expect(files.length).to.equal(82); + expect(files.length).to.equal(87); }); it(`should handle when a path doesn't exist`, () => { diff --git a/tsconfig.json b/tsconfig.json index 0c31fc5..c9e18bf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "declaration": true, "forceConsistentCasingInFileNames": true, "lib": [ "es2017", @@ -11,6 +10,7 @@ "noUnusedParameters": true, "pretty": true, "sourceMap": true, + "allowJs": true, "strict": true, "target": "ES5", "outDir": "./dist",