soy.js
437 lines
| 15.2 KiB
| application/javascript
|
JavascriptLexer
r1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others | |||
r4105 | // Distributed under an MIT license: https://codemirror.net/LICENSE | |||
r1 | ||||
(function(mod) { | ||||
if (typeof exports == "object" && typeof module == "object") // CommonJS | ||||
mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed")); | ||||
else if (typeof define == "function" && define.amd) // AMD | ||||
define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod); | ||||
else // Plain browser env | ||||
mod(CodeMirror); | ||||
})(function(CodeMirror) { | ||||
"use strict"; | ||||
r4105 | var paramData = { noEndTag: true, soyState: "param-def" }; | |||
var tags = { | ||||
"alias": { noEndTag: true }, | ||||
"delpackage": { noEndTag: true }, | ||||
"namespace": { noEndTag: true, soyState: "namespace-def" }, | ||||
"@param": paramData, | ||||
"@param?": paramData, | ||||
"@inject": paramData, | ||||
"@inject?": paramData, | ||||
"@state": paramData, | ||||
"@state?": paramData, | ||||
"template": { soyState: "templ-def", variableScope: true}, | ||||
"literal": { }, | ||||
"msg": {}, | ||||
"fallbackmsg": { noEndTag: true, reduceIndent: true}, | ||||
"select": {}, | ||||
"plural": {}, | ||||
"let": { soyState: "var-def" }, | ||||
"if": {}, | ||||
"elseif": { noEndTag: true, reduceIndent: true}, | ||||
"else": { noEndTag: true, reduceIndent: true}, | ||||
"switch": {}, | ||||
"case": { noEndTag: true, reduceIndent: true}, | ||||
"default": { noEndTag: true, reduceIndent: true}, | ||||
"foreach": { variableScope: true, soyState: "var-def" }, | ||||
"ifempty": { noEndTag: true, reduceIndent: true}, | ||||
"for": { variableScope: true, soyState: "var-def" }, | ||||
"call": { soyState: "templ-ref" }, | ||||
"param": { soyState: "param-ref"}, | ||||
"print": { noEndTag: true }, | ||||
"deltemplate": { soyState: "templ-def", variableScope: true}, | ||||
"delcall": { soyState: "templ-ref" }, | ||||
"log": {}, | ||||
"element": { variableScope: true }, | ||||
}; | ||||
var indentingTags = Object.keys(tags).filter(function(tag) { | ||||
return !tags[tag].noEndTag || tags[tag].reduceIndent; | ||||
}); | ||||
r1 | ||||
CodeMirror.defineMode("soy", function(config) { | ||||
var textMode = CodeMirror.getMode(config, "text/plain"); | ||||
var modes = { | ||||
html: CodeMirror.getMode(config, {name: "text/html", multilineTagIndentFactor: 2, multilineTagIndentPastTag: false}), | ||||
attributes: textMode, | ||||
text: textMode, | ||||
uri: textMode, | ||||
r4105 | trusted_resource_uri: textMode, | |||
r1 | css: CodeMirror.getMode(config, "text/css"), | |||
js: CodeMirror.getMode(config, {name: "text/javascript", statementIndent: 2 * config.indentUnit}) | ||||
}; | ||||
function last(array) { | ||||
return array[array.length - 1]; | ||||
} | ||||
function tokenUntil(stream, state, untilRegExp) { | ||||
r4105 | if (stream.sol()) { | |||
for (var indent = 0; indent < state.indent; indent++) { | ||||
if (!stream.eat(/\s/)) break; | ||||
} | ||||
if (indent) return null; | ||||
} | ||||
r1 | var oldString = stream.string; | |||
var match = untilRegExp.exec(oldString.substr(stream.pos)); | ||||
if (match) { | ||||
// We don't use backUp because it backs up just the position, not the state. | ||||
// This uses an undocumented API. | ||||
stream.string = oldString.substr(0, stream.pos + match.index); | ||||
} | ||||
var result = stream.hideFirstChars(state.indent, function() { | ||||
r4105 | var localState = last(state.localStates); | |||
return localState.mode.token(stream, localState.state); | ||||
r1 | }); | |||
stream.string = oldString; | ||||
return result; | ||||
} | ||||
r4105 | function contains(list, element) { | |||
while (list) { | ||||
if (list.element === element) return true; | ||||
list = list.next; | ||||
} | ||||
return false; | ||||
} | ||||
function prepend(list, element) { | ||||
return { | ||||
element: element, | ||||
next: list | ||||
}; | ||||
} | ||||
function popcontext(state) { | ||||
if (!state.context) return; | ||||
if (state.context.scope) { | ||||
state.variables = state.context.scope; | ||||
} | ||||
state.context = state.context.previousContext; | ||||
} | ||||
// Reference a variable `name` in `list`. | ||||
// Let `loose` be truthy to ignore missing identifiers. | ||||
function ref(list, name, loose) { | ||||
return contains(list, name) ? "variable-2" : (loose ? "variable" : "variable-2 error"); | ||||
} | ||||
// Data for an open soy tag. | ||||
function Context(previousContext, tag, scope) { | ||||
this.previousContext = previousContext; | ||||
this.tag = tag; | ||||
this.kind = null; | ||||
this.scope = scope; | ||||
} | ||||
r1 | return { | |||
startState: function() { | ||||
return { | ||||
soyState: [], | ||||
r4105 | templates: null, | |||
variables: prepend(null, 'ij'), | ||||
scopes: null, | ||||
r1 | indent: 0, | |||
r4105 | quoteKind: null, | |||
context: null, | ||||
localStates: [{ | ||||
mode: modes.html, | ||||
state: CodeMirror.startState(modes.html) | ||||
}] | ||||
r1 | }; | |||
}, | ||||
copyState: function(state) { | ||||
return { | ||||
tag: state.tag, // Last seen Soy tag. | ||||
soyState: state.soyState.concat([]), | ||||
r4105 | templates: state.templates, | |||
variables: state.variables, | ||||
context: state.context, | ||||
r1 | indent: state.indent, // Indentation of the following line. | |||
r4105 | quoteKind: state.quoteKind, | |||
localStates: state.localStates.map(function(localState) { | ||||
return { | ||||
mode: localState.mode, | ||||
state: CodeMirror.copyState(localState.mode, localState.state) | ||||
}; | ||||
}) | ||||
r1 | }; | |||
}, | ||||
token: function(stream, state) { | ||||
var match; | ||||
switch (last(state.soyState)) { | ||||
case "comment": | ||||
if (stream.match(/^.*?\*\//)) { | ||||
state.soyState.pop(); | ||||
} else { | ||||
stream.skipToEnd(); | ||||
} | ||||
r4105 | if (!state.context || !state.context.scope) { | |||
var paramRe = /@param\??\s+(\S+)/g; | ||||
var current = stream.current(); | ||||
for (var match; (match = paramRe.exec(current)); ) { | ||||
state.variables = prepend(state.variables, match[1]); | ||||
} | ||||
} | ||||
r1 | return "comment"; | |||
r4105 | case "string": | |||
var match = stream.match(/^.*?(["']|\\[\s\S])/); | ||||
if (!match) { | ||||
stream.skipToEnd(); | ||||
} else if (match[1] == state.quoteKind) { | ||||
state.quoteKind = null; | ||||
state.soyState.pop(); | ||||
} | ||||
return "string"; | ||||
} | ||||
if (!state.soyState.length || last(state.soyState) != "literal") { | ||||
if (stream.match(/^\/\*/)) { | ||||
state.soyState.push("comment"); | ||||
return "comment"; | ||||
} else if (stream.match(stream.sol() ? /^\s*\/\/.*/ : /^\s+\/\/.*/)) { | ||||
return "comment"; | ||||
} | ||||
} | ||||
switch (last(state.soyState)) { | ||||
case "templ-def": | ||||
if (match = stream.match(/^\.?([\w]+(?!\.[\w]+)*)/)) { | ||||
state.templates = prepend(state.templates, match[1]); | ||||
state.soyState.pop(); | ||||
return "def"; | ||||
} | ||||
stream.next(); | ||||
return null; | ||||
case "templ-ref": | ||||
if (match = stream.match(/(\.?[a-zA-Z_][a-zA-Z_0-9]+)+/)) { | ||||
r1 | state.soyState.pop(); | |||
r4105 | // If the first character is '.', it can only be a local template. | |||
if (match[0][0] == '.') { | ||||
return "variable-2" | ||||
} | ||||
// Otherwise | ||||
return "variable"; | ||||
} | ||||
stream.next(); | ||||
return null; | ||||
case "namespace-def": | ||||
if (match = stream.match(/^\.?([\w\.]+)/)) { | ||||
state.soyState.pop(); | ||||
return "variable"; | ||||
} | ||||
stream.next(); | ||||
return null; | ||||
case "param-def": | ||||
if (match = stream.match(/^\w+/)) { | ||||
state.variables = prepend(state.variables, match[0]); | ||||
state.soyState.pop(); | ||||
state.soyState.push("param-type"); | ||||
return "def"; | ||||
} | ||||
stream.next(); | ||||
return null; | ||||
case "param-ref": | ||||
if (match = stream.match(/^\w+/)) { | ||||
state.soyState.pop(); | ||||
return "property"; | ||||
} | ||||
stream.next(); | ||||
return null; | ||||
case "param-type": | ||||
if (stream.peek() == "}") { | ||||
state.soyState.pop(); | ||||
return null; | ||||
} | ||||
if (stream.eatWhile(/^([\w]+|[?])/)) { | ||||
return "type"; | ||||
} | ||||
stream.next(); | ||||
return null; | ||||
case "var-def": | ||||
if (match = stream.match(/^\$([\w]+)/)) { | ||||
state.variables = prepend(state.variables, match[1]); | ||||
state.soyState.pop(); | ||||
return "def"; | ||||
r1 | } | |||
stream.next(); | ||||
return null; | ||||
case "tag": | ||||
r4105 | var endTag = state.tag[0] == "/"; | |||
var tagName = endTag ? state.tag.substring(1) : state.tag; | ||||
var tag = tags[tagName]; | ||||
r1 | if (stream.match(/^\/?}/)) { | |||
r4105 | var selfClosed = stream.current() == "/}"; | |||
if (selfClosed && !endTag) { | ||||
popcontext(state); | ||||
} | ||||
if (state.tag == "/template" || state.tag == "/deltemplate") { | ||||
state.variables = prepend(null, 'ij'); | ||||
state.indent = 0; | ||||
} else { | ||||
state.indent -= config.indentUnit * | ||||
(selfClosed || indentingTags.indexOf(state.tag) == -1 ? 2 : 1); | ||||
} | ||||
r1 | state.soyState.pop(); | |||
return "keyword"; | ||||
} else if (stream.match(/^([\w?]+)(?==)/)) { | ||||
r4105 | if (state.context && state.context.tag == tagName && stream.current() == "kind" && (match = stream.match(/^="([^"]+)/, false))) { | |||
r1 | var kind = match[1]; | |||
r4105 | state.context.kind = kind; | |||
var mode = modes[kind] || modes.html; | ||||
var localState = last(state.localStates); | ||||
if (localState.mode.indent) { | ||||
state.indent += localState.mode.indent(localState.state, "", ""); | ||||
} | ||||
state.localStates.push({ | ||||
mode: mode, | ||||
state: CodeMirror.startState(mode) | ||||
}); | ||||
r1 | } | |||
return "attribute"; | ||||
r4105 | } else if (match = stream.match(/([\w]+)(?=\()/)) { | |||
return "variable callee"; | ||||
} else if (match = stream.match(/^["']/)) { | ||||
r1 | state.soyState.push("string"); | |||
r4105 | state.quoteKind = match; | |||
r1 | return "string"; | |||
} | ||||
r4105 | if (stream.match(/(null|true|false)(?!\w)/) || | |||
stream.match(/0x([0-9a-fA-F]{2,})/) || | ||||
stream.match(/-?([0-9]*[.])?[0-9]+(e[0-9]*)?/)) { | ||||
return "atom"; | ||||
} | ||||
if (stream.match(/(\||[+\-*\/%]|[=!]=|\?:|[<>]=?)/)) { | ||||
// Tokenize filter, binary, null propagator, and equality operators. | ||||
return "operator"; | ||||
} | ||||
if (match = stream.match(/^\$([\w]+)/)) { | ||||
return ref(state.variables, match[1]); | ||||
} | ||||
if (match = stream.match(/^\w+/)) { | ||||
return /^(?:as|and|or|not|in)$/.test(match[0]) ? "keyword" : null; | ||||
} | ||||
r1 | stream.next(); | |||
return null; | ||||
case "literal": | ||||
if (stream.match(/^(?=\{\/literal})/)) { | ||||
state.indent -= config.indentUnit; | ||||
state.soyState.pop(); | ||||
return this.token(stream, state); | ||||
} | ||||
return tokenUntil(stream, state, /\{\/literal}/); | ||||
} | ||||
r4105 | if (stream.match(/^\{literal}/)) { | |||
r1 | state.indent += config.indentUnit; | |||
state.soyState.push("literal"); | ||||
r4105 | state.context = new Context(state.context, "literal", state.variables); | |||
r1 | return "keyword"; | |||
r4105 | ||||
// A tag-keyword must be followed by whitespace, comment or a closing tag. | ||||
} else if (match = stream.match(/^\{([/@\\]?\w+\??)(?=$|[\s}]|\/[/*])/)) { | ||||
var prevTag = state.tag; | ||||
r1 | state.tag = match[1]; | |||
r4105 | var endTag = state.tag[0] == "/"; | |||
var indentingTag = !!tags[state.tag]; | ||||
var tagName = endTag ? state.tag.substring(1) : state.tag; | ||||
var tag = tags[tagName]; | ||||
if (state.tag != "/switch") | ||||
state.indent += ((endTag || tag && tag.reduceIndent) && prevTag != "switch" ? 1 : 2) * config.indentUnit; | ||||
state.soyState.push("tag"); | ||||
var tagError = false; | ||||
if (tag) { | ||||
if (!endTag) { | ||||
if (tag.soyState) state.soyState.push(tag.soyState); | ||||
} | ||||
// If a new tag, open a new context. | ||||
if (!tag.noEndTag && (indentingTag || !endTag)) { | ||||
state.context = new Context(state.context, state.tag, tag.variableScope ? state.variables : null); | ||||
// Otherwise close the current context. | ||||
} else if (endTag) { | ||||
if (!state.context || state.context.tag != tagName) { | ||||
tagError = true; | ||||
} else if (state.context) { | ||||
if (state.context.kind) { | ||||
state.localStates.pop(); | ||||
var localState = last(state.localStates); | ||||
if (localState.mode.indent) { | ||||
state.indent -= localState.mode.indent(localState.state, "", ""); | ||||
} | ||||
} | ||||
popcontext(state); | ||||
} | ||||
} | ||||
} else if (endTag) { | ||||
// Assume all tags with a closing tag are defined in the config. | ||||
tagError = true; | ||||
r1 | } | |||
r4105 | return (tagError ? "error " : "") + "keyword"; | |||
// Not a tag-keyword; it's an implicit print tag. | ||||
} else if (stream.eat('{')) { | ||||
state.tag = "print"; | ||||
state.indent += 2 * config.indentUnit; | ||||
r1 | state.soyState.push("tag"); | |||
return "keyword"; | ||||
} | ||||
return tokenUntil(stream, state, /\{|\s+\/\/|\/\*/); | ||||
}, | ||||
r4105 | indent: function(state, textAfter, line) { | |||
r1 | var indent = state.indent, top = last(state.soyState); | |||
if (top == "comment") return CodeMirror.Pass; | ||||
if (top == "literal") { | ||||
if (/^\{\/literal}/.test(textAfter)) indent -= config.indentUnit; | ||||
} else { | ||||
if (/^\s*\{\/(template|deltemplate)\b/.test(textAfter)) return 0; | ||||
if (/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(textAfter)) indent -= config.indentUnit; | ||||
if (state.tag != "switch" && /^\{(case|default)\b/.test(textAfter)) indent -= config.indentUnit; | ||||
if (/^\{\/switch\b/.test(textAfter)) indent -= config.indentUnit; | ||||
} | ||||
r4105 | var localState = last(state.localStates); | |||
if (indent && localState.mode.indent) { | ||||
indent += localState.mode.indent(localState.state, textAfter, line); | ||||
} | ||||
r1 | return indent; | |||
}, | ||||
innerMode: function(state) { | ||||
if (state.soyState.length && last(state.soyState) != "literal") return null; | ||||
r4105 | else return last(state.localStates); | |||
r1 | }, | |||
electricInput: /^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/, | ||||
lineComment: "//", | ||||
blockCommentStart: "/*", | ||||
blockCommentEnd: "*/", | ||||
blockCommentContinue: " * ", | ||||
r4105 | useInnerComments: false, | |||
r1 | fold: "indent" | |||
}; | ||||
}, "htmlmixed"); | ||||
r4105 | CodeMirror.registerHelper("wordChars", "soy", /[\w$]/); | |||
CodeMirror.registerHelper("hintWords", "soy", Object.keys(tags).concat( | ||||
["css", "debugger"])); | ||||
r1 | ||||
CodeMirror.defineMIME("text/x-soy", "soy"); | ||||
}); | ||||