Pman.Gnumeric.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 = typeof(s) == 'string' ? s : '';
18         this.strlen = typeof(s) == 'string' ? s.length : 0;
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         // sort tokens longest first..
53         
54         
55         
56         // should not get here...
57         return this.tokens;
58     },
59     strParse : function ()
60     {
61         var c;
62         var str = '';
63         while(true) {
64             c = this.getChar();
65             if (false === c) {
66                 this.addStr(str);
67                 return;
68             }
69             switch(c) {
70                 // end chars.
71                 case ' ': 
72                 case ':': 
73                 case '(': 
74                 case ')': this.addStr(str); this.ungetChar(); return;
75                 case '"': 
76                     if (str.length) {
77                         this.addStr(str); 
78                         str = '';
79                     }
80                     this.strParseQuoted(c);
81                     break;
82                     
83                 default : 
84                     str += c;
85                     continue;
86             }
87             
88         }
89     },
90     
91     strParseQuoted: function (end) 
92     {
93         var str = '';   /// ignore \" slashed ???
94         var c;
95         while(true) {
96             c = this.getChar();
97             if (false === c) {
98                 this.addStr(str,true);
99                 return;
100             }
101             if (c == end) {
102                 this.addStr(str,true);
103                 return;
104             }
105             str += c;
106         }
107             
108     },
109     addStr : function (s,q) { //q == quoted..
110         q = q || false;
111         
112         s = q ? s : Roo.util.Format.trim(s);
113         if (!s.length) {
114             return;
115         }
116         
117         if (!q) {
118             if ((s.toUpperCase() == 'AND') || (s.toUpperCase() == 'OR')) {
119                 this.tokens.push( { type: s.toUpperCase() });
120                 return;
121             }
122         }
123         this.tokens.push( { type : 's' , v : s, q: q });
124     },
125     
126     getChar : function ()
127     {
128         
129         if (this.i >= this.strlen) {
130             return false;
131         }
132         c = this.str[this.i];
133         this.i++;
134         return c;
135     },
136     ungetChar : function ()
137     {
138         this.i--;
139     }
140      
141 };
142
143 Pman.SearchTokenizer.parse = function(v) {
144     var x = new Pman.SearchTokenizer(v);
145     return x.parse();
146     
147 }