diff options
author | Stanley Stuart <stanley@stan.li> | 2013-12-28 18:55:36 -0600 |
---|---|---|
committer | Stanley Stuart <stanley@stan.li> | 2013-12-28 18:55:36 -0600 |
commit | e1878050b5e0148947054275b14773cfc054e535 (patch) | |
tree | 769c4f821b441a868d174c0a8a4c02f09ee0afc7 /lib/handlebars/compiler/ast.js | |
parent | 1c0614bd88b835b7cdfe1fb7f39650c18f645763 (diff) | |
download | handlebars.js-e1878050b5e0148947054275b14773cfc054e535.zip handlebars.js-e1878050b5e0148947054275b14773cfc054e535.tar.gz handlebars.js-e1878050b5e0148947054275b14773cfc054e535.tar.bz2 |
add line numbers to nodes when parsing
closes #691
Diffstat (limited to 'lib/handlebars/compiler/ast.js')
-rw-r--r-- | lib/handlebars/compiler/ast.js | 70 |
1 files changed, 55 insertions, 15 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index 64cd3b7..88d64e3 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -1,20 +1,48 @@ import Exception from "../exception"; +function LocationInfo(locInfo){ + locInfo = locInfo || {}; + this.firstLine = locInfo.first_line; + this.firstColumn = locInfo.first_column; + this.lastColumn = locInfo.last_column; + this.lastLine = locInfo.last_line; +} + var AST = { - ProgramNode: function(statements, inverseStrip, inverse) { + ProgramNode: function(statements, inverseStrip, inverse, locInfo) { + var inverseLocationInfo, firstInverseNode; + if (arguments.length === 3) { + inverse = null; + locInfo = arguments[arguments.length - 1]; + } else if (arguments.length === 2 ) { + locInfo = arguments[1]; + } + LocationInfo.call(this, locInfo); this.type = "program"; this.statements = statements; this.strip = {}; if(inverse) { - this.inverse = new AST.ProgramNode(inverse, inverseStrip); + firstInverseNode = inverse[0]; + if (firstInverseNode) { + inverseLocationInfo = { + first_line: firstInverseNode.firstLine, + last_line: firstInverseNode.lastLine, + last_column: firstInverseNode.lastColumn, + first_column: firstInverseNode.firstColumn + }; + this.inverse = new AST.ProgramNode(inverse, inverseStrip, inverseLocationInfo); + } else { + this.inverse = new AST.ProgramNode(inverse, inverseStrip); + } this.strip.right = inverseStrip.left; } else if (inverseStrip) { this.strip.left = inverseStrip.right; } }, - MustacheNode: function(rawParams, hash, open, strip) { + MustacheNode: function(rawParams, hash, open, strip, locInfo) { + LocationInfo.call(this, locInfo); this.type = "mustache"; this.hash = hash; this.strip = strip; @@ -45,19 +73,22 @@ var AST = { // pass or at runtime. }, - PartialNode: function(partialName, context, strip) { + PartialNode: function(partialName, context, strip, locInfo) { + LocationInfo.call(this, locInfo); this.type = "partial"; this.partialName = partialName; this.context = context; this.strip = strip; }, - BlockNode: function(mustache, program, inverse, close) { + BlockNode: function(mustache, program, inverse, close, locInfo) { if(mustache.id.original !== close.path.original) { throw new Exception(mustache.id.original + " doesn't match " + close.path.original); } - this.type = "block"; + LocationInfo.call(this, locInfo); + + this.type = 'block'; this.mustache = mustache; this.program = program; this.inverse = inverse; @@ -75,17 +106,20 @@ var AST = { } }, - ContentNode: function(string) { + ContentNode: function(string, locInfo) { + LocationInfo.call(this, locInfo); this.type = "content"; this.string = string; }, - HashNode: function(pairs) { + HashNode: function(pairs, locInfo) { + LocationInfo.call(this, locInfo); this.type = "hash"; this.pairs = pairs; }, - IdNode: function(parts) { + IdNode: function(parts, locInfo) { + LocationInfo.call(this, locInfo); this.type = "ID"; var original = "", @@ -116,37 +150,43 @@ var AST = { this.stringModeValue = this.string; }, - PartialNameNode: function(name) { + PartialNameNode: function(name, locInfo) { + LocationInfo.call(this, locInfo); this.type = "PARTIAL_NAME"; this.name = name.original; }, - DataNode: function(id) { + DataNode: function(id, locInfo) { + LocationInfo.call(this, locInfo); this.type = "DATA"; this.id = id; }, - StringNode: function(string) { + StringNode: function(string, locInfo) { + LocationInfo.call(this, locInfo); this.type = "STRING"; this.original = this.string = this.stringModeValue = string; }, - IntegerNode: function(integer) { + IntegerNode: function(integer, locInfo) { + LocationInfo.call(this, locInfo); this.type = "INTEGER"; this.original = this.integer = integer; this.stringModeValue = Number(integer); }, - BooleanNode: function(bool) { + BooleanNode: function(bool, locInfo) { + LocationInfo.call(this, locInfo); this.type = "BOOLEAN"; this.bool = bool; this.stringModeValue = bool === "true"; }, - CommentNode: function(comment) { + CommentNode: function(comment, locInfo) { + LocationInfo.call(this, locInfo); this.type = "comment"; this.comment = comment; } |