Theme Inspinia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

verilog.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. function words(str) {
  19. var obj = {}, words = str.split(" ");
  20. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  21. return obj;
  22. }
  23. /**
  24. * Keywords from IEEE 1800-2012
  25. */
  26. var keywords = words(
  27. "accept_on alias always always_comb always_ff always_latch and assert assign assume automatic before begin bind " +
  28. "bins binsof bit break buf bufif0 bufif1 byte case casex casez cell chandle checker class clocking cmos config " +
  29. "const constraint context continue cover covergroup coverpoint cross deassign default defparam design disable " +
  30. "dist do edge else end endcase endchecker endclass endclocking endconfig endfunction endgenerate endgroup " +
  31. "endinterface endmodule endpackage endprimitive endprogram endproperty endspecify endsequence endtable endtask " +
  32. "enum event eventually expect export extends extern final first_match for force foreach forever fork forkjoin " +
  33. "function generate genvar global highz0 highz1 if iff ifnone ignore_bins illegal_bins implements implies import " +
  34. "incdir include initial inout input inside instance int integer interconnect interface intersect join join_any " +
  35. "join_none large let liblist library local localparam logic longint macromodule matches medium modport module " +
  36. "nand negedge nettype new nexttime nmos nor noshowcancelled not notif0 notif1 null or output package packed " +
  37. "parameter pmos posedge primitive priority program property protected pull0 pull1 pulldown pullup " +
  38. "pulsestyle_ondetect pulsestyle_onevent pure rand randc randcase randsequence rcmos real realtime ref reg " +
  39. "reject_on release repeat restrict return rnmos rpmos rtran rtranif0 rtranif1 s_always s_eventually s_nexttime " +
  40. "s_until s_until_with scalared sequence shortint shortreal showcancelled signed small soft solve specify " +
  41. "specparam static string strong strong0 strong1 struct super supply0 supply1 sync_accept_on sync_reject_on " +
  42. "table tagged task this throughout time timeprecision timeunit tran tranif0 tranif1 tri tri0 tri1 triand trior " +
  43. "trireg type typedef union unique unique0 unsigned until until_with untyped use uwire var vectored virtual void " +
  44. "wait wait_order wand weak weak0 weak1 while wildcard wire with within wor xnor xor");
  45. /** Operators from IEEE 1800-2012
  46. unary_operator ::=
  47. + | - | ! | ~ | & | ~& | | | ~| | ^ | ~^ | ^~
  48. binary_operator ::=
  49. + | - | * | / | % | == | != | === | !== | ==? | !=? | && | || | **
  50. | < | <= | > | >= | & | | | ^ | ^~ | ~^ | >> | << | >>> | <<<
  51. | -> | <->
  52. inc_or_dec_operator ::= ++ | --
  53. unary_module_path_operator ::=
  54. ! | ~ | & | ~& | | | ~| | ^ | ~^ | ^~
  55. binary_module_path_operator ::=
  56. == | != | && | || | & | | | ^ | ^~ | ~^
  57. */
  58. var isOperatorChar = /[\+\-\*\/!~&|^%=?:]/;
  59. var isBracketChar = /[\[\]{}()]/;
  60. var unsignedNumber = /\d[0-9_]*/;
  61. var decimalLiteral = /\d*\s*'s?d\s*\d[0-9_]*/i;
  62. var binaryLiteral = /\d*\s*'s?b\s*[xz01][xz01_]*/i;
  63. var octLiteral = /\d*\s*'s?o\s*[xz0-7][xz0-7_]*/i;
  64. var hexLiteral = /\d*\s*'s?h\s*[0-9a-fxz?][0-9a-fxz?_]*/i;
  65. var realLiteral = /(\d[\d_]*(\.\d[\d_]*)?E-?[\d_]+)|(\d[\d_]*\.\d[\d_]*)/i;
  66. var closingBracketOrWord = /^((\w+)|[)}\]])/;
  67. var closingBracket = /[)}\]]/;
  68. var curPunc;
  69. var curKeyword;
  70. // Block openings which are closed by a matching keyword in the form of ("end" + keyword)
  71. // E.g. "task" => "endtask"
  72. var blockKeywords = words(
  73. "case checker class clocking config function generate interface module package" +
  74. "primitive program property specify sequence table task"
  75. );
  76. // Opening/closing pairs
  77. var openClose = {};
  78. for (var keyword in blockKeywords) {
  79. openClose[keyword] = "end" + keyword;
  80. }
  81. openClose["begin"] = "end";
  82. openClose["casex"] = "endcase";
  83. openClose["casez"] = "endcase";
  84. openClose["do" ] = "while";
  85. openClose["fork" ] = "join;join_any;join_none";
  86. openClose["covergroup"] = "endgroup";
  87. for (var i in noIndentKeywords) {
  88. var keyword = noIndentKeywords[i];
  89. if (openClose[keyword]) {
  90. openClose[keyword] = undefined;
  91. }
  92. }
  93. // Keywords which open statements that are ended with a semi-colon
  94. var statementKeywords = words("always always_comb always_ff always_latch assert assign assume else export for foreach forever if import initial repeat while");
  95. function tokenBase(stream, state) {
  96. var ch = stream.peek();
  97. if (/[,;:\.]/.test(ch)) {
  98. curPunc = stream.next();
  99. return null;
  100. }
  101. if (isBracketChar.test(ch)) {
  102. curPunc = stream.next();
  103. return "bracket";
  104. }
  105. // Macros (tick-defines)
  106. if (ch == '`') {
  107. stream.next();
  108. if (stream.eatWhile(/[\w\$_]/)) {
  109. return "def";
  110. } else {
  111. return null;
  112. }
  113. }
  114. // System calls
  115. if (ch == '$') {
  116. stream.next();
  117. if (stream.eatWhile(/[\w\$_]/)) {
  118. return "meta";
  119. } else {
  120. return null;
  121. }
  122. }
  123. // Time literals
  124. if (ch == '#') {
  125. stream.next();
  126. stream.eatWhile(/[\d_.]/);
  127. return "def";
  128. }
  129. // Strings
  130. if (ch == '"') {
  131. stream.next();
  132. state.tokenize = tokenString(ch);
  133. return state.tokenize(stream, state);
  134. }
  135. // Comments
  136. if (ch == "/") {
  137. stream.next();
  138. if (stream.eat("*")) {
  139. state.tokenize = tokenComment;
  140. return tokenComment(stream, state);
  141. }
  142. if (stream.eat("/")) {
  143. stream.skipToEnd();
  144. return "comment";
  145. }
  146. stream.backUp(1);
  147. }
  148. // Numeric literals
  149. if (stream.match(realLiteral) ||
  150. stream.match(decimalLiteral) ||
  151. stream.match(binaryLiteral) ||
  152. stream.match(octLiteral) ||
  153. stream.match(hexLiteral) ||
  154. stream.match(unsignedNumber) ||
  155. stream.match(realLiteral)) {
  156. return "number";
  157. }
  158. // Operators
  159. if (stream.eatWhile(isOperatorChar)) {
  160. return "meta";
  161. }
  162. // Keywords / plain variables
  163. if (stream.eatWhile(/[\w\$_]/)) {
  164. var cur = stream.current();
  165. if (keywords[cur]) {
  166. if (openClose[cur]) {
  167. curPunc = "newblock";
  168. }
  169. if (statementKeywords[cur]) {
  170. curPunc = "newstatement";
  171. }
  172. curKeyword = cur;
  173. return "keyword";
  174. }
  175. return "variable";
  176. }
  177. stream.next();
  178. return null;
  179. }
  180. function tokenString(quote) {
  181. return function(stream, state) {
  182. var escaped = false, next, end = false;
  183. while ((next = stream.next()) != null) {
  184. if (next == quote && !escaped) {end = true; break;}
  185. escaped = !escaped && next == "\\";
  186. }
  187. if (end || !(escaped || multiLineStrings))
  188. state.tokenize = tokenBase;
  189. return "string";
  190. };
  191. }
  192. function tokenComment(stream, state) {
  193. var maybeEnd = false, ch;
  194. while (ch = stream.next()) {
  195. if (ch == "/" && maybeEnd) {
  196. state.tokenize = tokenBase;
  197. break;
  198. }
  199. maybeEnd = (ch == "*");
  200. }
  201. return "comment";
  202. }
  203. function Context(indented, column, type, align, prev) {
  204. this.indented = indented;
  205. this.column = column;
  206. this.type = type;
  207. this.align = align;
  208. this.prev = prev;
  209. }
  210. function pushContext(state, col, type) {
  211. var indent = state.indented;
  212. var c = new Context(indent, col, type, null, state.context);
  213. return state.context = c;
  214. }
  215. function popContext(state) {
  216. var t = state.context.type;
  217. if (t == ")" || t == "]" || t == "}") {
  218. state.indented = state.context.indented;
  219. }
  220. return state.context = state.context.prev;
  221. }
  222. function isClosing(text, contextClosing) {
  223. if (text == contextClosing) {
  224. return true;
  225. } else {
  226. // contextClosing may be mulitple keywords separated by ;
  227. var closingKeywords = contextClosing.split(";");
  228. for (var i in closingKeywords) {
  229. if (text == closingKeywords[i]) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. }
  236. function buildElectricInputRegEx() {
  237. // Reindentation should occur on any bracket char: {}()[]
  238. // or on a match of any of the block closing keywords, at
  239. // the end of a line
  240. var allClosings = [];
  241. for (var i in openClose) {
  242. if (openClose[i]) {
  243. var closings = openClose[i].split(";");
  244. for (var j in closings) {
  245. allClosings.push(closings[j]);
  246. }
  247. }
  248. }
  249. var re = new RegExp("[{}()\\[\\]]|(" + allClosings.join("|") + ")$");
  250. return re;
  251. }
  252. // Interface
  253. return {
  254. // Regex to force current line to reindent
  255. electricInput: buildElectricInputRegEx(),
  256. startState: function(basecolumn) {
  257. return {
  258. tokenize: null,
  259. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  260. indented: 0,
  261. startOfLine: true
  262. };
  263. },
  264. token: function(stream, state) {
  265. var ctx = state.context;
  266. if (stream.sol()) {
  267. if (ctx.align == null) ctx.align = false;
  268. state.indented = stream.indentation();
  269. state.startOfLine = true;
  270. }
  271. if (stream.eatSpace()) return null;
  272. curPunc = null;
  273. curKeyword = null;
  274. var style = (state.tokenize || tokenBase)(stream, state);
  275. if (style == "comment" || style == "meta" || style == "variable") return style;
  276. if (ctx.align == null) ctx.align = true;
  277. if (curPunc == ctx.type) {
  278. popContext(state);
  279. }
  280. else if ((curPunc == ";" && ctx.type == "statement") ||
  281. (ctx.type && isClosing(curKeyword, ctx.type))) {
  282. ctx = popContext(state);
  283. while (ctx && ctx.type == "statement") ctx = popContext(state);
  284. }
  285. else if (curPunc == "{") { pushContext(state, stream.column(), "}"); }
  286. else if (curPunc == "[") { pushContext(state, stream.column(), "]"); }
  287. else if (curPunc == "(") { pushContext(state, stream.column(), ")"); }
  288. else if (ctx && ctx.type == "endcase" && curPunc == ":") { pushContext(state, stream.column(), "statement"); }
  289. else if (curPunc == "newstatement") {
  290. pushContext(state, stream.column(), "statement");
  291. } else if (curPunc == "newblock") {
  292. if (curKeyword == "function" && ctx && (ctx.type == "statement" || ctx.type == "endgroup")) {
  293. // The 'function' keyword can appear in some other contexts where it actually does not
  294. // indicate a function (import/export DPI and covergroup definitions).
  295. // Do nothing in this case
  296. } else if (curKeyword == "task" && ctx && ctx.type == "statement") {
  297. // Same thing for task
  298. } else {
  299. var close = openClose[curKeyword];
  300. pushContext(state, stream.column(), close);
  301. }
  302. }
  303. state.startOfLine = false;
  304. return style;
  305. },
  306. indent: function(state, textAfter) {
  307. if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
  308. var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
  309. if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
  310. var closing = false;
  311. var possibleClosing = textAfter.match(closingBracketOrWord);
  312. if (possibleClosing) {
  313. closing = isClosing(possibleClosing[0], ctx.type);
  314. }
  315. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
  316. else if (closingBracket.test(ctx.type) && ctx.align && !dontAlignCalls) return ctx.column + (closing ? 0 : 1);
  317. else if (ctx.type == ")" && !closing) return ctx.indented + statementIndentUnit;
  318. else return ctx.indented + (closing ? 0 : indentUnit);
  319. },
  320. blockCommentStart: "/*",
  321. blockCommentEnd: "*/",
  322. lineComment: "//"
  323. };
  324. });
  325. CodeMirror.defineMIME("text/x-verilog", {
  326. name: "verilog"
  327. });
  328. CodeMirror.defineMIME("text/x-systemverilog", {
  329. name: "systemverilog"
  330. });
  331. });