JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / JSDOC / Lang.vala
1 //<script type="text/javscript">
2
3 /**
4         @namespace
5 */
6 // test
7 // valac gitlive/app.Builder.js/JsRender/Lang.vala --pkg gee-1.0 -o /tmp/Lang ;/tmp/Lang
8
9 /*
10 void main () {
11     new JsRender.Lang_Class();
12     print(JsRender.Lang.keyword("delete") + "\n");
13 }
14 */
15  
16
17 namespace JSDOC {
18
19     public Lang_Class Lang = null;
20     
21     public class Lang_Class : Object {
22         
23         GLib.List<string> coreObjects;
24         Gee.HashMap<string,string> whitespaceNames;
25         Gee.HashMap<string,string> newlineNames;
26         Gee.HashMap<string,string> keywordNames;
27         Gee.HashMap<string,string> puncNames;
28         Gee.HashMap<string,string> matchingNames;
29         public Gee.ArrayList<string> match_strings;
30         
31         public Lang_Class ()
32         {
33             if (Lang != null) {
34                 //print("lang not null\n");
35                 return;
36             }
37             //print("init\n");
38             this.init();
39             //print("init Lang");
40             Lang = this;
41             Lang.ref();
42             
43         }
44         
45         
46         public bool isBuiltin(string  name) {
47             return (this.coreObjects.index(name) > -1);
48         }
49         
50         public string whitespace (string ch) {
51             return this.whitespaceNames.get(ch);
52         }
53         public string  newline (string ch) {
54             return this.newlineNames.get(ch);
55         }
56         public string keyword(string word) {
57             return this.keywordNames.get("="+word);
58         }
59         
60         public string? matching(string name) {
61         
62             
63             switch(name) {
64                                 case   "LEFT_PAREN": return "RIGHT_PAREN";
65                case "RIGHT_PAREN": return "LEFT_PAREN";
66                case "LEFT_CURLY": return "RIGHT_CURLY";
67                case "RIGHT_CURLY": return "LEFT_CURLY";
68                case "LEFT_BRACE": return "RIGHT_BRACE";
69                case "RIGHT_BRACE": return "LEFT_BRACE";
70                default:
71                                 return null;
72            };
73         
74             //return this.matchingNames.get(name);
75         }
76         
77         public bool isKeyword(string word) {
78             return this.keywordNames.get("=" + word) != null;
79             
80         }
81         public string punc (string ch) {
82             return this.puncNames.get(ch); // ?? does [xxx] work!?
83         }
84         
85         public bool isNumber (string str) {
86             return Regex.match_simple("^(\\.[0-9]|[0-9]+\\.|[0-9])[0-9]*([eE][+-][0-9]+)?$",str);
87         }
88     
89         public bool  isHexDec (string str) {
90             return Regex.match_simple("^0x[0-9A-F]+$",str);
91         }
92     
93         public bool isWordChar (string str) {
94             return Regex.match_simple("^[a-zA-Z0-9$_.]+$", str);
95         }
96     
97         public bool isSpace (string str) {
98             return this.whitespaceNames.get(str) != null;
99         }
100             public bool isSpaceC (char str) {
101                         var s = str.to_string();
102             return this.whitespaceNames.get(s) != null;
103         }
104         
105         public bool isNewline (string str) {
106             return this.newlineNames.get(str) != null;
107                 }          
108         public bool isNewlineC (char str) {
109                 var s =str.to_string();
110             return this.newlineNames.get(s) != null;
111         }
112             public bool isBoolean (string str) {
113                         var ss = str.down();
114             return ss == "false" || ss == "true";
115         }
116         
117          
118         
119         void init() {
120             
121             this.coreObjects = new GLib.List<string>();
122             
123             this.whitespaceNames = new Gee.HashMap<string,string>();
124             this.newlineNames = new Gee.HashMap<string,string>();
125             this.keywordNames = new Gee.HashMap<string,string>();
126             this.puncNames = new Gee.HashMap<string,string>();
127             this.matchingNames = new Gee.HashMap<string,string>();
128             this.match_strings = new Gee.ArrayList<string>();
129             
130             
131             string[] co = { "_global_", "Array", "Boolean", "Date", "Error", 
132                 "Function", "Math", "Number", "Object", "RegExp", "String" };
133             for(var i =0; i< co.length;i++ ) {
134                 this.coreObjects.append(co[i]);
135                 this.match_strings.add(co[i]);
136             }
137             string[] ws =  {
138                 " :SPACE",
139                 "\f:FORMFEED",
140                 "\t:TAB" //,
141               //  "\u0009:UNICODE_TAB",
142               //  "\u000A:UNICODE_NBR",
143               //  "\u0008:VERTICAL_TAB"
144             };
145             for(var i =0; i< ws.length;i++ ) {
146                 var x = ws[i].split(":");
147                 this.whitespaceNames.set(x[0],x[1]);
148             }
149             
150             ws = {
151                 "\n:NEWLINE",
152                 "\r:RETURN" //,
153     //            "\u000A:UNICODE_LF",
154       //          "\u000D:UNICODE_CR",
155         //        "\u2029:UNICODE_PS",
156           //      "\u2028:UNICODE_LS"
157             };
158             for(var i =0; i< ws.length;i++ ) {
159                 var x = ws[i].split(":");
160                 this.newlineNames.set(x[0],x[1]);
161             }
162             ws = {
163                 "=break:BREAK",
164                 "=case:CASE",
165                 "=catch:CATCH",
166                 "=const:VAR",
167                 "=continue:CONTINUE",
168                 "=default:DEFAULT",
169                 "=delete:DELETE",
170                 "=do:DO",
171                 "=else:ELSE",
172                 "=false:FALSE",
173                 "=finally:FINALLY",
174                 "=for:FOR",
175                 "=function:FUNCTION",
176                 "=if:IF",
177                 "=in:IN",
178                 "=instanceof:INSTANCEOF",
179                 "=new:NEW",
180                 "=null:NULL",
181                 "=return:RETURN",
182                 "=switch:SWITCH",
183                 "=this:THIS",
184                 "=throw:THROW",
185                 "=true:TRUE",
186                 "=try:TRY",
187                 "=typeof:TYPEOF",
188                 "=void:VOID",
189                 "=while:WHILE",
190                 "=with:WITH",
191                 "=var:VAR"
192              };
193             for(var i =0; i< ws.length;i++ ) {
194                 var x = ws[i].split(":");
195                 this.keywordNames.set(x[0],x[1]);
196                 this.match_strings.add(x[0].substring(1));
197             }
198         
199       
200             ws={
201                 "; SEMICOLON",
202                 ", COMMA",
203                 "? HOOK",
204                 ": COLON",
205                 "|| OR", 
206                 "&& AND",
207                 "| BITWISE_OR",
208                 "^ BITWISE_XOR",
209                 "& BITWISE_AND",
210                 "=== STRICT_EQ", 
211                 "== EQ",
212                 "= ASSIGN",
213                 "!== STRICT_NE",
214                 "!= NE",
215                 "<< LSH",
216                 "<= LE", 
217                 "< LT",
218                 ">>> URSH",
219                 ">> RSH",
220                 ">= GE",
221                 "> GT", 
222                 "++ INCREMENT",
223                 "-- DECREMENT",
224                 "+ PLUS",
225                 "- MINUS",
226                 "* MUL",
227                 "/ DIV", 
228                 "% MOD",
229                 "! NOT",
230                 "~ BITWISE_NOT",
231                 ". DOT",
232                 "[ LEFT_BRACE",
233                 "] RIGHT_BRACE",
234                 "{ LEFT_CURLY",
235                 "} RIGHT_CURLY",
236                 "( LEFT_PAREN",
237                 ") RIGHT_PAREN"
238             };
239             for(var i =0; i< ws.length;i++ ) {
240                 var x = ws[i].split(" ");
241                 this.puncNames.set(x[0],x[1]);
242             }
243         
244            string[] wsp = {
245                "LEFT_PAREN:RIGHT_PAREN",
246                "RIGHT_PAREN:LEFT_PAREN",
247                "LEFT_CURLY:RIGHT_CURLY",
248                "RIGHT_CURLY:LEFT_CURLY",
249                "LEFT_BRACE:RIGHT_BRACE",
250                "RIGHT_BRACE:LEFT_BRACE"
251            };
252            for(var i =0; i< wsp.length;i++ ) {
253                var x = wsp[i].split(":");
254                this.matchingNames.set(x[0],x[1]);
255            }
256            
257            
258            
259            
260         }
261         
262         
263     }
264 }