LaravelTest
204 строки · 5.3 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// Yacas mode copyright (c) 2015 by Grzegorz Mazur
5// Loosely based on mathematica mode by Calin Barbat
6
7(function(mod) {8if (typeof exports == "object" && typeof module == "object") // CommonJS9mod(require("../../lib/codemirror"));10else if (typeof define == "function" && define.amd) // AMD11define(["../../lib/codemirror"], mod);12else // Plain browser env13mod(CodeMirror);14})(function(CodeMirror) {15"use strict";16
17CodeMirror.defineMode('yacas', function(_config, _parserConfig) {18
19function words(str) {20var obj = {}, words = str.split(" ");21for (var i = 0; i < words.length; ++i) obj[words[i]] = true;22return obj;23}24
25var bodiedOps = words("Assert BackQuote D Defun Deriv For ForEach FromFile " +26"FromString Function Integrate InverseTaylor Limit " +27"LocalSymbols Macro MacroRule MacroRulePattern " +28"NIntegrate Rule RulePattern Subst TD TExplicitSum " +29"TSum Taylor Taylor1 Taylor2 Taylor3 ToFile " +30"ToStdout ToString TraceRule Until While");31
32// patterns33var pFloatForm = "(?:(?:\\.\\d+|\\d+\\.\\d*|\\d+)(?:[eE][+-]?\\d+)?)";34var pIdentifier = "(?:[a-zA-Z\\$'][a-zA-Z0-9\\$']*)";35
36// regular expressions37var reFloatForm = new RegExp(pFloatForm);38var reIdentifier = new RegExp(pIdentifier);39var rePattern = new RegExp(pIdentifier + "?_" + pIdentifier);40var reFunctionLike = new RegExp(pIdentifier + "\\s*\\(");41
42function tokenBase(stream, state) {43var ch;44
45// get next character46ch = stream.next();47
48// string49if (ch === '"') {50state.tokenize = tokenString;51return state.tokenize(stream, state);52}53
54// comment55if (ch === '/') {56if (stream.eat('*')) {57state.tokenize = tokenComment;58return state.tokenize(stream, state);59}60if (stream.eat("/")) {61stream.skipToEnd();62return "comment";63}64}65
66// go back one character67stream.backUp(1);68
69// update scope info70var m = stream.match(/^(\w+)\s*\(/, false);71if (m !== null && bodiedOps.hasOwnProperty(m[1]))72state.scopes.push('bodied');73
74var scope = currentScope(state);75
76if (scope === 'bodied' && ch === '[')77state.scopes.pop();78
79if (ch === '[' || ch === '{' || ch === '(')80state.scopes.push(ch);81
82scope = currentScope(state);83
84if (scope === '[' && ch === ']' ||85scope === '{' && ch === '}' ||86scope === '(' && ch === ')')87state.scopes.pop();88
89if (ch === ';') {90while (scope === 'bodied') {91state.scopes.pop();92scope = currentScope(state);93}94}95
96// look for ordered rules97if (stream.match(/\d+ *#/, true, false)) {98return 'qualifier';99}100
101// look for numbers102if (stream.match(reFloatForm, true, false)) {103return 'number';104}105
106// look for placeholders107if (stream.match(rePattern, true, false)) {108return 'variable-3';109}110
111// match all braces separately112if (stream.match(/(?:\[|\]|{|}|\(|\))/, true, false)) {113return 'bracket';114}115
116// literals looking like function calls117if (stream.match(reFunctionLike, true, false)) {118stream.backUp(1);119return 'variable';120}121
122// all other identifiers123if (stream.match(reIdentifier, true, false)) {124return 'variable-2';125}126
127// operators; note that operators like @@ or /; are matched separately for each symbol.128if (stream.match(/(?:\\|\+|\-|\*|\/|,|;|\.|:|@|~|=|>|<|&|\||_|`|'|\^|\?|!|%|#)/, true, false)) {129return 'operator';130}131
132// everything else is an error133return 'error';134}135
136function tokenString(stream, state) {137var next, end = false, escaped = false;138while ((next = stream.next()) != null) {139if (next === '"' && !escaped) {140end = true;141break;142}143escaped = !escaped && next === '\\';144}145if (end && !escaped) {146state.tokenize = tokenBase;147}148return 'string';149};150
151function tokenComment(stream, state) {152var prev, next;153while((next = stream.next()) != null) {154if (prev === '*' && next === '/') {155state.tokenize = tokenBase;156break;157}158prev = next;159}160return 'comment';161}162
163function currentScope(state) {164var scope = null;165if (state.scopes.length > 0)166scope = state.scopes[state.scopes.length - 1];167return scope;168}169
170return {171startState: function() {172return {173tokenize: tokenBase,174scopes: []175};176},177token: function(stream, state) {178if (stream.eatSpace()) return null;179return state.tokenize(stream, state);180},181indent: function(state, textAfter) {182if (state.tokenize !== tokenBase && state.tokenize !== null)183return CodeMirror.Pass;184
185var delta = 0;186if (textAfter === ']' || textAfter === '];' ||187textAfter === '}' || textAfter === '};' ||188textAfter === ');')189delta = -1;190
191return (state.scopes.length + delta) * _config.indentUnit;192},193electricChars: "{}[]();",194blockCommentStart: "/*",195blockCommentEnd: "*/",196lineComment: "//"197};198});199
200CodeMirror.defineMIME('text/x-yacas', {201name: 'yacas'202});203
204});205