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         
58         str = '';
59         while(true) {
60             if (false === (c = this.getChar())) {
61                 this.addStr(str);
62                 return;
63             }
64             switch(c) {
65                 // end chars.
66                 case ' ': 
67                 case ':': 
68                 case '(': 
69                 case ')': this.addStr(str); this.ungetChar(); return;
70                 case '"': 
71                     if (strlen(str)) {
72                         this.addStr(str); 
73                         str = '';
74                     }
75                     this.strParseQuoted(c);
76                     break;
77                     
78                 default : 
79                     str .= c;
80                     continue;
81             }
82             
83         }
84     }
85     function strParseQuoted(end) 
86     {
87         str = '';   /// ignore \" slashed ???
88         while(true) {
89             if (false === (c = this.getChar())) {
90                 this.addStr(str,true);
91                 return;
92             }
93             if (c == end) {
94                 this.addStr(str,true);
95                 return;
96             }
97             str .= c;
98         }
99             
100     }
101     function addStr(s, q=false) { //q == quoted..
102         s = q ? s : trim(s);
103         if (!strlen(s)) {
104             return;
105         }
106         if (!q) {
107             
108             if ((strtoupper(s) == 'AND') || (strtoupper(s) == 'OR')) {
109                 this.tokens[] = new Text_SearchParser_Token_Op(strtoupper(s));
110                 return;
111             }
112         }
113         this.tokens[] = new Text_SearchParser_Token_String(s);
114     }
115     
116     function getChar()
117     {
118         if (this.i >= this.strlen) {
119             return false;
120         }
121         c = this.str[this.i];
122         this.i++;
123         return c;
124     }
125     function ungetChar()
126     {
127         this.i--;
128     }
129     
130     
131     
132     
133 }