1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/coffee_script/grammar.y

410 lines
15 KiB
Text
Raw Normal View History

2009-12-13 17:07:16 -05:00
class Parser
# Declare tokens produced by the lexer
token IF ELSE UNLESS
token NUMBER STRING REGEX
token TRUE FALSE YES NO ON OFF
2009-12-13 17:07:16 -05:00
token IDENTIFIER PROPERTY_ACCESS
token CODE PARAM NEW RETURN
2009-12-14 10:00:31 -05:00
token TRY CATCH FINALLY THROW
2009-12-14 23:11:28 -05:00
token BREAK CONTINUE
token FOR IN WHILE
token SWITCH WHEN
2009-12-24 14:50:44 -05:00
token DELETE INSTANCEOF TYPEOF
token SUPER EXTENDS
token NEWLINE
token COMMENT
2009-12-15 09:11:27 -05:00
token JS
token INDENT OUTDENT
2009-12-13 17:07:16 -05:00
2009-12-16 20:19:52 -05:00
# Declare order of operations.
2009-12-13 17:07:16 -05:00
prechigh
nonassoc UMINUS NOT '!' '!!' '~' '++' '--'
2009-12-13 17:07:16 -05:00
left '*' '/' '%'
left '+' '-'
left '<<' '>>' '>>>'
left '&' '|' '^'
2009-12-13 17:07:16 -05:00
left '<=' '<' '>' '>='
right '==' '!=' IS ISNT
2009-12-13 17:07:16 -05:00
left '&&' '||' AND OR
right '-=' '+=' '/=' '*=' '%='
2009-12-24 14:50:44 -05:00
right DELETE INSTANCEOF TYPEOF
left '.'
2009-12-28 23:08:02 -05:00
right INDENT
left OUTDENT
right THROW FOR IN WHILE WHEN NEW SUPER ELSE
2009-12-30 00:22:27 -05:00
left UNLESS EXTENDS IF
left ASSIGN '||=' '&&='
right RETURN '=>'
2009-12-13 17:07:16 -05:00
preclow
rule
# All parsing will end in this rule, being the trunk of the AST.
Root:
2009-12-17 22:58:40 -05:00
/* nothing */ { result = Expressions.new([]) }
| Terminator { result = Expressions.new([]) }
| Expressions { result = val[0] }
2009-12-13 17:07:16 -05:00
;
# Any list of expressions or method body, seperated by line breaks or semis.
2009-12-13 17:07:16 -05:00
Expressions:
2009-12-17 22:58:40 -05:00
Expression { result = Expressions.new(val) }
| Expressions Terminator Expression { result = val[0] << val[2] }
2009-12-13 23:25:31 -05:00
| Expressions Terminator { result = val[0] }
2009-12-13 17:07:16 -05:00
;
# All types of expressions in our language.
2009-12-13 17:07:16 -05:00
Expression:
Value
2009-12-13 17:07:16 -05:00
| Call
| Code
| Operation
2009-12-26 03:16:40 -05:00
| Range
| Assign
2009-12-13 17:07:16 -05:00
| If
2009-12-14 10:00:31 -05:00
| Try
| Throw
2009-12-13 18:37:29 -05:00
| Return
| While
| For
2009-12-15 22:30:27 -05:00
| Switch
| Extends
| Comment
2009-12-13 17:07:16 -05:00
;
Block:
2009-12-28 23:08:02 -05:00
INDENT Expressions OUTDENT { result = val[1] }
| INDENT OUTDENT { result = Expressions.new([]) }
;
# All tokens that can terminate an expression.
2009-12-13 17:07:16 -05:00
Terminator:
"\n"
| ";"
;
# All hard-coded values.
2009-12-13 17:07:16 -05:00
Literal:
NUMBER { result = LiteralNode.new(val[0]) }
| STRING { result = LiteralNode.new(val[0]) }
2009-12-15 09:11:27 -05:00
| JS { result = LiteralNode.new(val[0]) }
2009-12-13 18:37:29 -05:00
| REGEX { result = LiteralNode.new(val[0]) }
2009-12-14 23:11:28 -05:00
| BREAK { result = LiteralNode.new(val[0]) }
| CONTINUE { result = LiteralNode.new(val[0]) }
| TRUE { result = LiteralNode.new(true) }
| FALSE { result = LiteralNode.new(false) }
| YES { result = LiteralNode.new(true) }
| NO { result = LiteralNode.new(false) }
| ON { result = LiteralNode.new(true) }
| OFF { result = LiteralNode.new(false) }
2009-12-13 17:07:16 -05:00
;
# Assignment to a variable.
2009-12-13 17:07:16 -05:00
Assign:
2009-12-28 23:08:02 -05:00
Value ASSIGN Expression { result = AssignNode.new(val[0], val[2]) }
2009-12-13 17:07:16 -05:00
;
# Assignment within an object literal.
AssignObj:
2009-12-28 23:08:02 -05:00
IDENTIFIER ASSIGN Expression { result = AssignNode.new(ValueNode.new(val[0]), val[2], :object) }
| STRING ASSIGN Expression { result = AssignNode.new(ValueNode.new(LiteralNode.new(val[0])), val[2], :object) }
| Comment { result = val[0] }
2009-12-13 17:07:16 -05:00
;
# A return statement.
2009-12-13 18:37:29 -05:00
Return:
RETURN Expression { result = ReturnNode.new(val[1]) }
;
# A comment.
Comment:
COMMENT { result = CommentNode.new(val[0]) }
;
2009-12-13 17:07:16 -05:00
# Arithmetic and logical operators
# For Ruby's Operator precedence, see:
# https://www.cs.auckland.ac.nz/references/ruby/ProgrammingRuby/language.html
Operation:
'!' Expression { result = OpNode.new(val[0], val[1]) }
| '!!' Expression { result = OpNode.new(val[0], val[1]) }
| '-' Expression = UMINUS { result = OpNode.new(val[0], val[1]) }
| NOT Expression { result = OpNode.new(val[0], val[1]) }
| '~' Expression { result = OpNode.new(val[0], val[1]) }
| '--' Expression { result = OpNode.new(val[0], val[1]) }
| '++' Expression { result = OpNode.new(val[0], val[1]) }
| DELETE Expression { result = OpNode.new(val[0], val[1]) }
| TYPEOF Expression { result = OpNode.new(val[0], val[1]) }
| Expression '--' { result = OpNode.new(val[1], val[0], nil, true) }
| Expression '++' { result = OpNode.new(val[1], val[0], nil, true) }
| Expression '*' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '/' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '%' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '+' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '-' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '<<' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>>>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '&' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '|' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '^' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '<=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '<' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '==' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '!=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression IS Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression ISNT Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '&&' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '||' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression AND Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression OR Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '-=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '+=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '/=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '*=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '%=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '||=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '&&=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression INSTANCEOF Expression { result = OpNode.new(val[1], val[0], val[2]) }
2009-12-28 23:08:02 -05:00
;
# Function definition.
Code:
ParamList "=>" Block { result = CodeNode.new(val[0], val[2]) }
| "=>" Block { result = CodeNode.new([], val[1]) }
2009-12-17 09:07:42 -05:00
;
# The parameters to a function definition.
2009-12-13 17:07:16 -05:00
ParamList:
2009-12-13 23:59:12 -05:00
PARAM { result = val }
2009-12-13 20:29:44 -05:00
| ParamList "," PARAM { result = val[0] << val[2] }
2009-12-13 17:07:16 -05:00
;
# Expressions that can be treated as values.
2009-12-14 10:00:31 -05:00
Value:
IDENTIFIER { result = ValueNode.new(val[0]) }
| Literal { result = ValueNode.new(val[0]) }
| Array { result = ValueNode.new(val[0]) }
| Object { result = ValueNode.new(val[0]) }
| Parenthetical { result = ValueNode.new(val[0]) }
2009-12-14 10:00:31 -05:00
| Value Accessor { result = val[0] << val[1] }
| Invocation Accessor { result = ValueNode.new(val[0], [val[1]]) }
2009-12-13 20:29:44 -05:00
;
# Accessing into an object or array, through dot or index notation.
2009-12-13 20:29:44 -05:00
Accessor:
PROPERTY_ACCESS IDENTIFIER { result = AccessorNode.new(val[1]) }
| Index { result = val[0] }
2009-12-26 03:16:40 -05:00
| Range { result = SliceNode.new(val[0]) }
2009-12-13 20:29:44 -05:00
;
# Indexing into an object or array.
2009-12-13 20:29:44 -05:00
Index:
2009-12-15 11:36:24 -05:00
"[" Expression "]" { result = IndexNode.new(val[1]) }
2009-12-13 17:07:16 -05:00
;
# An object literal.
2009-12-13 17:07:16 -05:00
Object:
"{" AssignList "}" { result = ObjectNode.new(val[1]) }
;
# Assignment within an object literal (comma or newline separated).
2009-12-13 17:07:16 -05:00
AssignList:
/* nothing */ { result = [] }
2009-12-13 17:07:16 -05:00
| AssignObj { result = val }
| AssignList "," AssignObj { result = val[0] << val[2] }
| AssignList Terminator AssignObj { result = val[0] << val[2] }
| AssignList ","
Terminator AssignObj { result = val[0] << val[3] }
| INDENT AssignList OUTDENT { result = val[1] }
2009-12-13 17:07:16 -05:00
;
# All flavors of function call (instantiation, super, and regular).
2009-12-13 17:07:16 -05:00
Call:
2009-12-13 23:59:12 -05:00
Invocation { result = val[0] }
2009-12-14 10:00:31 -05:00
| NEW Invocation { result = val[1].new_instance }
2009-12-17 09:07:42 -05:00
| Super { result = val[0] }
2009-12-13 23:45:18 -05:00
;
# Extending an object's prototype.
Extends:
Value EXTENDS Value { result = ExtendsNode.new(val[0], val[2]) }
;
# A generic function invocation.
2009-12-13 23:45:18 -05:00
Invocation:
Value "(" ArgList ")" { result = CallNode.new(val[0], val[2]) }
| Invocation "(" ArgList ")" { result = CallNode.new(val[0], val[2]) }
2009-12-13 17:07:16 -05:00
;
# Calling super.
2009-12-17 09:07:42 -05:00
Super:
SUPER "(" ArgList ")" { result = CallNode.new(:super, val[2]) }
;
# The range literal.
Range:
2009-12-26 03:16:40 -05:00
"[" Value "." "." Value "]" { result = RangeNode.new(val[1], val[4]) }
| "[" Value "." "." "." Value "]" { result = RangeNode.new(val[1], val[5], true) }
;
# The array literal.
2009-12-13 17:07:16 -05:00
Array:
"[" ArgList "]" { result = ArrayNode.new(val[1]) }
2009-12-13 17:07:16 -05:00
;
# A list of arguments to a method call, or as the contents of an array.
2009-12-13 17:07:16 -05:00
ArgList:
/* nothing */ { result = [] }
| Expression { result = val }
| ArgList "," Expression { result = val[0] << val[2] }
2009-12-13 20:29:44 -05:00
| ArgList Terminator Expression { result = val[0] << val[2] }
| ArgList "," Terminator Expression { result = val[0] << val[3] }
| INDENT ArgList OUTDENT { result = val[1] }
2009-12-13 17:07:16 -05:00
;
2009-12-17 22:22:35 -05:00
# Try/catch/finally exception handling blocks.
2009-12-14 10:00:31 -05:00
Try:
TRY Block Catch { result = TryNode.new(val[1], val[2][0], val[2][1]) }
2009-12-30 00:22:27 -05:00
| TRY Block FINALLY Block { result = TryNode.new(val[1], nil, nil, val[3]) }
| TRY Block Catch
2009-12-28 23:08:02 -05:00
FINALLY Block { result = TryNode.new(val[1], val[2][0], val[2][1], val[4]) }
2009-12-24 02:01:39 -05:00
;
# A catch clause.
Catch:
2009-12-30 00:22:27 -05:00
CATCH IDENTIFIER Block { result = [val[1], val[2]] }
;
2009-12-17 22:22:35 -05:00
# Throw an exception.
Throw:
THROW Expression { result = ThrowNode.new(val[1]) }
2009-12-14 10:00:31 -05:00
;
2009-12-17 22:22:35 -05:00
# Parenthetical expressions.
2009-12-13 23:25:00 -05:00
Parenthetical:
"(" Expression ")" { result = ParentheticalNode.new(val[1], val[0].line) }
2009-12-13 23:25:00 -05:00
;
2009-12-17 22:22:35 -05:00
# The while loop. (there is no do..while).
While:
WHILE Expression Block { result = WhileNode.new(val[1], val[2]) }
;
2009-12-17 22:22:35 -05:00
# Array comprehensions, including guard and current index.
# Looks a little confusing, check nodes.rb for the arguments to ForNode.
For:
Expression FOR
ForVariables ForSource { result = ForNode.new(val[0], val[3][0], val[2][0], val[3][1], val[2][1]) }
2009-12-28 23:08:02 -05:00
| FOR ForVariables ForSource Block { result = ForNode.new(val[3], val[2][0], val[1][0], val[2][1], val[1][1]) }
;
2009-12-25 16:40:57 -05:00
# An array comprehension has variables for the current element and index.
ForVariables:
IDENTIFIER { result = val }
| IDENTIFIER "," IDENTIFIER { result = [val[0], val[2]] }
;
2009-12-25 16:40:57 -05:00
# The source of the array comprehension can optionally be filtered.
ForSource:
IN Expression { result = [val[1]] }
| IN Expression
WHEN Expression { result = [val[1], val[3]] }
;
# Switch/When blocks.
2009-12-15 22:30:27 -05:00
Switch:
2009-12-28 23:08:02 -05:00
SWITCH Expression INDENT
Whens OUTDENT { result = val[3].rewrite_condition(val[1]) }
| SWITCH Expression INDENT
Whens ELSE Block OUTDENT { result = val[3].rewrite_condition(val[1]).add_else(val[5]) }
2009-12-15 22:30:27 -05:00
;
# The inner list of whens.
Whens:
When { result = val[0] }
| Whens When { result = val[0] << val[1] }
2009-12-15 22:30:27 -05:00
;
# An individual when.
When:
WHEN Expression Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| WHEN Expression Block Terminator { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| Comment
2009-12-15 22:30:27 -05:00
;
# All of the following nutso if-else destructuring is to make the
# grammar expand unambiguously.
2009-12-28 23:08:02 -05:00
IfBlock:
IF Expression Block { result = IfNode.new(val[1], val[2]) }
2009-12-28 23:08:02 -05:00
;
# An elsif portion of an if-else block.
ElsIf:
ELSE IfBlock { result = val[1].force_statement }
;
# Multiple elsifs can be chained together.
ElsIfs:
ElsIf { result = val[0] }
| ElsIfs ElsIf { result = val[0].add_else(val[1]) }
;
# Terminating else bodies are strictly optional.
ElseBody
/* nothing */ { result = nil }
| ELSE Block { result = val[1] }
;
# All the alternatives for ending an if-else block.
IfEnd:
ElseBody { result = val[0] }
| ElsIfs ElseBody { result = val[0].add_else(val[1]) }
;
# The full complement of if blocks, including postfix one-liner ifs and unlesses.
If:
2009-12-28 23:08:02 -05:00
IfBlock IfEnd { result = val[0].add_else(val[1]) }
| Expression IF Expression { result = IfNode.new(val[2], Expressions.new([val[0]]), nil, {:statement => true}) }
| Expression UNLESS Expression { result = IfNode.new(val[2], Expressions.new([val[0]]), nil, {:statement => true, :invert => true}) }
;
2009-12-13 17:07:16 -05:00
end
---- header
module CoffeeScript
2009-12-13 17:07:16 -05:00
---- inner
2009-12-17 22:22:35 -05:00
# Lex and parse a CoffeeScript.
2009-12-17 10:04:43 -05:00
def parse(code)
2009-12-17 22:22:35 -05:00
# Uncomment the following line to enable grammar debugging, in combination
# with the -g flag in the Rake build task.
2009-12-13 17:07:16 -05:00
# @yydebug = true
@tokens = Lexer.new.tokenize(code)
do_parse
end
2009-12-17 22:22:35 -05:00
# Retrieve the next token from the list.
2009-12-13 17:07:16 -05:00
def next_token
@tokens.shift
2009-12-17 10:04:43 -05:00
end
2009-12-17 22:22:35 -05:00
# Raise a custom error class that knows about line numbers.
2009-12-17 10:04:43 -05:00
def on_error(error_token_id, error_value, value_stack)
raise ParseError.new(token_to_str(error_token_id), error_value, value_stack)
end
---- footer
end