coffee-script 0.3.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +0,0 @@
1
- module CoffeeScript
2
-
3
- # Racc will raise this Exception whenever a syntax error occurs. The main
4
- # benefit over the Racc::ParseError is that the CoffeeScript::ParseError is
5
- # line-number aware.
6
- class ParseError < Racc::ParseError
7
-
8
- TOKEN_MAP = {
9
- 'INDENT' => 'indent',
10
- 'OUTDENT' => 'outdent',
11
- "\n" => 'newline'
12
- }
13
-
14
- def initialize(token_id, value, stack=nil, message=nil)
15
- @token_id, @value, @stack, @message = token_id, value, stack, message
16
- end
17
-
18
- def message
19
- line = @value.respond_to?(:line) ? @value.line : "END"
20
- line_part = "line #{line}:"
21
- id_part = @token_id != @value.to_s ? " unexpected #{@token_id.to_s.downcase}" : ""
22
- val_part = @message || "for #{TOKEN_MAP[@value.to_s] || "'#{@value}'"}"
23
- "#{line_part} syntax error, #{val_part}#{id_part}"
24
- end
25
- alias_method :inspect, :message
26
-
27
- end
28
-
29
- end
@@ -1,477 +0,0 @@
1
- (function(){
2
- var Parser, __a, __b, __c, __d, __e, __f, bnf, grammar, name, non_terminal, o, operators, option, parser, part, tokens, unwrap;
3
- var __hasProp = Object.prototype.hasOwnProperty;
4
- Parser = require('jison').Parser;
5
- process.mixin(require('./nodes'));
6
- // DSL ===================================================================
7
- // Detect functions: [
8
- unwrap = /function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
9
- // Quickie DSL for Jison access.
10
- o = function o(pattern_string, func) {
11
- var match;
12
- if (func) {
13
- func = (match = (func + "").match(unwrap)) ? match[1] : '(' + func + '())';
14
- return [pattern_string, '$$ = ' + func + ';'];
15
- } else {
16
- return [pattern_string, '$$ = $1;'];
17
- }
18
- };
19
- // Precedence ===========================================================
20
- operators = [["left", '?'], ["right", 'NOT', '!', '!!', '~', '++', '--'], ["left", '*', '/', '%'], ["left", '+', '-'], ["left", '<<', '>>', '>>>'], ["left", '&', '|', '^'], ["left", '<=', '<', '>', '>='], ["right", '==', '!=', 'IS', 'ISNT'], ["left", '&&', '||', 'AND', 'OR'], ["right", '-=', '+=', '/=', '*=', '%='], ["right", 'DELETE', 'INSTANCEOF', 'TYPEOF'], ["left", '.'], ["right", 'INDENT'], ["left", 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY'], ["right", 'THROW', 'FOR', 'NEW', 'SUPER'], ["left", 'EXTENDS'], ["left", '||=', '&&=', '?='], ["right", 'ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'WHILE']];
21
- // Grammar ==============================================================
22
- grammar = {
23
- // All parsing will end in this rule, being the trunk of the AST.
24
- Root: [o("", function() {
25
- return new Expressions();
26
- }), o("TERMINATOR", function() {
27
- return new Expressions();
28
- }), o("Expressions"), o("Block TERMINATOR")
29
- ],
30
- // Any list of expressions or method body, seperated by line breaks or semis.
31
- Expressions: [o("Expression", function() {
32
- return Expressions.wrap([$1]);
33
- }), o("Expressions TERMINATOR Expression", function() {
34
- return $1.push($3);
35
- }), o("Expressions TERMINATOR")
36
- ],
37
- // All types of expressions in our language. The basic unit of CoffeeScript
38
- // is the expression.
39
- Expression: [o("Value"), o("Call"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("Throw"), o("Return"), o("While"), o("For"), o("Switch"), o("Extends"), o("Splat"), o("Existence"), o("Comment")],
40
- // A block of expressions. Note that the Rewriter will convert some postfix
41
- // forms into blocks for us, by altering the token stream.
42
- Block: [o("INDENT Expressions OUTDENT", function() {
43
- return $2;
44
- }), o("INDENT OUTDENT", function() {
45
- return new Expressions();
46
- })
47
- ],
48
- // All hard-coded values. These can be printed straight to JavaScript.
49
- Literal: [o("NUMBER", function() {
50
- return new LiteralNode(yytext);
51
- }), o("STRING", function() {
52
- return new LiteralNode(yytext);
53
- }), o("JS", function() {
54
- return new LiteralNode(yytext);
55
- }), o("REGEX", function() {
56
- return new LiteralNode(yytext);
57
- }), o("BREAK", function() {
58
- return new LiteralNode(yytext);
59
- }), o("CONTINUE", function() {
60
- return new LiteralNode(yytext);
61
- }), o("ARGUMENTS", function() {
62
- return new LiteralNode(yytext);
63
- }), o("TRUE", function() {
64
- return new LiteralNode(true);
65
- }), o("FALSE", function() {
66
- return new LiteralNode(false);
67
- }), o("YES", function() {
68
- return new LiteralNode(true);
69
- }), o("NO", function() {
70
- return new LiteralNode(false);
71
- }), o("ON", function() {
72
- return new LiteralNode(true);
73
- }), o("OFF", function() {
74
- return new LiteralNode(false);
75
- })
76
- ],
77
- // Assignment to a variable (or index).
78
- Assign: [o("Value ASSIGN Expression", function() {
79
- return new AssignNode($1, $3);
80
- })
81
- ],
82
- // Assignment within an object literal (can be quoted).
83
- AssignObj: [o("IDENTIFIER ASSIGN Expression", function() {
84
- return new AssignNode(new ValueNode(yytext), $3, 'object');
85
- }), o("STRING ASSIGN Expression", function() {
86
- return new AssignNode(new ValueNode(new LiteralNode(yytext)), $3, 'object');
87
- }), o("Comment")
88
- ],
89
- // A return statement.
90
- Return: [o("RETURN Expression", function() {
91
- return new ReturnNode($2);
92
- }), o("RETURN", function() {
93
- return new ReturnNode(new ValueNode(new LiteralNode('null')));
94
- })
95
- ],
96
- // A comment.
97
- Comment: [o("COMMENT", function() {
98
- return new CommentNode(yytext);
99
- })
100
- ],
101
- //
102
- // # Arithmetic and logical operators
103
- // # For Ruby's Operator precedence, see: [
104
- // # https://www.cs.auckland.ac.nz/references/ruby/ProgrammingRuby/language.html
105
- // Operation: [
106
- // o "! Expression", -> new OpNode($1, $2)
107
- // o "!! Expression", -> new OpNode($1, $2)
108
- // o "- Expression", -> new OpNode($1, $2)
109
- // o "+ Expression", -> new OpNode($1, $2)
110
- // o "NOT Expression", -> new OpNode($1, $2)
111
- // o "~ Expression", -> new OpNode($1, $2)
112
- // o "-- Expression", -> new OpNode($1, $2)
113
- // o "++ Expression", -> new OpNode($1, $2)
114
- // o "DELETE Expression", -> new OpNode($1, $2)
115
- // o "TYPEOF Expression", -> new OpNode($1, $2)
116
- // o "Expression --", -> new OpNode($2, $1, null, true)
117
- // o "Expression ++", -> new OpNode($2, $1, null, true)
118
- //
119
- // o "Expression * Expression", -> new OpNode($2, $1, $3)
120
- // o "Expression / Expression", -> new OpNode($2, $1, $3)
121
- // o "Expression % Expression", -> new OpNode($2, $1, $3)
122
- //
123
- // o "Expression + Expression", -> new OpNode($2, $1, $3)
124
- // o "Expression - Expression", -> new OpNode($2, $1, $3)
125
- //
126
- // o "Expression << Expression", -> new OpNode($2, $1, $3)
127
- // o "Expression >> Expression", -> new OpNode($2, $1, $3)
128
- // o "Expression >>> Expression", -> new OpNode($2, $1, $3)
129
- //
130
- // o "Expression & Expression", -> new OpNode($2, $1, $3)
131
- // o "Expression | Expression", -> new OpNode($2, $1, $3)
132
- // o "Expression ^ Expression", -> new OpNode($2, $1, $3)
133
- //
134
- // o "Expression <= Expression", -> new OpNode($2, $1, $3)
135
- // o "Expression < Expression", -> new OpNode($2, $1, $3)
136
- // o "Expression > Expression", -> new OpNode($2, $1, $3)
137
- // o "Expression >= Expression", -> new OpNode($2, $1, $3)
138
- //
139
- // o "Expression == Expression", -> new OpNode($2, $1, $3)
140
- // o "Expression != Expression", -> new OpNode($2, $1, $3)
141
- // o "Expression IS Expression", -> new OpNode($2, $1, $3)
142
- // o "Expression ISNT Expression", -> new OpNode($2, $1, $3)
143
- //
144
- // o "Expression && Expression", -> new OpNode($2, $1, $3)
145
- // o "Expression || Expression", -> new OpNode($2, $1, $3)
146
- // o "Expression AND Expression", -> new OpNode($2, $1, $3)
147
- // o "Expression OR Expression", -> new OpNode($2, $1, $3)
148
- // o "Expression ? Expression", -> new OpNode($2, $1, $3)
149
- //
150
- // o "Expression -= Expression", -> new OpNode($2, $1, $3)
151
- // o "Expression += Expression", -> new OpNode($2, $1, $3)
152
- // o "Expression /= Expression", -> new OpNode($2, $1, $3)
153
- // o "Expression *= Expression", -> new OpNode($2, $1, $3)
154
- // o "Expression %= Expression", -> new OpNode($2, $1, $3)
155
- // o "Expression ||= Expression", -> new OpNode($2, $1, $3)
156
- // o "Expression &&= Expression", -> new OpNode($2, $1, $3)
157
- // o "Expression ?= Expression", -> new OpNode($2, $1, $3)
158
- //
159
- // o "Expression INSTANCEOF Expression", -> new OpNode($2, $1, $3)
160
- // o "Expression IN Expression", -> new OpNode($2, $1, $3)
161
- // ]
162
- // The existence operator.
163
- Existence: [o("Expression ?", function() {
164
- return new ExistenceNode($1);
165
- })
166
- ],
167
- // Function definition.
168
- Code: [o("PARAM_START ParamList PARAM_END FuncGlyph Block", function() {
169
- return new CodeNode($2, $5, $4);
170
- }), o("FuncGlyph Block", function() {
171
- return new CodeNode([], $2, $1);
172
- })
173
- ],
174
- // The symbols to signify functions, and bound functions.
175
- FuncGlyph: [o("->", function() {
176
- return 'func';
177
- }), o("=>", function() {
178
- return 'boundfunc';
179
- })
180
- ],
181
- // The parameters to a function definition.
182
- ParamList: [o("Param", function() {
183
- return [$1];
184
- }), o("ParamList , Param", function() {
185
- return $1.push($3);
186
- })
187
- ],
188
- // A Parameter (or ParamSplat) in a function definition.
189
- Param: [o("PARAM", function() {
190
- return yytext;
191
- }), o("PARAM . . .", function() {
192
- return new SplatNode(yytext);
193
- })
194
- ],
195
- // A regular splat.
196
- Splat: [o("Expression . . .", function() {
197
- return new SplatNode($1);
198
- })
199
- ],
200
- // Expressions that can be treated as values.
201
- Value: [o("IDENTIFIER", function() {
202
- return new ValueNode(yytext);
203
- }), o("Literal", function() {
204
- return new ValueNode($1);
205
- }), o("Array", function() {
206
- return new ValueNode($1);
207
- }), o("Object", function() {
208
- return new ValueNode($1);
209
- }), o("Parenthetical", function() {
210
- return new ValueNode($1);
211
- }), o("Range", function() {
212
- return new ValueNode($1);
213
- }),
214
- // o "Value Accessor", -> $1.push($2)
215
- o("Invocation Accessor", function() {
216
- return new ValueNode($1, [$2]);
217
- })
218
- ]
219
- // # Accessing into an object or array, through dot or index notation.
220
- // Accessor: [
221
- // o "PROPERTY_ACCESS IDENTIFIER", -> new AccessorNode($2)
222
- // o "PROTOTYPE_ACCESS IDENTIFIER", -> new AccessorNode($2, 'prototype')
223
- // o "SOAK_ACCESS IDENTIFIER", -> new AccessorNode($2, 'soak')
224
- // o "Index"
225
- // o "Slice", -> new SliceNode($1)
226
- // ]
227
- //
228
- // # Indexing into an object or array.
229
- // Index: [
230
- // o "INDEX_START Expression INDEX_END", -> new IndexNode($2)
231
- // ]
232
- //
233
- // # An object literal.
234
- // Object: [
235
- // o "{ AssignList }", -> new ObjectNode($2)
236
- // ]
237
- //
238
- // # Assignment within an object literal (comma or newline separated).
239
- // AssignList: [
240
- // o "", -> []
241
- // o "AssignObj", -> [$1]
242
- // o "AssignList , AssignObj", -> $1.push $3
243
- // o "AssignList TERMINATOR AssignObj", -> $1.push $3
244
- // o "AssignList , TERMINATOR AssignObj", -> $1.push $4
245
- // o "INDENT AssignList OUTDENT", -> $2
246
- // ]
247
- //
248
- // # All flavors of function call (instantiation, super, and regular).
249
- // Call: [
250
- // o "Invocation", -> $1
251
- // o "NEW Invocation", -> $2.new_instance()
252
- // o "Super", -> $1
253
- // ]
254
- //
255
- // # Extending an object's prototype.
256
- // Extends: [
257
- // o "Value EXTENDS Value", -> new ExtendsNode($1, $3)
258
- // ]
259
- //
260
- // # A generic function invocation.
261
- // Invocation: [
262
- // o "Value Arguments", -> new CallNode($1, $2)
263
- // o "Invocation Arguments", -> new CallNode($1, $2)
264
- // ]
265
- //
266
- // # The list of arguments to a function invocation.
267
- // Arguments: [
268
- // o "CALL_START ArgList CALL_END", -> $2
269
- // ]
270
- //
271
- // # Calling super.
272
- // Super: [
273
- // o "SUPER CALL_START ArgList CALL_END", -> new CallNode('super', $3)
274
- // ]
275
- //
276
- // # The range literal.
277
- // Range: [
278
- // o "[ Expression . . Expression ]", -> new RangeNode($2, $5)
279
- // o "[ Expression . . . Expression ]", -> new RangeNode($2, $6, true)
280
- // ]
281
- //
282
- // # The slice literal.
283
- // Slice: [
284
- // o "INDEX_START Expression . . Expression INDEX_END", -> new RangeNode($2, $5)
285
- // o "INDEX_START Expression . . . Expression INDEX_END", -> new RangeNode($2, $6, true)
286
- // ]
287
- //
288
- // # The array literal.
289
- // Array: [
290
- // o "[ ArgList ]", -> new ArrayNode($2)
291
- // ]
292
- //
293
- // # A list of arguments to a method call, or as the contents of an array.
294
- // ArgList: [
295
- // o "", -> []
296
- // o "Expression", -> val
297
- // o "INDENT Expression", -> [$2]
298
- // o "ArgList , Expression", -> $1.push $3
299
- // o "ArgList TERMINATOR Expression", -> $1.push $3
300
- // o "ArgList , TERMINATOR Expression", -> $1.push $4
301
- // o "ArgList , INDENT Expression", -> $1.push $4
302
- // o "ArgList OUTDENT", -> $1
303
- // ]
304
- //
305
- // # Just simple, comma-separated, required arguments (no fancy syntax).
306
- // SimpleArgs: [
307
- // o "Expression", -> $1
308
- // o "SimpleArgs , Expression", ->
309
- // ([$1].push($3)).reduce (a, b) -> a.concat(b)
310
- // ]
311
- //
312
- // # Try/catch/finally exception handling blocks.
313
- // Try: [
314
- // o "TRY Block Catch", -> new TryNode($2, $3[0], $3[1])
315
- // o "TRY Block FINALLY Block", -> new TryNode($2, nil, nil, $4)
316
- // o "TRY Block Catch FINALLY Block", -> new TryNode($2, $3[0], $3[1], $5)
317
- // ]
318
- //
319
- // # A catch clause.
320
- // Catch: [
321
- // o "CATCH IDENTIFIER Block", -> [$2, $3]
322
- // ]
323
- //
324
- // # Throw an exception.
325
- // Throw: [
326
- // o "THROW Expression", -> new ThrowNode($2)
327
- // ]
328
- //
329
- // # Parenthetical expressions.
330
- // Parenthetical: [
331
- // o "( Expression )", -> new ParentheticalNode($2)
332
- // ]
333
- //
334
- // # The while loop. (there is no do..while).
335
- // While: [
336
- // o "WHILE Expression Block", -> new WhileNode($2, $3)
337
- // o "WHILE Expression", -> new WhileNode($2, nil)
338
- // o "Expression WHILE Expression", -> new WhileNode($3, Expressions.wrap($1))
339
- // ]
340
- //
341
- // # Array comprehensions, including guard and current index.
342
- // # Looks a little confusing, check nodes.rb for the arguments to ForNode.
343
- // For: [
344
- // o "Expression FOR ForVariables ForSource", -> new ForNode($1, $4, $3[0], $3[1])
345
- // o "FOR ForVariables ForSource Block", -> new ForNode($4, $3, $2[0], $2[1])
346
- // ]
347
- //
348
- // # An array comprehension has variables for the current element and index.
349
- // ForVariables: [
350
- // o "IDENTIFIER", -> [$1]
351
- // o "IDENTIFIER , IDENTIFIER", -> [$1, $3]
352
- // ]
353
- //
354
- // # The source of the array comprehension can optionally be filtered.
355
- // ForSource: [
356
- // o "IN Expression", -> {source: $2}
357
- // o "OF Expression", -> {source: $2, object: true}
358
- // o "ForSource WHEN Expression", -> $1.filter: $3; $1
359
- // o "ForSource BY Expression", -> $1.step: $3; $1
360
- // ]
361
- //
362
- // # Switch/When blocks.
363
- // Switch: [
364
- // o "SWITCH Expression INDENT Whens OUTDENT", -> $4.rewrite_condition($2)
365
- // o "SWITCH Expression INDENT Whens ELSE Block OUTDENT", -> $4.rewrite_condition($2).add_else($6)
366
- // ]
367
- //
368
- // # The inner list of whens.
369
- // Whens: [
370
- // o "When", -> $1
371
- // o "Whens When", -> $1.push $2
372
- // ]
373
- //
374
- // # An individual when.
375
- // When: [
376
- // o "LEADING_WHEN SimpleArgs Block", -> new IfNode($2, $3, nil, {statement: true})
377
- // o "LEADING_WHEN SimpleArgs Block TERMINATOR", -> new IfNode($2, $3, nil, {statement: true})
378
- // o "Comment TERMINATOR When", -> $3.add_comment($1)
379
- // ]
380
- //
381
- // # The most basic form of "if".
382
- // IfBlock: [
383
- // o "IF Expression Block", -> new IfNode($2, $3)
384
- // ]
385
- //
386
- // # An elsif portion of an if-else block.
387
- // ElsIf: [
388
- // o "ELSE IfBlock", -> $2.force_statement()
389
- // ]
390
- //
391
- // # Multiple elsifs can be chained together.
392
- // ElsIfs: [
393
- // o "ElsIf", -> $1
394
- // o "ElsIfs ElsIf", -> $1.add_else($2)
395
- // ]
396
- //
397
- // # Terminating else bodies are strictly optional.
398
- // ElseBody: [
399
- // o "", -> null
400
- // o "ELSE Block", -> $2
401
- // ]
402
- //
403
- // # All the alternatives for ending an if-else block.
404
- // IfEnd: [
405
- // o "ElseBody", -> $1
406
- // o "ElsIfs ElseBody", -> $1.add_else($2)
407
- // ]
408
- //
409
- // # The full complement of if blocks, including postfix one-liner ifs and unlesses.
410
- // If: [
411
- // o "IfBlock IfEnd", -> $1.add_else($2)
412
- // o "Expression IF Expression", -> new IfNode($3, Expressions.wrap($1), nil, {statement: true})
413
- // o "Expression UNLESS Expression", -> new IfNode($3, Expressions.wrap($1), nil, {statement: true, invert: true})
414
- // ]
415
- };
416
- // Helpers ==============================================================
417
- // Make the Jison parser.
418
- bnf = {
419
- };
420
- tokens = [];
421
- __a = grammar;
422
- for (name in __a) {
423
- non_terminal = __a[name];
424
- if (__hasProp.call(__a, name)) {
425
- bnf[name] = (function() {
426
- __b = []; __c = non_terminal;
427
- for (__d = 0; __d < __c.length; __d++) {
428
- option = __c[__d];
429
- __b.push((function() {
430
- __e = option[0].split(" ");
431
- for (__f = 0; __f < __e.length; __f++) {
432
- part = __e[__f];
433
- !grammar[part] ? tokens.push(part) : null;
434
- }
435
- name === "Root" ? (option[1] = "return " + option[1]) : null;
436
- return option;
437
- }).call(this));
438
- }
439
- return __b;
440
- }).call(this);
441
- }
442
- }
443
- tokens = tokens.join(" ");
444
- parser = new Parser({
445
- tokens: tokens,
446
- bnf: bnf,
447
- operators: operators,
448
- startSymbol: 'Root'
449
- }, {
450
- debug: false
451
- });
452
- // Thin wrapper around the real lexer
453
- parser.lexer = {
454
- lex: function lex() {
455
- var token;
456
- token = this.tokens[this.pos] || [""];
457
- this.pos += 1;
458
- this.yylineno = token[2];
459
- this.yytext = token[1];
460
- return token[0];
461
- },
462
- setInput: function setInput(tokens) {
463
- this.tokens = tokens;
464
- return this.pos = 0;
465
- },
466
- upcomingInput: function upcomingInput() {
467
- return "";
468
- },
469
- showPosition: function showPosition() {
470
- return this.pos;
471
- }
472
- };
473
- exports.Parser = function Parser() { };
474
- exports.Parser.prototype.parse = function parse(tokens) {
475
- return parser.parse(tokens);
476
- };
477
- })();