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