js2 0.1.8 → 0.3.0.pre5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/js2 +9 -126
- data/bin/js2-ruby +13 -0
- data/lib/js2/command.rb +16 -0
- data/lib/js2/context.rb +35 -0
- data/lib/js2/fs.rb +56 -0
- data/lib/js2/js2.js +1265 -0
- data/lib/js2/rack.rb +35 -0
- data/lib/js2.rb +19 -30
- metadata +28 -91
- data/CHANGELOG +0 -17
- data/Manifest +0 -45
- data/README.md +0 -75
- data/Rakefile +0 -28
- data/config/js2.yml +0 -2
- data/js2.gemspec +0 -36
- data/lib/js2/parser/haml.rb +0 -145
- data/lib/js2/parser/haml_engine.rb +0 -19
- data/lib/js2/parser/lexer.rb +0 -37
- data/lib/js2/parser/tokenizer.rb +0 -3551
- data/lib/js2/ragel/helper.rb +0 -117
- data/lib/js2/ragel/tokenizer.rl +0 -561
- data/lib/js2/ragel/tokenizer.rl.erb +0 -347
- data/lib/js2/standard/class_node.rb +0 -0
- data/lib/js2/standard/factory.rb +0 -289
- data/lib/js2/standard/node.rb +0 -75
- data/lib/js2/util/compilation.rb +0 -77
- data/lib/js2/util/config.rb +0 -84
- data/lib/js2/util/exec.rb +0 -34
- data/lib/js2/util/file_handler.rb +0 -73
- data/lib/js2/util/haml_filter.rb +0 -13
- data/lib/js2/util/jamis.rb +0 -600
- data/lib/js2/util/js2bootstrap.js2 +0 -448
- data/lib/js2/util/processor.rb +0 -88
- data/lib/js2/util/rdoc.rb +0 -37
- data/lib/js2/util/sel_decorator.rb +0 -155
- data/test/compiled/bar.js +0 -3
- data/test/compiled/basic.comp.js +0 -31
- data/test/compiled/basic.js +0 -27
- data/test/compiled/foo.js +0 -3
- data/test/fixtures/bar.js2 +0 -3
- data/test/fixtures/basic.js2 +0 -27
- data/test/fixtures/basic.js2.haml +0 -4
- data/test/fixtures/basic.js2.yml +0 -5
- data/test/fixtures/curry.js2 +0 -5
- data/test/fixtures/foo.js2 +0 -3
- data/test/fixtures/member.js2 +0 -14
- data/test/fixtures/private.js2 +0 -5
- data/test/fixtures/property.js2 +0 -4
- data/test/test_helper.rb +0 -25
- data/test/test_js2.rb +0 -43
- data/wiki/features.md +0 -73
- data/wiki/installation.md +0 -13
@@ -1,347 +0,0 @@
|
|
1
|
-
# Somewhat based on http://www.mozilla.org/js/language/js20-2000-07/formal/lexer-grammar.html
|
2
|
-
# Regular Expression Literals determined with these rules:
|
3
|
-
# http://www.mozilla.org/js/language/js20-1999-03-25/tokens.html
|
4
|
-
|
5
|
-
%%{
|
6
|
-
machine dude;
|
7
|
-
alphtype char;
|
8
|
-
|
9
|
-
action actionStringBegin {
|
10
|
-
}
|
11
|
-
|
12
|
-
action actionStringAcc {
|
13
|
-
}
|
14
|
-
|
15
|
-
action actionStringAccBsEscape {
|
16
|
-
}
|
17
|
-
|
18
|
-
action actionStringAccUtf16 {
|
19
|
-
}
|
20
|
-
|
21
|
-
unicodecharacter = any;
|
22
|
-
unicodeinitialalphabetic = alpha;
|
23
|
-
unicodealphanumeric = alnum;
|
24
|
-
whitespacecharacter = [\t\v\f ];
|
25
|
-
lineterminator = ('\r' | '\n');
|
26
|
-
asciidigit = digit;
|
27
|
-
ws = (whitespacecharacter | lineterminator);
|
28
|
-
|
29
|
-
literals = (
|
30
|
-
'==' | '!=' | '===' | '!==' | '<=' | '>=' | '||' | '&&' | '++' | '--' | '<<' | '<<=' | '>>' | '>>=' | '>>>' | '>>>='| '&=' | '%=' | '^=' | '|=' | '+=' | '-=' | '*=' | '/='
|
31
|
-
);
|
32
|
-
|
33
|
-
# put in class
|
34
|
-
# put in accessor
|
35
|
-
keywords = (
|
36
|
-
'break' | 'case' | 'catch' | 'continue' | 'default' | 'delete' | 'do' |
|
37
|
-
'else' | 'finally' | 'for' | 'function' | 'if' | 'in' | 'instanceof' |
|
38
|
-
'new' | 'return' | 'switch' | 'this' | 'throw' | 'try' | 'typeof' | 'var' | 'accessor' |
|
39
|
-
'void' | 'while' | 'with' | 'const' | 'true' | 'false' | 'null' | 'debugger' |
|
40
|
-
'class' | 'static' | 'foreach' | 'module' | 'include' | 'property'
|
41
|
-
);
|
42
|
-
|
43
|
-
# took out class
|
44
|
-
reserved = (
|
45
|
-
'abstract' | 'boolean' | 'byte' | 'char' | 'double' | 'enum' | 'export' |
|
46
|
-
'extends' | 'final' | 'float' | 'goto' | 'implements' | 'import' | 'int' | 'interface' |
|
47
|
-
'long' | 'native' | 'package' | 'private' | 'protected' | 'public' | 'short' |
|
48
|
-
'super' | 'synchronized' | 'throws' | 'transient' | 'volatile'
|
49
|
-
);
|
50
|
-
|
51
|
-
nonterminator = any - lineterminator;
|
52
|
-
linecommentcharacters = nonterminator*;
|
53
|
-
nonterminatororslash = nonterminator - '/';
|
54
|
-
nonterminatororastreisk = nonterminator - '*';
|
55
|
-
nonterminatororasteriskorslash = nonterminator - ('*' | '/');
|
56
|
-
blockcommentcharacters = (nonterminatororslash | (nonterminatororastreisk '/'))*;
|
57
|
-
multilineblockcommentcharacters = (blockcommentcharacters lineterminator)*;
|
58
|
-
|
59
|
-
linecomment = '//' linecommentcharacters;
|
60
|
-
singlelineblockcomment = '/*' blockcommentcharacters '*/';
|
61
|
-
multilineblockcomment = '/*' multilineblockcommentcharacters blockcommentcharacters '*/';
|
62
|
-
comment = linecomment | singlelineblockcomment | multilineblockcomment;
|
63
|
-
|
64
|
-
string_begin = '\'' @ actionStringBegin;
|
65
|
-
string_end = '\'';
|
66
|
-
stringchar_normal = ^(['\\] | 0..0x1f) @ actionStringAcc;
|
67
|
-
stringchar_bs_esc = '\\'['\\/bfnrt] @ actionStringAccBsEscape;
|
68
|
-
stringchar_utf16 = '\\u'[0-9a-fA-F]{4} @ actionStringAccUtf16;
|
69
|
-
stringchar_bs_other = '\\'^(['\\/bfnrtu]|0..0x1f) @ actionStringAccBsEscape;
|
70
|
-
stringchar = (stringchar_normal | stringchar_bs_esc | stringchar_utf16 | stringchar_bs_other);
|
71
|
-
string = string_begin . stringchar* . string_end;
|
72
|
-
|
73
|
-
ds_string_begin = '"' @ actionStringBegin;
|
74
|
-
ds_string_end = '"';
|
75
|
-
ds_stringchar_normal = ^(["\\] | 0..0x1f) @ actionStringAcc;
|
76
|
-
ds_stringchar_bs_esc = '\\'["\\/bfnrt] @ actionStringAccBsEscape;
|
77
|
-
ds_stringchar_utf16 = '\\u'[0-9a-fA-F]{4} @ actionStringAccUtf16;
|
78
|
-
ds_stringchar_bs_other = '\\'^(["\\/bfnrtu]|0..0x1f) @ actionStringAccBsEscape;
|
79
|
-
ds_stringchar = (ds_stringchar_normal | ds_stringchar_bs_esc | ds_stringchar_utf16 | ds_stringchar_bs_other);
|
80
|
-
ds_string = ds_string_begin . ds_stringchar* . ds_string_end;
|
81
|
-
|
82
|
-
all_string = string | ds_string;
|
83
|
-
|
84
|
-
integer = '-'? . digit+;
|
85
|
-
float = '-'? (
|
86
|
-
(('0' | [1-9][0-9]*) '.' [0-9]+ ([Ee] [+\-]?[0-9]+)?)
|
87
|
-
| (('0' | [1-9][0-9]*) ([Ee] [+\-]?[0-9]+))
|
88
|
-
);
|
89
|
-
number = integer | float;
|
90
|
-
|
91
|
-
identifier_char = ([a-zA-Z0-9_]) | '$';
|
92
|
-
identifier = identifier_char+;
|
93
|
-
|
94
|
-
ordinaryregexpchar = nonterminator - ('/' | '\\');
|
95
|
-
regexpchars = (ordinaryregexpchar | '\\' nonterminator)+;
|
96
|
-
regexpbody = '/' @ {regexp_start = p;} regexpchars '/';
|
97
|
-
regexpflags = ('g' | 'i' | 'm')*;
|
98
|
-
regexpliteral = regexpbody regexpflags;
|
99
|
-
|
100
|
-
specialcasedivide = (
|
101
|
-
identifier '/'
|
102
|
-
);
|
103
|
-
single_char = any;
|
104
|
-
|
105
|
-
var_list = (identifier . ws* . ',' . ws*)* . identifier?;
|
106
|
-
arg_list = '(' . ws* . var_list . ws* .')';
|
107
|
-
|
108
|
-
curry_with = 'with' . ws* . arg_list;
|
109
|
-
curry = 'curry' . ws* . arg_list? . ws* . curry_with? . ws*;
|
110
|
-
|
111
|
-
foreach = 'foreach' . ws* . '(' . ws* . 'var' . ws* . identifier . (ws* . ':' . ws* . identifier)? . ws*;
|
112
|
-
|
113
|
-
js2_include = 'include' . ws+ . identifier;
|
114
|
-
js2_class = 'class' . ws+ . identifier;
|
115
|
-
js2_module = 'module' . ws+ . identifier;
|
116
|
-
|
117
|
-
property = ws . 'property' . ws . var_list;
|
118
|
-
member = ws . ('static' . ws+)? . 'var' . ws+ . identifier;
|
119
|
-
method = ws . ('static' . ws+)? . 'function' . ws* . identifier;
|
120
|
-
private = ws . 'private' . ws;
|
121
|
-
accessor = ws . 'accessor' . ws;
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
main := |*
|
127
|
-
|
128
|
-
lineterminator => {
|
129
|
-
//line_number++;
|
130
|
-
};
|
131
|
-
|
132
|
-
js2_class => {
|
133
|
-
if (in_class == 0) {
|
134
|
-
<%= replacer.start_node :CLASS %>
|
135
|
-
in_class = curly_idx;
|
136
|
-
}
|
137
|
-
};
|
138
|
-
|
139
|
-
private => {
|
140
|
-
if (in_class && in_class == curly_idx) {
|
141
|
-
<%= replacer.start_node :PRIVATE %>
|
142
|
-
<%= replacer.stop_node %>
|
143
|
-
}
|
144
|
-
};
|
145
|
-
|
146
|
-
js2_module => {
|
147
|
-
if (in_class == 0) {
|
148
|
-
<%= replacer.start_node :MODULE %>
|
149
|
-
in_class = curly_idx;
|
150
|
-
}
|
151
|
-
};
|
152
|
-
|
153
|
-
property => {
|
154
|
-
if (in_class && in_class == curly_idx) {
|
155
|
-
<%= replacer.start_member :PROPERTY %>
|
156
|
-
}
|
157
|
-
};
|
158
|
-
|
159
|
-
accessor => {
|
160
|
-
if (in_class && in_class == curly_idx) {
|
161
|
-
<%= replacer.start_member :ACCESSOR %>
|
162
|
-
}
|
163
|
-
};
|
164
|
-
|
165
|
-
|
166
|
-
member => {
|
167
|
-
if (in_class && in_class == curly_idx) {
|
168
|
-
<%= replacer.start_member :MEMBER %>
|
169
|
-
}
|
170
|
-
};
|
171
|
-
|
172
|
-
method => {
|
173
|
-
if (in_class && in_class == curly_idx) {
|
174
|
-
<%= replacer.start_node :METHOD %>
|
175
|
-
}
|
176
|
-
};
|
177
|
-
|
178
|
-
|
179
|
-
js2_include => {
|
180
|
-
if (in_class && in_class == curly_idx) {
|
181
|
-
<%= replacer.start_member :INCLUDE %>
|
182
|
-
}
|
183
|
-
};
|
184
|
-
|
185
|
-
foreach => {
|
186
|
-
<%= replacer.start_node :FOREACH %>
|
187
|
-
mark_on_br = br_count;
|
188
|
-
br_count++;
|
189
|
-
};
|
190
|
-
|
191
|
-
curry => {
|
192
|
-
<%= replacer.start_node :CURRY %>
|
193
|
-
};
|
194
|
-
|
195
|
-
whitespacecharacter => {};
|
196
|
-
comment => {
|
197
|
-
<%= replacer.comment %>
|
198
|
-
};
|
199
|
-
|
200
|
-
all_string => {};
|
201
|
-
number => {};
|
202
|
-
keywords => { };
|
203
|
-
|
204
|
-
literals => { };
|
205
|
-
reserved => { };
|
206
|
-
identifier => { };
|
207
|
-
|
208
|
-
<%= replacer.def_literals %>
|
209
|
-
|
210
|
-
single_char | (('(' | '{' | ';' | '}') ws* regexpliteral) => {
|
211
|
-
char single = data[ts-data];
|
212
|
-
|
213
|
-
if (single == '{') {
|
214
|
-
in_foreach = 0;
|
215
|
-
cb_count++;
|
216
|
-
} else if (single == '}') {
|
217
|
-
cb_count--;
|
218
|
-
if (curly_idx && curlies[curly_idx] == cb_count) {
|
219
|
-
in_foreach = 0;
|
220
|
-
<%= replacer.stop_node %>
|
221
|
-
if (curlies[in_class] == cb_count) {
|
222
|
-
in_class = 0;
|
223
|
-
}
|
224
|
-
}
|
225
|
-
} else if (single == ';' && close_on_semi == 1) {
|
226
|
-
<%= replacer.stop_member %>
|
227
|
-
}
|
228
|
-
|
229
|
-
if (single == ';' || single == '}') {
|
230
|
-
classable = 1;
|
231
|
-
}
|
232
|
-
|
233
|
-
if (single == '(') {
|
234
|
-
br_count++;
|
235
|
-
} else if (single == ')') {
|
236
|
-
br_count--;
|
237
|
-
if (mark_on_br == br_count) {
|
238
|
-
<%= replacer.stop_node %>
|
239
|
-
mark_on_br = -1;
|
240
|
-
}
|
241
|
-
}
|
242
|
-
};
|
243
|
-
|
244
|
-
*|;
|
245
|
-
}%%
|
246
|
-
|
247
|
-
require 'rubygems'
|
248
|
-
require 'inline'
|
249
|
-
|
250
|
-
class JS2::Parser::Tokenizer
|
251
|
-
attr_accessor :data
|
252
|
-
|
253
|
-
inline do |builder|
|
254
|
-
builder.c_raw <<-END
|
255
|
-
|
256
|
-
int tokenize (int argc, VALUE *argv, VALUE self) {
|
257
|
-
// convert ruby string to char*
|
258
|
-
VALUE r_str = argv[0];
|
259
|
-
int data_length = #{RUBY_VERSION.match(/^1\.8/) ? "RSTRING(r_str)->len" : "RSTRING_LEN(r_str)"};
|
260
|
-
char* data = StringValuePtr(r_str);
|
261
|
-
|
262
|
-
int in_class = 0;
|
263
|
-
int in_module = 0;
|
264
|
-
int close_on_semi = 0;
|
265
|
-
int classable = 1;
|
266
|
-
int in_foreach = 0;
|
267
|
-
|
268
|
-
// start vars
|
269
|
-
VALUE start_argv[3];
|
270
|
-
ID start_sym = rb_intern("start_node");
|
271
|
-
|
272
|
-
// stop vars
|
273
|
-
VALUE stop_argv[1];
|
274
|
-
ID stop_sym = rb_intern("stop_node");
|
275
|
-
|
276
|
-
// mark vars
|
277
|
-
VALUE mark_argv[1];
|
278
|
-
ID mark_sym = rb_intern("mark_node");
|
279
|
-
int mark_on_br = -1;
|
280
|
-
|
281
|
-
|
282
|
-
VALUE warn_intv[1];
|
283
|
-
ID warn_sym = rb_intern("warn_int");
|
284
|
-
|
285
|
-
|
286
|
-
// state vars
|
287
|
-
int i = 0; // iterator
|
288
|
-
int j = 0; // iterator
|
289
|
-
|
290
|
-
char keyword[100]; // keyword
|
291
|
-
char single;
|
292
|
-
int is_static = 0;
|
293
|
-
int is_private = 0;
|
294
|
-
|
295
|
-
// curly handling
|
296
|
-
int curlies[1000];
|
297
|
-
curlies[0] = 0;
|
298
|
-
int curly_idx = 0; // on purpose!
|
299
|
-
int cb_count = 0;
|
300
|
-
int br_count = 0;
|
301
|
-
int close_on_br = 0;
|
302
|
-
|
303
|
-
<%= replacer.declare_vars [ :CLASS, :METHOD, :MEMBER, :STUFF, :ACCESSOR, :COMMENT, :FOREACH, :MODULE, :INCLUDE, :CURRY, :PROPERTY, :PRIVATE, :STATIC ] %>
|
304
|
-
// ragel variables
|
305
|
-
int cs, act;
|
306
|
-
char *ts = NULL;
|
307
|
-
char *te = NULL;
|
308
|
-
char *p = data;
|
309
|
-
char *pe = p + data_length;
|
310
|
-
char *eof = pe;
|
311
|
-
char* regexp_start = NULL;
|
312
|
-
|
313
|
-
<%= replacer.start_node :STUFF, 0, true %>
|
314
|
-
|
315
|
-
%% write data;
|
316
|
-
%% write init;
|
317
|
-
%% write exec;
|
318
|
-
|
319
|
-
<%= replacer.stop_node('data_length-1') %>
|
320
|
-
}
|
321
|
-
|
322
|
-
END
|
323
|
-
end
|
324
|
-
|
325
|
-
def warn_int (int)
|
326
|
-
puts int.to_s
|
327
|
-
end
|
328
|
-
|
329
|
-
def start_node (type, idx, is_static)
|
330
|
-
@lexer.start_node(type, idx, is_static == 1)
|
331
|
-
end
|
332
|
-
|
333
|
-
def stop_node (idx)
|
334
|
-
@lexer.stop_node(idx)
|
335
|
-
end
|
336
|
-
|
337
|
-
def mark_node (idx)
|
338
|
-
@lexer.mark_node(idx)
|
339
|
-
end
|
340
|
-
|
341
|
-
def tokenize! (data, lexer)
|
342
|
-
@data = data
|
343
|
-
@lexer = lexer
|
344
|
-
self.tokenize(data)
|
345
|
-
end
|
346
|
-
|
347
|
-
end
|
File without changes
|
data/lib/js2/standard/factory.rb
DELETED
@@ -1,289 +0,0 @@
|
|
1
|
-
# TODO: break this up into multiple files
|
2
|
-
class JS2::Standard::PageNode < JS2::Standard::Node
|
3
|
-
attr_accessor :klasses, :file
|
4
|
-
|
5
|
-
def initialize (idx, string, factory)
|
6
|
-
@klasses = []
|
7
|
-
super(idx, string, factory)
|
8
|
-
end
|
9
|
-
|
10
|
-
def stop (idx)
|
11
|
-
JS2::Standard::ForeachNode.reset!
|
12
|
-
super(idx)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class JS2::Standard::CommentNode < JS2::Standard::Node
|
17
|
-
def clean
|
18
|
-
return to_s.gsub(%r|^\s*\/?\*+\s?\/?|, '')
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class JS2::Standard::ClassNode < JS2::Standard::Node
|
23
|
-
REGEX = /^(\s*class|module)\s+([\w\.]+)\s+(extends\s+([\w\.]+))?\s*\{(.*)/m
|
24
|
-
|
25
|
-
def handle_first_string (str)
|
26
|
-
if m = str.match(REGEX)
|
27
|
-
@name = m[2]
|
28
|
-
@extends = m[4]
|
29
|
-
@ending = m[5]
|
30
|
-
|
31
|
-
if @extends
|
32
|
-
@extends = "#{@name}.oo('extends', #{@extends});"
|
33
|
-
end
|
34
|
-
|
35
|
-
return %|JS2.OO.#{create_name}("#{@name}"); #{@extends} (function (K,Package) {var self=K; var _super=JS2.OO['super']; #{@ending}|
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def methods
|
40
|
-
ret = []
|
41
|
-
|
42
|
-
@children.each do |c|
|
43
|
-
if c.is_a?(JS2::Standard::MethodNode)
|
44
|
-
ret << c
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
return ret
|
49
|
-
end
|
50
|
-
|
51
|
-
def name
|
52
|
-
if m = @string[@start_idx .. @stop_idx].match(REGEX)
|
53
|
-
return m[2]
|
54
|
-
else
|
55
|
-
return ''
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def create_name
|
60
|
-
return 'createClass'
|
61
|
-
end
|
62
|
-
|
63
|
-
def handle_ending (str)
|
64
|
-
pkg = @name.split(/\./)
|
65
|
-
pkg.pop()
|
66
|
-
|
67
|
-
if pkg.empty?
|
68
|
-
pkg = 'null'
|
69
|
-
else
|
70
|
-
pkg = pkg.join('.')
|
71
|
-
end
|
72
|
-
|
73
|
-
return str.sub(/\s*\z/, ")(#{@name}, #{pkg});")
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
class JS2::Standard::ModuleNode < JS2::Standard::ClassNode
|
78
|
-
def createName
|
79
|
-
return 'createModule'
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
class JS2::Standard::PrivateNode < JS2::Standard::Node
|
84
|
-
def handle_first_string (str)
|
85
|
-
return str.sub(/private/m, '// private')
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
class JS2::Standard::IncludeNode < JS2::Standard::Node
|
90
|
-
def handle_first_string (s)
|
91
|
-
return s.sub(/include\s*/, "K.oo('include', ").sub(/;$/, ');');
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
|
96
|
-
class JS2::Standard::MemberNode < JS2::Standard::Node
|
97
|
-
def handle_first_string (s)
|
98
|
-
method = 'member'
|
99
|
-
|
100
|
-
if s.match(/^(\s*)(static)\s*/)
|
101
|
-
method = 'staticMember'
|
102
|
-
s = s.sub(/static\s*/, '')
|
103
|
-
end
|
104
|
-
|
105
|
-
return s.sub(/var\s*([\$\w]+)/) { "K.oo('#{method}', '" + $1 + "'" }.sub(/;(\s*)$/) { ");#{$1}" }.sub(/\s*=/, ', ');
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|
109
|
-
|
110
|
-
class JS2::Standard::CurryNode < JS2::Standard::Node
|
111
|
-
REGEX = %r|^curry\s*([^\{]*)?\{(.*)$|m
|
112
|
-
REGEX_WITH = %r|with\s+\(([^)]*)\)|
|
113
|
-
REGEX_ARGS = %r|^\s*\(([^)]*)\)|
|
114
|
-
REGEX_ENDING = %r|\}([^\}]*)\z|
|
115
|
-
|
116
|
-
def handle_first_string (str)
|
117
|
-
m = str.match(REGEX)
|
118
|
-
@decl = m[1].strip
|
119
|
-
@stop = m[2]
|
120
|
-
|
121
|
-
@args = ''
|
122
|
-
@in_scoped = ''
|
123
|
-
@scoped = ''
|
124
|
-
|
125
|
-
if m = @decl.match(REGEX_WITH)
|
126
|
-
in_scoped = m[1].split(',').collect { |v| v.strip }
|
127
|
-
scoped = in_scoped.collect { |v| v == 'this' ? 'self' : v }
|
128
|
-
if in_scoped.length
|
129
|
-
@scoped = scoped.join(', ')
|
130
|
-
@in_scoped = in_scoped.join(', ')
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
if m = @decl.match(%r|^\s*\(([^)]*)\)|)
|
135
|
-
@args = m[1].strip
|
136
|
-
end
|
137
|
-
|
138
|
-
return %|(function (#{@scoped}) { return function (#{@args}) {#{@stop}|
|
139
|
-
end
|
140
|
-
|
141
|
-
def handle_ending (str)
|
142
|
-
if m = str.match(REGEX_ENDING)
|
143
|
-
return str.sub(REGEX_ENDING) { |str| "}})(#{@in_scoped})#{m[1]}" }
|
144
|
-
end
|
145
|
-
|
146
|
-
return str
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
|
151
|
-
class JS2::Standard::MethodNode < JS2::Standard::Node
|
152
|
-
attr_accessor :name, :args, :static
|
153
|
-
|
154
|
-
REGEX = /^(\s*)(static\s+)?function\s+([\$\w\.]+)\s*\(([^)]*)\)\s*\{(.*)/m
|
155
|
-
|
156
|
-
def handle_first_string (s)
|
157
|
-
m = s.match(REGEX)
|
158
|
-
space = m[1]
|
159
|
-
@static = m[2]
|
160
|
-
@name = m[3]
|
161
|
-
@args = m[4]
|
162
|
-
@start = m[5]
|
163
|
-
|
164
|
-
m = static ? 'staticMember' : 'method'
|
165
|
-
return %|#{space}K.oo('#{m}', "#{@name}", function (#{@args}) {#{@start}|
|
166
|
-
end
|
167
|
-
|
168
|
-
def handle_ending (str)
|
169
|
-
return str.sub!(/(\s*)\z/) { |m| ");#{m}" }
|
170
|
-
end
|
171
|
-
|
172
|
-
end
|
173
|
-
|
174
|
-
class JS2::Standard::AccessorNode < JS2::Standard::Node
|
175
|
-
REGEX = /(\s*)accessor(\s+)([\w+,\s]+\w)(\s*);(\s*)/
|
176
|
-
|
177
|
-
def handle_first_string (str)
|
178
|
-
m = str.match(REGEX)
|
179
|
-
space = m[1]
|
180
|
-
mid_space = m[2]
|
181
|
-
list = m[3].split(/,/).collect { |i| i.strip }
|
182
|
-
end_space = m[4]
|
183
|
-
trailing = m[5]
|
184
|
-
|
185
|
-
return %|#{space}K.oo('accessor',#{mid_space}[ #{list.collect { |i| "'#{i}'" }.join(',')} ])#{end_space};#{trailing}|
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
class JS2::Standard::ForeachNode < JS2::Standard::Node
|
190
|
-
# start var iterator array
|
191
|
-
REGEX = /(\s*)foreach\s*\(\s*var\s*([\$\w]+)(\s*:\s*([\$\w]))?\s*in\s*([^\s]+)\s*\)/
|
192
|
-
attr_accessor :iterator, :item, :array
|
193
|
-
@@inc = 0
|
194
|
-
|
195
|
-
def handle_first_string (s)
|
196
|
-
m = s.match(REGEX)
|
197
|
-
start = m[1]
|
198
|
-
item = m[2]
|
199
|
-
iterator = m[4]
|
200
|
-
array = m[5]
|
201
|
-
|
202
|
-
it = iterator
|
203
|
-
|
204
|
-
unless iterator
|
205
|
-
it = 'it' + @@inc.to_s
|
206
|
-
@@inc += 1
|
207
|
-
end
|
208
|
-
|
209
|
-
len = it + '__len'
|
210
|
-
arr = it + '__arr'
|
211
|
-
|
212
|
-
return %(#{start}for (var #{it}=0,#{item},#{arr}=#{array},#{len}=#{arr}.length; (#{item}=#{arr}[#{it}]) || #{it}<#{len}; #{it}++))
|
213
|
-
end
|
214
|
-
|
215
|
-
def self.reset!
|
216
|
-
@@inc = 0
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
class JS2::Standard::PropertyNode < JS2::Standard::Node
|
221
|
-
REGEX = /(\s*)property(\s+)([\w+,\s]+\w)(\s*);(\s*)/
|
222
|
-
def handle_first_string (s)
|
223
|
-
m = s.match(REGEX)
|
224
|
-
space = m[1]
|
225
|
-
mid_space = m[2]
|
226
|
-
list = m[3].split(/,/).collect { |i| i.strip }
|
227
|
-
end_space = m[4]
|
228
|
-
trailing = m[5]
|
229
|
-
|
230
|
-
return %|#{space}K.oo('property',#{mid_space}[ #{list.collect { |i| "'#{i}'" }.join(',')} ])#{end_space};#{trailing}|
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
|
235
|
-
class JS2::Standard::StuffNode < JS2::Standard::Node
|
236
|
-
end
|
237
|
-
|
238
|
-
|
239
|
-
class JS2::Standard::Factory
|
240
|
-
attr_accessor :decorators
|
241
|
-
|
242
|
-
@@supports = [ :CLASS, :MEMBER, :METHOD, :ACCESSOR, :FOREACH, :PROPERTY, :INCLUDE, :CURRY, :PAGE, :COMMENT, :STUFF, :MODULE, :PRIVATE ]
|
243
|
-
@@lookup = Hash.new
|
244
|
-
|
245
|
-
@@supports.each do |v|
|
246
|
-
name = v.to_s.downcase.sub(/(\w)/) { |m| m.upcase }
|
247
|
-
@@lookup[v] = eval "JS2::Standard::#{name}Node"
|
248
|
-
end
|
249
|
-
|
250
|
-
def initialize
|
251
|
-
@decorators = []
|
252
|
-
end
|
253
|
-
|
254
|
-
def get_class (type)
|
255
|
-
klass = @@lookup[type] || JS2::Standard::Node
|
256
|
-
end
|
257
|
-
|
258
|
-
def page_node (string, file = nil)
|
259
|
-
@page = new_node(:PAGE, 0, string)
|
260
|
-
@page.file = file
|
261
|
-
@comment = nil
|
262
|
-
return @page
|
263
|
-
end
|
264
|
-
|
265
|
-
def new_node (type, idx, string)
|
266
|
-
klass = get_class(type)
|
267
|
-
node = klass.new(idx, string, self)
|
268
|
-
|
269
|
-
if type == :CLASS || type == :MODULE
|
270
|
-
@page.klasses << node
|
271
|
-
end
|
272
|
-
|
273
|
-
if type == :COMMENT
|
274
|
-
@comment = node
|
275
|
-
elsif @comment
|
276
|
-
node.comment = @comment
|
277
|
-
@comment = nil
|
278
|
-
end
|
279
|
-
|
280
|
-
setup_node(node)
|
281
|
-
|
282
|
-
return node
|
283
|
-
end
|
284
|
-
|
285
|
-
def setup_node (node)
|
286
|
-
# virtual
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
data/lib/js2/standard/node.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
class JS2::Standard::Node
|
2
|
-
attr_accessor :type, :start_idx, :stop_idx, :children, :starting, :factory, :comment
|
3
|
-
|
4
|
-
def initialize (start_idx, str, factory)
|
5
|
-
@start_idx = start_idx
|
6
|
-
@string = str
|
7
|
-
@children = []
|
8
|
-
@factory = factory
|
9
|
-
@output = ''
|
10
|
-
end
|
11
|
-
|
12
|
-
def add_child (type, start_idx)
|
13
|
-
child = factory.new_node(type, start_idx, @string)
|
14
|
-
@children.push(child)
|
15
|
-
return child
|
16
|
-
end
|
17
|
-
|
18
|
-
def stop (idx)
|
19
|
-
@stop_idx = idx
|
20
|
-
process!
|
21
|
-
end
|
22
|
-
|
23
|
-
def to_s
|
24
|
-
return @output
|
25
|
-
end
|
26
|
-
|
27
|
-
def process! ()
|
28
|
-
last_idx = @start_idx
|
29
|
-
str = ''
|
30
|
-
|
31
|
-
first = true
|
32
|
-
@children.each do |c|
|
33
|
-
if c.start_idx > last_idx
|
34
|
-
str << handle_string(@string[last_idx .. c.start_idx-1], first)
|
35
|
-
first = false
|
36
|
-
end
|
37
|
-
|
38
|
-
result = c.to_s()
|
39
|
-
|
40
|
-
@factory.decorators.each do |d|
|
41
|
-
d.decorate(result, c)
|
42
|
-
end
|
43
|
-
|
44
|
-
str << result
|
45
|
-
last_idx = c.stop_idx + 1
|
46
|
-
end
|
47
|
-
|
48
|
-
if last_idx < @stop_idx
|
49
|
-
str << handle_string(@string[last_idx .. @stop_idx], first)
|
50
|
-
end
|
51
|
-
|
52
|
-
str = handle_ending(str)
|
53
|
-
|
54
|
-
@output = str
|
55
|
-
end
|
56
|
-
|
57
|
-
private
|
58
|
-
|
59
|
-
def handle_string (str, first)
|
60
|
-
if first
|
61
|
-
return handle_first_string(str)
|
62
|
-
else
|
63
|
-
return str
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def handle_ending (str)
|
68
|
-
return str
|
69
|
-
end
|
70
|
-
|
71
|
-
def handle_first_string (str)
|
72
|
-
return str
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|