editorMode2.js 12 KB

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