grasp-equery
Advanced tools
Comparing version 0.1.0 to 0.2.0
234
lib/match.js
// Generated by LiveScript 1.2.0 | ||
(function(){ | ||
var ref$, primitiveOnlyAttributes, eitherAttributes, all, getNodeAtPath, toString$ = {}.toString; | ||
var ref$, primitiveOnlyAttributes, eitherAttributes, all, tail, getNodeAtPath, toString$ = {}.toString, slice$ = [].slice; | ||
ref$ = require('grasp-syntax-javascript'), primitiveOnlyAttributes = ref$.primitiveOnlyAttributes, eitherAttributes = ref$.eitherAttributes; | ||
all = require('prelude-ls').all; | ||
ref$ = require('prelude-ls'), all = ref$.all, tail = ref$.tail; | ||
getNodeAtPath = require('./common').getNodeAtPath; | ||
function matchNode(results, query, node){ | ||
function matchNode(results, query, mainNode){ | ||
var key, val, ref$; | ||
if (eq(node, query)) { | ||
results.push(node); | ||
if (eq(mainNode, query)) { | ||
results.push(mainNode); | ||
} | ||
for (key in node) { | ||
val = node[key]; | ||
if ((key !== 'loc' && key !== 'start' && key !== 'end') && ((ref$ = toString$.call(val).slice(8, -1)) === 'Object' || ref$ === 'Array')) { | ||
for (key in mainNode) { | ||
val = mainNode[key]; | ||
if ((key !== 'loc' && key !== 'start' && key !== 'end' && key !== '_named') && ((ref$ = toString$.call(val).slice(8, -1)) === 'Object' || ref$ === 'Array')) { | ||
matchNode(results, query, val); | ||
} | ||
} | ||
} | ||
function eq(targetNode, selectorNode){ | ||
var selectorNodeType, prop, len, targetLen, arrWildcardMatched, i, i$, len$, node, targetsLeft; | ||
selectorNodeType = toString$.call(selectorNode).slice(8, -1); | ||
if (selectorNode === targetNode) { | ||
return true; | ||
} else if (selectorNodeType !== toString$.call(targetNode).slice(8, -1)) { | ||
return false; | ||
} else if (selectorNodeType === 'Object') { | ||
if (selectorNode.type === 'Grasp') { | ||
return matchSpecial(targetNode, selectorNode); | ||
function eq(targetNode, selectorNode){ | ||
var selectorNodeType, prop; | ||
selectorNodeType = toString$.call(selectorNode).slice(8, -1); | ||
if (selectorNode === targetNode) { | ||
return true; | ||
} else if (selectorNodeType !== toString$.call(targetNode).slice(8, -1)) { | ||
return false; | ||
} else if (selectorNodeType === 'Object') { | ||
if (selectorNode.type === 'Grasp') { | ||
return matchSpecial(targetNode, selectorNode); | ||
} else { | ||
for (prop in targetNode) { | ||
if (prop !== 'loc' && prop !== 'start' && prop !== 'end' && prop !== '_named') { | ||
if (!eq(targetNode[prop], selectorNode[prop])) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} else if (selectorNodeType === 'Array') { | ||
return matchArray(selectorNode, targetNode); | ||
} else { | ||
for (prop in targetNode) { | ||
if (prop !== 'loc' && prop !== 'start' && prop !== 'end') { | ||
if (!eq(targetNode[prop], selectorNode[prop])) { | ||
return false; | ||
return false; | ||
} | ||
} | ||
function matchArray(pattern, input){ | ||
var patternLen, that, ref$, patternFirst, patternRest, inputFirst, inputRest, arrayWildcardName, wildcardName; | ||
patternLen = pattern.length; | ||
if (patternLen === 0) { | ||
return input.length === 0; | ||
} else if (patternLen === 1) { | ||
if (that = isArrayWildcard(pattern[0])) { | ||
if (that = that.name) { | ||
mainNode._named == null && (mainNode._named = {}); | ||
(ref$ = mainNode._named)[that] == null && (ref$[that] = []); | ||
(ref$ = mainNode._named)[that] = ref$[that].concat(input); | ||
} | ||
return true; | ||
} else { | ||
return input.length === 1 && eq(input[0], pattern[0]); | ||
} | ||
} else if (input.length === 0) { | ||
return false; | ||
} else { | ||
patternFirst = pattern[0], patternRest = slice$.call(pattern, 1); | ||
inputFirst = input[0], inputRest = slice$.call(input, 1); | ||
if (that = isArrayWildcard(patternFirst)) { | ||
if (that = that.name) { | ||
arrayWildcardName = that; | ||
mainNode._named == null && (mainNode._named = {}); | ||
(ref$ = mainNode._named)[arrayWildcardName] == null && (ref$[arrayWildcardName] = []); | ||
} | ||
if (that = eq(inputFirst, patternRest[0])) { | ||
wildcardName = that; | ||
if (matchArray(tail(patternRest), inputRest)) { | ||
return true; | ||
} else { | ||
if (toString$.call(wildcardName).slice(8, -1) === 'String') { | ||
delete mainNode._named[wildcardName]; | ||
} | ||
return matchArray(pattern, inputRest); | ||
} | ||
} else { | ||
if (arrayWildcardName) { | ||
mainNode._named[arrayWildcardName].push(inputFirst); | ||
} | ||
return matchArray(pattern, inputRest); | ||
} | ||
} else { | ||
return eq(inputFirst, patternFirst) && matchArray(patternRest, inputRest); | ||
} | ||
} | ||
} | ||
function matchSpecial(targetNode, selectorNode){ | ||
var named, name, that, identMatch, attrMatch; | ||
switch (selectorNode.graspType) { | ||
case 'wildcard': | ||
return true; | ||
} | ||
} else if (selectorNodeType === 'Array') { | ||
len = selectorNode.length; | ||
targetLen = targetNode.length; | ||
arrWildcardMatched = false; | ||
i = 0; | ||
for (i$ = 0, len$ = selectorNode.length; i$ < len$; ++i$) { | ||
node = selectorNode[i$]; | ||
if (matchArrayWildcard(node)) { | ||
if (i + 1 === len) { | ||
case 'named-wildcard': | ||
mainNode._named == null && (mainNode._named = {}); | ||
named = mainNode._named; | ||
name = selectorNode.name; | ||
if (that = named[name]) { | ||
if (eq(targetNode, that)) { | ||
return true; | ||
} else { | ||
arrWildcardMatched = true; | ||
targetsLeft = len - i - 1; | ||
i = targetLen - targetsLeft; | ||
return false; | ||
} | ||
} else { | ||
if (!eq(targetNode[i], node)) { | ||
return false; | ||
} | ||
i++; | ||
named[name] = targetNode; | ||
return name; | ||
} | ||
break; | ||
case 'node-type': | ||
return targetNode.type === selectorNode.value; | ||
case 'matches': | ||
return in$(targetNode.type, selectorNode.value); | ||
case 'literal': | ||
return targetNode.type === 'Literal' && toString$.call(targetNode.value).slice(8, -1) === selectorNode.value; | ||
case 'compound': | ||
identMatch = matchSpecial(targetNode, selectorNode.ident); | ||
attrMatch = all(matchAttr(targetNode), selectorNode.attrs); | ||
return identMatch && attrMatch; | ||
} | ||
return arrWildcardMatched || len === targetLen; | ||
} else { | ||
return false; | ||
} | ||
} | ||
function matchSpecial(targetNode, selectorNode){ | ||
var identMatch, attrMatch; | ||
switch (selectorNode.graspType) { | ||
case 'wildcard': | ||
return true; | ||
case 'node-type': | ||
return targetNode.type === selectorNode.value; | ||
case 'matches': | ||
return in$(targetNode.type, selectorNode.value); | ||
case 'literal': | ||
return targetNode.type === 'Literal' && toString$.call(targetNode.value).slice(8, -1) === selectorNode.value; | ||
case 'compound': | ||
identMatch = matchSpecial(targetNode, selectorNode.ident); | ||
attrMatch = all(matchAttr(targetNode), selectorNode.attrs); | ||
return identMatch && attrMatch; | ||
function isArrayWildcard(node){ | ||
var cleanNode; | ||
cleanNode = node.type === 'ExpressionStatement' ? node.expression : node; | ||
return cleanNode.type === 'Grasp' && cleanNode.graspType === 'array-wildcard' && cleanNode; | ||
} | ||
} | ||
function matchArrayWildcard(node){ | ||
var cleanNode; | ||
cleanNode = node.type === 'ExpressionStatement' ? node.expression : node; | ||
return cleanNode.type === 'Grasp' && cleanNode.graspType === 'array-wildcard'; | ||
} | ||
function matchAttr(targetNode){ | ||
return function(attr){ | ||
var node, attrValue, lastPath, ref$; | ||
node = getNodeAtPath(targetNode, attr.path); | ||
if (node != null) { | ||
attrValue = attr.value; | ||
if (attrValue) { | ||
lastPath = (ref$ = attr.path)[ref$.length - 1]; | ||
if (in$(lastPath, primitiveOnlyAttributes)) { | ||
return matchPrimitive(attr.op, node, attrValue); | ||
} else if (in$(lastPath, eitherAttributes)) { | ||
return matchEither(attr.op, node, attrValue); | ||
function matchAttr(targetNode){ | ||
return function(attr){ | ||
var node, attrValue, lastPath, ref$; | ||
node = getNodeAtPath(targetNode, attr.path); | ||
if (node != null) { | ||
attrValue = attr.value; | ||
if (attrValue) { | ||
lastPath = (ref$ = attr.path)[ref$.length - 1]; | ||
if (in$(lastPath, primitiveOnlyAttributes)) { | ||
return matchPrimitive(attr.op, node, attrValue); | ||
} else if (in$(lastPath, eitherAttributes)) { | ||
return matchEither(attr.op, node, attrValue); | ||
} else { | ||
return matchComplex(attr.op, node, attrValue); | ||
} | ||
} else { | ||
return matchComplex(attr.op, node, attrValue); | ||
return true; | ||
} | ||
} else { | ||
return true; | ||
return false; | ||
} | ||
}; | ||
} | ||
function matchPrimitive(op, node, attrValue){ | ||
if (op === '=') { | ||
return node === attrValue.value; | ||
} else { | ||
return false; | ||
return node !== attrValue.value; | ||
} | ||
}; | ||
} | ||
function matchPrimitive(op, node, attrValue){ | ||
if (op === '=') { | ||
return node === attrValue.value; | ||
} else { | ||
return node !== attrValue.value; | ||
} | ||
} | ||
function matchComplex(op, node, attrValue){ | ||
if (op === '=') { | ||
return eq(node, attrValue); | ||
} else { | ||
return !eq(node, attrValue); | ||
function matchComplex(op, node, attrValue){ | ||
if (op === '=') { | ||
return eq(node, attrValue); | ||
} else { | ||
return !eq(node, attrValue); | ||
} | ||
} | ||
function matchEither(op, node, attrValue){ | ||
return matchPrimitive(op, node, attrValue) || matchComplex(op, node, attrValue); | ||
} | ||
} | ||
function matchEither(op, node, attrValue){ | ||
return matchPrimitive(op, node, attrValue) || matchComplex(op, node, attrValue); | ||
} | ||
module.exports = { | ||
@@ -129,0 +171,0 @@ matchNode: matchNode |
@@ -17,2 +17,5 @@ // Generated by LiveScript 1.2.0 | ||
}, { | ||
code: "(" + selector + ")", | ||
path: [] | ||
}, { | ||
code: "while (true) { " + selector + "; }", | ||
@@ -47,3 +50,3 @@ path: ['body', 'body', 0] | ||
extractedSelector = getNodeAtPath(selectorBody[0], path); | ||
finalSelector = extractedSelector.type === 'ExpressionStatement' ? extractedSelector.expression : extractedSelector; | ||
finalSelector = extractedSelector.type === 'ExpressionStatement' && !/;\s*$/.test(selector) ? extractedSelector.expression : extractedSelector; | ||
root = { | ||
@@ -57,3 +60,3 @@ type: 'Root', | ||
function processSelector(ast){ | ||
var key, node, i$, len$, i, n, that; | ||
var key, node, nodeType, i$, len$, i, n, that; | ||
delete ast.start; | ||
@@ -64,3 +67,4 @@ delete ast.end; | ||
if (key !== 'type') { | ||
if (toString$.call(node).slice(8, -1) === 'Array') { | ||
nodeType = toString$.call(node).slice(8, -1); | ||
if (nodeType === 'Array') { | ||
for (i$ = 0, len$ = node.length; i$ < len$; ++i$) { | ||
@@ -75,3 +79,3 @@ i = i$; | ||
} | ||
} else if (toString$.call(node).slice(8, -1) === 'Object') { | ||
} else if (nodeType === 'Object') { | ||
if (that = processNode(node)) { | ||
@@ -87,5 +91,5 @@ ast[key] = that; | ||
function processNode(node){ | ||
var nodeType, name, that, ident, attrs, n, processedAttrs, i$, len$, attr; | ||
nodeType = node.type; | ||
if (nodeType === 'Identifier') { | ||
var name, that, ident, attrs, n, processedAttrs, i$, len$, attr, nodeKey, nodeValue; | ||
switch (node.type) { | ||
case 'Identifier': | ||
name = node.name; | ||
@@ -99,7 +103,14 @@ if (name === '_') { | ||
}; | ||
} else if (name === '_$') { | ||
} else if (that = /^_\$(\w*)$/.exec(name)) { | ||
return { | ||
type: 'Grasp', | ||
graspType: 'array-wildcard' | ||
graspType: 'array-wildcard', | ||
name: that[1] | ||
}; | ||
} else if (that = /^\$(\w+)$/.exec(name)) { | ||
return { | ||
type: 'Grasp', | ||
graspType: 'named-wildcard', | ||
name: that[1] | ||
}; | ||
} else if (that = /^_([_a-zA-Z]+)/.exec(name)) { | ||
@@ -127,3 +138,4 @@ ident = that[1].replace(/_/, '-'); | ||
} | ||
} else if (nodeType === 'MemberExpression') { | ||
break; | ||
case 'MemberExpression': | ||
if (!node.computed) { | ||
@@ -163,4 +175,32 @@ return; | ||
}; | ||
} else if (nodeType === 'ExpressionStatement') { | ||
case 'ExpressionStatement': | ||
return processNode(node.expression); | ||
default: | ||
if (!(!node.type && node.key != null && node.value != null)) { | ||
return; | ||
} | ||
nodeKey = node.key, nodeValue = node.value; | ||
if (!(nodeKey.type === 'Identifier' && nodeValue.type === 'Identifier')) { | ||
return; | ||
} | ||
if (nodeKey.name === '_') { | ||
if (node.value.name === '_') { | ||
return { | ||
type: 'Grasp', | ||
graspType: 'wildcard' | ||
}; | ||
} else if (/^\$/.test(nodeValue.name)) { | ||
return { | ||
type: 'Grasp', | ||
graspType: 'array-wildcard', | ||
name: /^\$(\w*)$/.exec(nodeValue.name)[1] | ||
}; | ||
} | ||
} else if (nodeKey.name === '$') { | ||
return { | ||
type: 'Grasp', | ||
graspType: 'named-wildcard', | ||
name: nodeValue.name | ||
}; | ||
} | ||
} | ||
@@ -167,0 +207,0 @@ } |
{ | ||
"name": "grasp-equery", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "George Zahariev <z@georgezahariev.com>", | ||
@@ -40,3 +40,3 @@ "description": "grasp query using example code with wildcards", | ||
"prelude-ls": "~1.0.3", | ||
"acorn": "~0.3.1", | ||
"acorn": "~0.4.2", | ||
"grasp-syntax-javascript": "~0.1.0" | ||
@@ -43,0 +43,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# grasp equery | ||
# grasp equery [![Build Status](https://travis-ci.org/gkz/grasp-equery.png?branch=master)](https://travis-ci.org/gkz/grasp-equery) | ||
A query engine for [grasp](http://graspjs.com) - use JavaScript code examples with wildcards. | ||
@@ -3,0 +3,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17718
462
+ Addedacorn@0.4.2(transitive)
- Removedacorn@0.3.1(transitive)
Updatedacorn@~0.4.2