123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- (function(mod) {
- if (typeof exports == "object" && typeof module == "object")
- mod(require("../../lib/codemirror"));
- else if (typeof define == "function" && define.amd)
- define(["../../lib/codemirror"], mod);
- else
- mod(CodeMirror);
- })(function(CodeMirror) {
- "use strict";
- CodeMirror.defineMode('mathematica', function(_config, _parserConfig) {
-
- var Identifier = '[a-zA-Z\\$][a-zA-Z0-9\\$]*';
- var pBase = "(?:\\d+)";
- var pFloat = "(?:\\.\\d+|\\d+\\.\\d*|\\d+)";
- var pFloatBase = "(?:\\.\\w+|\\w+\\.\\w*|\\w+)";
- var pPrecision = "(?:`(?:`?"+pFloat+")?)";
-
- var reBaseForm = new RegExp('(?:'+pBase+'(?:\\^\\^'+pFloatBase+pPrecision+'?(?:\\*\\^[+-]?\\d+)?))');
- var reFloatForm = new RegExp('(?:' + pFloat + pPrecision + '?(?:\\*\\^[+-]?\\d+)?)');
- var reIdInContext = new RegExp('(?:`?)(?:' + Identifier + ')(?:`(?:' + Identifier + '))*(?:`?)');
- function tokenBase(stream, state) {
- var ch;
-
- ch = stream.next();
-
- if (ch === '"') {
- state.tokenize = tokenString;
- return state.tokenize(stream, state);
- }
-
- if (ch === '(') {
- if (stream.eat('*')) {
- state.commentLevel++;
- state.tokenize = tokenComment;
- return state.tokenize(stream, state);
- }
- }
-
- stream.backUp(1);
-
-
- if (stream.match(reBaseForm, true, false)) {
- return 'number';
- }
-
-
- if (stream.match(reFloatForm, true, false)) {
- return 'number';
- }
-
- if (stream.match(/(?:In|Out)\[[0-9]*\]/, true, false)) {
- return 'atom';
- }
-
- if (stream.match(/([a-zA-Z\$]+(?:`?[a-zA-Z0-9\$])*::usage)/, true, false)) {
- return 'meta';
- }
-
- if (stream.match(/([a-zA-Z\$]+(?:`?[a-zA-Z0-9\$])*::[a-zA-Z\$][a-zA-Z0-9\$]*):?/, true, false)) {
- return 'string-2';
- }
-
-
- if (stream.match(/([a-zA-Z\$][a-zA-Z0-9\$]*\s*:)(?:(?:[a-zA-Z\$][a-zA-Z0-9\$]*)|(?:[^:=>~@\^\&\*\)\[\]'\?,\|])).*/, true, false)) {
- return 'variable-2';
- }
-
-
-
- if (stream.match(/[a-zA-Z\$][a-zA-Z0-9\$]*_+[a-zA-Z\$][a-zA-Z0-9\$]*/, true, false)) {
- return 'variable-2';
- }
- if (stream.match(/[a-zA-Z\$][a-zA-Z0-9\$]*_+/, true, false)) {
- return 'variable-2';
- }
- if (stream.match(/_+[a-zA-Z\$][a-zA-Z0-9\$]*/, true, false)) {
- return 'variable-2';
- }
-
- if (stream.match(/\\\[[a-zA-Z\$][a-zA-Z0-9\$]*\]/, true, false)) {
- return 'variable-3';
- }
-
- if (stream.match(/(?:\[|\]|{|}|\(|\))/, true, false)) {
- return 'bracket';
- }
-
-
- if (stream.match(/(?:#[a-zA-Z\$][a-zA-Z0-9\$]*|#+[0-9]?)/, true, false)) {
- return 'variable-2';
- }
-
- if (stream.match(reIdInContext, true, false)) {
- return 'keyword';
- }
-
- if (stream.match(/(?:\\|\+|\-|\*|\/|,|;|\.|:|@|~|=|>|<|&|\||_|`|'|\^|\?|!|%)/, true, false)) {
- return 'operator';
- }
-
- stream.next();
- return 'error';
- }
- function tokenString(stream, state) {
- var next, end = false, escaped = false;
- while ((next = stream.next()) != null) {
- if (next === '"' && !escaped) {
- end = true;
- break;
- }
- escaped = !escaped && next === '\\';
- }
- if (end && !escaped) {
- state.tokenize = tokenBase;
- }
- return 'string';
- };
- function tokenComment(stream, state) {
- var prev, next;
- while(state.commentLevel > 0 && (next = stream.next()) != null) {
- if (prev === '(' && next === '*') state.commentLevel++;
- if (prev === '*' && next === ')') state.commentLevel--;
- prev = next;
- }
- if (state.commentLevel <= 0) {
- state.tokenize = tokenBase;
- }
- return 'comment';
- }
- return {
- startState: function() {return {tokenize: tokenBase, commentLevel: 0};},
- token: function(stream, state) {
- if (stream.eatSpace()) return null;
- return state.tokenize(stream, state);
- },
- blockCommentStart: "(*",
- blockCommentEnd: "*)"
- };
- });
- CodeMirror.defineMIME('text/x-mathematica', {
- name: 'mathematica'
- });
- });
|