File size: 4,784 Bytes
537a6f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function words(str) {
  var obj = {}, words2 = str.split(" ");
  for (var i = 0; i < words2.length; ++i)
    obj[words2[i]] = true;
  return obj;
}
var keywords = words("algorithm and annotation assert block break class connect connector constant constrainedby der discrete each else elseif elsewhen encapsulated end enumeration equation expandable extends external false final flow for function if import impure in initial inner input loop model not operator or outer output package parameter partial protected public pure record redeclare replaceable return stream then true type when while within");
var builtin = words("abs acos actualStream asin atan atan2 cardinality ceil cos cosh delay div edge exp floor getInstanceName homotopy inStream integer log log10 mod pre reinit rem semiLinear sign sin sinh spatialDistribution sqrt tan tanh");
var atoms = words("Real Boolean Integer String");
var completions = [].concat(Object.keys(keywords), Object.keys(builtin), Object.keys(atoms));
var isSingleOperatorChar = /[;=\(:\),{}.*<>+\-\/^\[\]]/;
var isDoubleOperatorChar = /(:=|<=|>=|==|<>|\.\+|\.\-|\.\*|\.\/|\.\^)/;
var isDigit = /[0-9]/;
var isNonDigit = /[_a-zA-Z]/;
function tokenLineComment(stream, state) {
  stream.skipToEnd();
  state.tokenize = null;
  return "comment";
}
function tokenBlockComment(stream, state) {
  var maybeEnd = false, ch;
  while (ch = stream.next()) {
    if (maybeEnd && ch == "/") {
      state.tokenize = null;
      break;
    }
    maybeEnd = ch == "*";
  }
  return "comment";
}
function tokenString(stream, state) {
  var escaped = false, ch;
  while ((ch = stream.next()) != null) {
    if (ch == '"' && !escaped) {
      state.tokenize = null;
      state.sol = false;
      break;
    }
    escaped = !escaped && ch == "\\";
  }
  return "string";
}
function tokenIdent(stream, state) {
  stream.eatWhile(isDigit);
  while (stream.eat(isDigit) || stream.eat(isNonDigit)) {
  }
  var cur = stream.current();
  if (state.sol && (cur == "package" || cur == "model" || cur == "when" || cur == "connector")) state.level++;
  else if (state.sol && cur == "end" && state.level > 0) state.level--;
  state.tokenize = null;
  state.sol = false;
  if (keywords.propertyIsEnumerable(cur)) return "keyword";
  else if (builtin.propertyIsEnumerable(cur)) return "builtin";
  else if (atoms.propertyIsEnumerable(cur)) return "atom";
  else return "variable";
}
function tokenQIdent(stream, state) {
  while (stream.eat(/[^']/)) {
  }
  state.tokenize = null;
  state.sol = false;
  if (stream.eat("'"))
    return "variable";
  else
    return "error";
}
function tokenUnsignedNumber(stream, state) {
  stream.eatWhile(isDigit);
  if (stream.eat(".")) {
    stream.eatWhile(isDigit);
  }
  if (stream.eat("e") || stream.eat("E")) {
    if (!stream.eat("-"))
      stream.eat("+");
    stream.eatWhile(isDigit);
  }
  state.tokenize = null;
  state.sol = false;
  return "number";
}
const modelica = {
  name: "modelica",
  startState: function() {
    return {
      tokenize: null,
      level: 0,
      sol: true
    };
  },
  token: function(stream, state) {
    if (state.tokenize != null) {
      return state.tokenize(stream, state);
    }
    if (stream.sol()) {
      state.sol = true;
    }
    if (stream.eatSpace()) {
      state.tokenize = null;
      return null;
    }
    var ch = stream.next();
    if (ch == "/" && stream.eat("/")) {
      state.tokenize = tokenLineComment;
    } else if (ch == "/" && stream.eat("*")) {
      state.tokenize = tokenBlockComment;
    } else if (isDoubleOperatorChar.test(ch + stream.peek())) {
      stream.next();
      state.tokenize = null;
      return "operator";
    } else if (isSingleOperatorChar.test(ch)) {
      state.tokenize = null;
      return "operator";
    } else if (isNonDigit.test(ch)) {
      state.tokenize = tokenIdent;
    } else if (ch == "'" && stream.peek() && stream.peek() != "'") {
      state.tokenize = tokenQIdent;
    } else if (ch == '"') {
      state.tokenize = tokenString;
    } else if (isDigit.test(ch)) {
      state.tokenize = tokenUnsignedNumber;
    } else {
      state.tokenize = null;
      return "error";
    }
    return state.tokenize(stream, state);
  },
  indent: function(state, textAfter, cx) {
    if (state.tokenize != null) return null;
    var level = state.level;
    if (/(algorithm)/.test(textAfter)) level--;
    if (/(equation)/.test(textAfter)) level--;
    if (/(initial algorithm)/.test(textAfter)) level--;
    if (/(initial equation)/.test(textAfter)) level--;
    if (/(end)/.test(textAfter)) level--;
    if (level > 0)
      return cx.unit * level;
    else
      return 0;
  },
  languageData: {
    commentTokens: { line: "//", block: { open: "/*", close: "*/" } },
    autocomplete: completions
  }
};
export {
  modelica
};