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