// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
})(function(CodeMirror) {
CodeMirror.defineMode("smarty", function(config, parserConf) {
var rightDelimiter = parserConf.rightDelimiter || "}";
var leftDelimiter = parserConf.leftDelimiter || "{";
var version = parserConf.version || 2;
var baseMode = CodeMirror.getMode(config, parserConf.baseMode || "null");
var keyFunctions = ["debug", "extends", "function", "include", "literal"];
operatorChars: /[+\-*&%=<>!?]/,
validIdentifier: /[a-zA-Z0-9_]/,
function cont(style, lastType) {
function chain(stream, state, parser) {
return parser(stream, state);
// Smarty 3 allows { and } surrounded by whitespace to NOT slip into Smarty mode
function doesNotCount(stream, pos) {
if (pos == null) pos = stream.pos;
return version === 3 && leftDelimiter == "{" &&
(pos == stream.string.length || /\s/.test(stream.string.charAt(pos)));
function tokenTop(stream, state) {
var string = stream.string;
for (var scan = stream.pos;;) {
var nextMatch = string.indexOf(leftDelimiter, scan);
scan = nextMatch + leftDelimiter.length;
if (nextMatch == -1 || !doesNotCount(stream, nextMatch + leftDelimiter.length)) break;
if (nextMatch == stream.pos) {
stream.match(leftDelimiter);
return chain(stream, state, tokenBlock("comment", "*" + rightDelimiter));
state.tokenize = tokenSmarty;
if (nextMatch > -1) stream.string = string.slice(0, nextMatch);
var token = baseMode.token(stream, state.base);
if (nextMatch > -1) stream.string = string;
// parsing Smarty content
function tokenSmarty(stream, state) {
if (stream.match(rightDelimiter, true)) {
state.tokenize = tokenTop;
state.tokenize = tokenTop;
return cont("tag", null);
if (stream.match(leftDelimiter, true)) {
return cont("tag", "startTag");
stream.eatWhile(regs.validIdentifier);
return cont("variable-2", "variable");
return cont("operator", "pipe");
return cont("operator", "property");
} else if (regs.stringChar.test(ch)) {
state.tokenize = tokenAttribute(ch);
return cont("string", "string");
} else if (regs.operatorChars.test(ch)) {
stream.eatWhile(regs.operatorChars);
return cont("operator", "operator");
} else if (ch == "[" || ch == "]") {
return cont("bracket", "bracket");
} else if (ch == "(" || ch == ")") {
return cont("bracket", "operator");
} else if (/\d/.test(ch)) {
return cont("number", "number");
if (state.last == "variable") {
stream.eatWhile(regs.validIdentifier);
return cont("property", "property");
stream.eatWhile(regs.validIdentifier);
return cont("qualifier", "modifier");
} else if (state.last == "pipe") {
stream.eatWhile(regs.validIdentifier);
return cont("qualifier", "modifier");
} else if (state.last == "whitespace") {
stream.eatWhile(regs.validIdentifier);
return cont("attribute", "modifier");
} if (state.last == "property") {
stream.eatWhile(regs.validIdentifier);
return cont("property", null);
} else if (/\s/.test(ch)) {
while (c = stream.eat(regs.validIdentifier)) {
for (var i=0, j=keyFunctions.length; i<j; i++) {
if (keyFunctions[i] == str) {
return cont("keyword", "keyword");
return cont("tag", "tag");
function tokenAttribute(quote) {
return function(stream, state) {
currChar = stream.peek();
if (stream.next() == quote && prevChar !== '\\') {
state.tokenize = tokenSmarty;
function tokenBlock(style, terminator) {
return function(stream, state) {
if (stream.match(terminator)) {
state.tokenize = tokenTop;
base: CodeMirror.startState(baseMode),
copyState: function(state) {
base: CodeMirror.copyState(baseMode, state.base),
tokenize: state.tokenize,
innerMode: function(state) {
if (state.tokenize == tokenTop)
return {mode: baseMode, state: state.base};
token: function(stream, state) {
var style = state.tokenize(stream, state);
indent: function(state, text) {
if (state.tokenize == tokenTop && baseMode.indent)
return baseMode.indent(state.base, text);
blockCommentStart: leftDelimiter + "*",
blockCommentEnd: "*" + rightDelimiter
CodeMirror.defineMIME("text/x-smarty", "smarty");