Debian Codegraph @

codemirror-legacy-modes/mode/vhdl.js

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