diff options
author | Jonathan Duran | 2012-03-06 17:08:55 -0800 |
---|---|---|
committer | Jonathan Duran | 2012-03-06 17:08:55 -0800 |
commit | bb6a1d82b2884b410f5859cc0c2cafd380acbe6a (patch) | |
tree | 6ec3e960a0c38ce8fd88c9bc17f5227d072ebe76 /imports/codemirror/mode/verilog/verilog.js | |
parent | 2815adfd7c19b3dff89dc3e1bda9af8d30dca8d6 (diff) | |
parent | 2e3943a8f751ec572066f168b58464c24b9f29e5 (diff) | |
download | ninja-bb6a1d82b2884b410f5859cc0c2cafd380acbe6a.tar.gz |
Merge branch 'refs/heads/NINJAmaster' into TimelineUber
Diffstat (limited to 'imports/codemirror/mode/verilog/verilog.js')
-rw-r--r-- | imports/codemirror/mode/verilog/verilog.js | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/imports/codemirror/mode/verilog/verilog.js b/imports/codemirror/mode/verilog/verilog.js new file mode 100644 index 00000000..736d16ad --- /dev/null +++ b/imports/codemirror/mode/verilog/verilog.js | |||
@@ -0,0 +1,194 @@ | |||
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 | |||
10 | var curPunc; | ||
11 | |||
12 | function tokenBase(stream, state) { | ||
13 | var ch = stream.next(); | ||
14 | if (hooks[ch]) { | ||
15 | var result = hooks[ch](stream, state); | ||
16 | if (result !== false) return result; | ||
17 | } | ||
18 | if (ch == '"') { | ||
19 | state.tokenize = tokenString(ch); | ||
20 | return state.tokenize(stream, state); | ||
21 | } | ||
22 | if (/[\[\]{}\(\),;\:\.]/.test(ch)) { | ||
23 | curPunc = ch; | ||
24 | return null | ||
25 | } | ||
26 | if (/[\d']/.test(ch)) { | ||
27 | stream.eatWhile(/[\w\.']/); | ||
28 | return "number"; | ||
29 | } | ||
30 | if (ch == "/") { | ||
31 | if (stream.eat("*")) { | ||
32 | state.tokenize = tokenComment; | ||
33 | return tokenComment(stream, state); | ||
34 | } | ||
35 | if (stream.eat("/")) { | ||
36 | stream.skipToEnd(); | ||
37 | return "comment"; | ||
38 | } | ||
39 | } | ||
40 | if (isOperatorChar.test(ch)) { | ||
41 | stream.eatWhile(isOperatorChar); | ||
42 | return "operator"; | ||
43 | } | ||
44 | stream.eatWhile(/[\w\$_]/); | ||
45 | var cur = stream.current(); | ||
46 | if (keywords.propertyIsEnumerable(cur)) { | ||
47 | if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement"; | ||
48 | return "keyword"; | ||
49 | } | ||
50 | if (atoms.propertyIsEnumerable(cur)) return "atom"; | ||
51 | return "word"; | ||
52 | } | ||
53 | |||
54 | function tokenString(quote) { | ||
55 | return function(stream, state) { | ||
56 | var escaped = false, next, end = false; | ||
57 | while ((next = stream.next()) != null) { | ||
58 | if (next == quote && !escaped) {end = true; break;} | ||
59 | escaped = !escaped && next == "\\"; | ||
60 | } | ||
61 | if (end || !(escaped || multiLineStrings)) | ||
62 | state.tokenize = tokenBase; | ||
63 | return "string"; | ||
64 | }; | ||
65 | } | ||
66 | |||
67 | function tokenComment(stream, state) { | ||
68 | var maybeEnd = false, ch; | ||
69 | while (ch = stream.next()) { | ||
70 | if (ch == "/" && maybeEnd) { | ||
71 | state.tokenize = tokenBase; | ||
72 | break; | ||
73 | } | ||
74 | maybeEnd = (ch == "*"); | ||
75 | } | ||
76 | return "comment"; | ||
77 | } | ||
78 | |||
79 | function Context(indented, column, type, align, prev) { | ||
80 | this.indented = indented; | ||
81 | this.column = column; | ||
82 | this.type = type; | ||
83 | this.align = align; | ||
84 | this.prev = prev; | ||
85 | } | ||
86 | function pushContext(state, col, type) { | ||
87 | return state.context = new Context(state.indented, col, type, null, state.context); | ||
88 | } | ||
89 | function popContext(state) { | ||
90 | var t = state.context.type; | ||
91 | if (t == ")" || t == "]" || t == "}") | ||
92 | state.indented = state.context.indented; | ||
93 | return state.context = state.context.prev; | ||
94 | } | ||
95 | |||
96 | // Interface | ||
97 | |||
98 | return { | ||
99 | startState: function(basecolumn) { | ||
100 | return { | ||
101 | tokenize: null, | ||
102 | context: new Context((basecolumn || 0) - indentUnit, 0, "top", false), | ||
103 | indented: 0, | ||
104 | startOfLine: true | ||
105 | }; | ||
106 | }, | ||
107 | |||
108 | token: function(stream, state) { | ||
109 | var ctx = state.context; | ||
110 | if (stream.sol()) { | ||
111 | if (ctx.align == null) ctx.align = false; | ||
112 | state.indented = stream.indentation(); | ||
113 | state.startOfLine = true; | ||
114 | } | ||
115 | if (stream.eatSpace()) return null; | ||
116 | curPunc = null; | ||
117 | var style = (state.tokenize || tokenBase)(stream, state); | ||
118 | if (style == "comment" || style == "meta") return style; | ||
119 | if (ctx.align == null) ctx.align = true; | ||
120 | |||
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 | |||
137 | indent: function(state, textAfter) { | ||
138 | if (state.tokenize != tokenBase && state.tokenize != null) return 0; | ||
139 | var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type; | ||
140 | if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit); | ||
141 | else if (ctx.align) return ctx.column + (closing ? 0 : 1); | ||
142 | else return ctx.indented + (closing ? 0 : indentUnit); | ||
143 | }, | ||
144 | |||
145 | electricChars: "{}" | ||
146 | }; | ||
147 | }); | ||
148 | |||
149 | (function() { | ||
150 | function words(str) { | ||
151 | var obj = {}, words = str.split(" "); | ||
152 | for (var i = 0; i < words.length; ++i) obj[words[i]] = true; | ||
153 | return obj; | ||
154 | } | ||
155 | |||
156 | var verilogKeywords = "always and assign automatic begin buf bufif0 bufif1 case casex casez cell cmos config " + | ||
157 | "deassign default defparam design disable edge else end endcase endconfig endfunction endgenerate endmodule " + | ||
158 | "endprimitive endspecify endtable endtask event for force forever fork function generate genvar highz0 " + | ||
159 | "highz1 if ifnone incdir include initial inout input instance integer join large liblist library localparam " + | ||
160 | "macromodule medium module nand negedge nmos nor noshowcancelled not notif0 notif1 or output parameter pmos " + | ||
161 | "posedge primitive pull0 pull1 pulldown pullup pulsestyle_onevent pulsestyle_ondetect rcmos real realtime " + | ||
162 | "reg release repeat rnmos rpmos rtran rtranif0 rtranif1 scalared showcancelled signed small specify specparam " + | ||
163 | "strong0 strong1 supply0 supply1 table task time tran tranif0 tranif1 tri tri0 tri1 triand trior trireg " + | ||
164 | "unsigned use vectored wait wand weak0 weak1 while wire wor xnor xor"; | ||
165 | |||
166 | var verilogBlockKeywords = "begin bufif0 bufif1 case casex casez config else end endcase endconfig endfunction " + | ||
167 | "endgenerate endmodule endprimitive endspecify endtable endtask for forever function generate if ifnone " + | ||
168 | "macromodule module primitive repeat specify table task while"; | ||
169 | |||
170 | function metaHook(stream, state) { | ||
171 | stream.eatWhile(/[\w\$_]/); | ||
172 | return "meta"; | ||
173 | } | ||
174 | |||
175 | // C#-style strings where "" escapes a quote. | ||
176 | function tokenAtString(stream, state) { | ||
177 | var next; | ||
178 | while ((next = stream.next()) != null) { | ||
179 | if (next == '"' && !stream.eat('"')) { | ||
180 | state.tokenize = null; | ||
181 | break; | ||
182 | } | ||
183 | } | ||
184 | return "string"; | ||
185 | } | ||
186 | |||
187 | CodeMirror.defineMIME("text/x-verilog", { | ||
188 | name: "verilog", | ||
189 | keywords: words(verilogKeywords), | ||
190 | blockKeywords: words(verilogBlockKeywords), | ||
191 | atoms: words("null"), | ||
192 | hooks: {"`": metaHook, "$": metaHook} | ||
193 | }); | ||
194 | }()); | ||