X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=Pman.SearchTokenizer.js;h=8db0be1b8579dbfad2a4dac29c7e41c7d518e78a;hp=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391;hb=refs%2Fheads%2Fwip_alan_T5884_add_photo_to_report;hpb=68b7f3fcc3e03a79add2a76b62dad75a4e3b8d8c diff --git a/Pman.SearchTokenizer.js b/Pman.SearchTokenizer.js index e69de29b..8db0be1b 100644 --- a/Pman.SearchTokenizer.js +++ b/Pman.SearchTokenizer.js @@ -0,0 +1,147 @@ +/** + * + * Search tokenizer - used with fields that are sent to Text_SearchParser + * + * + * usage : + * x = new Pman.SearchTokenizer('a and b or "test this" or ( tst:aval and vvv:erer }') + * data = x.parse(); + * + * + * or + * data = Pman.SearchTokenizer.parse(....) + */ + +Pman.SearchTokenizer = function(s) +{ + this.str = typeof(s) == 'string' ? s : ''; + this.strlen = typeof(s) == 'string' ? s.length : 0; + this.i = 0; + this.tokens = []; + //print_r(this); +} +Pman.SearchTokenizer.prototype = { + i : 0, + str : '', + strlen : 0, + tokens : false , + //print_r(this); + + parse : function () + { + var c; + while(true) { + c = this.getChar(); + + if (false === c) { //eof.. + return this.tokens; + } + switch(c) { + case ' ': continue; + case ':': this.tokens.push( { type : ':' }) ; break; + case '(': this.tokens.push( { type : '(' }) ; break; + case ')': this.tokens.push( { type : ')' }) ; break; + default: + this.ungetChar(); + this.strParse(); + break; + + + } + } + // sort tokens longest first.. + + + + // should not get here... + return this.tokens; + }, + strParse : function () + { + var c; + var str = ''; + while(true) { + c = this.getChar(); + if (false === c) { + this.addStr(str); + return; + } + switch(c) { + // end chars. + case ' ': + case ':': + case '(': + case ')': this.addStr(str); this.ungetChar(); return; + case '"': + if (str.length) { + this.addStr(str); + str = ''; + } + this.strParseQuoted(c); + break; + + default : + str += c; + continue; + } + + } + }, + + strParseQuoted: function (end) + { + var str = ''; /// ignore \" slashed ??? + var c; + while(true) { + c = this.getChar(); + if (false === c) { + this.addStr(str,true); + return; + } + if (c == end) { + this.addStr(str,true); + return; + } + str += c; + } + + }, + addStr : function (s,q) { //q == quoted.. + q = q || false; + + s = q ? s : Roo.util.Format.trim(s); + if (!s.length) { + return; + } + + if (!q) { + if ((s.toUpperCase() == 'AND') || (s.toUpperCase() == 'OR')) { + this.tokens.push( { type: s.toUpperCase() }); + return; + } + } + this.tokens.push( { type : 's' , v : s, q: q }); + }, + + getChar : function () + { + + if (this.i >= this.strlen) { + return false; + } + c = this.str[this.i]; + this.i++; + return c; + }, + ungetChar : function () + { + this.i--; + } + +}; + +Pman.SearchTokenizer.parse = function(v) { + var x = new Pman.SearchTokenizer(v); + return x.parse(); + +} \ No newline at end of file