Skip to content

Commit

Permalink
- WIP #55 Remove ParserNodeRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Mar 3, 2013
1 parent fae60bc commit 8671ac7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
34 changes: 15 additions & 19 deletions lib/lang/DefaultTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,21 @@ var ParserNodeAutoescape = (function (_super) {
return ParserNodeAutoescape;
})(ParserNode.ParserNodeStatement);
exports.ParserNodeAutoescape = ParserNodeAutoescape;
var ParserNodeStatementFilter = (function (_super) {
__extends(ParserNodeStatementFilter, _super);
function ParserNodeStatementFilter(inner) {
var ParserNodeExpressionFilter = (function (_super) {
__extends(ParserNodeExpressionFilter, _super);
function ParserNodeExpressionFilter(inner) {
_super.call(this);
this.inner = inner;
this.filters = [];
}
ParserNodeStatementFilter.prototype.addFilter = function (filterName, filterParameters) {
ParserNodeExpressionFilter.prototype.addFilter = function (filterName, filterParameters) {
this.filters.push({
name: filterName,
parameters: filterParameters
});
};
ParserNodeStatementFilter.prototype.generateCode = function () {
ParserNodeExpressionFilter.prototype.generateCode = function () {
var out = '';
out += 'runtimeContext.write(';
this.filters.reverse().forEach(function (filter) {
out += 'runtimeContext.filter(' + JSON.stringify(filter.name) + ', [';
});
Expand All @@ -88,12 +87,11 @@ var ParserNodeStatementFilter = (function (_super) {
}
out += '])';
});
out += ');';
return out;
};
return ParserNodeStatementFilter;
})(ParserNode.ParserNodeStatement);
exports.ParserNodeStatementFilter = ParserNodeStatementFilter;
return ParserNodeExpressionFilter;
})(ParserNode.ParserNodeExpression);
exports.ParserNodeExpressionFilter = ParserNodeExpressionFilter;
var ParserNodeScopeSet = (function (_super) {
__extends(ParserNodeScopeSet, _super);
function ParserNodeScopeSet(key, value) {
Expand Down Expand Up @@ -259,7 +257,7 @@ var DefaultTags = (function () {
}, function (node) {
innerNode.add(node);
});
var filterNode = new ParserNodeStatementFilter(innerNode);
var filterNode = new ParserNodeExpressionFilter(innerNode);
var expressionParser = new ExpressionParser.ExpressionParser(expressionTokenReader, tokenParserContext);
while(true) {
var filterName = (expressionParser.parseIdentifier()).value;
Expand All @@ -280,7 +278,7 @@ var DefaultTags = (function () {
}
}
checkNoMoreTokens(expressionTokenReader);
return filterNode;
return new ParserNode.ParserNodeStatementExpression(new ParserNode.ParserNodeOutputNodeExpression(filterNode));
};
DefaultTags.flush = function flush(blockType, templateParser, tokenParserContext, templateTokenReader, expressionTokenReader) {
};
Expand Down Expand Up @@ -481,11 +479,11 @@ var DefaultTags = (function () {
}, function (node) {
innerNode.add(node);
});
return new ParserNode.ParserNodeContainer([
new ParserNode.ParserNodeRaw('runtimeContext.write(runtimeContext.filter("spaceless", [runtimeContext.captureOutput(function() { '),
return new ParserNode.ParserNodeStatementExpression(new ParserNode.ParserNodeOutputNodeExpression(new ParserNode.ParserNodeContainer([
new ParserNode.ParserNodeRaw('runtimeContext.filter("spaceless", [runtimeContext.captureOutput(function() { '),
innerNode,
new ParserNode.ParserNodeRaw('})]));')
]);
new ParserNode.ParserNodeRaw('})])')
])));
};
DefaultTags.$else = _flowexception;
DefaultTags.$elseif = _flowexception;
Expand Down Expand Up @@ -527,9 +525,7 @@ var DefaultTags = (function () {
if(expressionTokenReader.hasMore()) {
var expressionNode = (new ExpressionParser.ExpressionParser(expressionTokenReader, tokenParserContext)).parseExpression();
checkNoMoreTokens(expressionTokenReader);
innerNode.add(new ParserNode.ParserNodeRaw('return runtimeContext.write('));
innerNode.add(expressionNode);
innerNode.add(new ParserNode.ParserNodeRaw(');'));
innerNode.add(new ParserNode.ParserNodeReturnStatement(new ParserNode.ParserNodeWriteExpression(expressionNode)));
} else {
handleOpenedTag(blockType, templateParser, tokenParserContext, templateTokenReader, expressionTokenReader, {
'endblock': function (e) {
Expand Down
25 changes: 8 additions & 17 deletions lib/lang/DefaultTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ParserNodeAutoescape extends ParserNode.ParserNodeStatement {
}
}

export class ParserNodeStatementFilter extends ParserNode.ParserNodeStatement {
export class ParserNodeExpressionFilter extends ParserNode.ParserNodeExpression {
filters = <{ name: string; parameters: ParserNode.ParserNodeCommaExpression; }[]>[];

constructor(public inner: ParserNode.ParserNode) {
Expand All @@ -71,7 +71,6 @@ export class ParserNodeStatementFilter extends ParserNode.ParserNodeStatement {
generateCode() {
var out = '';

out += 'runtimeContext.write(';
this.filters.reverse().forEach((filter) => {
out += 'runtimeContext.filter(' + JSON.stringify(filter.name) + ', [';
});
Expand All @@ -88,8 +87,6 @@ export class ParserNodeStatementFilter extends ParserNode.ParserNodeStatement {
out += '])';
});

out += ');';

return out
}
}
Expand Down Expand Up @@ -256,7 +253,7 @@ export class DefaultTags {
innerNode.add(node);
});

var filterNode = new ParserNodeStatementFilter(innerNode);
var filterNode = new ParserNodeExpressionFilter(innerNode);

var expressionParser = new ExpressionParser.ExpressionParser(expressionTokenReader, tokenParserContext);
while (true) {
Expand All @@ -280,7 +277,7 @@ export class DefaultTags {

checkNoMoreTokens(expressionTokenReader);

return filterNode;
return new ParserNode.ParserNodeStatementExpression(new ParserNode.ParserNodeOutputNodeExpression(filterNode));
}

// FLUSH
Expand Down Expand Up @@ -506,11 +503,11 @@ export class DefaultTags {
});
//console.log('************************');

return new ParserNode.ParserNodeContainer([
new ParserNode.ParserNodeRaw('runtimeContext.write(runtimeContext.filter("spaceless", [runtimeContext.captureOutput(function() { '),
return new ParserNode.ParserNodeStatementExpression(new ParserNode.ParserNodeOutputNodeExpression(new ParserNode.ParserNodeContainer([
new ParserNode.ParserNodeRaw('runtimeContext.filter("spaceless", [runtimeContext.captureOutput(function() { '),
innerNode,
new ParserNode.ParserNodeRaw('})]));')
]);
new ParserNode.ParserNodeRaw('})])')
])));
}

// IF/ELSEIF/ELSE/ENDIF
Expand All @@ -528,10 +525,6 @@ export class DefaultTags {

parserNodeIf.addCaseCondition(expressionNode);

//tokenParserContext.write('if (' + expressionNode.generateCode() + ') {');

//parseExpressionExpressionSync

handleOpenedTag(blockType, templateParser, tokenParserContext, templateTokenReader, expressionTokenReader, {
'elseif': (e) => {
if (didElse) throw (new Error("Can't put 'elseif' after the 'else'"));
Expand Down Expand Up @@ -566,9 +559,7 @@ export class DefaultTags {
var expressionNode = (new ExpressionParser.ExpressionParser(expressionTokenReader, tokenParserContext)).parseExpression();
checkNoMoreTokens(expressionTokenReader);

innerNode.add(new ParserNode.ParserNodeRaw('return runtimeContext.write('));
innerNode.add(expressionNode);
innerNode.add(new ParserNode.ParserNodeRaw(');'));
innerNode.add(new ParserNode.ParserNodeReturnStatement(new ParserNode.ParserNodeWriteExpression(expressionNode)));
} else {

handleOpenedTag(blockType, templateParser, tokenParserContext, templateTokenReader, expressionTokenReader, {
Expand Down
26 changes: 26 additions & 0 deletions lib/parser/ParserNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,29 @@ var ParserNodeOutputText = (function (_super) {
return ParserNodeOutputText;
})(ParserNode);
exports.ParserNodeOutputText = ParserNodeOutputText;
var ParserNodeOutputNodeExpression = (function (_super) {
__extends(ParserNodeOutputNodeExpression, _super);
function ParserNodeOutputNodeExpression(expression) {
_super.call(this);
this.expression = expression;
this.type = 'ParserNodeOutputNodeExpression';
}
ParserNodeOutputNodeExpression.prototype.generateCode = function () {
return 'runtimeContext.write(' + this.expression.generateCode() + ')';
};
return ParserNodeOutputNodeExpression;
})(ParserNodeExpression);
exports.ParserNodeOutputNodeExpression = ParserNodeOutputNodeExpression;
var ParserNodeReturnStatement = (function (_super) {
__extends(ParserNodeReturnStatement, _super);
function ParserNodeReturnStatement(expression) {
_super.call(this);
this.expression = expression;
this.type = 'ParserNodeReturnStatement';
}
ParserNodeReturnStatement.prototype.generateCode = function () {
return 'return ' + this.expression.generateCode() + ';';
};
return ParserNodeReturnStatement;
})(ParserNodeStatement);
exports.ParserNodeReturnStatement = ParserNodeReturnStatement;
24 changes: 24 additions & 0 deletions lib/parser/ParserNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,5 +388,29 @@ export class ParserNodeOutputText extends ParserNode {
}
}

export class ParserNodeOutputNodeExpression extends ParserNodeExpression {
type: string = 'ParserNodeOutputNodeExpression';

constructor(public expression: ParserNode) {
super();
}

generateCode() {
return 'runtimeContext.write(' + this.expression.generateCode() + ')';
}
}

export class ParserNodeReturnStatement extends ParserNodeStatement {
type: string = 'ParserNodeReturnStatement';

constructor(public expression: ParserNodeExpression) {
super();
}

generateCode() {
return 'return ' + this.expression.generateCode() + ';';
}
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

0 comments on commit 8671ac7

Please sign in to comment.