2
0

verilog.js 6.4 KB

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