Navigation Menu

Skip to content

Commit

Permalink
bq: support multi phrase in a string
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 6, 2012
1 parent 7d8d01e commit 6f91758
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions lib/bq-translator.js
Expand Up @@ -225,39 +225,43 @@ BooleanQueryTranslator.prototype = {
if (this.query[this.offset] != "'") {
this.throwTranslateError("open single quote for string value is missing");
}
if (this.query[this.offset + 1] == "\"") {
return this.translateExpressionValuePhrase(field);
}

this.offset++;

var tokens = [];
var value = "";
for (; this.offset < this.query.length; this.offset++) {
var character = this.query[this.offset];
if (character == "'") {
this.offset++;
tokens.push(this.translateExpressionValueStringKeyword(field, value));
if (value.length > 0) {
tokens.push(this.translateExpressionValueStringKeyword(field, value));
}
return tokens.join(" ");
}

if (character == " " || character == "+") {
if (value.length > 0) {
tokens.push(this.translateExpressionValueStringKeyword(field, value));
tokens.push("&&");
value = "";
}
tokens.push("&&");
} else if (character == "|") {
if (value.length > 0) {
tokens.push(this.translateExpressionValueStringKeyword(field, value));
tokens.push("||");
value = "";
}
tokens.push("||");
} else if (character == "\\") {
this.offset++;
// TODO: check length
character = this.query[this.offset];
value += character;
} else if (character == "\"") {
value += "\\\"";
if (value.length > 0) {
this.throwTranslateError("TODO");
}
tokens.push(this.translateExpressionValuePhrase(field));
this.offset--;
} else {
value += character;
}
Expand All @@ -274,31 +278,20 @@ BooleanQueryTranslator.prototype = {
return field + " " + operator + " " + "\"" + value + "\"";
},
translateExpressionValuePhrase: function(field) {
if (!(this.query[this.offset] == "'" &&
this.query[this.offset + 1] == "\"")) {
// TODO: report error
return "";
if (this.query[this.offset] != "\"") {
this.throwTranslateError("open double quote for phrase value is missing");
}

this.offset += 2;
this.offset += 1;
var value = "";
for (; this.offset < this.query.length; this.offset++) {
var character = this.query[this.offset];
if (character == "'") {
// TODO: report error: missing close quote <">
return "";
}

if (character == "\\") {
this.offset++;
// TODO: check length
character = this.query[this.offset];
value += character;
} else if (character == "\"") {
this.offset++;
if (this.query[this.offset] != "'") {
// TODO: report error: missing close quote <'> after <">
return "";
}
this.offset++;
return field + " @ " + "\"" + value + "\"";
} else {
Expand Down

0 comments on commit 6f91758

Please sign in to comment.