Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created March 10, 2009 21:42
Show Gist options
  • Save thomaslang/77157 to your computer and use it in GitHub Desktop.
Save thomaslang/77157 to your computer and use it in GitHub Desktop.
parseQuery = function (queryString) {
var literalStrings = getLiteralStrings(queryString);
var strippedString = stripLiteralStrings(queryString, literalStrings);
var booleanExpression = getBooleanExpression(strippedString);
var strippedComparisons = getStrippedComparisons(strippedString);
var comparisons = constructComparisons(strippedComparisons, literalStrings);
return { booleanExpression: booleanExpression,
comparisons: comparisons
};
};
getLiteralStrings = function (queryString) {
// this function steps through the input string, recognizes literal strings,
// remembers their position in the input string and returns an array that
// contains objects like this:
// {startPoint: n, endPoint: m, sequenceOfCharacters: s}
var currentlyInString = false;
var delimeterOfCurrentString = "";
var numberOfCurrentString = 0;
var isStringDelimeter = {"'": true, '"': true};
var isEscapeCharacter = {"\\": true};
var currentCharacter = "";
var previousCharacter = "";
var literalStrings = []; //to be returned
// first find the strings start- and end-points:
for (var i=0; i < queryString.length; i++) {
currentCharacter = queryString[i];
if ( currentlyInString ) {
if ( currentCharacter == delimeterOfCurrentString
&& ! isEscapeCharacter[currentCharacter] ) {
currentlyInString = false;
literalStrings[numberOfCurrentString].endPoint = i;
numberOfCurrentString++;
};
}
else {
if ( isStringDelimeter[currentCharacter] ) {
currentlyInString = true;
delimeterOfCurrentString = currentCharacter;
literalStrings[numberOfCurrentString] = {startPoint: i};
};
};
previousCharacter = currentCharacter;
};
// then extract the strings:
var s = literalStrings[0];
for (var i=0; i < literalStrings.length; i++) {
s = literalStrings[i];
s.string = queryString.substring(s.startPoint+1, s.endPoint)
};
return literalStrings;
};
stripLiteralStrings = function (queryString, literalStrings) {
var strippedString = "";
return strippedString;
};
getBooleanExpression = function (strippedString) {
var booleanExpression = "";
return booleanExpression;
};
getStrippedComparisons = function (strippedString) {
var strippedComparisons = [];
return strippedComparisons;
};
constructComparisons = function (strippedComparisons, literalStrings) {
var comparisons = [];
return comparisons;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment