Skip to content

Commit

Permalink
removed need for -{: token
Browse files Browse the repository at this point in the history
  • Loading branch information
malgorithms committed Aug 8, 2012
1 parent 0a4ce33 commit f0698d9
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 101 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -130,7 +130,9 @@ In toffee mode, you can comment out a region with `{##` and `##}`.
```


### Switching to toffee mode without indenting
### Toffee is smart about indenting.

You can indent your `{# #}` region however, you want, and

By default, when you enter `{: ... :}`, the Toffee compiler assumes you're entering an indented region,
probably because of a loop or conditional.
Expand Down
50 changes: 23 additions & 27 deletions lib/toffee_lang.js
Expand Up @@ -2,9 +2,9 @@
var toffee_lang = (function(){
var parser = {trace: function trace() { },
yy: {},
symbols_: {"error":2,"starter":3,"toffee_zone":4,"EOF":5,"toffee_code":6,"flip_to_coffee":7,"flip_to_toffee_comment":8,"START_TOFFEE_COMMENT":9,"toffee_commented_region":10,"END_TOFFEE_COMMENT":11,"START_INDENTED_TOFFEE":12,"START_COFFEE":13,"END_COFFEE":14,"START_TOFFEE":15,"END_TOFFEE":16,"CODE":17,"coffee_zone":18,"coffee_code":19,"flip_to_toffee":20,"code":21,"$accept":0,"$end":1},
terminals_: {2:"error",5:"EOF",9:"START_TOFFEE_COMMENT",11:"END_TOFFEE_COMMENT",12:"START_INDENTED_TOFFEE",13:"START_COFFEE",14:"END_COFFEE",15:"START_TOFFEE",16:"END_TOFFEE",17:"CODE"},
productions_: [0,[3,2],[4,1],[4,3],[4,2],[4,3],[4,2],[4,0],[8,3],[10,2],[10,2],[10,2],[10,2],[10,2],[10,2],[10,0],[7,3],[18,1],[18,3],[18,2],[18,0],[20,3],[20,3],[6,1],[19,1],[21,1],[21,2]],
symbols_: {"error":2,"starter":3,"toffee_zone":4,"EOF":5,"toffee_code":6,"flip_to_coffee":7,"flip_to_toffee_comment":8,"START_TOFFEE_COMMENT":9,"toffee_commented_region":10,"END_TOFFEE_COMMENT":11,"START_COFFEE":12,"END_COFFEE":13,"START_TOFFEE":14,"END_TOFFEE":15,"CODE":16,"coffee_zone":17,"coffee_code":18,"flip_to_toffee":19,"code":20,"$accept":0,"$end":1},
terminals_: {2:"error",5:"EOF",9:"START_TOFFEE_COMMENT",11:"END_TOFFEE_COMMENT",12:"START_COFFEE",13:"END_COFFEE",14:"START_TOFFEE",15:"END_TOFFEE",16:"CODE"},
productions_: [0,[3,2],[4,1],[4,3],[4,2],[4,3],[4,2],[4,0],[8,3],[10,2],[10,2],[10,2],[10,2],[10,2],[10,0],[7,3],[17,1],[17,3],[17,2],[17,0],[19,3],[6,1],[18,1],[20,1],[20,2]],
performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {

var $0 = $$.length - 1;
Expand All @@ -23,37 +23,35 @@ case 6: this.$ = $$[$0];
break;
case 7: this.$ = [];
break;
case 16: this.$ = ["COFFEE_ZONE", $$[$0-1]];
case 15: this.$ = ["COFFEE_ZONE", $$[$0-1]];
break;
case 17: this.$ = [$$[$0]];
case 16: this.$ = [$$[$0]];
break;
case 18: this.$ = $$[$0]; $$[$0].splice(0,0,$$[$0-2],$$[$0-1]);
case 17: this.$ = $$[$0]; $$[$0].splice(0,0,$$[$0-2],$$[$0-1]);
break;
case 19: this.$ = $$[$0]; $$[$0].splice(0,0,$$[$0-1]);
case 18: this.$ = $$[$0]; $$[$0].splice(0,0,$$[$0-1]);
break;
case 20: this.$ = [];
case 19: this.$ = [];
break;
case 21: this.$ = ["TOFFEE_ZONE", $$[$0-1]];
case 20: this.$ = ["TOFFEE_ZONE", $$[$0-1]];
break;
case 22: this.$ = ["INDENTED_TOFFEE_ZONE", $$[$0-1]];
case 21: this.$ = ["TOFFEE", $$[$0][0], $$[$0][1] ];
break;
case 23: this.$ = ["TOFFEE", $$[$0][0], $$[$0][1] ];
case 22: this.$ = ["COFFEE", $$[$0][0], $$[$0][1] ];
break;
case 24: this.$ = ["COFFEE", $$[$0][0], $$[$0][1] ];
break;
case 25: var ln = yylineno + 1 - $$[$0].split("\n").length + 1;
case 23: var ln = yylineno + 1 - $$[$0].split("\n").length + 1;
this.$ = [$$[$0], ln];

break;
case 26: var c = $$[$0-1][0] + $$[$0];
case 24: var c = $$[$0-1][0] + $$[$0];
var ln = yylineno + 1 - c.split("\n").length + 1;
this.$ = [c, ln];

break;
}
},
table: [{3:1,4:2,5:[2,7],6:3,7:4,8:5,9:[1,8],13:[1,7],17:[1,9],21:6},{1:[3]},{5:[1,10]},{5:[2,2],7:11,8:12,9:[1,8],13:[1,7],16:[2,2]},{4:13,5:[2,7],6:3,7:4,8:5,9:[1,8],13:[1,7],16:[2,7],17:[1,9],21:6},{4:14,5:[2,7],6:3,7:4,8:5,9:[1,8],13:[1,7],16:[2,7],17:[1,9],21:6},{5:[2,23],9:[2,23],13:[2,23],16:[2,23],17:[1,15]},{12:[1,21],14:[2,20],15:[1,20],17:[1,9],18:16,19:17,20:18,21:19},{10:22,11:[2,15],12:[2,15],13:[2,15],14:[2,15],15:[2,15],16:[2,15],17:[2,15]},{5:[2,25],9:[2,25],12:[2,25],13:[2,25],14:[2,25],15:[2,25],16:[2,25],17:[2,25]},{1:[2,1]},{4:23,5:[2,7],6:3,7:4,8:5,9:[1,8],13:[1,7],16:[2,7],17:[1,9],21:6},{4:24,5:[2,7],6:3,7:4,8:5,9:[1,8],13:[1,7],16:[2,7],17:[1,9],21:6},{5:[2,4],16:[2,4]},{5:[2,6],16:[2,6]},{5:[2,26],9:[2,26],12:[2,26],13:[2,26],14:[2,26],15:[2,26],16:[2,26],17:[2,26]},{14:[1,25]},{12:[1,21],14:[2,17],15:[1,20],20:26},{12:[1,21],14:[2,20],15:[1,20],17:[1,9],18:27,19:17,20:18,21:19},{12:[2,24],14:[2,24],15:[2,24],17:[1,15]},{4:28,6:3,7:4,8:5,9:[1,8],13:[1,7],16:[2,7],17:[1,9],21:6},{4:29,6:3,7:4,8:5,9:[1,8],13:[1,7],16:[2,7],17:[1,9],21:6},{11:[1,30],12:[1,31],13:[1,32],14:[1,33],15:[1,34],16:[1,35],17:[1,36]},{5:[2,3],16:[2,3]},{5:[2,5],16:[2,5]},{5:[2,16],9:[2,16],13:[2,16],16:[2,16],17:[2,16]},{12:[1,21],14:[2,20],15:[1,20],17:[1,9],18:37,19:17,20:18,21:19},{14:[2,19]},{16:[1,38]},{16:[1,39]},{5:[2,8],9:[2,8],13:[2,8],16:[2,8],17:[2,8]},{11:[2,9],12:[2,9],13:[2,9],14:[2,9],15:[2,9],16:[2,9],17:[2,9]},{11:[2,10],12:[2,10],13:[2,10],14:[2,10],15:[2,10],16:[2,10],17:[2,10]},{11:[2,11],12:[2,11],13:[2,11],14:[2,11],15:[2,11],16:[2,11],17:[2,11]},{11:[2,12],12:[2,12],13:[2,12],14:[2,12],15:[2,12],16:[2,12],17:[2,12]},{11:[2,13],12:[2,13],13:[2,13],14:[2,13],15:[2,13],16:[2,13],17:[2,13]},{11:[2,14],12:[2,14],13:[2,14],14:[2,14],15:[2,14],16:[2,14],17:[2,14]},{14:[2,18]},{12:[2,21],14:[2,21],15:[2,21],17:[2,21]},{12:[2,22],14:[2,22],15:[2,22],17:[2,22]}],
defaultActions: {10:[2,1],27:[2,19],37:[2,18]},
table: [{3:1,4:2,5:[2,7],6:3,7:4,8:5,9:[1,8],12:[1,7],16:[1,9],20:6},{1:[3]},{5:[1,10]},{5:[2,2],7:11,8:12,9:[1,8],12:[1,7],15:[2,2]},{4:13,5:[2,7],6:3,7:4,8:5,9:[1,8],12:[1,7],15:[2,7],16:[1,9],20:6},{4:14,5:[2,7],6:3,7:4,8:5,9:[1,8],12:[1,7],15:[2,7],16:[1,9],20:6},{5:[2,21],9:[2,21],12:[2,21],15:[2,21],16:[1,15]},{13:[2,19],14:[1,20],16:[1,9],17:16,18:17,19:18,20:19},{10:21,11:[2,14],12:[2,14],13:[2,14],14:[2,14],15:[2,14],16:[2,14]},{5:[2,23],9:[2,23],12:[2,23],13:[2,23],14:[2,23],15:[2,23],16:[2,23]},{1:[2,1]},{4:22,5:[2,7],6:3,7:4,8:5,9:[1,8],12:[1,7],15:[2,7],16:[1,9],20:6},{4:23,5:[2,7],6:3,7:4,8:5,9:[1,8],12:[1,7],15:[2,7],16:[1,9],20:6},{5:[2,4],15:[2,4]},{5:[2,6],15:[2,6]},{5:[2,24],9:[2,24],12:[2,24],13:[2,24],14:[2,24],15:[2,24],16:[2,24]},{13:[1,24]},{13:[2,16],14:[1,20],19:25},{13:[2,19],14:[1,20],16:[1,9],17:26,18:17,19:18,20:19},{13:[2,22],14:[2,22],16:[1,15]},{4:27,6:3,7:4,8:5,9:[1,8],12:[1,7],15:[2,7],16:[1,9],20:6},{11:[1,28],12:[1,29],13:[1,30],14:[1,31],15:[1,32],16:[1,33]},{5:[2,3],15:[2,3]},{5:[2,5],15:[2,5]},{5:[2,15],9:[2,15],12:[2,15],15:[2,15],16:[2,15]},{13:[2,19],14:[1,20],16:[1,9],17:34,18:17,19:18,20:19},{13:[2,18]},{15:[1,35]},{5:[2,8],9:[2,8],12:[2,8],15:[2,8],16:[2,8]},{11:[2,9],12:[2,9],13:[2,9],14:[2,9],15:[2,9],16:[2,9]},{11:[2,10],12:[2,10],13:[2,10],14:[2,10],15:[2,10],16:[2,10]},{11:[2,11],12:[2,11],13:[2,11],14:[2,11],15:[2,11],16:[2,11]},{11:[2,12],12:[2,12],13:[2,12],14:[2,12],15:[2,12],16:[2,12]},{11:[2,13],12:[2,13],13:[2,13],14:[2,13],15:[2,13],16:[2,13]},{13:[2,17]},{13:[2,20],14:[2,20],16:[2,20]}],
defaultActions: {10:[2,1],26:[2,18],34:[2,17]},
parseError: function parseError(str, hash) {
throw new Error(str);
},
Expand Down Expand Up @@ -380,24 +378,22 @@ case 0:return 9;
break;
case 1:return 11;
break;
case 2:return 16;
break;
case 3:return 12;
case 2:return 15;
break;
case 4:return 13;
case 3:return 14;
break;
case 5:return 14;
case 4:return 12;
break;
case 6:return 15;
case 5:return 13;
break;
case 7:return 17;
case 6:return 16;
break;
case 8:return 5;
case 7:return 5;
break;
}
};
lexer.rules = [/^\{##/,/^##\}/,/^:\}/,/^\{:/,/^\{#/,/^#\}/,/^[\-][\t\r\n ]*\{:/,/^[^{}#\\:\-]+|[\\{}#:\-]/,/^$/];
lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8],"inclusive":true}};
lexer.rules = [/^\{##/,/^##\}/,/^:\}/,/^\{:/,/^\{#/,/^#\}/,/^[^{}#\\:\-]+|[\\{}#:\-]/,/^$/];
lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7],"inclusive":true}};
return lexer;})()
parser.lexer = lexer;
return parser;
Expand Down
51 changes: 28 additions & 23 deletions lib/view.js

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

