Skip to content

Commit 498ad91

Browse files
committed
Upgrade CodeMirror mode used to highlight the tutorial code
1 parent 936c933 commit 498ad91

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

doc/tutorial/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
# Rust language tutorial
2-
3-
*(Not quite finished yet. Proceed with caution.)*

doc/tutorial/lib/codemirror-rust.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ CodeMirror.defineMode("rust", function() {
44
"if": "if-style", "while": "if-style", "else": "else-style",
55
"do": "else-style", "ret": "else-style", "fail": "else-style",
66
"break": "atom", "cont": "atom", "const": "let", "resource": "fn",
7-
"let": "let", "fn": "fn", "for": "for", "alt": "alt", "obj": "fn",
8-
"lambda": "fn", "type": "type", "tag": "tag", "mod": "mod",
7+
"let": "let", "fn": "fn", "for": "for", "alt": "alt", "iface": "iface",
8+
"impl": "impl", "type": "type", "tag": "tag", "mod": "mod",
99
"as": "op", "true": "atom", "false": "atom", "assert": "op", "check": "op",
1010
"claim": "op", "native": "ignore", "unsafe": "ignore", "import": "else-style",
1111
"export": "else-style", "copy": "op", "log": "op", "log_err": "op",
12-
"use": "op", "bind": "op"
12+
"use": "op", "bind": "op", "self": "atom"
1313
};
1414
var typeKeywords = function() {
1515
var keywords = {"fn": "fn", "block": "fn", "obj": "obj"};
@@ -169,13 +169,18 @@ CodeMirror.defineMode("rust", function() {
169169
};
170170
}
171171

172+
function stat_of(comb, tag) {
173+
return cont(pushlex("stat", tag), comb, poplex, block);
174+
}
172175
function block(type) {
173176
if (type == "}") return cont();
174-
if (type == "let") return cont(pushlex("stat", "let"), letdef1, poplex, block);
175-
if (type == "fn") return cont(pushlex("stat"), fndef, poplex, block);
177+
if (type == "let") return stat_of(letdef1, "let");
178+
if (type == "fn") return stat_of(fndef);
176179
if (type == "type") return cont(pushlex("stat"), tydef, endstatement, poplex, block);
177-
if (type == "tag") return cont(pushlex("stat"), tagdef, poplex, block);
178-
if (type == "mod") return cont(pushlex("stat"), mod, poplex, block);
180+
if (type == "tag") return stat_of(tagdef);
181+
if (type == "mod") return stat_of(mod);
182+
if (type == "iface") return stat_of(iface);
183+
if (type == "impl") return stat_of(impl);
179184
if (type == "open-attr") return cont(pushlex("]"), commasep(expression, "]"), poplex);
180185
if (type == "ignore" || type.match(/[\]\);,]/)) return cont(block);
181186
return pass(pushlex("stat"), expression, poplex, endstatement, block);
@@ -253,11 +258,13 @@ CodeMirror.defineMode("rust", function() {
253258
return pass();
254259
}
255260
function fndef(type) {
261+
if (content == "@" || content == "~") {cx.marked = "keyword"; return cont(fndef);}
256262
if (type == "name") {cx.marked = "def"; return cont(fndef);}
257263
if (content == "<") return cont(typarams, fndef);
258264
if (type == "{") return pass(expression);
259265
if (type == "(") return cont(pushlex(")"), commasep(argdef, ")"), poplex, fndef);
260266
if (type == "->") return cont(typecx, rtype, valcx, fndef);
267+
if (type == ";") return cont();
261268
return cont(fndef);
262269
}
263270
function tydef(type) {
@@ -284,9 +291,23 @@ CodeMirror.defineMode("rust", function() {
284291
if (type == "{") return cont(pushlex("}"), block, poplex);
285292
return pass();
286293
}
294+
function iface(type) {
295+
if (type == "name") {cx.marked = "def"; return cont(iface);}
296+
if (content == "<") return cont(typarams, iface);
297+
if (type == "{") return cont(pushlex("}"), block, poplex);
298+
return pass();
299+
}
300+
function impl(type) {
301+
if (content == "<") return cont(typarams, impl);
302+
if (content == "of" || content == "for") {cx.marked = "keyword"; return cont(rtype, impl);}
303+
if (type == "name") {cx.marked = "def"; return cont(impl);}
304+
if (type == "{") return cont(pushlex("}"), block, poplex);
305+
return pass();
306+
}
287307
function typarams(type) {
288308
if (content == ">") return cont();
289309
if (content == ",") return cont(typarams);
310+
if (content == ":") return cont(rtype, typarams);
290311
return pass(rtype, typarams);
291312
}
292313
function argdef(type) {

0 commit comments

Comments
 (0)