verilog.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("verilog", function(config, parserConfig) {
  11. var indentUnit = config.indentUnit,
  12. keywords = parserConfig.keywords || {},
  13. blockKeywords = parserConfig.blockKeywords || {},
  14. atoms = parserConfig.atoms || {},
  15. hooks = parserConfig.hooks || {},
  16. multiLineStrings = parserConfig.multiLineStrings;
  17. var isOperatorChar = /[&|~><!\)\(*#%@+\/=?\:;}{,\.\^\-\[\]]/;
  18. var curPunc;
  19. function tokenBase(stream, state) {
  20. var ch = stream.next();
  21. if (hooks[ch]) {
  22. var result = hooks[ch](stream, state);
  23. if (result !== false) return result;
  24. }
  25. if (ch == '"') {
  26. state.tokenize = tokenString(ch);
  27. return state.tokenize(stream, state);
  28. }
  29. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  30. curPunc = ch;
  31. return null;
  32. }
  33. if (/[\d']/.test(ch)) {
  34. stream.eatWhile(/[\w\.']/);
  35. return "number";
  36. }
  37. if (ch == "/") {
  38. if (stream.eat("*")) {
  39. state.tokenize = tokenComment;
  40. return tokenComment(stream, state);
  41. }
  42. if (stream.eat("/")) {
  43. stream.skipToEnd();
  44. return "comment";
  45. }
  46. }
  47. if (isOperatorChar.test(ch)) {
  48. stream.eatWhile(isOperatorChar);
  49. return "operator";
  50. }
  51. stream.eatWhile(/[\w\$_]/);
  52. var cur = stream.current();
  53. if (keywords.propertyIsEnumerable(cur)) {
  54. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  55. return "keyword";
  56. }
  57. if (atoms.propertyIsEnumerable(cur)) return "atom";
  58. return "variable";
  59. }
  60. function tokenString(quote) {
  61. return function(stream, state) {
  62. var escaped = false, next, end = false;
  63. while ((next = stream.next()) != null) {
  64. if (next == quote && !escaped) {end = true; break;}
  65. escaped = !escaped && next == "\\";
  66. }
  67. if (end || !(escaped || multiLineStrings))
  68. state.tokenize = tokenBase;
  69. return "string";
  70. };
  71. }
  72. function tokenComment(stream, state) {
  73. var maybeEnd = false, ch;
  74. while (ch = stream.next()) {
  75. if (ch == "/" && maybeEnd) {
  76. state.tokenize = tokenBase;
  77. break;
  78. }
  79. maybeEnd = (ch == "*");
  80. }
  81. return "comment";
  82. }
  83. function Context(indented, column, type, align, prev) {
  84. this.indented = indented;
  85. this.column = column;
  86. this.type = type;
  87. this.align = align;
  88. this.prev = prev;
  89. }
  90. function pushContext(state, col, type) {
  91. return state.context = new Context(state.indented, col, type, null, state.context);
  92. }
  93. function popContext(state) {
  94. var t = state.context.type;
  95. if (t == ")" || t == "]" || t == "}")
  96. state.indented = state.context.indented;
  97. return state.context = state.context.prev;
  98. }
  99. // Interface
  100. return {
  101. startState: function(basecolumn) {
  102. return {
  103. tokenize: null,
  104. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  105. indented: 0,
  106. startOfLine: true
  107. };
  108. },
  109. token: function(stream, state) {
  110. var ctx = state.context;
  111. if (stream.sol()) {
  112. if (ctx.align == null) ctx.align = false;
  113. state.indented = stream.indentation();
  114. state.startOfLine = true;
  115. }
  116. if (stream.eatSpace()) return null;
  117. curPunc = null;
  118. var style = (state.tokenize || tokenBase)(stream, state);
  119. if (style == "comment" || style == "meta") return style;
  120. if (ctx.align == null) ctx.align = true;
  121. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  122. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  123. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  124. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  125. else if (curPunc == "}") {
  126. while (ctx.type == "statement") ctx = popContext(state);
  127. if (ctx.type == "}") ctx = popContext(state);
  128. while (ctx.type == "statement") ctx = popContext(state);
  129. }
  130. else if (curPunc == ctx.type) popContext(state);
  131. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  132. pushContext(state, stream.column(), "statement");
  133. state.startOfLine = false;
  134. return style;
  135. },
  136. indent: function(state, textAfter) {
  137. if (state.tokenize != tokenBase && state.tokenize != null) return 0;
  138. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type;
  139. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit);
  140. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  141. else return ctx.indented + (closing ? 0 : indentUnit);
  142. },
  143. electricChars: "{}"
  144. };
  145. });
  146. function words(str) {
  147. var obj = {}, words = str.split(" ");
  148. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  149. return obj;
  150. }
  151. var verilogKeywords = "always and assign automatic begin buf bufif0 bufif1 case casex casez cell cmos config " +
  152. "deassign default defparam design disable edge else end endcase endconfig endfunction endgenerate endmodule " +
  153. "endprimitive endspecify endtable endtask event for force forever fork function generate genvar highz0 " +
  154. "highz1 if ifnone incdir include initial inout input instance integer join large liblist library localparam " +
  155. "macromodule medium module nand negedge nmos nor noshowcancelled not notif0 notif1 or output parameter pmos " +
  156. "posedge primitive pull0 pull1 pulldown pullup pulsestyle_onevent pulsestyle_ondetect rcmos real realtime " +
  157. "reg release repeat rnmos rpmos rtran rtranif0 rtranif1 scalared showcancelled signed small specify specparam " +
  158. "strong0 strong1 supply0 supply1 table task time tran tranif0 tranif1 tri tri0 tri1 triand trior trireg " +
  159. "unsigned use vectored wait wand weak0 weak1 while wire wor xnor xor";
  160. var verilogBlockKeywords = "begin bufif0 bufif1 case casex casez config else end endcase endconfig endfunction " +
  161. "endgenerate endmodule endprimitive endspecify endtable endtask for forever function generate if ifnone " +
  162. "macromodule module primitive repeat specify table task while";
  163. function metaHook(stream) {
  164. stream.eatWhile(/[\w\$_]/);
  165. return "meta";
  166. }
  167. CodeMirror.defineMIME("text/x-verilog", {
  168. name: "verilog",
  169. keywords: words(verilogKeywords),
  170. blockKeywords: words(verilogBlockKeywords),
  171. atoms: words("null"),
  172. hooks: {"`": metaHook, "$": metaHook}
  173. });
  174. });