Skip to content

Commit

Permalink
compound assignments are now represented as Assign nodes (rather than…
Browse files Browse the repository at this point in the history
… Op) and have the same precedence as `=`
  • Loading branch information
satyr committed Oct 20, 2010
1 parent 15cfe8e commit 90a13bd
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 112 deletions.
2 changes: 1 addition & 1 deletion extras/coffee-script.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions lib/grammar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 28 additions & 44 deletions lib/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/optparse.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/parser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ Parser = require('jison').Parser
# Since we're going to be wrapped in a function by Jison in any case, if our
# action immediately returns a value, we can optimize by removing the function
# wrapper and just returning the value directly.
unwrap = /function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/

# Our handy DSL for Jison grammar generation, thanks to
# [Tim Caswell](http://github.com/creationix). For every rule in the grammar,
# we pass the pattern-defining string, the action to run, and extra options,
# optionally. If no action is specified, we simply pass the value of the
# previous nonterminal.
o = (patternString, action, options) ->
patternString = patternString.replace /\s{2,}/g, ' '
return [patternString, '$$ = $1;', options] unless action
action = if match = (action + '').match(unwrap) then match[1] else "(#{action}())"
action = action.replace(/\bnew (\w+)\b/g, 'new yy.$1').replace(/Expressions\.wrap/g, 'yy.Expressions.wrap');
action = if match = unwrap.exec action then match[1] else "(#{action}())"
action = action.replace /\bnew /g, '$&yy.'
action = action.replace /\bExpressions\.wrap\b/g, 'yy.$&'
[patternString, "$$ = #{action};", options]

# Grammatical Rules
Expand Down Expand Up @@ -145,9 +147,9 @@ grammar =
o "Identifier", -> new Value $1
o "AlphaNumeric"
o "ThisProperty"
o "Identifier : Expression", -> new Assign new Value($1), $3, 'object'
o "AlphaNumeric : Expression", -> new Assign new Value($1), $3, 'object'
o "Identifier : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'object'
o "Identifier : Expression", -> new Assign new Value($1), $3, 'object'
o "AlphaNumeric : Expression", -> new Assign new Value($1), $3, 'object'
o "Identifier : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'object'
o "AlphaNumeric : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'object'
o "Comment"
]
Expand Down Expand Up @@ -283,8 +285,8 @@ grammar =

# Assignments that can happen directly inside a class declaration.
ClassAssign: [
o "AssignObj", -> $1
o "ThisProperty : Expression", -> new Assign new Value($1), $3, 'this'
o "AssignObj", -> $1
o "ThisProperty : Expression", -> new Assign new Value($1), $3, 'this'
o "ThisProperty : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'this'
]

Expand Down Expand Up @@ -541,8 +543,10 @@ grammar =
o "Expression SHIFT Expression", -> new Op $2, $1, $3
o "Expression COMPARE Expression", -> new Op $2, $1, $3
o "Expression LOGIC Expression", -> new Op $2, $1, $3
o "SimpleAssignable COMPOUND_ASSIGN Expression", -> new Op $2, $1, $3
o "SimpleAssignable COMPOUND_ASSIGN INDENT Expression OUTDENT", -> new Op $2, $1, $4
o "SimpleAssignable COMPOUND_ASSIGN Expression",
-> new Assign $1, $3, $2
o "SimpleAssignable COMPOUND_ASSIGN INDENT Expression OUTDENT",
-> new Assign $1, $4, $2

o "Expression RELATION Expression", ->
if $2.charAt(0) is '!'
Expand Down Expand Up @@ -578,12 +582,11 @@ operators = [
["left", 'RELATION']
["left", '==', '!=']
["left", 'LOGIC']
["right", 'COMPOUND_ASSIGN']
["left", '.']
["nonassoc", 'INDENT', 'OUTDENT']
["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'BY', 'THROW']
["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS']
["right", '=', ':', 'RETURN']
["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN']
["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']
]

Expand Down
Loading

0 comments on commit 90a13bd

Please sign in to comment.