Debian Codegraph @

codemirror-legacy-modes/mode/smalltalk.js

raw
1 var specialChars = /[+\-\/\\*~<>=@%|&?!.,:;^]/;
2 var keywords = /true|false|nil|self|super|thisContext/;
3
4 var Context = function(tokenizer, parent) {
5 this.next = tokenizer;
6 this.parent = parent;
7 };
8
9 var Token = function(name, context, eos) {
10 this.name = name;
11 this.context = context;
12 this.eos = eos;
13 };
14
15 var State = function() {
16 this.context = new Context(next, null);
17 this.expectVariable = true;
18 this.indentation = 0;
19 this.userIndentationDelta = 0;
20 };
21
22 State.prototype.userIndent = function(indentation, indentUnit) {
23 this.userIndentationDelta = indentation > 0 ? (indentation / indentUnit - this.indentation) : 0;
24 };
25
26 var next = function(stream, context, state) {
27 var token = new Token(null, context, false);
28 var aChar = stream.next();
29
30 if (aChar === '"') {
31 token = nextComment(stream, new Context(nextComment, context));
32
33 } else if (aChar === '\'') {
34 token = nextString(stream, new Context(nextString, context));
35
36 } else if (aChar === '#') {
37 if (stream.peek() === '\'') {
38 stream.next();
39 token = nextSymbol(stream, new Context(nextSymbol, context));
40 } else {
41 if (stream.eatWhile(/[^\s.{}\[\]()]/))
42 token.name = 'string.special';
43 else
44 token.name = 'meta';
45 }
46
47 } else if (aChar === '$') {
48 if (stream.next() === '<') {
49 stream.eatWhile(/[^\s>]/);
50 stream.next();
51 }
52 token.name = 'string.special';
53
54 } else if (aChar === '|' && state.expectVariable) {
55 token.context = new Context(nextTemporaries, context);
56
57 } else if (/[\[\]{}()]/.test(aChar)) {
58 token.name = 'bracket';
59 token.eos = /[\[{(]/.test(aChar);
60
61 if (aChar === '[') {
62 state.indentation++;
63 } else if (aChar === ']') {
64 state.indentation = Math.max(0, state.indentation - 1);
65 }
66
67 } else if (specialChars.test(aChar)) {
68 stream.eatWhile(specialChars);
69 token.name = 'operator';
70 token.eos = aChar !== ';'; // ; cascaded message expression
71
72 } else if (/\d/.test(aChar)) {
73 stream.eatWhile(/[\w\d]/);
74 token.name = 'number';
75
76 } else if (/[\w_]/.test(aChar)) {
77 stream.eatWhile(/[\w\d_]/);
78 token.name = state.expectVariable ? (keywords.test(stream.current()) ? 'keyword' : 'variable') : null;
79
80 } else {
81 token.eos = state.expectVariable;
82 }
83
84 return token;
85 };
86
87 var nextComment = function(stream, context) {
88 stream.eatWhile(/[^"]/);
89 return new Token('comment', stream.eat('"') ? context.parent : context, true);
90 };
91
92 var nextString = function(stream, context) {
93 stream.eatWhile(/[^']/);
94 return new Token('string', stream.eat('\'') ? context.parent : context, false);
95 };
96
97 var nextSymbol = function(stream, context) {
98 stream.eatWhile(/[^']/);
99 return new Token('string.special', stream.eat('\'') ? context.parent : context, false);
100 };
101
102 var nextTemporaries = function(stream, context) {
103 var token = new Token(null, context, false);
104 var aChar = stream.next();
105
106 if (aChar === '|') {
107 token.context = context.parent;
108 token.eos = true;
109
110 } else {
111 stream.eatWhile(/[^|]/);
112 token.name = 'variable';
113 }
114
115 return token;
116 };
117
118 export const smalltalk = {
119 name: "smalltalk",
120
121 startState: function() {
122 return new State;
123 },
124
125 token: function(stream, state) {
126 state.userIndent(stream.indentation(), stream.indentUnit);
127
128 if (stream.eatSpace()) {
129 return null;
130 }
131
132 var token = state.context.next(stream, state.context, state);
133 state.context = token.context;
134 state.expectVariable = token.eos;
135
136 return token.name;
137 },
138
139 blankLine: function(state, indentUnit) {
140 state.userIndent(0, indentUnit);
141 },
142
143 indent: function(state, textAfter, cx) {
144 var i = state.context.next === next && textAfter && textAfter.charAt(0) === ']' ? -1 : state.userIndentationDelta;
145 return (state.indentation + i) * cx.unit;
146 },
147
148 languageData: {
149 indentOnInput: /^\s*\]$/
150 }
151 }