{
function filterEmptyStrings(array) {
var result = [], i;
for (i = 0; i < array.length; i++) {
if (array[i] !== "") {
result.push(array[i]);
}
}
return result;
}
function extractOptional(optional, index) {
return optional ? optional[index] : null;
}
function extractList(list, index) {
var result = new Array(list.length), i;
for (i = 0; i < list.length; i++) {
result[i] = list[i][index];
}
return result;
}
function buildList(first, rest, index) {
return [first].concat(extractList(rest, index));
}
}
Template
= html:HTML? {
return {
type: 'Template',
body: html || []
};
}
HTML
= (
COMMENT
/ TEMPLATE_ELEMENT
/ CUSTOM_ELEMENT
/ CONTENT_ELEMENT
/ INCLUDE_ELEMENT
/ OUTPUT_STATEMENT
/ SCRIPT_ELEMENT
/ RAW
)+
RAW "Raw Content"
= body:$(!(
TEMPLATE_ELEMENT
/ CUSTOM_ELEMENT
/ CONTENT_ELEMENT
/ INCLUDE_ELEMENT
/ OUTPUT_STATEMENT
/ SCRIPT_ELEMENT
/ ""
/ ""
/ ""
/ ""
/ CustomElementEnder
) .)+ {
return {
type: 'OutputStatement',
raw: true,
expression: {
type: 'Literal',
value: body,
raw: JSON.stringify(body)
}
};
}
COMMENT
= c:$(Comment) {
return {
type: 'OutputStatement',
raw: true,
expression: {
type: 'Literal',
value: c,
raw: JSON.stringify(c)
}
};
}
TEMPLATE_ELEMENT ""
= "" _ body:HTML? _ "" {
attrs = attrs || {};
var obj;
if (attrs.bind) {
obj = {
type: 'BindStatement',
expression: attrs.bind.expression,
body: body
}
}
else if (attrs.repeat) {
obj = {
type: 'RepeatStatement',
expression: attrs.repeat.expression,
body: body
};
}
else {
obj = {
type: 'BlockStatement',
body: body
};
}
if (attrs.if) {
obj = {
type: 'IfStatement',
test: attrs.if.expression,
consequent: obj,
alternate: null
};
}
if (obj.type === 'BlockStatement') {
obj.attributes = attrs;
}
return obj;
}
/ body:MUTE_TEMPLATE {
return {
type: 'OutputStatement',
expression: {
type: 'Literal',
value: body,
raw: JSON.stringify(body)
}
};
}
MUTE_TEMPLATE "Mute Template"
= $("" MUTE_TEMPLATE_CONTENT? "")
MUTE_TEMPLATE_CONTENT "Mute Template Content"
= $(MUTE_TEMPLATE / (!("" / "" {
return {
type: 'CustomElement',
name: tag,
attributes: attrs || {},
selfClosing: true,
body: []
};
}
/ "<" tag:CustomElementName attrs:ATTRIBUTES? _ ">" _ body:HTML? _ "" ender:CustomElementName ">" &{return tag === ender && options.elements && options.elements[tag]; } {
return {
type: 'CustomElement',
name: tag,
attributes: attrs || {},
selfClosing: false,
body: body || []
};
}
CustomElementName
= $(CustomElementStart CustomElementPart+)
CustomElementStart
= $([a-z0-9]+)
CustomElementPart
= $("-" [a-z0-9]+)
CustomElementEnder
= "" tag:CustomElementName ">" &{ return options.elements && options.elements[tag]; } {
return "" + tag + ">";
}
CONTENT_ELEMENT ""
= "" body:HTML? "" {
return {
type: 'ContentStatement',
attributes: attrs || {},
body: body || []
};
}
INCLUDE_ELEMENT ""
= "" body:HTML? "" {
return {
type: 'IncludeStatement',
attributes: attrs || {},
body: body || []
};
}
SCRIPT_ELEMENT "") {
var statement = {
type: 'OutputStatement',
raw: true,
expression: {}
};
if (attrs) {
var parsed = parse(attrs);
if (parsed.body.length > 1) {
parsed.type = 'BlockStatement';
parsed.body.unshift({
type: 'OutputStatement',
raw: true,
expression: {
type: 'Literal',
value: '