2010-02-10 23:24:05 -05:00
|
|
|
(function(){
|
2010-02-25 23:39:14 -05:00
|
|
|
var Parser, _a, _b, _c, _d, _e, _f, _g, _h, bnf, grammar, name, non_terminal, o, operators, option, part, tokens, unwrap;
|
2010-02-10 23:24:05 -05:00
|
|
|
var __hasProp = Object.prototype.hasOwnProperty;
|
|
|
|
Parser = require('jison').Parser;
|
|
|
|
// DSL ===================================================================
|
|
|
|
// Detect functions: [
|
|
|
|
unwrap = /function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
|
|
|
|
// Quickie DSL for Jison access.
|
|
|
|
o = function o(pattern_string, func, options) {
|
|
|
|
var match;
|
|
|
|
if (func) {
|
|
|
|
func = (match = (func + "").match(unwrap)) ? match[1] : '(' + func + '())';
|
|
|
|
return [pattern_string, '$$ = ' + func + ';', options];
|
|
|
|
} else {
|
|
|
|
return [pattern_string, '$$ = $1;', options];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Precedence ===========================================================
|
2010-02-27 18:57:45 -05:00
|
|
|
operators = [["left", '?'], ["nonassoc", 'UMINUS', 'UPLUS', 'NOT', '!', '!!', '~', '++', '--'], ["left", '*', '/', '%'], ["left", '+', '-'], ["left", '<<', '>>', '>>>'], ["left", '&', '|', '^'], ["left", '<=', '<', '>', '>='], ["right", 'DELETE', 'INSTANCEOF', 'TYPEOF'], ["right", '==', '!=', 'IS', 'ISNT'], ["left", '&&', '||', 'AND', 'OR'], ["right", '-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?='], ["left", '.'], ["right", 'INDENT'], ["left", 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY', 'THROW'], ["right", 'FOR', 'NEW', 'SUPER', 'CLASS'], ["left", 'EXTENDS'], ["right", 'ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'IF', 'ELSE', 'WHILE']];
|
2010-02-10 23:24:05 -05:00
|
|
|
// Grammar ==============================================================
|
|
|
|
grammar = {
|
|
|
|
// All parsing will end in this rule, being the trunk of the AST.
|
|
|
|
Root: [o("", function() {
|
|
|
|
return new Expressions();
|
|
|
|
}), o("TERMINATOR", function() {
|
|
|
|
return new Expressions();
|
2010-02-12 19:45:20 -05:00
|
|
|
}), o("Expressions", function() {
|
|
|
|
return $1;
|
|
|
|
}), o("Block TERMINATOR", function() {
|
|
|
|
return $1;
|
|
|
|
})
|
2010-02-10 23:24:05 -05:00
|
|
|
],
|
|
|
|
// Any list of expressions or method body, seperated by line breaks or semis.
|
|
|
|
Expressions: [o("Expression", function() {
|
|
|
|
return Expressions.wrap([$1]);
|
|
|
|
}), o("Expressions TERMINATOR Expression", function() {
|
|
|
|
return $1.push($3);
|
2010-02-12 19:45:20 -05:00
|
|
|
}), o("Expressions TERMINATOR", function() {
|
|
|
|
return $1;
|
|
|
|
})
|
2010-02-10 23:24:05 -05:00
|
|
|
],
|
|
|
|
// All types of expressions in our language. The basic unit of CoffeeScript
|
|
|
|
// is the expression.
|
2010-02-27 18:57:45 -05:00
|
|
|
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("Class"), o("Splat"), o("Existence"), o("Comment")],
|
2010-02-10 23:24:05 -05:00
|
|
|
// A block of expressions. Note that the Rewriter will convert some postfix
|
|
|
|
// forms into blocks for us, by altering the token stream.
|
|
|
|
Block: [o("INDENT Expressions OUTDENT", function() {
|
|
|
|
return $2;
|
|
|
|
}), o("INDENT OUTDENT", function() {
|
|
|
|
return new Expressions();
|
|
|
|
})
|
|
|
|
],
|
|
|
|
Identifier: [o("IDENTIFIER", function() {
|
|
|
|
return new LiteralNode(yytext);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
AlphaNumeric: [o("NUMBER", function() {
|
|
|
|
return new LiteralNode(yytext);
|
|
|
|
}), o("STRING", function() {
|
|
|
|
return new LiteralNode(yytext);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// All hard-coded values. These can be printed straight to JavaScript.
|
|
|
|
Literal: [o("AlphaNumeric", function() {
|
|
|
|
return $1;
|
|
|
|
}), o("JS", function() {
|
|
|
|
return new LiteralNode(yytext);
|
|
|
|
}), o("REGEX", function() {
|
|
|
|
return new LiteralNode(yytext);
|
|
|
|
}), o("BREAK", function() {
|
|
|
|
return new LiteralNode(yytext);
|
|
|
|
}), o("CONTINUE", function() {
|
|
|
|
return new LiteralNode(yytext);
|
|
|
|
}), o("TRUE", function() {
|
|
|
|
return new LiteralNode(true);
|
|
|
|
}), o("FALSE", function() {
|
|
|
|
return new LiteralNode(false);
|
|
|
|
}), o("YES", function() {
|
|
|
|
return new LiteralNode(true);
|
|
|
|
}), o("NO", function() {
|
|
|
|
return new LiteralNode(false);
|
|
|
|
}), o("ON", function() {
|
|
|
|
return new LiteralNode(true);
|
|
|
|
}), o("OFF", function() {
|
|
|
|
return new LiteralNode(false);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Assignment to a variable (or index).
|
|
|
|
Assign: [o("Value ASSIGN Expression", function() {
|
|
|
|
return new AssignNode($1, $3);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Assignment within an object literal (can be quoted).
|
|
|
|
AssignObj: [o("Identifier ASSIGN Expression", function() {
|
|
|
|
return new AssignNode(new ValueNode($1), $3, 'object');
|
|
|
|
}), o("AlphaNumeric ASSIGN Expression", function() {
|
|
|
|
return new AssignNode(new ValueNode($1), $3, 'object');
|
|
|
|
}), o("Comment")
|
|
|
|
],
|
|
|
|
// A return statement.
|
|
|
|
Return: [o("RETURN Expression", function() {
|
|
|
|
return new ReturnNode($2);
|
|
|
|
}), o("RETURN", function() {
|
|
|
|
return new ReturnNode(new ValueNode(new LiteralNode('null')));
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// A comment.
|
|
|
|
Comment: [o("COMMENT", function() {
|
|
|
|
return new CommentNode(yytext);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Arithmetic and logical operators
|
|
|
|
// For Ruby's Operator precedence, see: [
|
|
|
|
// https://www.cs.auckland.ac.nz/references/ruby/ProgrammingRuby/language.html
|
|
|
|
Operation: [o("! Expression", function() {
|
|
|
|
return new OpNode('!', $2);
|
|
|
|
}), o("!! Expression", function() {
|
|
|
|
return new OpNode('!!', $2);
|
|
|
|
}), o("- Expression", (function() {
|
|
|
|
return new OpNode('-', $2);
|
2010-02-15 18:03:00 -05:00
|
|
|
}), {
|
2010-02-10 23:24:05 -05:00
|
|
|
prec: 'UMINUS'
|
2010-02-15 18:03:00 -05:00
|
|
|
}), o("+ Expression", (function() {
|
2010-02-10 23:24:05 -05:00
|
|
|
return new OpNode('+', $2);
|
2010-02-15 18:03:00 -05:00
|
|
|
}), {
|
2010-02-10 23:24:05 -05:00
|
|
|
prec: 'UPLUS'
|
2010-02-15 18:03:00 -05:00
|
|
|
}), o("NOT Expression", function() {
|
2010-02-10 23:24:05 -05:00
|
|
|
return new OpNode('not', $2);
|
|
|
|
}), o("~ Expression", function() {
|
|
|
|
return new OpNode('~', $2);
|
|
|
|
}), o("-- Expression", function() {
|
|
|
|
return new OpNode('--', $2);
|
|
|
|
}), o("++ Expression", function() {
|
|
|
|
return new OpNode('++', $2);
|
|
|
|
}), o("DELETE Expression", function() {
|
|
|
|
return new OpNode('delete', $2);
|
|
|
|
}), o("TYPEOF Expression", function() {
|
|
|
|
return new OpNode('typeof', $2);
|
|
|
|
}), o("Expression --", function() {
|
|
|
|
return new OpNode('--', $1, null, true);
|
|
|
|
}), o("Expression ++", function() {
|
|
|
|
return new OpNode('++', $1, null, true);
|
|
|
|
}), o("Expression * Expression", function() {
|
|
|
|
return new OpNode('*', $1, $3);
|
|
|
|
}), o("Expression / Expression", function() {
|
|
|
|
return new OpNode('/', $1, $3);
|
|
|
|
}), o("Expression % Expression", function() {
|
|
|
|
return new OpNode('%', $1, $3);
|
|
|
|
}), o("Expression + Expression", function() {
|
|
|
|
return new OpNode('+', $1, $3);
|
|
|
|
}), o("Expression - Expression", function() {
|
|
|
|
return new OpNode('-', $1, $3);
|
|
|
|
}), o("Expression << Expression", function() {
|
|
|
|
return new OpNode('<<', $1, $3);
|
|
|
|
}), o("Expression >> Expression", function() {
|
|
|
|
return new OpNode('>>', $1, $3);
|
|
|
|
}), o("Expression >>> Expression", function() {
|
|
|
|
return new OpNode('>>>', $1, $3);
|
|
|
|
}), o("Expression & Expression", function() {
|
|
|
|
return new OpNode('&', $1, $3);
|
|
|
|
}), o("Expression | Expression", function() {
|
|
|
|
return new OpNode('|', $1, $3);
|
|
|
|
}), o("Expression ^ Expression", function() {
|
|
|
|
return new OpNode('^', $1, $3);
|
|
|
|
}), o("Expression <= Expression", function() {
|
|
|
|
return new OpNode('<=', $1, $3);
|
|
|
|
}), o("Expression < Expression", function() {
|
|
|
|
return new OpNode('<', $1, $3);
|
|
|
|
}), o("Expression > Expression", function() {
|
|
|
|
return new OpNode('>', $1, $3);
|
|
|
|
}), o("Expression >= Expression", function() {
|
|
|
|
return new OpNode('>=', $1, $3);
|
|
|
|
}), o("Expression == Expression", function() {
|
|
|
|
return new OpNode('==', $1, $3);
|
|
|
|
}), o("Expression != Expression", function() {
|
|
|
|
return new OpNode('!=', $1, $3);
|
|
|
|
}), o("Expression IS Expression", function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
return new OpNode('is', $1, $3);
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Expression ISNT Expression", function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
return new OpNode('isnt', $1, $3);
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Expression && Expression", function() {
|
|
|
|
return new OpNode('&&', $1, $3);
|
|
|
|
}), o("Expression || Expression", function() {
|
|
|
|
return new OpNode('||', $1, $3);
|
|
|
|
}), o("Expression AND Expression", function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
return new OpNode('and', $1, $3);
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Expression OR Expression", function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
return new OpNode('or', $1, $3);
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Expression ? Expression", function() {
|
|
|
|
return new OpNode('?', $1, $3);
|
|
|
|
}), o("Expression -= Expression", function() {
|
|
|
|
return new OpNode('-=', $1, $3);
|
|
|
|
}), o("Expression += Expression", function() {
|
|
|
|
return new OpNode('+=', $1, $3);
|
|
|
|
}), o("Expression /= Expression", function() {
|
|
|
|
return new OpNode('/=', $1, $3);
|
|
|
|
}), o("Expression *= Expression", function() {
|
|
|
|
return new OpNode('*=', $1, $3);
|
|
|
|
}), o("Expression %= Expression", function() {
|
|
|
|
return new OpNode('%=', $1, $3);
|
|
|
|
}), o("Expression ||= Expression", function() {
|
|
|
|
return new OpNode('||=', $1, $3);
|
|
|
|
}), o("Expression &&= Expression", function() {
|
|
|
|
return new OpNode('&&=', $1, $3);
|
|
|
|
}), o("Expression ?= Expression", function() {
|
|
|
|
return new OpNode('?=', $1, $3);
|
|
|
|
}), o("Expression INSTANCEOF Expression", function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
return new OpNode('instanceof', $1, $3);
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Expression IN Expression", function() {
|
2010-02-11 01:57:33 -05:00
|
|
|
return new OpNode('in', $1, $3);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// The existence operator.
|
|
|
|
Existence: [o("Expression ?", function() {
|
|
|
|
return new ExistenceNode($1);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Function definition.
|
|
|
|
Code: [o("PARAM_START ParamList PARAM_END FuncGlyph Block", function() {
|
|
|
|
return new CodeNode($2, $5, $4);
|
|
|
|
}), o("FuncGlyph Block", function() {
|
|
|
|
return new CodeNode([], $2, $1);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// The symbols to signify functions, and bound functions.
|
|
|
|
FuncGlyph: [o("->", function() {
|
|
|
|
return 'func';
|
|
|
|
}), o("=>", function() {
|
|
|
|
return 'boundfunc';
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// The parameters to a function definition.
|
2010-02-15 18:03:00 -05:00
|
|
|
ParamList: [o("", function() {
|
|
|
|
return [];
|
|
|
|
}), o("Param", function() {
|
2010-02-10 23:24:05 -05:00
|
|
|
return [$1];
|
|
|
|
}), o("ParamList , Param", function() {
|
|
|
|
return $1.concat([$3]);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// A Parameter (or ParamSplat) in a function definition.
|
|
|
|
Param: [o("PARAM", function() {
|
2010-02-11 23:57:31 -05:00
|
|
|
return new LiteralNode(yytext);
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Param . . .", function() {
|
|
|
|
return new SplatNode($1);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// A regular splat.
|
|
|
|
Splat: [o("Expression . . .", function() {
|
|
|
|
return new SplatNode($1);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Expressions that can be treated as values.
|
|
|
|
Value: [o("Identifier", function() {
|
|
|
|
return new ValueNode($1);
|
|
|
|
}), o("Literal", function() {
|
|
|
|
return new ValueNode($1);
|
|
|
|
}), o("Array", function() {
|
|
|
|
return new ValueNode($1);
|
|
|
|
}), o("Object", function() {
|
|
|
|
return new ValueNode($1);
|
|
|
|
}), o("Parenthetical", function() {
|
|
|
|
return new ValueNode($1);
|
|
|
|
}), o("Range", function() {
|
|
|
|
return new ValueNode($1);
|
|
|
|
}), o("This", function() {
|
2010-02-19 07:51:52 -05:00
|
|
|
return $1;
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Value Accessor", function() {
|
|
|
|
return $1.push($2);
|
|
|
|
}), o("Invocation Accessor", function() {
|
|
|
|
return new ValueNode($1, [$2]);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Accessing into an object or array, through dot or index notation.
|
|
|
|
Accessor: [o("PROPERTY_ACCESS Identifier", function() {
|
|
|
|
return new AccessorNode($2);
|
|
|
|
}), o("PROTOTYPE_ACCESS Identifier", function() {
|
|
|
|
return new AccessorNode($2, 'prototype');
|
|
|
|
}), o("SOAK_ACCESS Identifier", function() {
|
|
|
|
return new AccessorNode($2, 'soak');
|
|
|
|
}), o("Index"), o("Slice", function() {
|
|
|
|
return new SliceNode($1);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Indexing into an object or array.
|
|
|
|
Index: [o("INDEX_START Expression INDEX_END", function() {
|
|
|
|
return new IndexNode($2);
|
2010-02-24 00:06:01 -05:00
|
|
|
}), o("SOAKED_INDEX_START Expression SOAKED_INDEX_END", function() {
|
|
|
|
return new IndexNode($2, 'soak');
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// An object literal.
|
|
|
|
Object: [o("{ AssignList }", function() {
|
|
|
|
return new ObjectNode($2);
|
2010-02-27 18:57:45 -05:00
|
|
|
}), o("{ IndentedAssignList }", function() {
|
|
|
|
return new ObjectNode($2);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// A class literal.
|
|
|
|
Class: [o("CLASS Value", function() {
|
|
|
|
return new ClassNode($2);
|
|
|
|
}), o("CLASS Value EXTENDS Value", function() {
|
|
|
|
return new ClassNode($2, $4);
|
|
|
|
}), o("CLASS Value IndentedAssignList", function() {
|
|
|
|
return new ClassNode($2, null, $3);
|
|
|
|
}), o("CLASS Value EXTENDS Value IndentedAssignList", function() {
|
|
|
|
return new ClassNode($2, $4, $5);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// Assignment within an object literal (comma or newline separated).
|
|
|
|
AssignList: [o("", function() {
|
|
|
|
return [];
|
|
|
|
}), o("AssignObj", function() {
|
|
|
|
return [$1];
|
|
|
|
}), o("AssignList , AssignObj", function() {
|
|
|
|
return $1.concat([$3]);
|
|
|
|
}), o("AssignList TERMINATOR AssignObj", function() {
|
|
|
|
return $1.concat([$3]);
|
|
|
|
}), o("AssignList , TERMINATOR AssignObj", function() {
|
|
|
|
return $1.concat([$4]);
|
2010-02-27 18:57:45 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// A list of assignments in a block indentation.
|
|
|
|
IndentedAssignList: [o("INDENT AssignList OUTDENT", function() {
|
2010-02-10 23:24:05 -05:00
|
|
|
return $2;
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// All flavors of function call (instantiation, super, and regular).
|
|
|
|
Call: [o("Invocation", function() {
|
|
|
|
return $1;
|
|
|
|
}), o("NEW Invocation", function() {
|
|
|
|
return $2.new_instance();
|
|
|
|
}), o("Super", function() {
|
|
|
|
return $1;
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Extending an object's prototype.
|
|
|
|
Extends: [o("Value EXTENDS Value", function() {
|
|
|
|
return new ExtendsNode($1, $3);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// A generic function invocation.
|
|
|
|
Invocation: [o("Value Arguments", function() {
|
|
|
|
return new CallNode($1, $2);
|
|
|
|
}), o("Invocation Arguments", function() {
|
|
|
|
return new CallNode($1, $2);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// The list of arguments to a function invocation.
|
|
|
|
Arguments: [o("CALL_START ArgList CALL_END", function() {
|
|
|
|
return $2;
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Calling super.
|
|
|
|
Super: [o("SUPER CALL_START ArgList CALL_END", function() {
|
|
|
|
return new CallNode('super', $3);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// This references, either naked or to a property.
|
|
|
|
This: [o("@", function() {
|
2010-02-19 07:51:52 -05:00
|
|
|
return new ValueNode(new LiteralNode('this'));
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("@ Identifier", function() {
|
2010-02-19 07:51:52 -05:00
|
|
|
return new ValueNode(new LiteralNode('this'), [new AccessorNode($2)]);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// The range literal.
|
|
|
|
Range: [o("[ Expression . . Expression ]", function() {
|
|
|
|
return new RangeNode($2, $5);
|
|
|
|
}), o("[ Expression . . . Expression ]", function() {
|
|
|
|
return new RangeNode($2, $6, true);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// The slice literal.
|
|
|
|
Slice: [o("INDEX_START Expression . . Expression INDEX_END", function() {
|
|
|
|
return new RangeNode($2, $5);
|
|
|
|
}), o("INDEX_START Expression . . . Expression INDEX_END", function() {
|
|
|
|
return new RangeNode($2, $6, true);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// The array literal.
|
|
|
|
Array: [o("[ ArgList ]", function() {
|
|
|
|
return new ArrayNode($2);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// A list of arguments to a method call, or as the contents of an array.
|
|
|
|
ArgList: [o("", function() {
|
|
|
|
return [];
|
|
|
|
}), o("Expression", function() {
|
|
|
|
return [$1];
|
|
|
|
}), o("INDENT Expression", function() {
|
|
|
|
return [$2];
|
|
|
|
}), o("ArgList , Expression", function() {
|
|
|
|
return $1.concat([$3]);
|
|
|
|
}), o("ArgList TERMINATOR Expression", function() {
|
|
|
|
return $1.concat([$3]);
|
|
|
|
}), o("ArgList , TERMINATOR Expression", function() {
|
|
|
|
return $1.concat([$4]);
|
|
|
|
}), o("ArgList , INDENT Expression", function() {
|
|
|
|
return $1.concat([$4]);
|
|
|
|
}), o("ArgList OUTDENT", function() {
|
|
|
|
return $1;
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Just simple, comma-separated, required arguments (no fancy syntax).
|
|
|
|
SimpleArgs: [o("Expression", function() {
|
|
|
|
return $1;
|
|
|
|
}), o("SimpleArgs , Expression", function() {
|
2010-02-12 16:23:52 -05:00
|
|
|
return $1 instanceof Array ? $1.concat([$3]) : [$1].concat([$3]);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// Try/catch/finally exception handling blocks.
|
|
|
|
Try: [o("TRY Block Catch", function() {
|
|
|
|
return new TryNode($2, $3[0], $3[1]);
|
|
|
|
}), o("TRY Block FINALLY Block", function() {
|
|
|
|
return new TryNode($2, null, null, $4);
|
|
|
|
}), o("TRY Block Catch FINALLY Block", function() {
|
|
|
|
return new TryNode($2, $3[0], $3[1], $5);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// A catch clause.
|
|
|
|
Catch: [o("CATCH Identifier Block", function() {
|
|
|
|
return [$2, $3];
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Throw an exception.
|
|
|
|
Throw: [o("THROW Expression", function() {
|
|
|
|
return new ThrowNode($2);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Parenthetical expressions.
|
|
|
|
Parenthetical: [o("( Expression )", function() {
|
|
|
|
return new ParentheticalNode($2);
|
|
|
|
})
|
|
|
|
],
|
2010-02-23 22:53:43 -05:00
|
|
|
// The condition for a while loop.
|
|
|
|
WhileSource: [o("WHILE Expression", function() {
|
|
|
|
return new WhileNode($2);
|
|
|
|
}), o("WHILE Expression WHEN Expression", function() {
|
|
|
|
return new WhileNode($2, {
|
|
|
|
filter: $4
|
|
|
|
});
|
|
|
|
})
|
|
|
|
],
|
2010-02-10 23:24:05 -05:00
|
|
|
// The while loop. (there is no do..while).
|
2010-02-23 22:53:43 -05:00
|
|
|
While: [o("WhileSource Block", function() {
|
|
|
|
return $1.add_body($2);
|
|
|
|
}), o("Expression WhileSource", function() {
|
|
|
|
return $2.add_body($1);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// Array comprehensions, including guard and current index.
|
|
|
|
// Looks a little confusing, check nodes.rb for the arguments to ForNode.
|
|
|
|
For: [o("Expression FOR ForVariables ForSource", function() {
|
|
|
|
return new ForNode($1, $4, $3[0], $3[1]);
|
|
|
|
}), o("FOR ForVariables ForSource Block", function() {
|
|
|
|
return new ForNode($4, $3, $2[0], $2[1]);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// An array comprehension has variables for the current element and index.
|
|
|
|
ForVariables: [o("Identifier", function() {
|
|
|
|
return [$1];
|
|
|
|
}), o("Identifier , Identifier", function() {
|
|
|
|
return [$1, $3];
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// The source of the array comprehension can optionally be filtered.
|
|
|
|
ForSource: [o("IN Expression", function() {
|
|
|
|
return {
|
|
|
|
source: $2
|
|
|
|
};
|
|
|
|
}), o("OF Expression", function() {
|
|
|
|
return {
|
|
|
|
source: $2,
|
|
|
|
object: true
|
|
|
|
};
|
|
|
|
}), o("ForSource WHEN Expression", function() {
|
|
|
|
$1.filter = $3;
|
|
|
|
return $1;
|
|
|
|
}), o("ForSource BY Expression", function() {
|
|
|
|
$1.step = $3;
|
|
|
|
return $1;
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// Switch/When blocks.
|
|
|
|
Switch: [o("SWITCH Expression INDENT Whens OUTDENT", function() {
|
|
|
|
return $4.rewrite_condition($2);
|
|
|
|
}), o("SWITCH Expression INDENT Whens ELSE Block OUTDENT", function() {
|
2010-02-22 19:22:09 -05:00
|
|
|
return $4.rewrite_condition($2).add_else($6, true);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// The inner list of whens.
|
|
|
|
Whens: [o("When", function() {
|
|
|
|
return $1;
|
|
|
|
}), o("Whens When", function() {
|
|
|
|
return $1.push($2);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// An individual when.
|
|
|
|
When: [o("LEADING_WHEN SimpleArgs Block", function() {
|
|
|
|
return new IfNode($2, $3, null, {
|
|
|
|
statement: true
|
|
|
|
});
|
|
|
|
}), o("LEADING_WHEN SimpleArgs Block TERMINATOR", function() {
|
|
|
|
return new IfNode($2, $3, null, {
|
|
|
|
statement: true
|
|
|
|
});
|
|
|
|
}), o("Comment TERMINATOR When", function() {
|
2010-02-12 16:26:28 -05:00
|
|
|
$3.comment = $1;
|
|
|
|
return $3;
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// The most basic form of "if".
|
2010-02-13 01:02:10 -05:00
|
|
|
IfStart: [o("IF Expression Block", function() {
|
2010-02-10 23:24:05 -05:00
|
|
|
return new IfNode($2, $3);
|
2010-02-13 01:02:10 -05:00
|
|
|
}), o("IfStart ElsIfs", function() {
|
|
|
|
return $1.add_else($2);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
2010-02-13 01:02:10 -05:00
|
|
|
IfBlock: [o("IfStart", function() {
|
|
|
|
return $1;
|
|
|
|
}), o("IfStart ELSE Block", function() {
|
|
|
|
return $1.add_else($3);
|
2010-02-10 23:24:05 -05:00
|
|
|
})
|
|
|
|
],
|
|
|
|
// Multiple elsifs can be chained together.
|
2010-02-13 01:02:10 -05:00
|
|
|
ElsIfs: [o("ELSE IF Expression Block", function() {
|
|
|
|
return (new IfNode($3, $4)).force_statement();
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("ElsIfs ElsIf", function() {
|
|
|
|
return $1.add_else($2);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
// The full complement of if blocks, including postfix one-liner ifs and unlesses.
|
2010-02-13 00:11:14 -05:00
|
|
|
If: [o("IfBlock", function() {
|
2010-02-12 22:16:26 -05:00
|
|
|
return $1;
|
2010-02-10 23:24:05 -05:00
|
|
|
}), o("Expression IF Expression", function() {
|
|
|
|
return new IfNode($3, Expressions.wrap([$1]), null, {
|
|
|
|
statement: true
|
|
|
|
});
|
|
|
|
}), o("Expression UNLESS Expression", function() {
|
|
|
|
return new IfNode($3, Expressions.wrap([$1]), null, {
|
|
|
|
statement: true,
|
|
|
|
invert: true
|
|
|
|
});
|
|
|
|
})
|
|
|
|
]
|
|
|
|
};
|
|
|
|
// Helpers ==============================================================
|
|
|
|
// Make the Jison parser.
|
2010-02-13 01:13:08 -05:00
|
|
|
bnf = {};
|
2010-02-10 23:24:05 -05:00
|
|
|
tokens = [];
|
2010-02-16 18:00:40 -05:00
|
|
|
_a = grammar;
|
2010-02-18 20:22:53 -05:00
|
|
|
for (name in _a) { if (__hasProp.call(_a, name)) {
|
2010-02-16 18:00:40 -05:00
|
|
|
non_terminal = _a[name];
|
2010-02-14 17:35:14 -05:00
|
|
|
bnf[name] = (function() {
|
2010-02-16 18:00:40 -05:00
|
|
|
_b = []; _c = non_terminal;
|
2010-02-25 23:39:14 -05:00
|
|
|
for (_d = 0, _e = _c.length; _d < _e; _d++) {
|
2010-02-16 18:00:40 -05:00
|
|
|
option = _c[_d];
|
|
|
|
_b.push((function() {
|
2010-02-25 23:39:14 -05:00
|
|
|
_f = option[0].split(" ");
|
|
|
|
for (_g = 0, _h = _f.length; _g < _h; _g++) {
|
|
|
|
part = _f[_g];
|
2010-02-14 17:35:14 -05:00
|
|
|
!grammar[part] ? tokens.push(part) : null;
|
|
|
|
}
|
|
|
|
name === "Root" ? (option[1] = "return " + option[1]) : null;
|
|
|
|
return option;
|
|
|
|
}).call(this));
|
|
|
|
}
|
2010-02-16 18:00:40 -05:00
|
|
|
return _b;
|
2010-02-14 17:35:14 -05:00
|
|
|
}).call(this);
|
2010-02-18 20:22:53 -05:00
|
|
|
}}
|
2010-02-10 23:24:05 -05:00
|
|
|
tokens = tokens.join(" ");
|
2010-02-16 01:04:48 -05:00
|
|
|
exports.parser = new Parser({
|
2010-02-10 23:24:05 -05:00
|
|
|
tokens: tokens,
|
|
|
|
bnf: bnf,
|
2010-02-11 23:29:12 -05:00
|
|
|
operators: operators.reverse(),
|
2010-02-10 23:24:05 -05:00
|
|
|
startSymbol: 'Root'
|
|
|
|
}, {
|
|
|
|
debug: false
|
|
|
|
});
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|