CoffeeScript language extensions are now working.

This commit is contained in:
Jeremy Ashkenas 2010-03-09 23:44:29 -05:00
parent 5754d36fdd
commit 54627f6807
4 changed files with 29 additions and 26 deletions

View File

@ -373,7 +373,7 @@
// A language extension to CoffeeScript from the outside. We simply pass
// it through unaltered.
Extension: [o("EXTENSION", function() {
return $1;
return yytext;
})
],
// The condition portion of a while loop.

View File

@ -245,7 +245,7 @@ case 117:this.$ = new ThrowNode($$[$0-2+2-1]);
break;
case 118:this.$ = new ParentheticalNode($$[$0-3+2-1]);
break;
case 119:this.$ = $$[$0-1+1-1];
case 119:this.$ = yytext;
break;
case 120:this.$ = new WhileNode($$[$0-2+2-1]);
break;

View File

@ -357,7 +357,7 @@ grammar: {
# A language extension to CoffeeScript from the outside. We simply pass
# it through unaltered.
Extension: [
o "EXTENSION", -> $1
o "EXTENSION", -> yytext
]
# The condition portion of a while loop.

View File

@ -5,27 +5,30 @@ js: CoffeeScript.compile("one\r\ntwo", {no_wrap: on})
ok js is "one;\ntwo;"
# Try out language extensions to CoffeeScript. (Not yet working.)
# Try out language extensions to CoffeeScript.
# class SplitNode extends BaseNode
# type: 'Split'
#
# constructor: (variable) ->
# @variable: variable
#
# compile_node: (o) ->
# "${variable}.split('')"
#
# CoffeeScript.extend ->
# return false unless variable: @match /^--(\w+)--/, 1
# @i += variable.length + 4
# node: new SplitNode(variable)
# p node
# @token 'EXTENSION', node
# true
#
# js: CoffeeScript.tokens('print --tobesplit--', {no_wrap: on})
#
# p js
#
# Lexer.extensions: []
# Create the Node were going to add -- a literal syntax for splitting
# strings into letters.
class SplitNode extends BaseNode
type: 'Split'
constructor: (variable) ->
@variable: variable
compile_node: (o) ->
"'${@variable}'.split('')"
# Extend CoffeeScript with our lexing function that matches --wordgoeshere--
# and creates a SplitNode.
CoffeeScript.extend ->
return false unless variable: @match /^--(\w+)--/, 1
@i += variable.length + 4
@token 'EXTENSION', new SplitNode(variable)
true
# Compile with the extension.
js: CoffeeScript.compile('return --tobesplit--', {no_wrap: on})
ok js is "return 'tobesplit'.split('');"
Lexer.extensions: []