Skip to content

Commit

Permalink
added template comments feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cstivers78 committed Jan 13, 2012
1 parent d553b0e commit 2fdae01
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 47 deletions.
43 changes: 1 addition & 42 deletions lib/tags.js
@@ -1,4 +1,4 @@
var Access, Anchor, AnonFunction, Block, Content, DoWhile, Else, For, Func, Group, If, Invoke, Member, NamedFunction, Parameters, ScriptBlock, Tag, Value, While,
var Access, Anchor, Block, Content, DoWhile, Else, For, Func, Group, If, Invoke, Member, Parameters, ScriptBlock, Tag, Value, While,
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

Expand Down Expand Up @@ -203,45 +203,6 @@ DoWhile = (function(_super) {

})(Tag);

AnonFunction = (function(_super) {

__extends(AnonFunction, _super);

AnonFunction.prototype.name = 'AnonFunction';

function AnonFunction(args, block) {
this.args = args;
this.block = block;
}

AnonFunction.prototype.parts = function() {
return ['function', this.args, this.block];
};

return AnonFunction;

})(Tag);

NamedFunction = (function(_super) {

__extends(NamedFunction, _super);

NamedFunction.prototype.name = 'NamedFunction';

function NamedFunction(identifier, args, block) {
this.identifier = identifier;
this.args = args;
this.block = block;
}

NamedFunction.prototype.parts = function() {
return ['function ', this.identifier, this.args, this.block];
};

return NamedFunction;

})(Tag);

Func = (function(_super) {

__extends(Func, _super);
Expand Down Expand Up @@ -365,8 +326,6 @@ module.exports = {
For: For,
While: While,
DoWhile: DoWhile,
AnonFunction: AnonFunction,
NamedFunction: NamedFunction,
Func: Func,
Parameters: Parameters,
Value: Value,
Expand Down
23 changes: 20 additions & 3 deletions lib/tokenizer.js
@@ -1,4 +1,4 @@
var ACCESS, ANCHOR, Access, Anchor, BLOCK, Block, Content, DO, DO_WHILE, DoWhile, ELSE, Else, FOR, FUNC, Failure, For, Func, GROUP, Group, IDENTIFIER, IF, INVOKE, If, Invoke, MEMBER, Member, NoMatch, PARAMETERS, Parameters, ScriptBlock, Success, TRAILING_SPACES, Tokenizer, Value, WHILE, WHITESPACE, While, failure, success, _ref, _ref2;
var ACCESS, ANCHOR, Access, Anchor, BLOCK, Block, COMMENT, Content, DO, DO_WHILE, DoWhile, ELSE, Else, FOR, FUNC, Failure, For, Func, GROUP, Group, IDENTIFIER, IF, INVOKE, If, Invoke, MEMBER, Member, NoMatch, PARAMETERS, Parameters, ScriptBlock, Success, TRAILING_SPACES, Tokenizer, Value, WHILE, WHITESPACE, While, failure, success, _ref, _ref2;

_ref = require('./tags'), Anchor = _ref.Anchor, Content = _ref.Content, Group = _ref.Group, Block = _ref.Block, ScriptBlock = _ref.ScriptBlock, If = _ref.If, Else = _ref.Else, For = _ref.For, While = _ref.While, DoWhile = _ref.DoWhile, Func = _ref.Func, Parameters = _ref.Parameters, Value = _ref.Value, Member = _ref.Member, Access = _ref.Access, Invoke = _ref.Invoke;

Expand Down Expand Up @@ -38,6 +38,8 @@ ACCESS = /^\s*\[/;

INVOKE = /^\s*\(/;

COMMENT = /^\*/;

module.exports = Tokenizer = (function() {

function Tokenizer() {}
Expand Down Expand Up @@ -92,7 +94,7 @@ module.exports = Tokenizer = (function() {
if (chunk[0] !== '@') return NoMatch;
start = 1;
chunk = chunk.slice(start);
result = this.Parameters(chunk) || this.Escape(chunk) || this.If(chunk) || this.For(chunk) || this.While(chunk) || this.DoWhile(chunk) || this.Func(chunk) || this.Group(chunk) || this.ScriptBlock(chunk) || this.Value(chunk);
result = this.Comment(chunk) || this.Parameters(chunk) || this.Escape(chunk) || this.If(chunk) || this.For(chunk) || this.While(chunk) || this.DoWhile(chunk) || this.Func(chunk) || this.Group(chunk) || this.ScriptBlock(chunk) || this.Value(chunk);
if (result != null ? result.success : void 0) {
offset = start + result.offset;
value = new Anchor(result.get());
Expand All @@ -103,6 +105,21 @@ module.exports = Tokenizer = (function() {
return result;
};

Tokenizer.prototype.Comment = function(chunk) {
var error, match, offset, start, value;
if (!(match = COMMENT.exec(chunk))) return NoMatch;
start = match[0].length;
match = chunk.slice(start).match(/\*\@\s*/);
if (!(match != null)) {
offset = start;
error = 'malformed comment';
return failure(offset, error);
}
offset = start + match.index + match[0].length;
value = new Content('');
return success(offset, value);
};

Tokenizer.prototype.Parameters = function(chunk) {
var end, error, match, offset, parameters, start, value;
if (!(match = PARAMETERS.exec(chunk))) return NoMatch;
Expand Down Expand Up @@ -392,7 +409,7 @@ module.exports = Tokenizer = (function() {
error = 'malformed array access';
return failure(offset, error);
}
results = this.replace(chunk.slice(start + 1, (end - 1)), 'function', this.Function.bind(this));
results = this.replace(chunk.slice(start + 1, (end - 1)), 'function', this.Func.bind(this));
offset = start + end;
value = new Access(results);
return success(offset, value);
Expand Down
2 changes: 0 additions & 2 deletions src/tags.coffee
Expand Up @@ -95,8 +95,6 @@ module.exports = {
For
While
DoWhile
AnonFunction
NamedFunction
Func
Parameters
Value
Expand Down
18 changes: 18 additions & 0 deletions src/tokenizer.coffee
Expand Up @@ -42,6 +42,7 @@ IDENTIFIER = /^[$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*/
MEMBER = /^\s*\./
ACCESS = /^\s*\[/
INVOKE = /^\s*\(/
COMMENT = /^\*/


module.exports = class Tokenizer
Expand Down Expand Up @@ -91,6 +92,7 @@ module.exports = class Tokenizer
chunk = chunk[start..]

result =
@Comment(chunk) or
@Parameters(chunk) or
@Escape(chunk) or
@If(chunk) or
Expand All @@ -111,6 +113,22 @@ module.exports = class Tokenizer

return result

Comment: (chunk) ->
return NoMatch unless match = COMMENT.exec chunk

start = match[0].length
match = chunk[start..].match /\*\@\s*/

if not match?
offset = start
error = 'malformed comment'
return failure offset, error

offset = start + match.index + match[0].length
value = new Content('')
return success offset, value


Parameters: (chunk) ->
return NoMatch unless match = PARAMETERS.exec chunk

Expand Down
4 changes: 4 additions & 0 deletions test/hello.txt
@@ -1,3 +1,7 @@
hello world!

@*
* Comment Block!
*@

@render('compose/header')

0 comments on commit 2fdae01

Please sign in to comment.