Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "procedures": functions that are guaranteed to not return a value #2726

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/coffee-script/grammar.js

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

4 changes: 2 additions & 2 deletions lib/coffee-script/lexer.js

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

28 changes: 15 additions & 13 deletions lib/coffee-script/nodes.js

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

327 changes: 164 additions & 163 deletions lib/coffee-script/parser.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/coffee-script/rewriter.js

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

6 changes: 6 additions & 0 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,15 @@ grammar =

# CoffeeScript has two different symbols for functions. `->` is for ordinary
# functions, and `=>` is for functions bound to the current value of *this*.
#
# Two additional symbols exist for defining a "procedure" - a function that
# does not implicitly return a value. These are `->>` and `=>>`; they operate
# in the same manner as the two function symbols.
FuncGlyph: [
o '->', -> 'func'
o '=>', -> 'boundfunc'
o '->>', -> 'procedure'
o '=>>', -> 'boundprocedure'
]

# An optional, trailing comma.
Expand Down
4 changes: 2 additions & 2 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ NUMBER = ///
HEREDOC = /// ^ ("""|''') ([\s\S]*?) (?:\n[^\n\S]*)? \1 ///

OPERATOR = /// ^ (
?: [-=]> # function
?: [-=]>>? # function
| [-+*/%<>&|^!?=]= # compound assign / compare
| >>>=? # zero-fill right shift
| ([-+:])\1 # doubles
Expand All @@ -778,7 +778,7 @@ COMMENT = /^###([^#][\s\S]*?)(?:###[^\n\S]*|(?:###)$)|^(?:\s*#(?!##[^#]).*)+/

LITERATE = /^([ ]{4}|\t)/

CODE = /^[-=]>/
CODE = /^[-=]>>?/

MULTI_DENT = /^(?:\n[^\n\S]*)+/

Expand Down
22 changes: 11 additions & 11 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,6 @@ exports.Class = class Class extends Base
@ctor.klass = null
@ctor.noReturn = yes

# Prevent constructor from returning a value.
returnExpr = null
@ctor.body.traverseChildren no, (node) ->
return no if node instanceof Return and (returnExpr = node.expression)
if returnExpr
throw SyntaxError "cannot return a value from a constructor: \"#{returnExpr.compileNode o}\" in class #{name}"

# Instead of generating the JavaScript string directly, we build up the
# equivalent syntax tree and compile that, in pieces. You can see the
# constructor, property assignments, and inheritance getting built out below.
Expand Down Expand Up @@ -1165,10 +1158,11 @@ exports.Assign = class Assign extends Base
# has no *children* -- they're within the inner scope.
exports.Code = class Code extends Base
constructor: (params, body, tag) ->
@params = params or []
@body = body or new Block
@bound = tag is 'boundfunc'
@context = '_this' if @bound
@params = params or []
@body = body or new Block
@bound = tag is 'boundfunc' or tag is 'boundprocedure'
@noReturn = tag is 'procedure' or tag is 'boundprocedure'
@context = '_this' if @bound

children: ['params', 'body']

Expand Down Expand Up @@ -1224,6 +1218,12 @@ exports.Code = class Code extends Base
@bound = @context = o.scope.parent.method.context
else if not @static
o.scope.parent.assign '_this', 'this'
if @noReturn
returnExpr = null
@body.traverseChildren no, (node) ->
return no if node instanceof Return and (returnExpr = node.expression)
if returnExpr
throw SyntaxError 'cannot return a value from a constructor or procedure.'
idt = o.indent
code = 'function'
code += ' ' + @name if @ctor
Expand Down
8 changes: 4 additions & 4 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class exports.Rewriter
condition = (token, i) ->
[tag] = token
return yes if not seenSingle and token.fromThen
seenSingle = yes if tag in ['IF', 'ELSE', 'CATCH', '->', '=>', 'CLASS']
seenSingle = yes if tag in ['IF', 'ELSE', 'CATCH', '->', '=>', '->>', '=>>', 'CLASS' ]
seenControl = yes if tag in ['IF', 'ELSE', 'SWITCH', 'TRY', '=']
return yes if tag in ['.', '?.', '::'] and @tag(i - 1) is 'OUTDENT'
not token.generated and @tag(i - 1) isnt ',' and (tag in IMPLICIT_END or
Expand Down Expand Up @@ -321,20 +321,20 @@ IMPLICIT_FUNC = ['IDENTIFIER', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@
IMPLICIT_CALL = [
'IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'CLASS'
'IF', 'TRY', 'SWITCH', 'THIS', 'BOOL', 'NULL', 'UNDEFINED', 'UNARY', 'SUPER'
'@', '->', '=>', '[', '(', '{', '--', '++'
'@', '->', '=>', '->>', '=>>', '[', '(', '{', '--', '++'
]

IMPLICIT_UNSPACED_CALL = ['+', '-']

# Tokens indicating that the implicit call must enclose a block of expressions.
IMPLICIT_BLOCK = ['->', '=>', '{', '[', ',']
IMPLICIT_BLOCK = ['->', '=>', '->>', '=>>', '{', '[', ',']

# Tokens that always mark the end of an implicit call for single-liners.
IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY', 'LOOP', 'TERMINATOR']

# Single-line flavors of block expressions that have unclosed endings.
# The grammar can't disambiguate them, so we insert the implicit indentation.
SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN']
SINGLE_LINERS = ['ELSE', '->', '=>', '->>', '=>>', 'TRY', 'FINALLY', 'THEN']
SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN']

# Tokens that end a line.
Expand Down
20 changes: 20 additions & 0 deletions test/functions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,23 @@ test "#1435 Indented property access", ->
rec.rec()
.rec()
1

test "procedures do not implicitly return a value", ->
a = ->> yes
eq undefined, a()

test "returning a value from a procedure throws a syntax error", ->
throws -> CoffeeScript.run "a = ->> return yes", bare: yes

test "procedures can return without returning a value", ->
passes = null
a = ->>
passes = yes
return
passes = no
a()
eq passes, true

test "bound procedures retain their \"this\" context", ->
that = this
a = =>> eq this, that