editorMode2.js 11 KB

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