feature: Allow hyphens in identifiers, working on #14

This commit is contained in:
harttle
2016-10-24 01:19:58 +08:00
parent fbcd244946
commit 1cd30733dd
6 changed files with 79 additions and 20 deletions
+10 -4
View File
@@ -4,13 +4,14 @@ var doubleQuoted = /"[^"]*"/;
var quoted = new RegExp(`${singleQuoted.source}|${doubleQuoted.source}`);
var quoteBalanced = new RegExp(`(?:${quoted.source}|[^'"])*`);
// values
// basic types
var integer = /-?\d+/;
var number = /-?\d+\.?\d*|\.?\d+/;
var bool = /true|false/;
// peoperty access
var identifier = /[\w-]+/;
var subscript = new RegExp(`\\[(?:${quoted.source}|[\\w-\\.]+)\\]`);
var literal = new RegExp(`(?:${quoted.source}|${bool.source}|${number.source})`);
var variable = new RegExp(`${identifier.source}(?:\\.${identifier.source}|${subscript.source})*`);
@@ -19,12 +20,13 @@ var rangeLimit = new RegExp(`(?:${variable.source}|${number.source})`);
var range = new RegExp(`\\(${rangeLimit.source}\\.\\.${rangeLimit.source}\\)`);
var rangeCapture = new RegExp(`\\((${rangeLimit.source})\\.\\.(${rangeLimit.source})\\)`);
var value = new RegExp(`(?:${literal.source}|${variable.source}|${range.source})`);
var value = new RegExp(`(?:${variable.source}|${literal.source}|${range.source})`);
// hash related
var hash = new RegExp(`(?:${identifier.source})\\s*:\\s*(?:${value.source})`);
var hashCapture = new RegExp(`(${identifier.source})\\s*:\\s*(${value.source})`, 'g');
// full match
var tagLine = new RegExp(`^\\s*(${identifier.source})\\s*(.*)\\s*$`);
var literalLine = new RegExp(`^${literal.source}$`, 'i');
var variableLine = new RegExp(`^${variable.source}$`);
@@ -57,6 +59,10 @@ function isVariable(str) {
return variableLine.test(str);
}
function matchValue(str) {
return value.exec(str);
}
function parseLiteral(str) {
var res;
if (res = str.match(numberLine)) {
@@ -76,5 +82,5 @@ module.exports = {
range, rangeCapture,
identifier, value, quoteBalanced, operators,
quotedLine, numberLine, boolLine, rangeLine, literalLine, filterLine, tagLine,
isLiteral, isVariable, parseLiteral, isRange
isLiteral, isVariable, parseLiteral, isRange, matchValue
};
+1 -1
View File
@@ -70,7 +70,7 @@ module.exports = function(Tag, Filter) {
}
function parseOutput(str) {
var match = lexical.value.exec(str);
var match = lexical.matchValue(str);
if(!match) throw new Error(`illegal output string: ${str}`);
var initial = match[0];
+1 -1
View File
@@ -14,7 +14,7 @@ var render = {
// It's fundamentally equivalent to the following...
// emptyPromise.then(renderTag(template0).then(renderTag(template1).then(renderTag(template2)...
var lastPromise = templates.reduce((promise, template) => {
return promise.then((partial) => {
return promise.then(() => {
if (scope.safeGet('forloop.skip')) {
return Promise.resolve('');
}