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     addStr : function (s,q) { //q == quoted..
106         q = q || false;
107         
108         s = q ? s : Roo.util.Format.trim(s);
109         if (!s.length) {
110             return;
111         }
112         
113         if (!q) {
114             if ((s..toUpperCase() == 'AND') || (s.toUpperCase() == 'OR')) {
115                 this.tokens.push( { type: s.toUpperCase() });
116                 return;
117             }
118         }
119         this.tokens.push( { type : 's' , v : s });
120     },
121     
122     getChar : function ()
123     {
124         
125         if (this.i >= this.strlen) {
126             return false;
127         }
128         c = this.str[this.i];
129         this.i++;
130         return c;
131     },
132     ungetChar : function ()
133     {
134         this.i--;
135     }
136      
137 }
138 Pman.SearchTokenizer.parse(v) {
139     var x = new Pman.SearchTokenizer();
140     return x.parse();
141     
142     
143 }