verilog.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. CodeMirror.defineMode("verilog", function(config, parserConfig) {
  13. var indentUnit = config.indentUnit,
  14. statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
  15. dontAlignCalls = parserConfig.dontAlignCalls,
  16. noIndentKeywords = parserConfig.noIndentKeywords || [],
  17. multiLineStrings = parserConfig.multiLineStrings,
  18. hooks = parserConfig.hooks || {};
  19. function words(str) {
  20. var obj = {}, words = str.split(" ");
  21. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  22. return obj;
  23. }
  24. /**
  25. * Keywords from IEEE 1800-2012
  26. */
  27. var keywords = words(
  28. "accept_on alias always always_comb always_ff always_latch and assert assign assume automatic before begin bind " +
  29. "bins binsof bit break buf bufif0 bufif1 byte case casex casez cell chandle checker class clocking cmos config " +
  30. "const constraint context continue cover covergroup coverpoint cross deassign default defparam design disable " +
  31. "dist do edge else end endcase endchecker endclass endclocking endconfig endfunction endgenerate endgroup " +
  32. "endinterface endmodule endpackage endprimitive endprogram endproperty endspecify endsequence endtable endtask " +
  33. "enum event eventually expect export extends extern final first_match for force foreach forever fork forkjoin " +
  34. "function generate genvar global highz0 highz1 if iff ifnone ignore_bins illegal_bins implements implies import " +
  35. "incdir include initial inout input inside instance int integer interconnect interface intersect join join_any " +
  36. "join_none large let liblist library local localparam logic longint macromodule matches medium modport module " +
  37. "nand negedge nettype new nexttime nmos nor noshowcancelled not notif0 notif1 null or output package packed " +
  38. "parameter pmos posedge primitive priority program property protected pull0 pull1 pulldown pullup " +
  39. "pulsestyle_ondetect pulsestyle_onevent pure rand randc randcase randsequence rcmos real realtime ref reg " +
  40. "reject_on release repeat restrict return rnmos rpmos rtran rtranif0 rtranif1 s_always s_eventually s_nexttime " +
  41. "s_until s_until_with scalared sequence shortint shortreal showcancelled signed small soft solve specify " +
  42. "specparam static string strong strong0 strong1 struct super supply0 supply1 sync_accept_on sync_reject_on " +
  43. "table tagged task this throughout time timeprecision timeunit tran tranif0 tranif1 tri tri0 tri1 triand trior " +
  44. "trireg type typedef union unique unique0 unsigned until until_with untyped use uwire var vectored virtual void " +
  45. "wait wait_order wand weak weak0 weak1 while wildcard wire with within wor xnor xor");
  46. /** Operators from IEEE 1800-2012
  47. unary_operator ::=
  48. + | - | ! | ~ | & | ~& | | | ~| | ^ | ~^ | ^~
  49. binary_operator ::=
  50. + | - | * | / | % | == | != | === | !== | ==? | !=? | && | || | **
  51. | < | <= | > | >= | & | | | ^ | ^~ | ~^ | >> | << | >>> | <<<
  52. | -> | <->
  53. inc_or_dec_operator ::= ++ | --
  54. unary_module_path_operator ::=
  55. ! | ~ | & | ~& | | | ~| | ^ | ~^ | ^~
  56. binary_module_path_operator ::=
  57. == | != | && | || | & | | | ^ | ^~ | ~^
  58. */
  59. var isOperatorChar = /[\+\-\*\/!~&|^%=?:]/;
  60. var isBracketChar = /[\[\]{}()]/;
  61. var unsignedNumber = /\d[0-9_]*/;
  62. var decimalLiteral = /\d*\s*'s?d\s*\d[0-9_]*/i;
  63. var binaryLiteral = /\d*\s*'s?b\s*[xz01][xz01_]*/i;
  64. var octLiteral = /\d*\s*'s?o\s*[xz0-7][xz0-7_]*/i;
  65. var hexLiteral = /\d*\s*'s?h\s*[0-9a-fxz?][0-9a-fxz?_]*/i;
  66. var realLiteral = /(\d[\d_]*(\.\d[\d_]*)?E-?[\d_]+)|(\d[\d_]*\.\d[\d_]*)/i;
  67. var closingBracketOrWord = /^((\w+)|[)}\]])/;
  68. var closingBracket = /[)}\]]/;
  69. var curPunc;
  70. var curKeyword;
  71. // Block openings which are closed by a matching keyword in the form of ("end" + keyword)
  72. // E.g. "task" => "endtask"
  73. var blockKeywords = words(
  74. "case checker class clocking config function generate interface module package " +
  75. "primitive program property specify sequence table task"
  76. );
  77. // Opening/closing pairs
  78. var openClose = {};
  79. for (var keyword in blockKeywords) {
  80. openClose[keyword] = "end" + keyword;
  81. }
  82. openClose["begin"] = "end";
  83. openClose["casex"] = "endcase";
  84. openClose["casez"] = "endcase";
  85. openClose["do" ] = "while";
  86. openClose["fork" ] = "join;join_any;join_none";
  87. openClose["covergroup"] = "endgroup";
  88. for (var i in noIndentKeywords) {
  89. var keyword = noIndentKeywords[i];
  90. if (openClose[keyword]) {
  91. openClose[keyword] = undefined;
  92. }
  93. }
  94. // Keywords which open statements that are ended with a semi-colon
  95. var statementKeywords = words("always always_comb always_ff always_latch assert assign assume else export for foreach forever if import initial repeat while");
  96. function tokenBase(stream, state) {
  97. var ch = stream.peek(), style;
  98. if (hooks[ch] && (style = hooks[ch](stream, state)) != false) return style;
  99. if (hooks.tokenBase && (style = hooks.tokenBase(stream, state)) != false)
  100. return style;
  101. if (/[,;:\.]/.test(ch)) {
  102. curPunc = stream.next();
  103. return null;
  104. }
  105. if (isBracketChar.test(ch)) {
  106. curPunc = stream.next();
  107. return "bracket";
  108. }
  109. // Macros (tick-defines)
  110. if (ch == '`') {
  111. stream.next();
  112. if (stream.eatWhile(/[\w\$_]/)) {
  113. return "def";
  114. } else {
  115. return null;
  116. }
  117. }
  118. // System calls
  119. if (ch == '$') {
  120. stream.next();
  121. if (stream.eatWhile(/[\w\$_]/)) {
  122. return "meta";
  123. } else {
  124. return null;
  125. }
  126. }
  127. // Time literals
  128. if (ch == '#') {
  129. stream.next();
  130. stream.eatWhile(/[\d_.]/);
  131. return "def";
  132. }
  133. // Strings
  134. if (ch == '"') {
  135. stream.next();
  136. state.tokenize = tokenString(ch);
  137. return state.tokenize(stream, state);
  138. }
  139. // Comments
  140. if (ch == "/") {
  141. stream.next();
  142. if (stream.eat("*")) {
  143. state.tokenize = tokenComment;
  144. return tokenComment(stream, state);
  145. }
  146. if (stream.eat("/")) {
  147. stream.skipToEnd();
  148. return "comment";
  149. }
  150. stream.backUp(1);
  151. }
  152. // Numeric literals
  153. if (stream.match(realLiteral) ||
  154. stream.match(decimalLiteral) ||
  155. stream.match(binaryLiteral) ||
  156. stream.match(octLiteral) ||
  157. stream.match(hexLiteral) ||
  158. stream.match(unsignedNumber) ||
  159. stream.match(realLiteral)) {
  160. return "number";
  161. }
  162. // Operators
  163. if (stream.eatWhile(isOperatorChar)) {
  164. return "meta";
  165. }
  166. // Keywords / plain variables
  167. if (stream.eatWhile(/[\w\$_]/)) {
  168. var cur = stream.current();
  169. if (keywords[cur]) {
  170. if (openClose[cur]) {
  171. curPunc = "newblock";
  172. }
  173. if (statementKeywords[cur]) {
  174. curPunc = "newstatement";
  175. }
  176. curKeyword = cur;
  177. return "keyword";
  178. }
  179. return "variable";
  180. }
  181. stream.next();
  182. return null;
  183. }
  184. function tokenString(quote) {
  185. return function(stream, state) {
  186. var escaped = false, next, end = false;
  187. while ((next = stream.next()) != null) {
  188. if (next == quote && !escaped) {end = true; break;}
  189. escaped = !escaped && next == "\\";
  190. }
  191. if (end || !(escaped || multiLineStrings))
  192. state.tokenize = tokenBase;
  193. return "string";
  194. };
  195. }
  196. function tokenComment(stream, state) {
  197. var maybeEnd = false, ch;
  198. while (ch = stream.next()) {
  199. if (ch == "/" && maybeEnd) {
  200. state.tokenize = tokenBase;
  201. break;
  202. }
  203. maybeEnd = (ch == "*");
  204. }
  205. return "comment";
  206. }
  207. function Context(indented, column, type, align, prev) {
  208. this.indented = indented;
  209. this.column = column;
  210. this.type = type;
  211. this.align = align;
  212. this.prev = prev;
  213. }
  214. function pushContext(state, col, type) {
  215. var indent = state.indented;
  216. var c = new Context(indent, col, type, null, state.context);
  217. return state.context = c;
  218. }
  219. function popContext(state) {
  220. var t = state.context.type;
  221. if (t == ")" || t == "]" || t == "}") {
  222. state.indented = state.context.indented;
  223. }
  224. return state.context = state.context.prev;
  225. }
  226. function isClosing(text, contextClosing) {
  227. if (text == contextClosing) {
  228. return true;
  229. } else {
  230. // contextClosing may be multiple keywords separated by ;
  231. var closingKeywords = contextClosing.split(";");
  232. for (var i in closingKeywords) {
  233. if (text == closingKeywords[i]) {
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. }
  240. function buildElectricInputRegEx() {
  241. // Reindentation should occur on any bracket char: {}()[]
  242. // or on a match of any of the block closing keywords, at
  243. // the end of a line
  244. var allClosings = [];
  245. for (var i in openClose) {
  246. if (openClose[i]) {
  247. var closings = openClose[i].split(";");
  248. for (var j in closings) {
  249. allClosings.push(closings[j]);
  250. }
  251. }
  252. }
  253. var re = new RegExp("[{}()\\[\\]]|(" + allClosings.join("|") + ")$");
  254. return re;
  255. }
  256. // Interface
  257. return {
  258. // Regex to force current line to reindent
  259. electricInput: buildElectricInputRegEx(),
  260. startState: function(basecolumn) {
  261. var state = {
  262. tokenize: null,
  263. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  264. indented: 0,
  265. startOfLine: true
  266. };
  267. if (hooks.startState) hooks.startState(state);
  268. return state;
  269. },
  270. token: function(stream, state) {
  271. var ctx = state.context;
  272. if (stream.sol()) {
  273. if (ctx.align == null) ctx.align = false;
  274. state.indented = stream.indentation();
  275. state.startOfLine = true;
  276. }
  277. if (hooks.token) {
  278. // Call hook, with an optional return value of a style to override verilog styling.
  279. var style = hooks.token(stream, state);
  280. if (style !== undefined) {
  281. return style;
  282. }
  283. }
  284. if (stream.eatSpace()) return null;
  285. curPunc = null;
  286. curKeyword = null;
  287. var style = (state.tokenize || tokenBase)(stream, state);
  288. if (style == "comment" || style == "meta" || style == "variable") return style;
  289. if (ctx.align == null) ctx.align = true;
  290. if (curPunc == ctx.type) {
  291. popContext(state);
  292. } else if ((curPunc == ";" && ctx.type == "statement") ||
  293. (ctx.type && isClosing(curKeyword, ctx.type))) {
  294. ctx = popContext(state);
  295. while (ctx && ctx.type == "statement") ctx = popContext(state);
  296. } else if (curPunc == "{") {
  297. pushContext(state, stream.column(), "}");
  298. } else if (curPunc == "[") {
  299. pushContext(state, stream.column(), "]");
  300. } else if (curPunc == "(") {
  301. pushContext(state, stream.column(), ")");
  302. } else if (ctx && ctx.type == "endcase" && curPunc == ":") {
  303. pushContext(state, stream.column(), "statement");
  304. } else if (curPunc == "newstatement") {
  305. pushContext(state, stream.column(), "statement");
  306. } else if (curPunc == "newblock") {
  307. if (curKeyword == "function" && ctx && (ctx.type == "statement" || ctx.type == "endgroup")) {
  308. // The 'function' keyword can appear in some other contexts where it actually does not
  309. // indicate a function (import/export DPI and covergroup definitions).
  310. // Do nothing in this case
  311. } else if (curKeyword == "task" && ctx && ctx.type == "statement") {
  312. // Same thing for task
  313. } else {
  314. var close = openClose[curKeyword];
  315. pushContext(state, stream.column(), close);
  316. }
  317. }
  318. state.startOfLine = false;
  319. return style;
  320. },
  321. indent: function(state, textAfter) {
  322. if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
  323. if (hooks.indent) {
  324. var fromHook = hooks.indent(state);
  325. if (fromHook >= 0) return fromHook;
  326. }
  327. var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
  328. if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
  329. var closing = false;
  330. var possibleClosing = textAfter.match(closingBracketOrWord);
  331. if (possibleClosing)
  332. closing = isClosing(possibleClosing[0], ctx.type);
  333. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
  334. else if (closingBracket.test(ctx.type) && ctx.align && !dontAlignCalls) return ctx.column + (closing ? 0 : 1);
  335. else if (ctx.type == ")" && !closing) return ctx.indented + statementIndentUnit;
  336. else return ctx.indented + (closing ? 0 : indentUnit);
  337. },
  338. blockCommentStart: "/*",
  339. blockCommentEnd: "*/",
  340. lineComment: "//"
  341. };
  342. });
  343. CodeMirror.defineMIME("text/x-verilog", {
  344. name: "verilog"
  345. });
  346. CodeMirror.defineMIME("text/x-systemverilog", {
  347. name: "verilog"
  348. });
  349. // TL-Verilog mode.
  350. // See tl-x.org for language spec.
  351. // See the mode in action at makerchip.com.
  352. // Contact: steve.hoover@redwoodeda.com
  353. // TLV Identifier prefixes.
  354. // Note that sign is not treated separately, so "+/-" versions of numeric identifiers
  355. // are included.
  356. var tlvIdentifierStyle = {
  357. "|": "link",
  358. ">": "property", // Should condition this off for > TLV 1c.
  359. "$": "variable",
  360. "$$": "variable",
  361. "?$": "qualifier",
  362. "?*": "qualifier",
  363. "-": "hr",
  364. "/": "property",
  365. "/-": "property",
  366. "@": "variable-3",
  367. "@-": "variable-3",
  368. "@++": "variable-3",
  369. "@+=": "variable-3",
  370. "@+=-": "variable-3",
  371. "@--": "variable-3",
  372. "@-=": "variable-3",
  373. "%+": "tag",
  374. "%-": "tag",
  375. "%": "tag",
  376. ">>": "tag",
  377. "<<": "tag",
  378. "<>": "tag",
  379. "#": "tag", // Need to choose a style for this.
  380. "^": "attribute",
  381. "^^": "attribute",
  382. "^!": "attribute",
  383. "*": "variable-2",
  384. "**": "variable-2",
  385. "\\": "keyword",
  386. "\"": "comment"
  387. };
  388. // Lines starting with these characters define scope (result in indentation).
  389. var tlvScopePrefixChars = {
  390. "/": "beh-hier",
  391. ">": "beh-hier",
  392. "-": "phys-hier",
  393. "|": "pipe",
  394. "?": "when",
  395. "@": "stage",
  396. "\\": "keyword"
  397. };
  398. var tlvIndentUnit = 3;
  399. var tlvTrackStatements = false;
  400. var tlvIdentMatch = /^([~!@#\$%\^&\*-\+=\?\/\\\|'"<>]+)([\d\w_]*)/; // Matches an identifiere.
  401. // Note that ':' is excluded, because of it's use in [:].
  402. var tlvFirstLevelIndentMatch = /^[! ] /;
  403. var tlvLineIndentationMatch = /^[! ] */;
  404. var tlvCommentMatch = /^\/[\/\*]/;
  405. // Returns a style specific to the scope at the given indentation column.
  406. // Type is one of: "indent", "scope-ident", "before-scope-ident".
  407. function tlvScopeStyle(state, indentation, type) {
  408. // Begin scope.
  409. var depth = indentation / tlvIndentUnit; // TODO: Pass this in instead.
  410. return "tlv-" + state.tlvIndentationStyle[depth] + "-" + type;
  411. }
  412. // Return true if the next thing in the stream is an identifier with a mnemonic.
  413. function tlvIdentNext(stream) {
  414. var match;
  415. return (match = stream.match(tlvIdentMatch, false)) && match[2].length > 0;
  416. }
  417. CodeMirror.defineMIME("text/x-tlv", {
  418. name: "verilog",
  419. hooks: {
  420. electricInput: false,
  421. // Return undefined for verilog tokenizing, or style for TLV token (null not used).
  422. // Standard CM styles are used for most formatting, but some TL-Verilog-specific highlighting
  423. // can be enabled with the definition of cm-tlv-* styles, including highlighting for:
  424. // - M4 tokens
  425. // - TLV scope indentation
  426. // - Statement delimitation (enabled by tlvTrackStatements)
  427. token: function(stream, state) {
  428. var style = undefined;
  429. var match; // Return value of pattern matches.
  430. // Set highlighting mode based on code region (TLV or SV).
  431. if (stream.sol() && ! state.tlvInBlockComment) {
  432. // Process region.
  433. if (stream.peek() == '\\') {
  434. style = "def";
  435. stream.skipToEnd();
  436. if (stream.string.match(/\\SV/)) {
  437. state.tlvCodeActive = false;
  438. } else if (stream.string.match(/\\TLV/)){
  439. state.tlvCodeActive = true;
  440. }
  441. }
  442. // Correct indentation in the face of a line prefix char.
  443. if (state.tlvCodeActive && stream.pos == 0 &&
  444. (state.indented == 0) && (match = stream.match(tlvLineIndentationMatch, false))) {
  445. state.indented = match[0].length;
  446. }
  447. // Compute indentation state:
  448. // o Auto indentation on next line
  449. // o Indentation scope styles
  450. var indented = state.indented;
  451. var depth = indented / tlvIndentUnit;
  452. if (depth <= state.tlvIndentationStyle.length) {
  453. // not deeper than current scope
  454. var blankline = stream.string.length == indented;
  455. var chPos = depth * tlvIndentUnit;
  456. if (chPos < stream.string.length) {
  457. var bodyString = stream.string.slice(chPos);
  458. var ch = bodyString[0];
  459. if (tlvScopePrefixChars[ch] && ((match = bodyString.match(tlvIdentMatch)) &&
  460. tlvIdentifierStyle[match[1]])) {
  461. // This line begins scope.
  462. // Next line gets indented one level.
  463. indented += tlvIndentUnit;
  464. // Style the next level of indentation (except non-region keyword identifiers,
  465. // which are statements themselves)
  466. if (!(ch == "\\" && chPos > 0)) {
  467. state.tlvIndentationStyle[depth] = tlvScopePrefixChars[ch];
  468. if (tlvTrackStatements) {state.statementComment = false;}
  469. depth++;
  470. }
  471. }
  472. }
  473. // Clear out deeper indentation levels unless line is blank.
  474. if (!blankline) {
  475. while (state.tlvIndentationStyle.length > depth) {
  476. state.tlvIndentationStyle.pop();
  477. }
  478. }
  479. }
  480. // Set next level of indentation.
  481. state.tlvNextIndent = indented;
  482. }
  483. if (state.tlvCodeActive) {
  484. // Highlight as TLV.
  485. var beginStatement = false;
  486. if (tlvTrackStatements) {
  487. // This starts a statement if the position is at the scope level
  488. // and we're not within a statement leading comment.
  489. beginStatement =
  490. (stream.peek() != " ") && // not a space
  491. (style === undefined) && // not a region identifier
  492. !state.tlvInBlockComment && // not in block comment
  493. //!stream.match(tlvCommentMatch, false) && // not comment start
  494. (stream.column() == state.tlvIndentationStyle.length * tlvIndentUnit); // at scope level
  495. if (beginStatement) {
  496. if (state.statementComment) {
  497. // statement already started by comment
  498. beginStatement = false;
  499. }
  500. state.statementComment =
  501. stream.match(tlvCommentMatch, false); // comment start
  502. }
  503. }
  504. var match;
  505. if (style !== undefined) {
  506. // Region line.
  507. style += " " + tlvScopeStyle(state, 0, "scope-ident")
  508. } else if (((stream.pos / tlvIndentUnit) < state.tlvIndentationStyle.length) &&
  509. (match = stream.match(stream.sol() ? tlvFirstLevelIndentMatch : /^ /))) {
  510. // Indentation
  511. style = // make this style distinct from the previous one to prevent
  512. // codemirror from combining spans
  513. "tlv-indent-" + (((stream.pos % 2) == 0) ? "even" : "odd") +
  514. // and style it
  515. " " + tlvScopeStyle(state, stream.pos - tlvIndentUnit, "indent");
  516. // Style the line prefix character.
  517. if (match[0].charAt(0) == "!") {
  518. style += " tlv-alert-line-prefix";
  519. }
  520. // Place a class before a scope identifier.
  521. if (tlvIdentNext(stream)) {
  522. style += " " + tlvScopeStyle(state, stream.pos, "before-scope-ident");
  523. }
  524. } else if (state.tlvInBlockComment) {
  525. // In a block comment.
  526. if (stream.match(/^.*?\*\//)) {
  527. // Exit block comment.
  528. state.tlvInBlockComment = false;
  529. if (tlvTrackStatements && !stream.eol()) {
  530. // Anything after comment is assumed to be real statement content.
  531. state.statementComment = false;
  532. }
  533. } else {
  534. stream.skipToEnd();
  535. }
  536. style = "comment";
  537. } else if ((match = stream.match(tlvCommentMatch)) && !state.tlvInBlockComment) {
  538. // Start comment.
  539. if (match[0] == "//") {
  540. // Line comment.
  541. stream.skipToEnd();
  542. } else {
  543. // Block comment.
  544. state.tlvInBlockComment = true;
  545. }
  546. style = "comment";
  547. } else if (match = stream.match(tlvIdentMatch)) {
  548. // looks like an identifier (or identifier prefix)
  549. var prefix = match[1];
  550. var mnemonic = match[2];
  551. if (// is identifier prefix
  552. tlvIdentifierStyle.hasOwnProperty(prefix) &&
  553. // has mnemonic or we're at the end of the line (maybe it hasn't been typed yet)
  554. (mnemonic.length > 0 || stream.eol())) {
  555. style = tlvIdentifierStyle[prefix];
  556. if (stream.column() == state.indented) {
  557. // Begin scope.
  558. style += " " + tlvScopeStyle(state, stream.column(), "scope-ident")
  559. }
  560. } else {
  561. // Just swallow one character and try again.
  562. // This enables subsequent identifier match with preceding symbol character, which
  563. // is legal within a statement. (Eg, !$reset). It also enables detection of
  564. // comment start with preceding symbols.
  565. stream.backUp(stream.current().length - 1);
  566. style = "tlv-default";
  567. }
  568. } else if (stream.match(/^\t+/)) {
  569. // Highlight tabs, which are illegal.
  570. style = "tlv-tab";
  571. } else if (stream.match(/^[\[\]{}\(\);\:]+/)) {
  572. // [:], (), {}, ;.
  573. style = "meta";
  574. } else if (match = stream.match(/^[mM]4([\+_])?[\w\d_]*/)) {
  575. // m4 pre proc
  576. style = (match[1] == "+") ? "tlv-m4-plus" : "tlv-m4";
  577. } else if (stream.match(/^ +/)){
  578. // Skip over spaces.
  579. if (stream.eol()) {
  580. // Trailing spaces.
  581. style = "error";
  582. } else {
  583. // Non-trailing spaces.
  584. style = "tlv-default";
  585. }
  586. } else if (stream.match(/^[\w\d_]+/)) {
  587. // alpha-numeric token.
  588. style = "number";
  589. } else {
  590. // Eat the next char w/ no formatting.
  591. stream.next();
  592. style = "tlv-default";
  593. }
  594. if (beginStatement) {
  595. style += " tlv-statement";
  596. }
  597. } else {
  598. if (stream.match(/^[mM]4([\w\d_]*)/)) {
  599. // m4 pre proc
  600. style = "tlv-m4";
  601. }
  602. }
  603. return style;
  604. },
  605. indent: function(state) {
  606. return (state.tlvCodeActive == true) ? state.tlvNextIndent : -1;
  607. },
  608. startState: function(state) {
  609. state.tlvIndentationStyle = []; // Styles to use for each level of indentation.
  610. state.tlvCodeActive = true; // True when we're in a TLV region (and at beginning of file).
  611. state.tlvNextIndent = -1; // The number of spaces to autoindent the next line if tlvCodeActive.
  612. state.tlvInBlockComment = false; // True inside /**/ comment.
  613. if (tlvTrackStatements) {
  614. state.statementComment = false; // True inside a statement's header comment.
  615. }
  616. }
  617. }
  618. });
  619. });