clike.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. function Context(indented, column, type, info, align, prev) {
  13. this.indented = indented;
  14. this.column = column;
  15. this.type = type;
  16. this.info = info;
  17. this.align = align;
  18. this.prev = prev;
  19. }
  20. function pushContext(state, col, type, info) {
  21. var indent = state.indented;
  22. if (state.context && state.context.type == "statement" && type != "statement")
  23. indent = state.context.indented;
  24. return state.context = new Context(indent, col, type, info, null, state.context);
  25. }
  26. function popContext(state) {
  27. var t = state.context.type;
  28. if (t == ")" || t == "]" || t == "}")
  29. state.indented = state.context.indented;
  30. return state.context = state.context.prev;
  31. }
  32. function typeBefore(stream, state, pos) {
  33. if (state.prevToken == "variable" || state.prevToken == "type") return true;
  34. if (/\S(?:[^- ]>|[*\]])\s*$|\*$/.test(stream.string.slice(0, pos))) return true;
  35. if (state.typeAtEndOfLine && stream.column() == stream.indentation()) return true;
  36. }
  37. function isTopScope(context) {
  38. for (;;) {
  39. if (!context || context.type == "top") return true;
  40. if (context.type == "}" && context.prev.info != "namespace") return false;
  41. context = context.prev;
  42. }
  43. }
  44. CodeMirror.defineMode("clike", function(config, parserConfig) {
  45. var indentUnit = config.indentUnit,
  46. statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
  47. dontAlignCalls = parserConfig.dontAlignCalls,
  48. keywords = parserConfig.keywords || {},
  49. types = parserConfig.types || {},
  50. builtin = parserConfig.builtin || {},
  51. blockKeywords = parserConfig.blockKeywords || {},
  52. defKeywords = parserConfig.defKeywords || {},
  53. atoms = parserConfig.atoms || {},
  54. hooks = parserConfig.hooks || {},
  55. multiLineStrings = parserConfig.multiLineStrings,
  56. indentStatements = parserConfig.indentStatements !== false,
  57. indentSwitch = parserConfig.indentSwitch !== false,
  58. namespaceSeparator = parserConfig.namespaceSeparator,
  59. isPunctuationChar = parserConfig.isPunctuationChar || /[\[\]{}\(\),;\:\.]/,
  60. numberStart = parserConfig.numberStart || /[\d\.]/,
  61. number = parserConfig.number || /^(?:0x[a-f\d]+|0b[01]+|(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?)(u|ll?|l|f)?/i,
  62. isOperatorChar = parserConfig.isOperatorChar || /[+\-*&%=<>!?|\/]/,
  63. isIdentifierChar = parserConfig.isIdentifierChar || /[\w\$_\xa1-\uffff]/,
  64. // An optional function that takes a {string} token and returns true if it
  65. // should be treated as a builtin.
  66. isReservedIdentifier = parserConfig.isReservedIdentifier || false;
  67. var curPunc, isDefKeyword;
  68. function tokenBase(stream, state) {
  69. var ch = stream.next();
  70. if (hooks[ch]) {
  71. var result = hooks[ch](stream, state);
  72. if (result !== false) return result;
  73. }
  74. if (ch == '"' || ch == "'") {
  75. state.tokenize = tokenString(ch);
  76. return state.tokenize(stream, state);
  77. }
  78. if (isPunctuationChar.test(ch)) {
  79. curPunc = ch;
  80. return null;
  81. }
  82. if (numberStart.test(ch)) {
  83. stream.backUp(1)
  84. if (stream.match(number)) return "number"
  85. stream.next()
  86. }
  87. if (ch == "/") {
  88. if (stream.eat("*")) {
  89. state.tokenize = tokenComment;
  90. return tokenComment(stream, state);
  91. }
  92. if (stream.eat("/")) {
  93. stream.skipToEnd();
  94. return "comment";
  95. }
  96. }
  97. if (isOperatorChar.test(ch)) {
  98. while (!stream.match(/^\/[\/*]/, false) && stream.eat(isOperatorChar)) {}
  99. return "operator";
  100. }
  101. stream.eatWhile(isIdentifierChar);
  102. if (namespaceSeparator) while (stream.match(namespaceSeparator))
  103. stream.eatWhile(isIdentifierChar);
  104. var cur = stream.current();
  105. if (contains(keywords, cur)) {
  106. if (contains(blockKeywords, cur)) curPunc = "newstatement";
  107. if (contains(defKeywords, cur)) isDefKeyword = true;
  108. return "keyword";
  109. }
  110. if (contains(types, cur)) return "type";
  111. if (contains(builtin, cur)
  112. || (isReservedIdentifier && isReservedIdentifier(cur))) {
  113. if (contains(blockKeywords, cur)) curPunc = "newstatement";
  114. return "builtin";
  115. }
  116. if (contains(atoms, cur)) return "atom";
  117. return "variable";
  118. }
  119. function tokenString(quote) {
  120. return function(stream, state) {
  121. var escaped = false, next, end = false;
  122. while ((next = stream.next()) != null) {
  123. if (next == quote && !escaped) {end = true; break;}
  124. escaped = !escaped && next == "\\";
  125. }
  126. if (end || !(escaped || multiLineStrings))
  127. state.tokenize = null;
  128. return "string";
  129. };
  130. }
  131. function tokenComment(stream, state) {
  132. var maybeEnd = false, ch;
  133. while (ch = stream.next()) {
  134. if (ch == "/" && maybeEnd) {
  135. state.tokenize = null;
  136. break;
  137. }
  138. maybeEnd = (ch == "*");
  139. }
  140. return "comment";
  141. }
  142. function maybeEOL(stream, state) {
  143. if (parserConfig.typeFirstDefinitions && stream.eol() && isTopScope(state.context))
  144. state.typeAtEndOfLine = typeBefore(stream, state, stream.pos)
  145. }
  146. // Interface
  147. return {
  148. startState: function(basecolumn) {
  149. return {
  150. tokenize: null,
  151. context: new Context((basecolumn || 0) - indentUnit, 0, "top", null, false),
  152. indented: 0,
  153. startOfLine: true,
  154. prevToken: null
  155. };
  156. },
  157. token: function(stream, state) {
  158. var ctx = state.context;
  159. if (stream.sol()) {
  160. if (ctx.align == null) ctx.align = false;
  161. state.indented = stream.indentation();
  162. state.startOfLine = true;
  163. }
  164. if (stream.eatSpace()) { maybeEOL(stream, state); return null; }
  165. curPunc = isDefKeyword = null;
  166. var style = (state.tokenize || tokenBase)(stream, state);
  167. if (style == "comment" || style == "meta") return style;
  168. if (ctx.align == null) ctx.align = true;
  169. if (curPunc == ";" || curPunc == ":" || (curPunc == "," && stream.match(/^\s*(?:\/\/.*)?$/, false)))
  170. while (state.context.type == "statement") popContext(state);
  171. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  172. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  173. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  174. else if (curPunc == "}") {
  175. while (ctx.type == "statement") ctx = popContext(state);
  176. if (ctx.type == "}") ctx = popContext(state);
  177. while (ctx.type == "statement") ctx = popContext(state);
  178. }
  179. else if (curPunc == ctx.type) popContext(state);
  180. else if (indentStatements &&
  181. (((ctx.type == "}" || ctx.type == "top") && curPunc != ";") ||
  182. (ctx.type == "statement" && curPunc == "newstatement"))) {
  183. pushContext(state, stream.column(), "statement", stream.current());
  184. }
  185. if (style == "variable" &&
  186. ((state.prevToken == "def" ||
  187. (parserConfig.typeFirstDefinitions && typeBefore(stream, state, stream.start) &&
  188. isTopScope(state.context) && stream.match(/^\s*\(/, false)))))
  189. style = "def";
  190. if (hooks.token) {
  191. var result = hooks.token(stream, state, style);
  192. if (result !== undefined) style = result;
  193. }
  194. if (style == "def" && parserConfig.styleDefs === false) style = "variable";
  195. state.startOfLine = false;
  196. state.prevToken = isDefKeyword ? "def" : style || curPunc;
  197. maybeEOL(stream, state);
  198. return style;
  199. },
  200. indent: function(state, textAfter) {
  201. if (state.tokenize != tokenBase && state.tokenize != null || state.typeAtEndOfLine) return CodeMirror.Pass;
  202. var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
  203. var closing = firstChar == ctx.type;
  204. if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
  205. if (parserConfig.dontIndentStatements)
  206. while (ctx.type == "statement" && parserConfig.dontIndentStatements.test(ctx.info))
  207. ctx = ctx.prev
  208. if (hooks.indent) {
  209. var hook = hooks.indent(state, ctx, textAfter, indentUnit);
  210. if (typeof hook == "number") return hook
  211. }
  212. var switchBlock = ctx.prev && ctx.prev.info == "switch";
  213. if (parserConfig.allmanIndentation && /[{(]/.test(firstChar)) {
  214. while (ctx.type != "top" && ctx.type != "}") ctx = ctx.prev
  215. return ctx.indented
  216. }
  217. if (ctx.type == "statement")
  218. return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
  219. if (ctx.align && (!dontAlignCalls || ctx.type != ")"))
  220. return ctx.column + (closing ? 0 : 1);
  221. if (ctx.type == ")" && !closing)
  222. return ctx.indented + statementIndentUnit;
  223. return ctx.indented + (closing ? 0 : indentUnit) +
  224. (!closing && switchBlock && !/^(?:case|default)\b/.test(textAfter) ? indentUnit : 0);
  225. },
  226. electricInput: indentSwitch ? /^\s*(?:case .*?:|default:|\{\}?|\})$/ : /^\s*[{}]$/,
  227. blockCommentStart: "/*",
  228. blockCommentEnd: "*/",
  229. blockCommentContinue: " * ",
  230. lineComment: "//",
  231. fold: "brace"
  232. };
  233. });
  234. function words(str) {
  235. var obj = {}, words = str.split(" ");
  236. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  237. return obj;
  238. }
  239. function contains(words, word) {
  240. if (typeof words === "function") {
  241. return words(word);
  242. } else {
  243. return words.propertyIsEnumerable(word);
  244. }
  245. }
  246. var cKeywords = "auto if break case register continue return default do sizeof " +
  247. "static else struct switch extern typedef union for goto while enum const " +
  248. "volatile inline restrict asm fortran";
  249. // Do not use this. Use the cTypes function below. This is global just to avoid
  250. // excessive calls when cTypes is being called multiple times during a parse.
  251. var basicCTypes = words("int long char short double float unsigned signed " +
  252. "void bool");
  253. // Do not use this. Use the objCTypes function below. This is global just to avoid
  254. // excessive calls when objCTypes is being called multiple times during a parse.
  255. var basicObjCTypes = words("SEL instancetype id Class Protocol BOOL");
  256. // Returns true if identifier is a "C" type.
  257. // C type is defined as those that are reserved by the compiler (basicTypes),
  258. // and those that end in _t (Reserved by POSIX for types)
  259. // http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html
  260. function cTypes(identifier) {
  261. return contains(basicCTypes, identifier) || /.+_t$/.test(identifier);
  262. }
  263. // Returns true if identifier is a "Objective C" type.
  264. function objCTypes(identifier) {
  265. return cTypes(identifier) || contains(basicObjCTypes, identifier);
  266. }
  267. var cBlockKeywords = "case do else for if switch while struct enum union";
  268. var cDefKeywords = "struct enum union";
  269. function cppHook(stream, state) {
  270. if (!state.startOfLine) return false
  271. for (var ch, next = null; ch = stream.peek();) {
  272. if (ch == "\\" && stream.match(/^.$/)) {
  273. next = cppHook
  274. break
  275. } else if (ch == "/" && stream.match(/^\/[\/\*]/, false)) {
  276. break
  277. }
  278. stream.next()
  279. }
  280. state.tokenize = next
  281. return "meta"
  282. }
  283. function pointerHook(_stream, state) {
  284. if (state.prevToken == "type") return "type";
  285. return false;
  286. }
  287. // For C and C++ (and ObjC): identifiers starting with __
  288. // or _ followed by a capital letter are reserved for the compiler.
  289. function cIsReservedIdentifier(token) {
  290. if (!token || token.length < 2) return false;
  291. if (token[0] != '_') return false;
  292. return (token[1] == '_') || (token[1] !== token[1].toLowerCase());
  293. }
  294. function cpp14Literal(stream) {
  295. stream.eatWhile(/[\w\.']/);
  296. return "number";
  297. }
  298. function cpp11StringHook(stream, state) {
  299. stream.backUp(1);
  300. // Raw strings.
  301. if (stream.match(/(R|u8R|uR|UR|LR)/)) {
  302. var match = stream.match(/"([^\s\\()]{0,16})\(/);
  303. if (!match) {
  304. return false;
  305. }
  306. state.cpp11RawStringDelim = match[1];
  307. state.tokenize = tokenRawString;
  308. return tokenRawString(stream, state);
  309. }
  310. // Unicode strings/chars.
  311. if (stream.match(/(u8|u|U|L)/)) {
  312. if (stream.match(/["']/, /* eat */ false)) {
  313. return "string";
  314. }
  315. return false;
  316. }
  317. // Ignore this hook.
  318. stream.next();
  319. return false;
  320. }
  321. function cppLooksLikeConstructor(word) {
  322. var lastTwo = /(\w+)::~?(\w+)$/.exec(word);
  323. return lastTwo && lastTwo[1] == lastTwo[2];
  324. }
  325. // C#-style strings where "" escapes a quote.
  326. function tokenAtString(stream, state) {
  327. var next;
  328. while ((next = stream.next()) != null) {
  329. if (next == '"' && !stream.eat('"')) {
  330. state.tokenize = null;
  331. break;
  332. }
  333. }
  334. return "string";
  335. }
  336. // C++11 raw string literal is <prefix>"<delim>( anything )<delim>", where
  337. // <delim> can be a string up to 16 characters long.
  338. function tokenRawString(stream, state) {
  339. // Escape characters that have special regex meanings.
  340. var delim = state.cpp11RawStringDelim.replace(/[^\w\s]/g, '\\$&');
  341. var match = stream.match(new RegExp(".*?\\)" + delim + '"'));
  342. if (match)
  343. state.tokenize = null;
  344. else
  345. stream.skipToEnd();
  346. return "string";
  347. }
  348. function def(mimes, mode) {
  349. if (typeof mimes == "string") mimes = [mimes];
  350. var words = [];
  351. function add(obj) {
  352. if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop))
  353. words.push(prop);
  354. }
  355. add(mode.keywords);
  356. add(mode.types);
  357. add(mode.builtin);
  358. add(mode.atoms);
  359. if (words.length) {
  360. mode.helperType = mimes[0];
  361. CodeMirror.registerHelper("hintWords", mimes[0], words);
  362. }
  363. for (var i = 0; i < mimes.length; ++i)
  364. CodeMirror.defineMIME(mimes[i], mode);
  365. }
  366. /* CUSTOM - HACKPOINT */
  367. def(["text/x-carduino"], {
  368. name: "clike",
  369. keywords: words(cKeywords+" Serial Stream Keyboard Mouse class"),
  370. types: words(cTypes + " void boolean char unsigned char byte int unsigned int word long unsigned long short float double string String array"),
  371. blockKeywords: words("case break continue do goto return else for if switch while"),
  372. defKeywords: words("struct setup loop"),
  373. builtin: words("PROGMEM sizeof pinMode digitalWrite digitalRead analogReference analogRead analogWrite analogReadResolution analogWriteResolution tone noTone shiftOut shiftIn pulseIn millis micros delay delayMicroseconds min max abs constrain map pow sqrt sin cos tan isAlphaNumeric isAlpha isAscii isWhitespace isControl isDigit isGraph isLowerCase isPrintable isPunct isSpace isUpperCase isHexadecimalDigit randomSeed random lowByte highByte bitRead bitWrite bitSet bitClear bit attachInterrupt detachInterrupt interrupts noInterrupts"),
  374. typeFirstDefinitions: true,
  375. atoms: words("null true false"),
  376. hooks: {"#": cppHook, "*": pointerHook},
  377. modeProps: {fold: ["brace", "include"]}
  378. });
  379. /**/
  380. def(["text/x-csrc", "text/x-c", "text/x-chdr"], {
  381. name: "clike",
  382. keywords: words(cKeywords),
  383. types: cTypes,
  384. blockKeywords: words(cBlockKeywords),
  385. defKeywords: words(cDefKeywords),
  386. typeFirstDefinitions: true,
  387. atoms: words("NULL true false"),
  388. isReservedIdentifier: cIsReservedIdentifier,
  389. hooks: {
  390. "#": cppHook,
  391. "*": pointerHook,
  392. },
  393. modeProps: {fold: ["brace", "include"]}
  394. });
  395. def(["text/x-c++src", "text/x-c++hdr"], {
  396. name: "clike",
  397. // Keywords from https://en.cppreference.com/w/cpp/keyword includes C++20.
  398. keywords: words(cKeywords + "alignas alignof and and_eq audit axiom bitand bitor catch " +
  399. "class compl concept constexpr const_cast decltype delete dynamic_cast " +
  400. "explicit export final friend import module mutable namespace new noexcept " +
  401. "not not_eq operator or or_eq override private protected public " +
  402. "reinterpret_cast requires static_assert static_cast template this " +
  403. "thread_local throw try typeid typename using virtual xor xor_eq"),
  404. types: cTypes,
  405. blockKeywords: words(cBlockKeywords + " class try catch"),
  406. defKeywords: words(cDefKeywords + " class namespace"),
  407. typeFirstDefinitions: true,
  408. atoms: words("true false NULL nullptr"),
  409. dontIndentStatements: /^template$/,
  410. isIdentifierChar: /[\w\$_~\xa1-\uffff]/,
  411. isReservedIdentifier: cIsReservedIdentifier,
  412. hooks: {
  413. "#": cppHook,
  414. "*": pointerHook,
  415. "u": cpp11StringHook,
  416. "U": cpp11StringHook,
  417. "L": cpp11StringHook,
  418. "R": cpp11StringHook,
  419. "0": cpp14Literal,
  420. "1": cpp14Literal,
  421. "2": cpp14Literal,
  422. "3": cpp14Literal,
  423. "4": cpp14Literal,
  424. "5": cpp14Literal,
  425. "6": cpp14Literal,
  426. "7": cpp14Literal,
  427. "8": cpp14Literal,
  428. "9": cpp14Literal,
  429. token: function(stream, state, style) {
  430. if (style == "variable" && stream.peek() == "(" &&
  431. (state.prevToken == ";" || state.prevToken == null ||
  432. state.prevToken == "}") &&
  433. cppLooksLikeConstructor(stream.current()))
  434. return "def";
  435. }
  436. },
  437. namespaceSeparator: "::",
  438. modeProps: {fold: ["brace", "include"]}
  439. });
  440. def("text/x-java", {
  441. name: "clike",
  442. keywords: words("abstract assert break case catch class const continue default " +
  443. "do else enum extends final finally float for goto if implements import " +
  444. "instanceof interface native new package private protected public " +
  445. "return static strictfp super switch synchronized this throw throws transient " +
  446. "try volatile while @interface"),
  447. types: words("byte short int long float double boolean char void Boolean Byte Character Double Float " +
  448. "Integer Long Number Object Short String StringBuffer StringBuilder Void"),
  449. blockKeywords: words("catch class do else finally for if switch try while"),
  450. defKeywords: words("class interface enum @interface"),
  451. typeFirstDefinitions: true,
  452. atoms: words("true false null"),
  453. number: /^(?:0x[a-f\d_]+|0b[01_]+|(?:[\d_]+\.?\d*|\.\d+)(?:e[-+]?[\d_]+)?)(u|ll?|l|f)?/i,
  454. hooks: {
  455. "@": function(stream) {
  456. // Don't match the @interface keyword.
  457. if (stream.match('interface', false)) return false;
  458. stream.eatWhile(/[\w\$_]/);
  459. return "meta";
  460. }
  461. },
  462. modeProps: {fold: ["brace", "import"]}
  463. });
  464. def("text/x-csharp", {
  465. name: "clike",
  466. keywords: words("abstract as async await base break case catch checked class const continue" +
  467. " default delegate do else enum event explicit extern finally fixed for" +
  468. " foreach goto if implicit in interface internal is lock namespace new" +
  469. " operator out override params private protected public readonly ref return sealed" +
  470. " sizeof stackalloc static struct switch this throw try typeof unchecked" +
  471. " unsafe using virtual void volatile while add alias ascending descending dynamic from get" +
  472. " global group into join let orderby partial remove select set value var yield"),
  473. types: words("Action Boolean Byte Char DateTime DateTimeOffset Decimal Double Func" +
  474. " Guid Int16 Int32 Int64 Object SByte Single String Task TimeSpan UInt16 UInt32" +
  475. " UInt64 bool byte char decimal double short int long object" +
  476. " sbyte float string ushort uint ulong"),
  477. blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
  478. defKeywords: words("class interface namespace struct var"),
  479. typeFirstDefinitions: true,
  480. atoms: words("true false null"),
  481. hooks: {
  482. "@": function(stream, state) {
  483. if (stream.eat('"')) {
  484. state.tokenize = tokenAtString;
  485. return tokenAtString(stream, state);
  486. }
  487. stream.eatWhile(/[\w\$_]/);
  488. return "meta";
  489. }
  490. }
  491. });
  492. function tokenTripleString(stream, state) {
  493. var escaped = false;
  494. while (!stream.eol()) {
  495. if (!escaped && stream.match('"""')) {
  496. state.tokenize = null;
  497. break;
  498. }
  499. escaped = stream.next() == "\\" && !escaped;
  500. }
  501. return "string";
  502. }
  503. function tokenNestedComment(depth) {
  504. return function (stream, state) {
  505. var ch
  506. while (ch = stream.next()) {
  507. if (ch == "*" && stream.eat("/")) {
  508. if (depth == 1) {
  509. state.tokenize = null
  510. break
  511. } else {
  512. state.tokenize = tokenNestedComment(depth - 1)
  513. return state.tokenize(stream, state)
  514. }
  515. } else if (ch == "/" && stream.eat("*")) {
  516. state.tokenize = tokenNestedComment(depth + 1)
  517. return state.tokenize(stream, state)
  518. }
  519. }
  520. return "comment"
  521. }
  522. }
  523. def("text/x-scala", {
  524. name: "clike",
  525. keywords: words(
  526. /* scala */
  527. "abstract case catch class def do else extends final finally for forSome if " +
  528. "implicit import lazy match new null object override package private protected return " +
  529. "sealed super this throw trait try type val var while with yield _ " +
  530. /* package scala */
  531. "assert assume require print println printf readLine readBoolean readByte readShort " +
  532. "readChar readInt readLong readFloat readDouble"
  533. ),
  534. types: words(
  535. "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " +
  536. "Enumeration Equiv Error Exception Fractional Function IndexedSeq Int Integral Iterable " +
  537. "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " +
  538. "Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " +
  539. "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector " +
  540. /* package java.lang */
  541. "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable " +
  542. "Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " +
  543. "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " +
  544. "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void"
  545. ),
  546. multiLineStrings: true,
  547. blockKeywords: words("catch class enum do else finally for forSome if match switch try while"),
  548. defKeywords: words("class enum def object package trait type val var"),
  549. atoms: words("true false null"),
  550. indentStatements: false,
  551. indentSwitch: false,
  552. isOperatorChar: /[+\-*&%=<>!?|\/#:@]/,
  553. hooks: {
  554. "@": function(stream) {
  555. stream.eatWhile(/[\w\$_]/);
  556. return "meta";
  557. },
  558. '"': function(stream, state) {
  559. if (!stream.match('""')) return false;
  560. state.tokenize = tokenTripleString;
  561. return state.tokenize(stream, state);
  562. },
  563. "'": function(stream) {
  564. stream.eatWhile(/[\w\$_\xa1-\uffff]/);
  565. return "atom";
  566. },
  567. "=": function(stream, state) {
  568. var cx = state.context
  569. if (cx.type == "}" && cx.align && stream.eat(">")) {
  570. state.context = new Context(cx.indented, cx.column, cx.type, cx.info, null, cx.prev)
  571. return "operator"
  572. } else {
  573. return false
  574. }
  575. },
  576. "/": function(stream, state) {
  577. if (!stream.eat("*")) return false
  578. state.tokenize = tokenNestedComment(1)
  579. return state.tokenize(stream, state)
  580. }
  581. },
  582. modeProps: {closeBrackets: {pairs: '()[]{}""', triples: '"'}}
  583. });
  584. function tokenKotlinString(tripleString){
  585. return function (stream, state) {
  586. var escaped = false, next, end = false;
  587. while (!stream.eol()) {
  588. if (!tripleString && !escaped && stream.match('"') ) {end = true; break;}
  589. if (tripleString && stream.match('"""')) {end = true; break;}
  590. next = stream.next();
  591. if(!escaped && next == "$" && stream.match('{'))
  592. stream.skipTo("}");
  593. escaped = !escaped && next == "\\" && !tripleString;
  594. }
  595. if (end || !tripleString)
  596. state.tokenize = null;
  597. return "string";
  598. }
  599. }
  600. def("text/x-kotlin", {
  601. name: "clike",
  602. keywords: words(
  603. /*keywords*/
  604. "package as typealias class interface this super val operator " +
  605. "var fun for is in This throw return annotation " +
  606. "break continue object if else while do try when !in !is as? " +
  607. /*soft keywords*/
  608. "file import where by get set abstract enum open inner override private public internal " +
  609. "protected catch finally out final vararg reified dynamic companion constructor init " +
  610. "sealed field property receiver param sparam lateinit data inline noinline tailrec " +
  611. "external annotation crossinline const operator infix suspend actual expect setparam"
  612. ),
  613. types: words(
  614. /* package java.lang */
  615. "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable " +
  616. "Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " +
  617. "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " +
  618. "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void Annotation Any BooleanArray " +
  619. "ByteArray Char CharArray DeprecationLevel DoubleArray Enum FloatArray Function Int IntArray Lazy " +
  620. "LazyThreadSafetyMode LongArray Nothing ShortArray Unit"
  621. ),
  622. intendSwitch: false,
  623. indentStatements: false,
  624. multiLineStrings: true,
  625. number: /^(?:0x[a-f\d_]+|0b[01_]+|(?:[\d_]+(\.\d+)?|\.\d+)(?:e[-+]?[\d_]+)?)(u|ll?|l|f)?/i,
  626. blockKeywords: words("catch class do else finally for if where try while enum"),
  627. defKeywords: words("class val var object interface fun"),
  628. atoms: words("true false null this"),
  629. hooks: {
  630. "@": function(stream) {
  631. stream.eatWhile(/[\w\$_]/);
  632. return "meta";
  633. },
  634. '*': function(_stream, state) {
  635. return state.prevToken == '.' ? 'variable' : 'operator';
  636. },
  637. '"': function(stream, state) {
  638. state.tokenize = tokenKotlinString(stream.match('""'));
  639. return state.tokenize(stream, state);
  640. },
  641. indent: function(state, ctx, textAfter, indentUnit) {
  642. var firstChar = textAfter && textAfter.charAt(0);
  643. if ((state.prevToken == "}" || state.prevToken == ")") && textAfter == "")
  644. return state.indented;
  645. if (state.prevToken == "operator" && textAfter != "}" ||
  646. state.prevToken == "variable" && firstChar == "." ||
  647. (state.prevToken == "}" || state.prevToken == ")") && firstChar == ".")
  648. return indentUnit * 2 + ctx.indented;
  649. if (ctx.align && ctx.type == "}")
  650. return ctx.indented + (state.context.type == (textAfter || "").charAt(0) ? 0 : indentUnit);
  651. }
  652. },
  653. modeProps: {closeBrackets: {triples: '"'}}
  654. });
  655. def(["x-shader/x-vertex", "x-shader/x-fragment"], {
  656. name: "clike",
  657. keywords: words("sampler1D sampler2D sampler3D samplerCube " +
  658. "sampler1DShadow sampler2DShadow " +
  659. "const attribute uniform varying " +
  660. "break continue discard return " +
  661. "for while do if else struct " +
  662. "in out inout"),
  663. types: words("float int bool void " +
  664. "vec2 vec3 vec4 ivec2 ivec3 ivec4 bvec2 bvec3 bvec4 " +
  665. "mat2 mat3 mat4"),
  666. blockKeywords: words("for while do if else struct"),
  667. builtin: words("radians degrees sin cos tan asin acos atan " +
  668. "pow exp log exp2 sqrt inversesqrt " +
  669. "abs sign floor ceil fract mod min max clamp mix step smoothstep " +
  670. "length distance dot cross normalize ftransform faceforward " +
  671. "reflect refract matrixCompMult " +
  672. "lessThan lessThanEqual greaterThan greaterThanEqual " +
  673. "equal notEqual any all not " +
  674. "texture1D texture1DProj texture1DLod texture1DProjLod " +
  675. "texture2D texture2DProj texture2DLod texture2DProjLod " +
  676. "texture3D texture3DProj texture3DLod texture3DProjLod " +
  677. "textureCube textureCubeLod " +
  678. "shadow1D shadow2D shadow1DProj shadow2DProj " +
  679. "shadow1DLod shadow2DLod shadow1DProjLod shadow2DProjLod " +
  680. "dFdx dFdy fwidth " +
  681. "noise1 noise2 noise3 noise4"),
  682. atoms: words("true false " +
  683. "gl_FragColor gl_SecondaryColor gl_Normal gl_Vertex " +
  684. "gl_MultiTexCoord0 gl_MultiTexCoord1 gl_MultiTexCoord2 gl_MultiTexCoord3 " +
  685. "gl_MultiTexCoord4 gl_MultiTexCoord5 gl_MultiTexCoord6 gl_MultiTexCoord7 " +
  686. "gl_FogCoord gl_PointCoord " +
  687. "gl_Position gl_PointSize gl_ClipVertex " +
  688. "gl_FrontColor gl_BackColor gl_FrontSecondaryColor gl_BackSecondaryColor " +
  689. "gl_TexCoord gl_FogFragCoord " +
  690. "gl_FragCoord gl_FrontFacing " +
  691. "gl_FragData gl_FragDepth " +
  692. "gl_ModelViewMatrix gl_ProjectionMatrix gl_ModelViewProjectionMatrix " +
  693. "gl_TextureMatrix gl_NormalMatrix gl_ModelViewMatrixInverse " +
  694. "gl_ProjectionMatrixInverse gl_ModelViewProjectionMatrixInverse " +
  695. "gl_TexureMatrixTranspose gl_ModelViewMatrixInverseTranspose " +
  696. "gl_ProjectionMatrixInverseTranspose " +
  697. "gl_ModelViewProjectionMatrixInverseTranspose " +
  698. "gl_TextureMatrixInverseTranspose " +
  699. "gl_NormalScale gl_DepthRange gl_ClipPlane " +
  700. "gl_Point gl_FrontMaterial gl_BackMaterial gl_LightSource gl_LightModel " +
  701. "gl_FrontLightModelProduct gl_BackLightModelProduct " +
  702. "gl_TextureColor gl_EyePlaneS gl_EyePlaneT gl_EyePlaneR gl_EyePlaneQ " +
  703. "gl_FogParameters " +
  704. "gl_MaxLights gl_MaxClipPlanes gl_MaxTextureUnits gl_MaxTextureCoords " +
  705. "gl_MaxVertexAttribs gl_MaxVertexUniformComponents gl_MaxVaryingFloats " +
  706. "gl_MaxVertexTextureImageUnits gl_MaxTextureImageUnits " +
  707. "gl_MaxFragmentUniformComponents gl_MaxCombineTextureImageUnits " +
  708. "gl_MaxDrawBuffers"),
  709. indentSwitch: false,
  710. hooks: {"#": cppHook},
  711. modeProps: {fold: ["brace", "include"]}
  712. });
  713. def("text/x-nesc", {
  714. name: "clike",
  715. keywords: words(cKeywords + " as atomic async call command component components configuration event generic " +
  716. "implementation includes interface module new norace nx_struct nx_union post provides " +
  717. "signal task uses abstract extends"),
  718. types: cTypes,
  719. blockKeywords: words(cBlockKeywords),
  720. atoms: words("null true false"),
  721. hooks: {"#": cppHook},
  722. modeProps: {fold: ["brace", "include"]}
  723. });
  724. def("text/x-objectivec", {
  725. name: "clike",
  726. keywords: words(cKeywords + " bycopy byref in inout oneway out self super atomic nonatomic retain copy " +
  727. "readwrite readonly strong weak assign typeof nullable nonnull null_resettable _cmd " +
  728. "@interface @implementation @end @protocol @encode @property @synthesize @dynamic @class " +
  729. "@public @package @private @protected @required @optional @try @catch @finally @import " +
  730. "@selector @encode @defs @synchronized @autoreleasepool @compatibility_alias @available"),
  731. types: objCTypes,
  732. builtin: words("FOUNDATION_EXPORT FOUNDATION_EXTERN NS_INLINE NS_FORMAT_FUNCTION NS_RETURNS_RETAINED " +
  733. "NS_ERROR_ENUM NS_RETURNS_NOT_RETAINED NS_RETURNS_INNER_POINTER NS_DESIGNATED_INITIALIZER " +
  734. "NS_ENUM NS_OPTIONS NS_REQUIRES_NIL_TERMINATION NS_ASSUME_NONNULL_BEGIN " +
  735. "NS_ASSUME_NONNULL_END NS_SWIFT_NAME NS_REFINED_FOR_SWIFT"),
  736. blockKeywords: words(cBlockKeywords + " @synthesize @try @catch @finally @autoreleasepool @synchronized"),
  737. defKeywords: words(cDefKeywords + " @interface @implementation @protocol @class"),
  738. dontIndentStatements: /^@.*$/,
  739. typeFirstDefinitions: true,
  740. atoms: words("YES NO NULL Nil nil true false nullptr"),
  741. isReservedIdentifier: cIsReservedIdentifier,
  742. hooks: {
  743. "#": cppHook,
  744. "*": pointerHook,
  745. },
  746. modeProps: {fold: ["brace", "include"]}
  747. });
  748. def("text/x-squirrel", {
  749. name: "clike",
  750. keywords: words("base break clone continue const default delete enum extends function in class" +
  751. " foreach local resume return this throw typeof yield constructor instanceof static"),
  752. types: cTypes,
  753. blockKeywords: words("case catch class else for foreach if switch try while"),
  754. defKeywords: words("function local class"),
  755. typeFirstDefinitions: true,
  756. atoms: words("true false null"),
  757. hooks: {"#": cppHook},
  758. modeProps: {fold: ["brace", "include"]}
  759. });
  760. // Ceylon Strings need to deal with interpolation
  761. var stringTokenizer = null;
  762. function tokenCeylonString(type) {
  763. return function(stream, state) {
  764. var escaped = false, next, end = false;
  765. while (!stream.eol()) {
  766. if (!escaped && stream.match('"') &&
  767. (type == "single" || stream.match('""'))) {
  768. end = true;
  769. break;
  770. }
  771. if (!escaped && stream.match('``')) {
  772. stringTokenizer = tokenCeylonString(type);
  773. end = true;
  774. break;
  775. }
  776. next = stream.next();
  777. escaped = type == "single" && !escaped && next == "\\";
  778. }
  779. if (end)
  780. state.tokenize = null;
  781. return "string";
  782. }
  783. }
  784. def("text/x-ceylon", {
  785. name: "clike",
  786. keywords: words("abstracts alias assembly assert assign break case catch class continue dynamic else" +
  787. " exists extends finally for function given if import in interface is let module new" +
  788. " nonempty object of out outer package return satisfies super switch then this throw" +
  789. " try value void while"),
  790. types: function(word) {
  791. // In Ceylon all identifiers that start with an uppercase are types
  792. var first = word.charAt(0);
  793. return (first === first.toUpperCase() && first !== first.toLowerCase());
  794. },
  795. blockKeywords: words("case catch class dynamic else finally for function if interface module new object switch try while"),
  796. defKeywords: words("class dynamic function interface module object package value"),
  797. builtin: words("abstract actual aliased annotation by default deprecated doc final formal late license" +
  798. " native optional sealed see serializable shared suppressWarnings tagged throws variable"),
  799. isPunctuationChar: /[\[\]{}\(\),;\:\.`]/,
  800. isOperatorChar: /[+\-*&%=<>!?|^~:\/]/,
  801. numberStart: /[\d#$]/,
  802. number: /^(?:#[\da-fA-F_]+|\$[01_]+|[\d_]+[kMGTPmunpf]?|[\d_]+\.[\d_]+(?:[eE][-+]?\d+|[kMGTPmunpf]|)|)/i,
  803. multiLineStrings: true,
  804. typeFirstDefinitions: true,
  805. atoms: words("true false null larger smaller equal empty finished"),
  806. indentSwitch: false,
  807. styleDefs: false,
  808. hooks: {
  809. "@": function(stream) {
  810. stream.eatWhile(/[\w\$_]/);
  811. return "meta";
  812. },
  813. '"': function(stream, state) {
  814. state.tokenize = tokenCeylonString(stream.match('""') ? "triple" : "single");
  815. return state.tokenize(stream, state);
  816. },
  817. '`': function(stream, state) {
  818. if (!stringTokenizer || !stream.match('`')) return false;
  819. state.tokenize = stringTokenizer;
  820. stringTokenizer = null;
  821. return state.tokenize(stream, state);
  822. },
  823. "'": function(stream) {
  824. stream.eatWhile(/[\w\$_\xa1-\uffff]/);
  825. return "atom";
  826. },
  827. token: function(_stream, state, style) {
  828. if ((style == "variable" || style == "type") &&
  829. state.prevToken == ".") {
  830. return "variable-2";
  831. }
  832. }
  833. },
  834. modeProps: {
  835. fold: ["brace", "import"],
  836. closeBrackets: {triples: '"'}
  837. }
  838. });
  839. });