move a few things away in to-be-integrated/
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / ocaml / ocaml.js
1 CodeMirror.defineMode('ocaml', function() {
2
3   var words = {
4     'true': 'atom',
5     'false': 'atom',
6     'let': 'keyword',
7     'rec': 'keyword',
8     'in': 'keyword',
9     'of': 'keyword',
10     'and': 'keyword',
11     'succ': 'keyword',
12     'if': 'keyword',
13     'then': 'keyword',
14     'else': 'keyword',
15     'for': 'keyword',
16     'to': 'keyword',
17     'while': 'keyword',
18     'do': 'keyword',
19     'done': 'keyword',
20     'fun': 'keyword',
21     'function': 'keyword',
22     'val': 'keyword',
23     'type': 'keyword',
24     'mutable': 'keyword',
25     'match': 'keyword',
26     'with': 'keyword',
27     'try': 'keyword',
28     'raise': 'keyword',
29     'begin': 'keyword',
30     'end': 'keyword',
31     'open': 'builtin',
32     'trace': 'builtin',
33     'ignore': 'builtin',
34     'exit': 'builtin',
35     'print_string': 'builtin',
36     'print_endline': 'builtin'
37   };
38
39   function tokenBase(stream, state) {
40     var ch = stream.next();
41
42     if (ch === '"') {
43       state.tokenize = tokenString;
44       return state.tokenize(stream, state);
45     }
46     if (ch === '(') {
47       if (stream.eat('*')) {
48         state.commentLevel++;
49         state.tokenize = tokenComment;
50         return state.tokenize(stream, state);
51       }
52     }
53     if (ch === '~') {
54       stream.eatWhile(/\w/);
55       return 'variable-2';
56     }
57     if (ch === '`') {
58       stream.eatWhile(/\w/);
59       return 'quote';
60     }
61     if (/\d/.test(ch)) {
62       stream.eatWhile(/[\d]/);
63       if (stream.eat('.')) {
64         stream.eatWhile(/[\d]/);
65       }
66       return 'number';
67     }
68     if ( /[+\-*&%=<>!?|]/.test(ch)) {
69       return 'operator';
70     }
71     stream.eatWhile(/\w/);
72     var cur = stream.current();
73     return words[cur] || 'variable';
74   }
75
76   function tokenString(stream, state) {
77     var next, end = false, escaped = false;
78     while ((next = stream.next()) != null) {
79       if (next === '"' && !escaped) {
80         end = true;
81         break;
82       }
83       escaped = !escaped && next === '\\';
84     }
85     if (end && !escaped) {
86       state.tokenize = tokenBase;
87     }
88     return 'string';
89   };
90
91   function tokenComment(stream, state) {
92     var prev, next;
93     while(state.commentLevel > 0 && (next = stream.next()) != null) {
94       if (prev === '(' && next === '*') state.commentLevel++;
95       if (prev === '*' && next === ')') state.commentLevel--;
96       prev = next;
97     }
98     if (state.commentLevel <= 0) {
99       state.tokenize = tokenBase;
100     }
101     return 'comment';
102   }
103
104   return {
105     startState: function() {return {tokenize: tokenBase, commentLevel: 0};},
106     token: function(stream, state) {
107       if (stream.eatSpace()) return null;
108       return state.tokenize(stream, state);
109     },
110
111     blockCommentStart: "(*",
112     blockCommentEnd: "*)"
113   };
114 });
115
116 CodeMirror.defineMIME('text/x-ocaml', 'ocaml');