vhdl.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Originally written by Alf Nielsen, re-written by Michael Zhou
  4. (function(mod) {
  5. if (typeof exports == "object" && typeof module == "object") // CommonJS
  6. mod(require("../../lib/codemirror"));
  7. else if (typeof define == "function" && define.amd) // AMD
  8. define(["../../lib/codemirror"], mod);
  9. else // Plain browser env
  10. mod(CodeMirror);
  11. })(function(CodeMirror) {
  12. "use strict";
  13. function words(str) {
  14. var obj = {}, words = str.split(",");
  15. for (var i = 0; i < words.length; ++i) {
  16. var allCaps = words[i].toUpperCase();
  17. var firstCap = words[i].charAt(0).toUpperCase() + words[i].slice(1);
  18. obj[words[i]] = true;
  19. obj[allCaps] = true;
  20. obj[firstCap] = true;
  21. }
  22. return obj;
  23. }
  24. function metaHook(stream) {
  25. stream.eatWhile(/[\w\$_]/);
  26. return "meta";
  27. }
  28. CodeMirror.defineMode("vhdl", function(config, parserConfig) {
  29. var indentUnit = config.indentUnit,
  30. atoms = parserConfig.atoms || words("null"),
  31. hooks = parserConfig.hooks || {"`": metaHook, "$": metaHook},
  32. multiLineStrings = parserConfig.multiLineStrings;
  33. var keywords = words("abs,access,after,alias,all,and,architecture,array,assert,attribute,begin,block," +
  34. "body,buffer,bus,case,component,configuration,constant,disconnect,downto,else,elsif,end,end block,end case," +
  35. "end component,end for,end generate,end if,end loop,end process,end record,end units,entity,exit,file,for," +
  36. "function,generate,generic,generic map,group,guarded,if,impure,in,inertial,inout,is,label,library,linkage," +
  37. "literal,loop,map,mod,nand,new,next,nor,null,of,on,open,or,others,out,package,package body,port,port map," +
  38. "postponed,procedure,process,pure,range,record,register,reject,rem,report,return,rol,ror,select,severity,signal," +
  39. "sla,sll,sra,srl,subtype,then,to,transport,type,unaffected,units,until,use,variable,wait,when,while,with,xnor,xor");
  40. var blockKeywords = words("architecture,entity,begin,case,port,else,elsif,end,for,function,if");
  41. var isOperatorChar = /[&|~><!\)\(*#%@+\/=?\:;}{,\.\^\-\[\]]/;
  42. var curPunc;
  43. function tokenBase(stream, state) {
  44. var ch = stream.next();
  45. if (hooks[ch]) {
  46. var result = hooks[ch](stream, state);
  47. if (result !== false) return result;
  48. }
  49. if (ch == '"') {
  50. state.tokenize = tokenString2(ch);
  51. return state.tokenize(stream, state);
  52. }
  53. if (ch == "'") {
  54. state.tokenize = tokenString(ch);
  55. return state.tokenize(stream, state);
  56. }
  57. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  58. curPunc = ch;
  59. return null;
  60. }
  61. if (/[\d']/.test(ch)) {
  62. stream.eatWhile(/[\w\.']/);
  63. return "number";
  64. }
  65. if (ch == "-") {
  66. if (stream.eat("-")) {
  67. stream.skipToEnd();
  68. return "comment";
  69. }
  70. }
  71. if (isOperatorChar.test(ch)) {
  72. stream.eatWhile(isOperatorChar);
  73. return "operator";
  74. }
  75. stream.eatWhile(/[\w\$_]/);
  76. var cur = stream.current();
  77. if (keywords.propertyIsEnumerable(cur.toLowerCase())) {
  78. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  79. return "keyword";
  80. }
  81. if (atoms.propertyIsEnumerable(cur)) return "atom";
  82. return "variable";
  83. }
  84. function tokenString(quote) {
  85. return function(stream, state) {
  86. var escaped = false, next, end = false;
  87. while ((next = stream.next()) != null) {
  88. if (next == quote && !escaped) {end = true; break;}
  89. escaped = !escaped && next == "--";
  90. }
  91. if (end || !(escaped || multiLineStrings))
  92. state.tokenize = tokenBase;
  93. return "string";
  94. };
  95. }
  96. function tokenString2(quote) {
  97. return function(stream, state) {
  98. var escaped = false, next, end = false;
  99. while ((next = stream.next()) != null) {
  100. if (next == quote && !escaped) {end = true; break;}
  101. escaped = !escaped && next == "--";
  102. }
  103. if (end || !(escaped || multiLineStrings))
  104. state.tokenize = tokenBase;
  105. return "string-2";
  106. };
  107. }
  108. function Context(indented, column, type, align, prev) {
  109. this.indented = indented;
  110. this.column = column;
  111. this.type = type;
  112. this.align = align;
  113. this.prev = prev;
  114. }
  115. function pushContext(state, col, type) {
  116. return state.context = new Context(state.indented, col, type, null, state.context);
  117. }
  118. function popContext(state) {
  119. var t = state.context.type;
  120. if (t == ")" || t == "]" || t == "}")
  121. state.indented = state.context.indented;
  122. return state.context = state.context.prev;
  123. }
  124. // Interface
  125. return {
  126. startState: function(basecolumn) {
  127. return {
  128. tokenize: null,
  129. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  130. indented: 0,
  131. startOfLine: true
  132. };
  133. },
  134. token: function(stream, state) {
  135. var ctx = state.context;
  136. if (stream.sol()) {
  137. if (ctx.align == null) ctx.align = false;
  138. state.indented = stream.indentation();
  139. state.startOfLine = true;
  140. }
  141. if (stream.eatSpace()) return null;
  142. curPunc = null;
  143. var style = (state.tokenize || tokenBase)(stream, state);
  144. if (style == "comment" || style == "meta") return style;
  145. if (ctx.align == null) ctx.align = true;
  146. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  147. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  148. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  149. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  150. else if (curPunc == "}") {
  151. while (ctx.type == "statement") ctx = popContext(state);
  152. if (ctx.type == "}") ctx = popContext(state);
  153. while (ctx.type == "statement") ctx = popContext(state);
  154. }
  155. else if (curPunc == ctx.type) popContext(state);
  156. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  157. pushContext(state, stream.column(), "statement");
  158. state.startOfLine = false;
  159. return style;
  160. },
  161. indent: function(state, textAfter) {
  162. if (state.tokenize != tokenBase && state.tokenize != null) return 0;
  163. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type;
  164. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit);
  165. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  166. else return ctx.indented + (closing ? 0 : indentUnit);
  167. },
  168. electricChars: "{}"
  169. };
  170. });
  171. CodeMirror.defineMIME("text/x-vhdl", "vhdl");
  172. });