Skip to content

Commit

Permalink
0.2.0: syntax for access loop variables ({{#array el}})
Browse files Browse the repository at this point in the history
  • Loading branch information
zemlanin committed Nov 4, 2015
1 parent 09d776d commit 1ec7874
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 58 deletions.
178 changes: 148 additions & 30 deletions dist/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,21 @@
var TreeNode16 = function(text, offset, elements) {
TreeNode.apply(this, arguments);
this['var_name'] = elements[2];
this['dot_var_name'] = elements[2];
};
inherit(TreeNode16, TreeNode);

var TreeNode17 = function(text, offset, elements) {
TreeNode.apply(this, arguments);
this['var_name'] = elements[2];
this['dot_var_name'] = elements[2];
};
inherit(TreeNode17, TreeNode);

var TreeNode18 = function(text, offset, elements) {
TreeNode.apply(this, arguments);
this['var_name'] = elements[2];
this['dot_var_name'] = elements[2];
};
inherit(TreeNode18, TreeNode);

Expand All @@ -177,12 +180,15 @@
var TreeNode20 = function(text, offset, elements) {
TreeNode.apply(this, arguments);
this['var_name'] = elements[2];
this['dot_var_name'] = elements[2];
this['inner_name'] = elements[4];
};
inherit(TreeNode20, TreeNode);

var TreeNode21 = function(text, offset, elements) {
TreeNode.apply(this, arguments);
this['var_name'] = elements[2];
this['dot_var_name'] = elements[2];
};
inherit(TreeNode21, TreeNode);

Expand All @@ -196,6 +202,7 @@
var TreeNode23 = function(text, offset, elements) {
TreeNode.apply(this, arguments);
this['var_name'] = elements[2];
this['dot_var_name'] = elements[2];
};
inherit(TreeNode23, TreeNode);

Expand Down Expand Up @@ -747,7 +754,7 @@
address3 = this._read_sq_string_literal();
if (address3 === FAILURE) {
this._offset = index3;
address3 = this._read_var_name();
address3 = this._read_dot_var_name();
if (address3 === FAILURE) {
this._offset = index3;
address3 = this._read_mustache_node();
Expand Down Expand Up @@ -1115,7 +1122,7 @@
elements0[0] = address1;
var address2 = FAILURE;
var index2 = this._offset;
address2 = this._read_var_name();
address2 = this._read_dot_var_name();
if (address2 === FAILURE) {
address2 = new TreeNode(this._input.substring(index2, index2), index2);
this._offset = index2;
Expand Down Expand Up @@ -1163,7 +1170,7 @@
elements0[0] = address1;
var address2 = FAILURE;
var index2 = this._offset;
address2 = this._read_var_name();
address2 = this._read_dot_var_name();
if (address2 === FAILURE) {
address2 = new TreeNode(this._input.substring(index2, index2), index2);
this._offset = index2;
Expand Down Expand Up @@ -1443,7 +1450,7 @@
if (address2 !== FAILURE) {
elements0[1] = address2;
var address3 = FAILURE;
address3 = this._read_var_name();
address3 = this._read_dot_var_name();
if (address3 !== FAILURE) {
elements0[2] = address3;
var address4 = FAILURE;
Expand Down Expand Up @@ -1558,7 +1565,7 @@
if (address2 !== FAILURE) {
elements0[1] = address2;
var address3 = FAILURE;
address3 = this._read_var_name();
address3 = this._read_dot_var_name();
if (address3 !== FAILURE) {
elements0[2] = address3;
var address4 = FAILURE;
Expand Down Expand Up @@ -1649,7 +1656,7 @@
if (address7 !== FAILURE) {
elements1[1] = address7;
var address8 = FAILURE;
address8 = this._read_var_name();
address8 = this._read_dot_var_name();
if (address8 !== FAILURE) {
elements1[2] = address8;
var address9 = FAILURE;
Expand Down Expand Up @@ -1792,7 +1799,7 @@
this._offset = cached[1];
return cached[0];
}
var index1 = this._offset, elements0 = new Array(6);
var index1 = this._offset, elements0 = new Array(8);
var address1 = FAILURE;
var chunk0 = null;
if (this._offset < this._inputSize) {
Expand Down Expand Up @@ -1823,7 +1830,7 @@
if (address2 !== FAILURE) {
elements0[1] = address2;
var address3 = FAILURE;
address3 = this._read_var_name();
address3 = this._read_dot_var_name();
if (address3 !== FAILURE) {
elements0[2] = address3;
var address4 = FAILURE;
Expand All @@ -1836,34 +1843,60 @@
if (address4 !== FAILURE) {
elements0[3] = address4;
var address5 = FAILURE;
var chunk1 = null;
if (this._offset < this._inputSize) {
chunk1 = this._input.substring(this._offset, this._offset + 2);
}
if (chunk1 === '}}') {
address5 = new TreeNode(this._input.substring(this._offset, this._offset + 2), this._offset);
this._offset = this._offset + 2;
} else {
address5 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('"}}"');
}
var index4 = this._offset;
address5 = this._read_var_name();
if (address5 === FAILURE) {
address5 = new TreeNode(this._input.substring(index4, index4), index4);
this._offset = index4;
}
if (address5 !== FAILURE) {
elements0[4] = address5;
var address6 = FAILURE;
var index4 = this._offset;
var index5 = this._offset;
address6 = this._read_whitespace();
if (address6 === FAILURE) {
address6 = new TreeNode(this._input.substring(index4, index4), index4);
this._offset = index4;
address6 = new TreeNode(this._input.substring(index5, index5), index5);
this._offset = index5;
}
if (address6 !== FAILURE) {
elements0[5] = address6;
var address7 = FAILURE;
var chunk1 = null;
if (this._offset < this._inputSize) {
chunk1 = this._input.substring(this._offset, this._offset + 2);
}
if (chunk1 === '}}') {
address7 = new TreeNode(this._input.substring(this._offset, this._offset + 2), this._offset);
this._offset = this._offset + 2;
} else {
address7 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('"}}"');
}
}
if (address7 !== FAILURE) {
elements0[6] = address7;
var address8 = FAILURE;
var index6 = this._offset;
address8 = this._read_whitespace();
if (address8 === FAILURE) {
address8 = new TreeNode(this._input.substring(index6, index6), index6);
this._offset = index6;
}
if (address8 !== FAILURE) {
elements0[7] = address8;
} else {
elements0 = null;
this._offset = index1;
}
} else {
elements0 = null;
this._offset = index1;
}
} else {
elements0 = null;
this._offset = index1;
Expand Down Expand Up @@ -1937,7 +1970,7 @@
if (address2 !== FAILURE) {
elements0[1] = address2;
var address3 = FAILURE;
address3 = this._read_var_name();
address3 = this._read_dot_var_name();
if (address3 !== FAILURE) {
elements0[2] = address3;
var address4 = FAILURE;
Expand Down Expand Up @@ -2107,7 +2140,7 @@
if (address2 !== FAILURE) {
elements0[1] = address2;
var address3 = FAILURE;
address3 = this._read_var_name();
address3 = this._read_dot_var_name();
if (address3 !== FAILURE) {
elements0[2] = address3;
var address4 = FAILURE;
Expand Down Expand Up @@ -2455,7 +2488,7 @@
var address2 = FAILURE;
var remaining0 = 0, index3 = this._offset, elements1 = [], address3 = true;
while (address3 !== FAILURE) {
address3 = this._read_dot_alphanum();
address3 = this._read_alphanum();
if (address3 !== FAILURE) {
elements1.push(address3);
--remaining0;
Expand Down Expand Up @@ -2487,6 +2520,62 @@
return address0;
},

_read_dot_var_name: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._dot_var_name = this._cache._dot_var_name || {};
var cached = this._cache._dot_var_name[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var index1 = this._offset, elements0 = new Array(2);
var address1 = FAILURE;
var index2 = this._offset;
address1 = this._read_letter();
this._offset = index2;
if (address1 !== FAILURE) {
address1 = new TreeNode(this._input.substring(this._offset, this._offset), this._offset);
this._offset = this._offset;
} else {
address1 = FAILURE;
}
if (address1 !== FAILURE) {
elements0[0] = address1;
var address2 = FAILURE;
var remaining0 = 0, index3 = this._offset, elements1 = [], address3 = true;
while (address3 !== FAILURE) {
address3 = this._read_dot_alphanum();
if (address3 !== FAILURE) {
elements1.push(address3);
--remaining0;
}
}
if (remaining0 <= 0) {
address2 = new TreeNode(this._input.substring(index3, this._offset), index3, elements1);
this._offset = this._offset;
} else {
address2 = FAILURE;
}
if (address2 !== FAILURE) {
elements0[1] = address2;
} else {
elements0 = null;
this._offset = index1;
}
} else {
elements0 = null;
this._offset = index1;
}
if (elements0 === null) {
address0 = FAILURE;
} else {
address0 = new TreeNode(this._input.substring(index1, this._offset), index1, elements0);
this._offset = this._offset;
}
this._cache._dot_var_name[index0] = [address0, this._offset];
return address0;
},

_read_dashed_var_name: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._dashed_var_name = this._cache._dashed_var_name || {};
Expand Down Expand Up @@ -2572,6 +2661,35 @@
return address0;
},

_read_alphanum: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._alphanum = this._cache._alphanum || {};
var cached = this._cache._alphanum[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var chunk0 = null;
if (this._offset < this._inputSize) {
chunk0 = this._input.substring(this._offset, this._offset + 1);
}
if (chunk0 !== null && /^[_a-zA-Z0-9]/.test(chunk0)) {
address0 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset);
this._offset = this._offset + 1;
} else {
address0 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('[_a-zA-Z0-9]');
}
}
this._cache._alphanum[index0] = [address0, this._offset];
return address0;
},

_read_dot_alphanum: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._dot_alphanum = this._cache._dot_alphanum || {};
Expand Down
18 changes: 12 additions & 6 deletions dist/schwartzman.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,29 @@ function compileMustache(nodesTree) {
var varName;
var children;
var compiledChildren;
var innerName;

if (nodesTree.variable_node) {
varName = nodesTree.variable_node.var_name.text;
code = context.varName + '.' + varName;
if (varName.indexOf('.') !== -1 && context.innerName && varName.indexOf(context.innerName + '.') === 0) {
code = varName;
} else {
code = context.varName + '.' + varName;
}
} else if (nodesTree.section_node) {
varName = nodesTree.section_node.var_name;
innerName = nodesTree.section_node.inner_name;
children = nodesTree.section_node.expr_node.elements;
// TODO: keys for children
// TODO: wrap text nodes in span
if (children && children.length) {
compiledChildren = children.map(function (n, index) {
return compileAny(n, { varName: varName }).code;
return compileAny(n, { varName: context.varName, innerName: innerName }).code;
});
if (children.length === 1) {
code = 'section(' + context.varName + ', "' + varName + '", function(' + varName + '){ return (' + compiledChildren + ') })';
code = innerName ? 'section(' + context.varName + ', "' + varName + '", function(' + innerName + '){ return (' + compiledChildren + ') })' : 'section(' + context.varName + ', "' + varName + '", function(){ return (' + compiledChildren + ') })';
} else {
code = 'section(' + context.varName + ', "' + varName + '", function(' + varName + '){ return [' + compiledChildren + '] })';
code = innerName ? 'section(' + context.varName + ', "' + varName + '", function(' + innerName + '){ return [' + compiledChildren + '] })' : 'section(' + context.varName + ', "' + varName + '", function(){ return [' + compiledChildren + '] })';
}
}
} else if (nodesTree.inverted_section_node) {
Expand Down Expand Up @@ -236,10 +242,10 @@ var actions = {
var expr_node = _ref42[1];
var close = _ref42[2];

if (open.var_name.text != close.var_name.text) {
if (open.dot_var_name.text != close.dot_var_name.text) {
throw new SyntaxError('miss closed tag: ' + open.text.trim() + ' and ' + close.text.trim());
}
return { var_name: open.var_name.text, expr_node: expr_node };
return { var_name: open.dot_var_name.text, inner_name: (open.inner_name || {}).text, expr_node: expr_node };
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/src/try/tmpl.jsx.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<i data-x="test {{name}}"></i>
<!--<i style="width: 200px"></i>-->
<ul>
{{#people}}
<li>{{name}}</li>
{{#people p}}
<li>{{p.name}}</li>
{{/people}}
</ul>
<span>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schwartzman",
"version": "0.1.3",
"version": "0.2.0",
"description": "Webpack loader for Mustache",
"main": "dist/schwartzman.js",
"repository": {
Expand Down
Loading

0 comments on commit 1ec7874

Please sign in to comment.