LaravelTest
264 строки · 7.6 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4/*
5* Author: Constantin Jucovschi (c.jucovschi@jacobs-university.de)
6* Licence: MIT
7*/
8
9(function(mod) {10if (typeof exports == "object" && typeof module == "object") // CommonJS11mod(require("../../lib/codemirror"));12else if (typeof define == "function" && define.amd) // AMD13define(["../../lib/codemirror"], mod);14else // Plain browser env15mod(CodeMirror);16})(function(CodeMirror) {17"use strict";18
19CodeMirror.defineMode("stex", function(_config, parserConfig) {20"use strict";21
22function pushCommand(state, command) {23state.cmdState.push(command);24}25
26function peekCommand(state) {27if (state.cmdState.length > 0) {28return state.cmdState[state.cmdState.length - 1];29} else {30return null;31}32}33
34function popCommand(state) {35var plug = state.cmdState.pop();36if (plug) {37plug.closeBracket();38}39}40
41// returns the non-default plugin closest to the end of the list42function getMostPowerful(state) {43var context = state.cmdState;44for (var i = context.length - 1; i >= 0; i--) {45var plug = context[i];46if (plug.name == "DEFAULT") {47continue;48}49return plug;50}51return { styleIdentifier: function() { return null; } };52}53
54function addPluginPattern(pluginName, cmdStyle, styles) {55return function () {56this.name = pluginName;57this.bracketNo = 0;58this.style = cmdStyle;59this.styles = styles;60this.argument = null; // \begin and \end have arguments that follow. These are stored in the plugin61
62this.styleIdentifier = function() {63return this.styles[this.bracketNo - 1] || null;64};65this.openBracket = function() {66this.bracketNo++;67return "bracket";68};69this.closeBracket = function() {};70};71}72
73var plugins = {};74
75plugins["importmodule"] = addPluginPattern("importmodule", "tag", ["string", "builtin"]);76plugins["documentclass"] = addPluginPattern("documentclass", "tag", ["", "atom"]);77plugins["usepackage"] = addPluginPattern("usepackage", "tag", ["atom"]);78plugins["begin"] = addPluginPattern("begin", "tag", ["atom"]);79plugins["end"] = addPluginPattern("end", "tag", ["atom"]);80
81plugins["label" ] = addPluginPattern("label" , "tag", ["atom"]);82plugins["ref" ] = addPluginPattern("ref" , "tag", ["atom"]);83plugins["eqref" ] = addPluginPattern("eqref" , "tag", ["atom"]);84plugins["cite" ] = addPluginPattern("cite" , "tag", ["atom"]);85plugins["bibitem" ] = addPluginPattern("bibitem" , "tag", ["atom"]);86plugins["Bibitem" ] = addPluginPattern("Bibitem" , "tag", ["atom"]);87plugins["RBibitem" ] = addPluginPattern("RBibitem" , "tag", ["atom"]);88
89plugins["DEFAULT"] = function () {90this.name = "DEFAULT";91this.style = "tag";92
93this.styleIdentifier = this.openBracket = this.closeBracket = function() {};94};95
96function setState(state, f) {97state.f = f;98}99
100// called when in a normal (no environment) context101function normal(source, state) {102var plug;103// Do we look like '\command' ? If so, attempt to apply the plugin 'command'104if (source.match(/^\\[a-zA-Z@]+/)) {105var cmdName = source.current().slice(1);106plug = plugins.hasOwnProperty(cmdName) ? plugins[cmdName] : plugins["DEFAULT"];107plug = new plug();108pushCommand(state, plug);109setState(state, beginParams);110return plug.style;111}112
113// escape characters114if (source.match(/^\\[$&%#{}_]/)) {115return "tag";116}117
118// white space control characters119if (source.match(/^\\[,;!\/\\]/)) {120return "tag";121}122
123// find if we're starting various math modes124if (source.match("\\[")) {125setState(state, function(source, state){ return inMathMode(source, state, "\\]"); });126return "keyword";127}128if (source.match("\\(")) {129setState(state, function(source, state){ return inMathMode(source, state, "\\)"); });130return "keyword";131}132if (source.match("$$")) {133setState(state, function(source, state){ return inMathMode(source, state, "$$"); });134return "keyword";135}136if (source.match("$")) {137setState(state, function(source, state){ return inMathMode(source, state, "$"); });138return "keyword";139}140
141var ch = source.next();142if (ch == "%") {143source.skipToEnd();144return "comment";145} else if (ch == '}' || ch == ']') {146plug = peekCommand(state);147if (plug) {148plug.closeBracket(ch);149setState(state, beginParams);150} else {151return "error";152}153return "bracket";154} else if (ch == '{' || ch == '[') {155plug = plugins["DEFAULT"];156plug = new plug();157pushCommand(state, plug);158return "bracket";159} else if (/\d/.test(ch)) {160source.eatWhile(/[\w.%]/);161return "atom";162} else {163source.eatWhile(/[\w\-_]/);164plug = getMostPowerful(state);165if (plug.name == 'begin') {166plug.argument = source.current();167}168return plug.styleIdentifier();169}170}171
172function inMathMode(source, state, endModeSeq) {173if (source.eatSpace()) {174return null;175}176if (endModeSeq && source.match(endModeSeq)) {177setState(state, normal);178return "keyword";179}180if (source.match(/^\\[a-zA-Z@]+/)) {181return "tag";182}183if (source.match(/^[a-zA-Z]+/)) {184return "variable-2";185}186// escape characters187if (source.match(/^\\[$&%#{}_]/)) {188return "tag";189}190// white space control characters191if (source.match(/^\\[,;!\/]/)) {192return "tag";193}194// special math-mode characters195if (source.match(/^[\^_&]/)) {196return "tag";197}198// non-special characters199if (source.match(/^[+\-<>|=,\/@!*:;'"`~#?]/)) {200return null;201}202if (source.match(/^(\d+\.\d*|\d*\.\d+|\d+)/)) {203return "number";204}205var ch = source.next();206if (ch == "{" || ch == "}" || ch == "[" || ch == "]" || ch == "(" || ch == ")") {207return "bracket";208}209
210if (ch == "%") {211source.skipToEnd();212return "comment";213}214return "error";215}216
217function beginParams(source, state) {218var ch = source.peek(), lastPlug;219if (ch == '{' || ch == '[') {220lastPlug = peekCommand(state);221lastPlug.openBracket(ch);222source.eat(ch);223setState(state, normal);224return "bracket";225}226if (/[ \t\r]/.test(ch)) {227source.eat(ch);228return null;229}230setState(state, normal);231popCommand(state);232
233return normal(source, state);234}235
236return {237startState: function() {238var f = parserConfig.inMathMode ? function(source, state){ return inMathMode(source, state); } : normal;239return {240cmdState: [],241f: f242};243},244copyState: function(s) {245return {246cmdState: s.cmdState.slice(),247f: s.f248};249},250token: function(stream, state) {251return state.f(stream, state);252},253blankLine: function(state) {254state.f = normal;255state.cmdState.length = 0;256},257lineComment: "%"258};259});260
261CodeMirror.defineMIME("text/x-stex", "stex");262CodeMirror.defineMIME("text/x-latex", "stex");263
264});265