diff options
author | Pushkar Joshi | 2012-03-06 17:04:40 -0800 |
---|---|---|
committer | Pushkar Joshi | 2012-03-06 17:04:40 -0800 |
commit | 7a43958033906b2273de88bc2a26cda7a905d202 (patch) | |
tree | f71817e712c4b07a48257a4f0b206cf9033421df /imports/codemirror/mode/go | |
parent | 264e3d8e6d3624083d2fab9fe2560234553bb2ad (diff) | |
parent | 2e3943a8f751ec572066f168b58464c24b9f29e5 (diff) | |
download | ninja-7a43958033906b2273de88bc2a26cda7a905d202.tar.gz |
Merge branch 'master' into brushtool
Diffstat (limited to 'imports/codemirror/mode/go')
-rw-r--r-- | imports/codemirror/mode/go/go.js | 170 | ||||
-rw-r--r-- | imports/codemirror/mode/go/index.html | 72 |
2 files changed, 242 insertions, 0 deletions
diff --git a/imports/codemirror/mode/go/go.js b/imports/codemirror/mode/go/go.js new file mode 100644 index 00000000..00e5d6c2 --- /dev/null +++ b/imports/codemirror/mode/go/go.js | |||
@@ -0,0 +1,170 @@ | |||
1 | CodeMirror.defineMode("go", function(config, parserConfig) { | ||
2 | var indentUnit = config.indentUnit; | ||
3 | |||
4 | var keywords = { | ||
5 | "break":true, "case":true, "chan":true, "const":true, "continue":true, | ||
6 | "default":true, "defer":true, "else":true, "fallthrough":true, "for":true, | ||
7 | "func":true, "go":true, "goto":true, "if":true, "import":true, | ||
8 | "interface":true, "map":true, "package":true, "range":true, "return":true, | ||
9 | "select":true, "struct":true, "switch":true, "type":true, "var":true, | ||
10 | "bool":true, "byte":true, "complex64":true, "complex128":true, | ||
11 | "float32":true, "float64":true, "int8":true, "int16":true, "int32":true, | ||
12 | "int64":true, "string":true, "uint8":true, "uint16":true, "uint32":true, | ||
13 | "uint64":true, "int":true, "uint":true, "uintptr":true | ||
14 | }; | ||
15 | |||
16 | var atoms = { | ||
17 | "true":true, "false":true, "iota":true, "nil":true, "append":true, | ||
18 | "cap":true, "close":true, "complex":true, "copy":true, "imag":true, | ||
19 | "len":true, "make":true, "new":true, "panic":true, "print":true, | ||
20 | "println":true, "real":true, "recover":true | ||
21 | }; | ||
22 | |||
23 | var blockKeywords = { | ||
24 | "else":true, "for":true, "func":true, "if":true, "interface":true, | ||
25 | "select":true, "struct":true, "switch":true | ||
26 | }; | ||
27 | |||
28 | var isOperatorChar = /[+\-*&^%:=<>!|\/]/; | ||
29 | |||
30 | var curPunc; | ||
31 | |||
32 | function tokenBase(stream, state) { | ||
33 | var ch = stream.next(); | ||
34 | if (ch == '"' || ch == "'" || ch == "`") { | ||
35 | state.tokenize = tokenString(ch); | ||
36 | return state.tokenize(stream, state); | ||
37 | } | ||
38 | if (/[\d\.]/.test(ch)) { | ||
39 | if (ch == ".") { | ||
40 | stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/); | ||
41 | } else if (ch == "0") { | ||
42 | stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/); | ||
43 | } else { | ||
44 | stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/); | ||
45 | } | ||
46 | return "number"; | ||
47 | } | ||
48 | if (/[\[\]{}\(\),;\:\.]/.test(ch)) { | ||
49 | curPunc = ch; | ||
50 | return null | ||
51 | } | ||
52 | if (ch == "/") { | ||
53 | if (stream.eat("*")) { | ||
54 | state.tokenize = tokenComment; | ||
55 | return tokenComment(stream, state); | ||
56 | } | ||
57 | if (stream.eat("/")) { | ||
58 | stream.skipToEnd(); | ||
59 | return "comment"; | ||
60 | } | ||
61 | } | ||
62 | if (isOperatorChar.test(ch)) { | ||
63 | stream.eatWhile(isOperatorChar); | ||
64 | return "operator"; | ||
65 | } | ||
66 | stream.eatWhile(/[\w\$_]/); | ||
67 | var cur = stream.current(); | ||
68 | if (keywords.propertyIsEnumerable(cur)) { | ||
69 | if (cur == "case" || cur == "default") curPunc = "case"; | ||
70 | return "keyword"; | ||
71 | } | ||
72 | if (atoms.propertyIsEnumerable(cur)) return "atom"; | ||
73 | return "word"; | ||
74 | } | ||
75 | |||
76 | function tokenString(quote) { | ||
77 | return function(stream, state) { | ||
78 | var escaped = false, next, end = false; | ||
79 | while ((next = stream.next()) != null) { | ||
80 | if (next == quote && !escaped) {end = true; break;} | ||
81 | escaped = !escaped && next == "\\"; | ||
82 | } | ||
83 | if (end || !(escaped || quote == "`")) | ||
84 | state.tokenize = tokenBase; | ||
85 | return "string"; | ||
86 | }; | ||
87 | } | ||
88 | |||
89 | function tokenComment(stream, state) { | ||
90 | var maybeEnd = false, ch; | ||
91 | while (ch = stream.next()) { | ||
92 | if (ch == "/" && maybeEnd) { | ||
93 | state.tokenize = tokenBase; | ||
94 | break; | ||
95 | } | ||
96 | maybeEnd = (ch == "*"); | ||
97 | } | ||
98 | return "comment"; | ||
99 | } | ||
100 | |||
101 | function Context(indented, column, type, align, prev) { | ||
102 | this.indented = indented; | ||
103 | this.column = column; | ||
104 | this.type = type; | ||
105 | this.align = align; | ||
106 | this.prev = prev; | ||
107 | } | ||
108 | function pushContext(state, col, type) { | ||
109 | return state.context = new Context(state.indented, col, type, null, state.context); | ||
110 | } | ||
111 | function popContext(state) { | ||
112 | var t = state.context.type; | ||
113 | if (t == ")" || t == "]" || t == "}") | ||
114 | state.indented = state.context.indented; | ||
115 | return state.context = state.context.prev; | ||
116 | } | ||
117 | |||
118 | // Interface | ||
119 | |||
120 | return { | ||
121 | startState: function(basecolumn) { | ||
122 | return { | ||
123 | tokenize: null, | ||
124 | context: new Context((basecolumn || 0) - indentUnit, 0, "top", false), | ||
125 | indented: 0, | ||
126 | startOfLine: true | ||
127 | }; | ||
128 | }, | ||
129 | |||
130 | token: function(stream, state) { | ||
131 | var ctx = state.context; | ||
132 | if (stream.sol()) { | ||
133 | if (ctx.align == null) ctx.align = false; | ||
134 | state.indented = stream.indentation(); | ||
135 | state.startOfLine = true; | ||
136 | if (ctx.type == "case") ctx.type = "}"; | ||
137 | } | ||
138 | if (stream.eatSpace()) return null; | ||
139 | curPunc = null; | ||
140 | var style = (state.tokenize || tokenBase)(stream, state); | ||
141 | if (style == "comment") return style; | ||
142 | if (ctx.align == null) ctx.align = true; | ||
143 | |||
144 | if (curPunc == "{") pushContext(state, stream.column(), "}"); | ||
145 | else if (curPunc == "[") pushContext(state, stream.column(), "]"); | ||
146 | else if (curPunc == "(") pushContext(state, stream.column(), ")"); | ||
147 | else if (curPunc == "case") ctx.type = "case" | ||
148 | else if (curPunc == "}" && ctx.type == "}") ctx = popContext(state); | ||
149 | else if (curPunc == ctx.type) popContext(state); | ||
150 | state.startOfLine = false; | ||
151 | return style; | ||
152 | }, | ||
153 | |||
154 | indent: function(state, textAfter) { | ||
155 | if (state.tokenize != tokenBase && state.tokenize != null) return 0; | ||
156 | var ctx = state.context, firstChar = textAfter && textAfter.charAt(0); | ||
157 | if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) { | ||
158 | state.context.type = "}"; | ||
159 | return ctx.indented; | ||
160 | } | ||
161 | var closing = firstChar == ctx.type; | ||
162 | if (ctx.align) return ctx.column + (closing ? 0 : 1); | ||
163 | else return ctx.indented + (closing ? 0 : indentUnit); | ||
164 | }, | ||
165 | |||
166 | electricChars: "{}:" | ||
167 | }; | ||
168 | }); | ||
169 | |||
170 | CodeMirror.defineMIME("text/x-go", "go"); | ||
diff --git a/imports/codemirror/mode/go/index.html b/imports/codemirror/mode/go/index.html new file mode 100644 index 00000000..9cdad1ad --- /dev/null +++ b/imports/codemirror/mode/go/index.html | |||
@@ -0,0 +1,72 @@ | |||
1 | <!doctype html> | ||
2 | <html> | ||
3 | <head> | ||
4 | <title>CodeMirror: Go mode</title> | ||
5 | <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
6 | <link rel="stylesheet" href="../../theme/elegant.css"> | ||
7 | <script src="../../lib/codemirror.js"></script> | ||
8 | <script src="go.js"></script> | ||
9 | <link rel="stylesheet" href="../../doc/docs.css"> | ||
10 | <style>.CodeMirror {border:1px solid #999; background:#ffc}</style> | ||
11 | </head> | ||
12 | <body> | ||
13 | <h1>CodeMirror: Go mode</h1> | ||
14 | |||
15 | <form><textarea id="code" name="code"> | ||
16 | // Prime Sieve in Go. | ||
17 | // Taken from the Go specification. | ||
18 | // Copyright © The Go Authors. | ||
19 | |||
20 | package main | ||
21 | |||
22 | import "fmt" | ||
23 | |||
24 | // Send the sequence 2, 3, 4, ... to channel 'ch'. | ||
25 | func generate(ch chan<- int) { | ||
26 | for i := 2; ; i++ { | ||
27 | ch <- i // Send 'i' to channel 'ch' | ||
28 | } | ||
29 | } | ||
30 | |||
31 | // Copy the values from channel 'src' to channel 'dst', | ||
32 | // removing those divisible by 'prime'. | ||
33 | func filter(src <-chan int, dst chan<- int, prime int) { | ||
34 | for i := range src { // Loop over values received from 'src'. | ||
35 | if i%prime != 0 { | ||
36 | dst <- i // Send 'i' to channel 'dst'. | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
41 | // The prime sieve: Daisy-chain filter processes together. | ||
42 | func sieve() { | ||
43 | ch := make(chan int) // Create a new channel. | ||
44 | go generate(ch) // Start generate() as a subprocess. | ||
45 | for { | ||
46 | prime := <-ch | ||
47 | fmt.Print(prime, "\n") | ||
48 | ch1 := make(chan int) | ||
49 | go filter(ch, ch1, prime) | ||
50 | ch = ch1 | ||
51 | } | ||
52 | } | ||
53 | |||
54 | func main() { | ||
55 | sieve() | ||
56 | } | ||
57 | </textarea></form> | ||
58 | |||
59 | <script> | ||
60 | var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | ||