1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function(mod) {
|
9 | if (typeof exports == "object" && typeof module == "object")
|
10 | mod(require("../../lib/codemirror"));
|
11 | else if (typeof define == "function" && define.amd)
|
12 | define(["../../lib/codemirror"], mod);
|
13 | else
|
14 | mod(CodeMirror);
|
15 | })(function(CodeMirror) {
|
16 | "use strict";
|
17 |
|
18 | CodeMirror.defineMode('mathematica', function(_config, _parserConfig) {
|
19 |
|
20 |
|
21 | var Identifier = '[a-zA-Z\\$][a-zA-Z0-9\\$]*';
|
22 | var pBase = "(?:\\d+)";
|
23 | var pFloat = "(?:\\.\\d+|\\d+\\.\\d*|\\d+)";
|
24 | var pFloatBase = "(?:\\.\\w+|\\w+\\.\\w*|\\w+)";
|
25 | var pPrecision = "(?:`(?:`?"+pFloat+")?)";
|
26 |
|
27 |
|
28 | var reBaseForm = new RegExp('(?:'+pBase+'(?:\\^\\^'+pFloatBase+pPrecision+'?(?:\\*\\^[+-]?\\d+)?))');
|
29 | var reFloatForm = new RegExp('(?:' + pFloat + pPrecision + '?(?:\\*\\^[+-]?\\d+)?)');
|
30 | var reIdInContext = new RegExp('(?:`?)(?:' + Identifier + ')(?:`(?:' + Identifier + '))*(?:`?)');
|
31 |
|
32 | function tokenBase(stream, state) {
|
33 | var ch;
|
34 |
|
35 |
|
36 | ch = stream.next();
|
37 |
|
38 |
|
39 | if (ch === '"') {
|
40 | state.tokenize = tokenString;
|
41 | return state.tokenize(stream, state);
|
42 | }
|
43 |
|
44 |
|
45 | if (ch === '(') {
|
46 | if (stream.eat('*')) {
|
47 | state.commentLevel++;
|
48 | state.tokenize = tokenComment;
|
49 | return state.tokenize(stream, state);
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 | stream.backUp(1);
|
55 |
|
56 |
|
57 |
|
58 | if (stream.match(reBaseForm, true, false)) {
|
59 | return 'number';
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 | if (stream.match(reFloatForm, true, false)) {
|
65 | return 'number';
|
66 | }
|
67 |
|
68 |
|
69 | if (stream.match(/(?:In|Out)\[[0-9]*\]/, true, false)) {
|
70 | return 'atom';
|
71 | }
|
72 |
|
73 |
|
74 | if (stream.match(/([a-zA-Z\$][a-zA-Z0-9\$]*(?:`[a-zA-Z0-9\$]+)*::usage)/, true, false)) {
|
75 | return 'meta';
|
76 | }
|
77 |
|
78 |
|
79 | if (stream.match(/([a-zA-Z\$][a-zA-Z0-9\$]*(?:`[a-zA-Z0-9\$]+)*::[a-zA-Z\$][a-zA-Z0-9\$]*):?/, true, false)) {
|
80 | return 'string-2';
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 | if (stream.match(/([a-zA-Z\$][a-zA-Z0-9\$]*\s*:)(?:(?:[a-zA-Z\$][a-zA-Z0-9\$]*)|(?:[^:=>~@\^\&\*\)\[\]'\?,\|])).*/, true, false)) {
|
86 | return 'variable-2';
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | if (stream.match(/[a-zA-Z\$][a-zA-Z0-9\$]*_+[a-zA-Z\$][a-zA-Z0-9\$]*/, true, false)) {
|
93 | return 'variable-2';
|
94 | }
|
95 | if (stream.match(/[a-zA-Z\$][a-zA-Z0-9\$]*_+/, true, false)) {
|
96 | return 'variable-2';
|
97 | }
|
98 | if (stream.match(/_+[a-zA-Z\$][a-zA-Z0-9\$]*/, true, false)) {
|
99 | return 'variable-2';
|
100 | }
|
101 |
|
102 |
|
103 | if (stream.match(/\\\[[a-zA-Z\$][a-zA-Z0-9\$]*\]/, true, false)) {
|
104 | return 'variable-3';
|
105 | }
|
106 |
|
107 |
|
108 | if (stream.match(/(?:\[|\]|{|}|\(|\))/, true, false)) {
|
109 | return 'bracket';
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 | if (stream.match(/(?:#[a-zA-Z\$][a-zA-Z0-9\$]*|#+[0-9]?)/, true, false)) {
|
115 | return 'variable-2';
|
116 | }
|
117 |
|
118 |
|
119 | if (stream.match(reIdInContext, true, false)) {
|
120 | return 'keyword';
|
121 | }
|
122 |
|
123 |
|
124 | if (stream.match(/(?:\\|\+|\-|\*|\/|,|;|\.|:|@|~|=|>|<|&|\||_|`|'|\^|\?|!|%)/, true, false)) {
|
125 | return 'operator';
|
126 | }
|
127 |
|
128 |
|
129 | stream.next();
|
130 | return 'error';
|
131 | }
|
132 |
|
133 | function tokenString(stream, state) {
|
134 | var next, end = false, escaped = false;
|
135 | while ((next = stream.next()) != null) {
|
136 | if (next === '"' && !escaped) {
|
137 | end = true;
|
138 | break;
|
139 | }
|
140 | escaped = !escaped && next === '\\';
|
141 | }
|
142 | if (end && !escaped) {
|
143 | state.tokenize = tokenBase;
|
144 | }
|
145 | return 'string';
|
146 | };
|
147 |
|
148 | function tokenComment(stream, state) {
|
149 | var prev, next;
|
150 | while(state.commentLevel > 0 && (next = stream.next()) != null) {
|
151 | if (prev === '(' && next === '*') state.commentLevel++;
|
152 | if (prev === '*' && next === ')') state.commentLevel--;
|
153 | prev = next;
|
154 | }
|
155 | if (state.commentLevel <= 0) {
|
156 | state.tokenize = tokenBase;
|
157 | }
|
158 | return 'comment';
|
159 | }
|
160 |
|
161 | return {
|
162 | startState: function() {return {tokenize: tokenBase, commentLevel: 0};},
|
163 | token: function(stream, state) {
|
164 | if (stream.eatSpace()) return null;
|
165 | return state.tokenize(stream, state);
|
166 | },
|
167 | blockCommentStart: "(*",
|
168 | blockCommentEnd: "*)"
|
169 | };
|
170 | });
|
171 |
|
172 | CodeMirror.defineMIME('text/x-mathematica', {
|
173 | name: 'mathematica'
|
174 | });
|
175 |
|
176 | });
|