Pman.SearchTokenizer.js
[Pman.Core] / Pman.SearchTokenizer.js
1 /**
2  *
3  * Search tokenizer - used with fields that are sent to Text_SearchParser
4  * 
5  *
6  * usage :
7  *  x = new Pman.SearchTokenizer('a and b or "test this" or ( tst:aval and vvv:erer }')
8  *  data = x.parse();
9  *
10  *
11  *  or
12  *  data = Pman.SearchTokenizer.parse(....)
13  */
14  
15 Pman.SearchTokenizer = function(s)
16 {
17         this.str = s;
18         this.strlen = s.length;
19         this.i = 0;
20         this.tokens = [];
21        //print_r(this);
22 }
23 Pman.SearchTokenizer.prototype =  {
24     i : 0,
25     str : '',
26     strlen : 0,
27     tokens : false ,
28        //print_r(this);
29     
30     parse : function ()
31     {
32         var c;
33         while(true) {
34             c = this.getChar();
35             
36             if (false === c) { //eof..
37                 return this.tokens;
38             }
39             switch(c) {
40                 case ' ': continue;
41                 case ':': this.tokens.push( { type : ':' }) ; break;
42                 case '(': this.tokens.push( { type : '(' }) ; break;
43                 case ')': this.tokens.push( { type : ')' }) ; break;
44                 default:
45                     this.ungetChar();
46                     this.strParse();
47                     break;
48                 
49                
50             }
51         }
52         // should not get here...
53         return this.tokens;
54     },
55     strParse : function ()
56     {
57         var c;
58         var str = '';
59         while(true) {
60             c = this.getChar();
61             if (false === c) {
62                 this.addStr(str);
63                 return;
64             }
65             switch(c) {
66                 // end chars.
67                 case ' ': 
68                 case ':': 
69                 case '(': 
70                 case ')': this.addStr(str); this.ungetChar(); return;
71                 case '"': 
72                     if (str.length) {
73                         this.addStr(str); 
74                         str = '';
75                     }
76                     this.strParseQuoted(c);
77                     break;
78                     
79                 default : 
80                     str += c;
81                     continue;
82             }
83             
84         }
85     },
86     
87     strParseQuoted: function (end) 
88     {
89         var str = '';   /// ignore \" slashed ???
90         var c;
91         while(true) {
92             c = this.getChar();
93             if (false === c) {
94                 this.addStr(str,true);
95                 return;
96             }
97             if (c == end) {
98                 this.addStr(str,true);
99                 return;
100             }
101             str += c;
102         }
103             
104     }
105     function addStr(s, q=false) { //q == quoted..
106         s = q ? s : trim(s);
107         if (!strlen(s)) {
108             return;
109         }
110         if (!q) {
111             
112             if ((strtoupper(s) == 'AND') || (strtoupper(s) == 'OR')) {
113                 this.tokens[] = new Text_SearchParser_Token_Op(strtoupper(s));
114                 return;
115             }
116         }
117         this.tokens[] = new Text_SearchParser_Token_String(s);
118     }
119     
120     function getChar()
121     {
122         if (this.i >= this.strlen) {
123             return false;
124         }
125         c = this.str[this.i];
126         this.i++;
127         return c;
128     }
129     function ungetChar()
130     {
131         this.i--;
132     }
133     
134     
135     
136     
137 }