2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "toffee",
"description": "An Express 3.x and 2.x templating language based on CoffeeScript with slicker tokens and syntax. Built with love at OkCupid.",
"version": "0.0.34",
"version": "0.0.35",
"directories": {
"lib": "./lib"
},
Expand Down
15 changes: 5 additions & 10 deletions src/toffee.jison
Expand Up @@ -10,10 +10,9 @@
"{##" return 'START_TOFFEE_COMMENT';
"##}" return 'END_TOFFEE_COMMENT';
":}" return 'END_TOFFEE';
"{:" return 'START_INDENTED_TOFFEE';
"{:" return 'START_TOFFEE';
"{#" return 'START_COFFEE';
"#}" return 'END_COFFEE';
[\-][\t\r\n ]*"{:" return 'START_TOFFEE';
[^{}#\\:\-]+|[\\{}#:\-] return 'CODE';
<<EOF>> return 'EOF';

Expand All @@ -31,17 +30,17 @@ starter

toffee_zone
:
toffee_code { $$ = [$1]; }
toffee_code { $$ = [$1]; }
|
toffee_code flip_to_coffee toffee_zone { $$ = $3; $3.splice(0,0,$1,$2); }
toffee_code flip_to_coffee toffee_zone { $$ = $3; $3.splice(0,0,$1,$2); }
|
flip_to_coffee toffee_zone { $$ = $2; $2.splice(0,0,$1); }
flip_to_coffee toffee_zone { $$ = $2; $2.splice(0,0,$1); }
|
toffee_code flip_to_toffee_comment toffee_zone { $$ = $3; $3.splice(0,0,$1); }
|
flip_to_toffee_comment toffee_zone { $$ = $2; }
|
{ $$ = []; }
{ $$ = []; }
;

flip_to_toffee_comment
Expand All @@ -51,8 +50,6 @@ flip_to_toffee_comment

toffee_commented_region
:
toffee_commented_region START_INDENTED_TOFFEE
|
toffee_commented_region START_COFFEE
|
toffee_commented_region END_COFFEE
Expand Down Expand Up @@ -84,8 +81,6 @@ coffee_zone
flip_to_toffee
:
START_TOFFEE toffee_zone END_TOFFEE { $$ = ["TOFFEE_ZONE", $2]; }
|
START_INDENTED_TOFFEE toffee_zone END_TOFFEE { $$ = ["INDENTED_TOFFEE_ZONE", $2]; }
;


Expand Down
25 changes: 15 additions & 10 deletions src/view.coffee
Expand Up @@ -170,7 +170,7 @@ class view
###
replaces tabs with spaces in their coffee regions
###
if obj[0] in ["INDENTED_TOFFEE_ZONE", "TOFFEE_ZONE", "COFFEE_ZONE"]
if obj[0] in ["TOFFEE_ZONE", "COFFEE_ZONE"]
@_cleanTabs item for item in obj[1]
else if obj[0] is "COFFEE"
obj[1] = obj[1].replace /\t/g, @_tabAsSpaces()
Expand Down Expand Up @@ -247,7 +247,7 @@ class view
d = Date.now()
res = @_coffeeHeaders()
try
res += @_toCoffeeRecurse(tobj, TAB_SPACES, 0)[0]
res += @_toCoffeeRecurse(tobj, TAB_SPACES, 0, {})[0]
res += @_coffeeFooters()
@coffeeScript = res
catch e
Expand Down Expand Up @@ -283,30 +283,27 @@ class view
return false


_toCoffeeRecurse: (obj, indent_level, indent_baseline) ->
_toCoffeeRecurse: (obj, indent_level, indent_baseline, state_carry) ->
# returns [res, indent_baseline_delta]
# indent_level = # of spaces to add to each coffeescript section
# indent_baseline = # of chars to strip from each line inside {# #}

res = ""
i_delta = 0
switch obj[0]
when "INDENTED_TOFFEE_ZONE"
indent_level += TAB_SPACES
for item in obj[1]
[s, delta] = @_toCoffeeRecurse item, indent_level, indent_baseline
res += s
when "TOFFEE_ZONE"
if state_carry.last_coffee_ends_with_newline is false
indent_level += TAB_SPACES
res += "\n#{@_space indent_level}_ts #{states.TOFFEE}"
for item in obj[1]
[s, delta] = @_toCoffeeRecurse item, indent_level, indent_baseline
[s, delta] = @_toCoffeeRecurse item, indent_level, indent_baseline, state_carry
res += s
when "COFFEE_ZONE"
res += "\n#{@_space indent_level}_ts #{states.COFFEE}"
zone_baseline = @_getZoneBaseline obj[1]
temp_indent_level = indent_level
for item in obj[1]
[s, delta] = @_toCoffeeRecurse item, temp_indent_level, zone_baseline
[s, delta] = @_toCoffeeRecurse item, temp_indent_level, zone_baseline, state_carry
res += s
temp_indent_level = indent_level + delta
when "TOFFEE"
Expand Down Expand Up @@ -346,6 +343,7 @@ class view
c = obj[1]
res += "\n#{@_reindent c, indent_level, indent_baseline}"
i_delta = @_getIndentationDelta c, indent_baseline
state_carry.last_coffee_ends_with_newline = @_doesEndWithNewline c
else
throw "Bad parsing. #{obj} not handled."
return ["",0]
Expand All @@ -372,6 +370,13 @@ class view
if follow.length then res += "+ \'#{follow}\'"
res

_doesEndWithNewline: (s) ->
parts = s.split "\n"
if (parts.length > 1) and parts[parts.length - 1].match /^[\t ]*$/
return true
else
return false

_escapeForStr: (s) ->
###
escapes a string so it can make it into coffeescript
Expand Down
2 changes: 1 addition & 1 deletion test/cases/include_order/input.toffee
@@ -1,6 +1,6 @@
{#
say_hi = ->
-{:hi:}
{:hi:}
#}1
2
#{partial "child.toffee", say_hi: say_hi}
Expand Down
4 changes: 2 additions & 2 deletions test/cases/include_techniques/input.toffee
Expand Up @@ -2,6 +2,6 @@
#{partial "message.toffee", from: "Max & Sam"}
{#
print partial "message.toffee", from: "Christian"
-{:#{partial "message.toffee", from: "Jennie"}:}
print partial "message.toffee", sender: "The enemy"
{:#{ partial "message.toffee", from: "Jennie"}:}
print partial "message.toffee", sender: "The enemy"
#}

0 comments on commit f0698d9

Please sign in to comment.