LaravelTest
245 строк · 6.8 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// Modelica support for CodeMirror, copyright (c) by Lennart Ochel
5
6(function(mod) {7if (typeof exports == "object" && typeof module == "object") // CommonJS8mod(require("../../lib/codemirror"));9else if (typeof define == "function" && define.amd) // AMD10define(["../../lib/codemirror"], mod);11else // Plain browser env12mod(CodeMirror);13})14
15(function(CodeMirror) {16"use strict";17
18CodeMirror.defineMode("modelica", function(config, parserConfig) {19
20var indentUnit = config.indentUnit;21var keywords = parserConfig.keywords || {};22var builtin = parserConfig.builtin || {};23var atoms = parserConfig.atoms || {};24
25var isSingleOperatorChar = /[;=\(:\),{}.*<>+\-\/^\[\]]/;26var isDoubleOperatorChar = /(:=|<=|>=|==|<>|\.\+|\.\-|\.\*|\.\/|\.\^)/;27var isDigit = /[0-9]/;28var isNonDigit = /[_a-zA-Z]/;29
30function tokenLineComment(stream, state) {31stream.skipToEnd();32state.tokenize = null;33return "comment";34}35
36function tokenBlockComment(stream, state) {37var maybeEnd = false, ch;38while (ch = stream.next()) {39if (maybeEnd && ch == "/") {40state.tokenize = null;41break;42}43maybeEnd = (ch == "*");44}45return "comment";46}47
48function tokenString(stream, state) {49var escaped = false, ch;50while ((ch = stream.next()) != null) {51if (ch == '"' && !escaped) {52state.tokenize = null;53state.sol = false;54break;55}56escaped = !escaped && ch == "\\";57}58
59return "string";60}61
62function tokenIdent(stream, state) {63stream.eatWhile(isDigit);64while (stream.eat(isDigit) || stream.eat(isNonDigit)) { }65
66
67var cur = stream.current();68
69if(state.sol && (cur == "package" || cur == "model" || cur == "when" || cur == "connector")) state.level++;70else if(state.sol && cur == "end" && state.level > 0) state.level--;71
72state.tokenize = null;73state.sol = false;74
75if (keywords.propertyIsEnumerable(cur)) return "keyword";76else if (builtin.propertyIsEnumerable(cur)) return "builtin";77else if (atoms.propertyIsEnumerable(cur)) return "atom";78else return "variable";79}80
81function tokenQIdent(stream, state) {82while (stream.eat(/[^']/)) { }83
84state.tokenize = null;85state.sol = false;86
87if(stream.eat("'"))88return "variable";89else90return "error";91}92
93function tokenUnsignedNumber(stream, state) {94stream.eatWhile(isDigit);95if (stream.eat('.')) {96stream.eatWhile(isDigit);97}98if (stream.eat('e') || stream.eat('E')) {99if (!stream.eat('-'))100stream.eat('+');101stream.eatWhile(isDigit);102}103
104state.tokenize = null;105state.sol = false;106return "number";107}108
109// Interface110return {111startState: function() {112return {113tokenize: null,114level: 0,115sol: true116};117},118
119token: function(stream, state) {120if(state.tokenize != null) {121return state.tokenize(stream, state);122}123
124if(stream.sol()) {125state.sol = true;126}127
128// WHITESPACE129if(stream.eatSpace()) {130state.tokenize = null;131return null;132}133
134var ch = stream.next();135
136// LINECOMMENT137if(ch == '/' && stream.eat('/')) {138state.tokenize = tokenLineComment;139}140// BLOCKCOMMENT141else if(ch == '/' && stream.eat('*')) {142state.tokenize = tokenBlockComment;143}144// TWO SYMBOL TOKENS145else if(isDoubleOperatorChar.test(ch+stream.peek())) {146stream.next();147state.tokenize = null;148return "operator";149}150// SINGLE SYMBOL TOKENS151else if(isSingleOperatorChar.test(ch)) {152state.tokenize = null;153return "operator";154}155// IDENT156else if(isNonDigit.test(ch)) {157state.tokenize = tokenIdent;158}159// Q-IDENT160else if(ch == "'" && stream.peek() && stream.peek() != "'") {161state.tokenize = tokenQIdent;162}163// STRING164else if(ch == '"') {165state.tokenize = tokenString;166}167// UNSIGNED_NUMBER168else if(isDigit.test(ch)) {169state.tokenize = tokenUnsignedNumber;170}171// ERROR172else {173state.tokenize = null;174return "error";175}176
177return state.tokenize(stream, state);178},179
180indent: function(state, textAfter) {181if (state.tokenize != null) return CodeMirror.Pass;182
183var level = state.level;184if(/(algorithm)/.test(textAfter)) level--;185if(/(equation)/.test(textAfter)) level--;186if(/(initial algorithm)/.test(textAfter)) level--;187if(/(initial equation)/.test(textAfter)) level--;188if(/(end)/.test(textAfter)) level--;189
190if(level > 0)191return indentUnit*level;192else193return 0;194},195
196blockCommentStart: "/*",197blockCommentEnd: "*/",198lineComment: "//"199};200});201
202function words(str) {203var obj = {}, words = str.split(" ");204for (var i=0; i<words.length; ++i)205obj[words[i]] = true;206return obj;207}208
209var modelicaKeywords = "algorithm and annotation assert block break class connect connector constant constrainedby der discrete each else elseif elsewhen encapsulated end enumeration equation expandable extends external false final flow for function if import impure in initial inner input loop model not operator or outer output package parameter partial protected public pure record redeclare replaceable return stream then true type when while within";210var modelicaBuiltin = "abs acos actualStream asin atan atan2 cardinality ceil cos cosh delay div edge exp floor getInstanceName homotopy inStream integer log log10 mod pre reinit rem semiLinear sign sin sinh spatialDistribution sqrt tan tanh";211var modelicaAtoms = "Real Boolean Integer String";212
213function def(mimes, mode) {214if (typeof mimes == "string")215mimes = [mimes];216
217var words = [];218
219function add(obj) {220if (obj)221for (var prop in obj)222if (obj.hasOwnProperty(prop))223words.push(prop);224}225
226add(mode.keywords);227add(mode.builtin);228add(mode.atoms);229
230if (words.length) {231mode.helperType = mimes[0];232CodeMirror.registerHelper("hintWords", mimes[0], words);233}234
235for (var i=0; i<mimes.length; ++i)236CodeMirror.defineMIME(mimes[i], mode);237}238
239def(["text/x-modelica"], {240name: "modelica",241keywords: words(modelicaKeywords),242builtin: words(modelicaBuiltin),243atoms: words(modelicaAtoms)244});245});246