editorMode2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { getCodeEditorModeConfig } from "./utils";
  2. export function CodeEditorMode (CodeMirror) {
  3. "use strict";
  4. function Context(indented, column, type, info, align, prev) {
  5. this.indented = indented;
  6. this.column = column;
  7. this.type = type;
  8. this.info = info;
  9. this.align = align;
  10. this.prev = prev;
  11. }
  12. function pushContext(state, col, type, info) {
  13. var indent = state.indented;
  14. if (state.context && state.context.type == "statement" && type != "statement")
  15. indent = state.context.indented;
  16. return state.context = new Context(indent, col, type, info, null, state.context);
  17. }
  18. function popContext(state) {
  19. var t = state.context.type;
  20. if (t == ")" || t == "]" || t == "}")
  21. state.indented = state.context.indented;
  22. return state.context = state.context.prev;
  23. }
  24. function typeBefore(stream, state, pos) {
  25. if (state.prevToken == "variable" || state.prevToken == "type") return true;
  26. if (/\S(?:[^- ]>|[*\]])\s*$|\*$/.test(stream.string.slice(0, pos))) return true;
  27. if (state.typeAtEndOfLine && stream.column() == stream.indentation()) return true;
  28. }
  29. function isTopScope(context) {
  30. for (; ;) {
  31. if (!context || context.type == "top") return true;
  32. if (context.type == "}" && context.prev.info != "namespace") return false;
  33. context = context.prev;
  34. }
  35. }
  36. CodeMirror.defineMode("ivprog", function (config, parserConfig) {
  37. var indentUnit = config.indentUnit,
  38. statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
  39. dontAlignCalls = parserConfig.dontAlignCalls,
  40. keywords = parserConfig.keywords || {},
  41. switchKeyword = parserConfig.switchKeyword,
  42. caseKeyword = parserConfig.caseKeyword,
  43. defaultKeyword = parserConfig.defaultKeyword,
  44. caseRegex = new RegExp(`^\s*(?:${caseKeyword} .*?:|${defaultKeyword}:|\{\}?|\})$`),////,
  45. types = parserConfig.types || {},
  46. builtin = parserConfig.builtin || {},
  47. blockKeywords = parserConfig.blockKeywords || {},
  48. defKeywords = parserConfig.defKeywords || {},
  49. atoms = parserConfig.atoms || {},
  50. hooks = parserConfig.hooks || {},
  51. multiLineStrings = parserConfig.multiLineStrings,
  52. indentStatements = false,
  53. namespaceSeparator = null,
  54. isPunctuationChar = /[\[\]{}\(\),;\:\n]/,
  55. numberStart = /[\d\.]/,
  56. number = /^(?:0x[a-f\d]+|0b[01]+|(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?)/i,
  57. isOperatorChar = /[+\-*%=<>!\/]/,
  58. isIdentifierChar = /[a-zA-Z0-9_]/,
  59. // An optional function that takes a {string} token and returns true if it
  60. // should be treated as a builtin.
  61. isReservedIdentifier = parserConfig.isReservedIdentifier || false;
  62. var curPunc, isDefKeyword;
  63. function tokenBase(stream, state) {
  64. var ch = stream.next();
  65. if (hooks[ch]) {
  66. var result = hooks[ch](stream, state);
  67. if (result !== false) return result;
  68. }
  69. if (ch == '"') {
  70. state.tokenize = tokenString(ch);
  71. return state.tokenize(stream, state);
  72. }
  73. if (isPunctuationChar.test(ch)) {
  74. curPunc = ch;
  75. return null;
  76. }
  77. if (numberStart.test(ch)) {
  78. stream.backUp(1)
  79. if (stream.match(number)) return "number"
  80. stream.next()
  81. }
  82. if (ch == "/") {
  83. if (stream.eat("*")) {
  84. state.tokenize = tokenComment;
  85. return tokenComment(stream, state);
  86. }
  87. if (stream.eat("/")) {
  88. stream.skipToEnd();
  89. return "comment";
  90. }
  91. }
  92. if (isOperatorChar.test(ch)) {
  93. while (!stream.match(/^\/[\/*]/, false) && stream.eat(isOperatorChar)) { }
  94. return "operator";
  95. }
  96. stream.eatWhile(isIdentifierChar);
  97. if (namespaceSeparator) while (stream.match(namespaceSeparator))
  98. stream.eatWhile(isIdentifierChar);
  99. var cur = stream.current();
  100. if (contains(keywords, cur)) {
  101. if (contains(blockKeywords, cur)) curPunc = "newstatement";
  102. if (contains(defKeywords, cur)) isDefKeyword = true;
  103. return "keyword";
  104. }
  105. if (contains(types, cur)) return "type";
  106. if (contains(builtin, cur)
  107. || (isReservedIdentifier && isReservedIdentifier(cur))) {
  108. if (contains(blockKeywords, cur)) curPunc = "newstatement";
  109. return "builtin";
  110. }
  111. if (contains(atoms, cur)) return "atom";
  112. return "variable";
  113. }
  114. function tokenString(quote) {
  115. return function (stream, state) {
  116. var escaped = false, next, end = false;
  117. while ((next = stream.next()) != null) {
  118. if (next == quote && !escaped) { end = true; break; }
  119. escaped = !escaped && next == "\\";
  120. }
  121. if (end || !(escaped || multiLineStrings))
  122. state.tokenize = null;
  123. return "string";
  124. };
  125. }
  126. function tokenComment(stream, state) {
  127. var maybeEnd = false, ch;
  128. while (ch = stream.next()) {
  129. if (ch == "/" && maybeEnd) {
  130. state.tokenize = null;
  131. break;
  132. }
  133. maybeEnd = (ch == "*");
  134. }
  135. return "comment";
  136. }
  137. function maybeEOL(stream, state) {
  138. if (parserConfig.typeFirstDefinitions && stream.eol() && isTopScope(state.context))
  139. state.typeAtEndOfLine = typeBefore(stream, state, stream.pos)
  140. }
  141. // Interface
  142. return {
  143. startState: function (basecolumn) {
  144. return {
  145. tokenize: null,
  146. context: new Context((basecolumn || 0) - indentUnit, 0, "top", null, false),
  147. indented: 0,
  148. startOfLine: true,
  149. prevToken: null
  150. };
  151. },
  152. token: function (stream, state) {
  153. var ctx = state.context;
  154. if (stream.sol()) {
  155. if (ctx.align == null) ctx.align = false;
  156. state.indented = stream.indentation();
  157. state.startOfLine = true;
  158. }
  159. if (stream.eatSpace()) { maybeEOL(stream, state); return null; }
  160. curPunc = isDefKeyword = null;
  161. var style = (state.tokenize || tokenBase)(stream, state);
  162. if (style == "comment" || style == "meta") return style;
  163. if (ctx.align == null) ctx.align = true;
  164. if (curPunc == ";" || curPunc == ":" || (curPunc == "," && stream.match(/^\s*(?:\/\/.*)?$/, false)))
  165. while (state.context.type == "statement") popContext(state);
  166. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  167. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  168. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  169. else if (curPunc == "}") {
  170. while (ctx.type == "statement") ctx = popContext(state);
  171. if (ctx.type == "}") ctx = popContext(state);
  172. while (ctx.type == "statement") ctx = popContext(state);
  173. }
  174. else if (curPunc == ctx.type) popContext(state);
  175. else if (indentStatements &&
  176. (((ctx.type == "}" || ctx.type == "top") && curPunc != ";") ||
  177. (ctx.type == "statement" && curPunc == "newstatement"))) {
  178. pushContext(state, stream.column(), "statement", stream.current());
  179. }
  180. if (style == "variable" &&
  181. ((state.prevToken == "def" ||
  182. (parserConfig.typeFirstDefinitions && typeBefore(stream, state, stream.start) &&
  183. isTopScope(state.context) && stream.match(/^\s*\(/, false)))))
  184. style = "def";
  185. if (hooks.token) {
  186. var result = hooks.token(stream, state, style);
  187. if (result !== undefined) style = result;
  188. }
  189. if (style == "def" && parserConfig.styleDefs === false) style = "variable";
  190. state.startOfLine = false;
  191. state.prevToken = isDefKeyword ? "def" : style || curPunc;
  192. maybeEOL(stream, state);
  193. return style;
  194. },
  195. indent: function (state, textAfter) {
  196. if (state.tokenize != tokenBase && state.tokenize != null || state.typeAtEndOfLine) return CodeMirror.Pass;
  197. var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
  198. var closing = firstChar == ctx.type;
  199. if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
  200. if (parserConfig.dontIndentStatements)
  201. while (ctx.type == "statement" && parserConfig.dontIndentStatements.test(ctx.info))
  202. ctx = ctx.prev
  203. if (hooks.indent) {
  204. var hook = hooks.indent(state, ctx, textAfter, indentUnit);
  205. if (typeof hook == "number") return hook
  206. }
  207. var switchBlock = ctx.prev && ctx.prev.info == switchKeyword;
  208. if (parserConfig.allmanIndentation && /[{(]/.test(firstChar)) {
  209. while (ctx.type != "top" && ctx.type != "}") ctx = ctx.prev
  210. return ctx.indented
  211. }
  212. if (ctx.type == "statement")
  213. return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
  214. if (ctx.align && (!dontAlignCalls || ctx.type != ")"))
  215. return ctx.column + (closing ? 0 : 1);
  216. if (ctx.type == ")" && !closing)
  217. return ctx.indented + statementIndentUnit;
  218. var caseTestRegex = new RegExp(`^(?:${caseKeyword}|${defaultKeyword})\b`)
  219. return ctx.indented + (closing ? 0 : indentUnit) +
  220. (!closing && switchBlock && !caseTestRegex.test(textAfter) ? indentUnit : 0);
  221. },
  222. electricInput: caseRegex,
  223. blockCommentStart: "/*",
  224. blockCommentEnd: "*/",
  225. blockCommentContinue: " * ",
  226. lineComment: "//",
  227. fold: "brace"
  228. };
  229. });
  230. function words(str) {
  231. var obj = {}, words = str.split(" ");
  232. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  233. return obj;
  234. }
  235. function contains(words, word) {
  236. if (typeof words === "function") {
  237. return words(word);
  238. } else {
  239. return words.propertyIsEnumerable(word);
  240. }
  241. }
  242. const codeConfig = getCodeEditorModeConfig();
  243. var ivprogKeywords = codeConfig.keywords.join(" ");
  244. // Do not use this. Use the cTypes function below. This is global just to avoid
  245. // excessive calls when cTypes is being called multiple times during a parse.
  246. var basicTypes = words(codeConfig.types.join(" "));
  247. // Returns true if identifier is a "C" type.
  248. // C type is defined as those that are reserved by the compiler (basicTypes),
  249. // and those that end in _t (Reserved by POSIX for types)
  250. // http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html
  251. function ivprogTypes(identifier) {
  252. return contains(basicTypes, identifier);
  253. }
  254. var ivprogBlockKeywords = codeConfig.blocks.join(" ");
  255. var ivprogDefKeywords = "funcao const";
  256. function def(mimes, mode) {
  257. if (typeof mimes == "string") mimes = [mimes];
  258. var words = [];
  259. function add(obj) {
  260. if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop))
  261. words.push(prop);
  262. }
  263. add(mode.keywords);
  264. add(mode.types);
  265. add(mode.builtin);
  266. add(mode.atoms);
  267. if (words.length) {
  268. mode.helperType = mimes[0];
  269. CodeMirror.registerHelper("hintWords", mimes[0], words);
  270. }
  271. for (var i = 0; i < mimes.length; ++i)
  272. CodeMirror.defineMIME(mimes[i], mode);
  273. }
  274. def(["text/x-ivprog"], {
  275. name: "ivprog",
  276. keywords: words(ivprogKeywords),
  277. types: ivprogTypes,
  278. blockKeywords: words(ivprogBlockKeywords),
  279. defKeywords: words(ivprogDefKeywords),
  280. typeFirstDefinitions: true,
  281. atoms: words(codeConfig.atoms.join(" ")),
  282. switchKeyword: codeConfig.switchString,
  283. caseKeyword: codeConfig.case_default[0],
  284. defaultKeyword: codeConfig.case_default[1],
  285. modeProps: { fold: ["brace"] }
  286. });
  287. }