diff --git a/lib/grammar.js b/lib/grammar.js index 0a2214c4..e87ad25a 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -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. diff --git a/lib/parser.js b/lib/parser.js index 6a37b945..4c324a17 100755 --- a/lib/parser.js +++ b/lib/parser.js @@ -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; diff --git a/src/grammar.coffee b/src/grammar.coffee index 891d68c8..a7e418dd 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -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. diff --git a/test/test_compilation.coffee b/test/test_compilation.coffee index 23186f8e..00a06b4b 100644 --- a/test/test_compilation.coffee +++ b/test/test_compilation.coffee @@ -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: [] \ No newline at end of file +# 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: [] \ No newline at end of file