Add syntax highlighting to the code snippets in the tutorial
Using the CodeMirror Rust mode.
This commit is contained in:
parent
8b57cb90e5
commit
e8e2cd44f4
@ -1,4 +1,22 @@
|
||||
var fs = require("fs"), md = require("./lib/markdown");
|
||||
CodeMirror = require("./lib/codemirror-node");
|
||||
require("./lib/codemirror-rust");
|
||||
|
||||
md.Markdown.dialects.Maruku.block.code = function code(block, next) {
|
||||
if (block.match(/^ /)) {
|
||||
var text = block.replace(/(^|\n) /g, "$1"), accum = [], curstr = "", curstyle = null;
|
||||
function add(str, style) {
|
||||
if (style != curstyle) {
|
||||
if (curstyle) accum.push(["span", {"class": "cm-" + curstyle}, curstr]);
|
||||
else if (curstr) accum.push(curstr);
|
||||
curstr = str; curstyle = style;
|
||||
} else curstr += str;
|
||||
}
|
||||
CodeMirror.runMode(text, "rust", add);
|
||||
add("", "bogus"); // Flush pending string.
|
||||
return [["pre", {"class": "cm-s-default"}].concat(accum)];
|
||||
}
|
||||
};
|
||||
|
||||
function markdown(str) { return md.toHTML(str, "Maruku"); }
|
||||
|
||||
@ -19,6 +37,7 @@ function fileDates(file, c) {
|
||||
|
||||
function head(title) {
|
||||
return "<html><head><link rel='stylesheet' href='style.css' type='text/css'>" +
|
||||
"<link rel='stylesheet' href='default.css' type='text/css'>" +
|
||||
"<meta http-equiv='Content-Type' content='text/html; charset=utf-8'><title>" +
|
||||
title + "</title></head><body>\n";
|
||||
}
|
||||
|
@ -13,11 +13,11 @@ types).
|
||||
|
||||
## Records
|
||||
|
||||
Rust record types are written `{field1: TYPE, field2: TYPE [,
|
||||
...]}`, and record literals are written in the same way, but with
|
||||
expressions instead of types. They are quite similar to C structs, and
|
||||
even laid out the same way in memory (so you can read from a Rust
|
||||
struct in C, and vice-versa).
|
||||
Rust record types are written `{field1: TYPE, field2: TYPE [, ...]}`,
|
||||
and record literals are written in the same way, but with expressions
|
||||
instead of types. They are quite similar to C structs, and even laid
|
||||
out the same way in memory (so you can read from a Rust struct in C,
|
||||
and vice-versa).
|
||||
|
||||
The dot operator is used to access record fields (`mypoint.x`).
|
||||
|
||||
@ -100,6 +100,8 @@ equivalent to an `enum` in C:
|
||||
This will define `north`, `east`, `south`, and `west` as constants,
|
||||
all of which have type `direction`.
|
||||
|
||||
<a name="single_variant_tag"></a>
|
||||
|
||||
There is a special case for tags with a single variant. These are used
|
||||
to define new types in such a way that the new name is not just a
|
||||
synonym for an existing type, but its own distinct type. If you say:
|
||||
|
124
doc/tutorial/lib/codemirror-node.js
Normal file
124
doc/tutorial/lib/codemirror-node.js
Normal file
@ -0,0 +1,124 @@
|
||||
exports.htmlEscape = function(text) {
|
||||
var replacements = {"<": "<", ">": ">",
|
||||
"&": "&", "\"": """};
|
||||
return text.replace(/[<>&"]/g, function(character) {
|
||||
return replacements[character];
|
||||
});
|
||||
};
|
||||
|
||||
exports.splitLines = function(string){return string.split(/\r?\n/);};
|
||||
|
||||
// Counts the column offset in a string, taking tabs into account.
|
||||
// Used mostly to find indentation.
|
||||
function countColumn(string, end) {
|
||||
if (end == null) {
|
||||
end = string.search(/[^\s\u00a0]/);
|
||||
if (end == -1) end = string.length;
|
||||
}
|
||||
for (var i = 0, n = 0; i < end; ++i) {
|
||||
if (string.charAt(i) == "\t") n += tabSize - (n % tabSize);
|
||||
else ++n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function StringStream(string) {
|
||||
this.pos = this.start = 0;
|
||||
this.string = string;
|
||||
}
|
||||
StringStream.prototype = {
|
||||
eol: function() {return this.pos >= this.string.length;},
|
||||
sol: function() {return this.pos == 0;},
|
||||
peek: function() {return this.string.charAt(this.pos);},
|
||||
next: function() {
|
||||
if (this.pos < this.string.length)
|
||||
return this.string.charAt(this.pos++);
|
||||
},
|
||||
eat: function(match) {
|
||||
var ch = this.string.charAt(this.pos);
|
||||
if (typeof match == "string") var ok = ch == match;
|
||||
else var ok = ch && (match.test ? match.test(ch) : match(ch));
|
||||
if (ok) {++this.pos; return ch;}
|
||||
},
|
||||
eatWhile: function(match) {
|
||||
var start = this.pos;
|
||||
while (this.eat(match)){}
|
||||
return this.pos > start;
|
||||
},
|
||||
eatSpace: function() {
|
||||
var start = this.pos;
|
||||
while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
|
||||
return this.pos > start;
|
||||
},
|
||||
skipToEnd: function() {this.pos = this.string.length;},
|
||||
skipTo: function(ch) {
|
||||
var found = this.string.indexOf(ch, this.pos);
|
||||
if (found > -1) {this.pos = found; return true;}
|
||||
},
|
||||
backUp: function(n) {this.pos -= n;},
|
||||
column: function() {return countColumn(this.string, this.start);},
|
||||
indentation: function() {return countColumn(this.string);},
|
||||
match: function(pattern, consume, caseInsensitive) {
|
||||
if (typeof pattern == "string") {
|
||||
function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
|
||||
if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
|
||||
if (consume !== false) this.pos += pattern.length;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
var match = this.string.slice(this.pos).match(pattern);
|
||||
if (match && consume !== false) this.pos += match[0].length;
|
||||
return match;
|
||||
}
|
||||
},
|
||||
current: function(){return this.string.slice(this.start, this.pos);}
|
||||
};
|
||||
exports.StringStream = StringStream;
|
||||
|
||||
exports.startState = function(mode, a1, a2) {
|
||||
return mode.startState ? mode.startState(a1, a2) : true;
|
||||
};
|
||||
|
||||
var modes = {}, mimeModes = {};
|
||||
exports.defineMode = function(name, mode) { modes[name] = mode; };
|
||||
exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
|
||||
exports.getMode = function(options, spec) {
|
||||
if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
|
||||
spec = mimeModes[spec];
|
||||
if (typeof spec == "string")
|
||||
var mname = spec, config = {};
|
||||
else if (spec != null)
|
||||
var mname = spec.name, config = spec;
|
||||
var mfactory = modes[mname];
|
||||
if (!mfactory) throw new Error("Unknown mode: " + spec);
|
||||
return mfactory(options, config || {});
|
||||
};
|
||||
|
||||
exports.runMode = function(string, modespec, callback) {
|
||||
var mode = exports.getMode({indentUnit: 2}, modespec);
|
||||
var isNode = callback.nodeType == 1;
|
||||
if (isNode) {
|
||||
var node = callback, accum = [];
|
||||
callback = function(string, style) {
|
||||
if (string == "\n")
|
||||
accum.push("<br>");
|
||||
else if (style)
|
||||
accum.push("<span class=\"cm-" + exports.htmlEscape(style) + "\">" + exports.htmlEscape(string) + "</span>");
|
||||
else
|
||||
accum.push(exports.htmlEscape(string));
|
||||
}
|
||||
}
|
||||
var lines = exports.splitLines(string), state = exports.startState(mode);
|
||||
for (var i = 0, e = lines.length; i < e; ++i) {
|
||||
if (i) callback("\n");
|
||||
var stream = new exports.StringStream(lines[i]);
|
||||
while (!stream.eol()) {
|
||||
var style = mode.token(stream, state);
|
||||
callback(stream.current(), style, i, stream.start);
|
||||
stream.start = stream.pos;
|
||||
}
|
||||
}
|
||||
if (isNode)
|
||||
node.innerHTML = accum.join("");
|
||||
};
|
405
doc/tutorial/lib/codemirror-rust.js
Normal file
405
doc/tutorial/lib/codemirror-rust.js
Normal file
@ -0,0 +1,405 @@
|
||||
CodeMirror.defineMode("rust", function() {
|
||||
var indentUnit = 4, altIndentUnit = 2;
|
||||
var valKeywords = {
|
||||
"if": "if-style", "while": "if-style", "else": "else-style",
|
||||
"do": "else-style", "ret": "else-style", "fail": "else-style",
|
||||
"break": "atom", "cont": "atom", "const": "let", "resource": "fn",
|
||||
"let": "let", "fn": "fn", "for": "for", "alt": "alt", "obj": "fn",
|
||||
"lambda": "fn", "type": "type", "tag": "tag", "mod": "mod",
|
||||
"as": "op", "true": "atom", "false": "atom", "assert": "op", "check": "op",
|
||||
"claim": "op", "native": "ignore", "unsafe": "ignore", "import": "else-style",
|
||||
"export": "else-style", "copy": "op", "log": "op", "log_err": "op", "use": "op"
|
||||
};
|
||||
var typeKeywords = function() {
|
||||
var keywords = {"fn": "fn", "block": "fn", "obj": "obj"};
|
||||
var atoms = "bool uint int i8 i16 i32 i64 u8 u16 u32 u64 float f32 f64 str char".split(" ");
|
||||
for (var i = 0, e = atoms.length; i < e; ++i) keywords[atoms[i]] = "atom";
|
||||
return keywords;
|
||||
}();
|
||||
var operatorChar = /[+\-*&%=<>!?|\.@]/;
|
||||
|
||||
// Tokenizer
|
||||
|
||||
// Used as scratch variable to communicate multiple values without
|
||||
// consing up tons of objects.
|
||||
var tcat, content;
|
||||
function r(tc, style) {
|
||||
tcat = tc;
|
||||
return style;
|
||||
}
|
||||
|
||||
function tokenBase(stream, state) {
|
||||
var ch = stream.next();
|
||||
if (ch == '"') {
|
||||
state.tokenize = tokenString;
|
||||
return state.tokenize(stream, state);
|
||||
}
|
||||
if (ch == "'") {
|
||||
tcat = "atom";
|
||||
if (stream.eat("\\")) {
|
||||
if (stream.skipTo("'")) { stream.next(); return "string"; }
|
||||
else { return "error"; }
|
||||
} else {
|
||||
stream.next();
|
||||
return stream.eat("'") ? "string" : "error";
|
||||
}
|
||||
}
|
||||
if (ch == "/") {
|
||||
if (stream.eat("/")) { stream.skipToEnd(); return "comment"; }
|
||||
if (stream.eat("*")) {
|
||||
state.tokenize = tokenComment(1);
|
||||
return state.tokenize(stream, state);
|
||||
}
|
||||
}
|
||||
if (ch == "#") {
|
||||
if (stream.eat("[")) { tcat = "open-attr"; return null; }
|
||||
stream.eatWhile(/\w/);
|
||||
return r("macro", "meta");
|
||||
}
|
||||
if (ch == ":" && stream.match(":<")) {
|
||||
return r("op", null);
|
||||
}
|
||||
if (ch.match(/\d/) || (ch == "." && stream.eat(/\d/))) {
|
||||
var flp = false;
|
||||
if (!stream.match(/^x[\da-f]+/i) && !stream.match(/^b[01]+/)) {
|
||||
stream.eatWhile(/\d/);
|
||||
if (stream.eat(".")) { flp = true; stream.eatWhile(/\d/); }
|
||||
if (stream.match(/^e[+\-]?\d+/i)) { flp = true; }
|
||||
}
|
||||
if (flp) stream.match(/^f(?:32|64)/);
|
||||
else stream.match(/^[ui](?:8|16|32|64)/);
|
||||
return r("atom", "number");
|
||||
}
|
||||
if (ch.match(/[()\[\]{}:;,]/)) return r(ch, null);
|
||||
if (ch == "-" && stream.eat(">")) return r("->", null);
|
||||
if (ch.match(operatorChar)) {
|
||||
stream.eatWhile(operatorChar);
|
||||
return r("op", null);
|
||||
}
|
||||
stream.eatWhile(/\w/);
|
||||
content = stream.current();
|
||||
if (stream.match(/^::\w/)) {
|
||||
stream.backUp(1);
|
||||
return r("prefix", "variable-2");
|
||||
}
|
||||
if (state.keywords.propertyIsEnumerable(content))
|
||||
return r(state.keywords[content], content.match(/true|false/) ? "atom" : "keyword");
|
||||
return r("name", "variable");
|
||||
}
|
||||
|
||||
function tokenString(stream, state) {
|
||||
var ch, escaped = false;
|
||||
while (ch = stream.next()) {
|
||||
if (ch == '"' && !escaped) {
|
||||
state.tokenize = tokenBase;
|
||||
return r("atom", "string");
|
||||
}
|
||||
escaped = !escaped && ch == "\\";
|
||||
}
|
||||
// Hack to not confuse the parser when a string is split in
|
||||
// pieces.
|
||||
return r("op", "string");
|
||||
}
|
||||
|
||||
function tokenComment(depth) {
|
||||
return function(stream, state) {
|
||||
var lastCh = null, ch;
|
||||
while (ch = stream.next()) {
|
||||
if (ch == "/" && lastCh == "*") {
|
||||
if (depth == 1) {
|
||||
state.tokenize = tokenBase;
|
||||
break;
|
||||
} else {
|
||||
state.tokenize = tokenComment(depth - 1);
|
||||
return state.tokenize(stream, state);
|
||||
}
|
||||
}
|
||||
if (ch == "*" && lastCh == "/") {
|
||||
state.tokenize = tokenComment(depth + 1);
|
||||
return state.tokenize(stream, state);
|
||||
}
|
||||
lastCh = ch;
|
||||
}
|
||||
return "comment";
|
||||
};
|
||||
}
|
||||
|
||||
// Parser
|
||||
|
||||
var cx = {state: null, stream: null, marked: null, cc: null};
|
||||
function pass() {
|
||||
for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
|
||||
}
|
||||
function cont() {
|
||||
pass.apply(null, arguments);
|
||||
return true;
|
||||
}
|
||||
|
||||
function pushlex(type, info) {
|
||||
var result = function() {
|
||||
var state = cx.state;
|
||||
state.lexical = {indented: state.indented, column: cx.stream.column(),
|
||||
type: type, prev: state.lexical, info: info};
|
||||
};
|
||||
result.lex = true;
|
||||
return result;
|
||||
}
|
||||
function poplex() {
|
||||
var state = cx.state;
|
||||
if (state.lexical.prev) {
|
||||
if (state.lexical.type == ")")
|
||||
state.indented = state.lexical.indented;
|
||||
state.lexical = state.lexical.prev;
|
||||
}
|
||||
}
|
||||
function typecx() { cx.state.keywords = typeKeywords; }
|
||||
function valcx() { cx.state.keywords = valKeywords; }
|
||||
poplex.lex = typecx.lex = valcx.lex = true;
|
||||
|
||||
function commasep(comb, end) {
|
||||
function more(type) {
|
||||
if (type == ",") return cont(comb, more);
|
||||
if (type == end) return cont();
|
||||
return cont(more);
|
||||
}
|
||||
return function(type) {
|
||||
if (type == end) return cont();
|
||||
return pass(comb, more);
|
||||
};
|
||||
}
|
||||
|
||||
function block(type) {
|
||||
if (type == "}") return cont();
|
||||
if (type == "let") return cont(pushlex("stat", "let"), letdef1, poplex, block);
|
||||
if (type == "fn") return cont(pushlex("stat"), fndef, poplex, block);
|
||||
if (type == "type") return cont(pushlex("stat"), tydef, endstatement, poplex, block);
|
||||
if (type == "tag") return cont(pushlex("stat"), tagdef, poplex, block);
|
||||
if (type == "mod") return cont(pushlex("stat"), mod, poplex, block);
|
||||
if (type == "open-attr") return cont(pushlex("]"), commasep(expression, "]"), poplex);
|
||||
if (type == "ignore" || type.match(/[\]\);,]/)) return cont(block);
|
||||
return pass(pushlex("stat"), expression, poplex, endstatement, block);
|
||||
}
|
||||
function endstatement(type) {
|
||||
if (type == ";") return cont();
|
||||
return pass();
|
||||
}
|
||||
function expression(type) {
|
||||
if (type == "atom" || type == "name") return cont(maybeop);
|
||||
if (type == "{") return cont(pushlex("}"), exprbrace, poplex);
|
||||
if (type.match(/[\[\(]/)) return matchBrackets(type, expression);
|
||||
if (type.match(/[\]\)\};,]/)) return pass();
|
||||
if (type == "if-style") return cont(expression, expression);
|
||||
if (type == "else-style" || type == "op") return cont(expression);
|
||||
if (type == "for") return cont(pattern, maybetype, inop, expression, expression);
|
||||
if (type == "alt") return cont(expression, altbody);
|
||||
if (type == "fn") return cont(fndef);
|
||||
if (type == "macro") return cont(macro);
|
||||
return cont();
|
||||
}
|
||||
function maybeop(type) {
|
||||
if (content == ".") return cont(maybeprop);
|
||||
if (content == "::<"){return cont(typarams, maybeop);}
|
||||
if (type == "op" || content == ":") return cont(expression);
|
||||
if (type == "(" || type == "[") return matchBrackets(type, expression);
|
||||
return pass();
|
||||
}
|
||||
function maybeprop(type) {
|
||||
if (content.match(/^\w+$/)) {cx.marked = "variable"; return cont(maybeop);}
|
||||
return pass(expression);
|
||||
}
|
||||
function exprbrace(type) {
|
||||
if (type == "op") {
|
||||
if (content == "|") return cont(blockvars, poplex, pushlex("}", "block"), block);
|
||||
if (content == "||") return cont(poplex, pushlex("}", "block"), block);
|
||||
}
|
||||
if (content == "mutable" || (content.match(/^\w+$/) && cx.stream.peek() == ":"
|
||||
&& !cx.stream.match("::", false))) return pass(recliteral);
|
||||
return pass(block);
|
||||
}
|
||||
function recliteral(type) {
|
||||
if (content == "mutable" || content == "with") {cx.marked = "keyword"; return cont(recliteral);}
|
||||
if (content.match(/^\w*$/)) {cx.marked = "variable"; return cont(recliteral);}
|
||||
if (type == ":") return cont(expression, recliteral);
|
||||
if (type == "}") return cont();
|
||||
return cont(recliteral);
|
||||
}
|
||||
function blockvars(type) {
|
||||
if (type == "name") {cx.marked = "def"; return cont(blockvars);}
|
||||
if (type == "op" && content == "|") return cont();
|
||||
return cont(blockvars);
|
||||
}
|
||||
|
||||
function letdef1(type) {
|
||||
if (type == ";") return cont();
|
||||
if (content == "=") return cont(expression, letdef2);
|
||||
if (type == ",") return cont(letdef1);
|
||||
return pass(pattern, maybetype, letdef1);
|
||||
}
|
||||
function letdef2(type) {
|
||||
if (type.match(/[\]\)\};,]/)) return pass(letdef1);
|
||||
else return pass(expression, letdef2);
|
||||
}
|
||||
function maybetype(type) {
|
||||
if (type == ":") return cont(typecx, rtype, valcx);
|
||||
return pass();
|
||||
}
|
||||
function inop(type) {
|
||||
if (type == "name" && content == "in") {cx.marked = "keyword"; return cont();}
|
||||
return pass();
|
||||
}
|
||||
function fndef(type) {
|
||||
if (type == "name") {cx.marked = "def"; return cont(fndef);}
|
||||
if (content == "<") return cont(typarams, fndef);
|
||||
if (type == "{") return pass(expression);
|
||||
if (type == "(") return cont(pushlex(")"), commasep(argdef, ")"), poplex, fndef);
|
||||
if (type == "->") return cont(typecx, rtype, valcx, fndef);
|
||||
return cont(fndef);
|
||||
}
|
||||
function tydef(type) {
|
||||
if (type == "name") {cx.marked = "def"; return cont(tydef);}
|
||||
if (content == "<") return cont(typarams, tydef);
|
||||
if (content == "=") return cont(typecx, rtype, valcx);
|
||||
return cont(tydef);
|
||||
}
|
||||
function tagdef(type) {
|
||||
if (type == "name") {cx.marked = "def"; return cont(tagdef);}
|
||||
if (content == "<") return cont(typarams, tagdef);
|
||||
if (content == "=") return cont(typecx, rtype, valcx, endstatement);
|
||||
if (type == "{") return cont(pushlex("}"), typecx, tagblock, valcx, poplex);
|
||||
return cont(tagdef);
|
||||
}
|
||||
function tagblock(type) {
|
||||
if (type == "}") return cont();
|
||||
if (type == "(") return cont(pushlex(")"), commasep(rtype, ")"), poplex, tagblock);
|
||||
if (content.match(/^\w+$/)) cx.marked = "def";
|
||||
return cont(tagblock);
|
||||
}
|
||||
function mod(type) {
|
||||
if (type == "name") {cx.marked = "def"; return cont(mod);}
|
||||
if (type == "{") return cont(pushlex("}"), block, poplex);
|
||||
return pass();
|
||||
}
|
||||
function typarams(type) {
|
||||
if (content == ">") return cont();
|
||||
if (content == ",") return cont(typarams);
|
||||
return pass(rtype, typarams);
|
||||
}
|
||||
function argdef(type) {
|
||||
if (type == "name") {cx.marked = "def"; return cont(argdef);}
|
||||
if (type == ":") return cont(typecx, rtype, valcx);
|
||||
return pass();
|
||||
}
|
||||
function rtype(type) {
|
||||
if (type == "name") {cx.marked = "variable-3"; return cont(rtypemaybeparam); }
|
||||
if (content == "mutable") {cx.marked = "keyword"; return cont(rtype);}
|
||||
if (type == "atom") return cont(rtypemaybeparam);
|
||||
if (type == "op" || type == "obj") return cont(rtype);
|
||||
if (type == "fn") return cont(fntype);
|
||||
return matchBrackets(type, rtype);
|
||||
}
|
||||
function rtypemaybeparam(type) {
|
||||
if (content == "<") return cont(typarams);
|
||||
return pass();
|
||||
}
|
||||
function fntype(type) {
|
||||
if (type == "(") return cont(pushlex("("), commasep(rtype, ")"), poplex, fntype);
|
||||
if (type == "->") return cont(rtype);
|
||||
return pass();
|
||||
}
|
||||
function pattern(type) {
|
||||
if (type == "name") {cx.marked = "def"; return cont(patternmaybeop);}
|
||||
if (type == "atom") return cont(patternmaybeop);
|
||||
if (type == "op") return cont(pattern);
|
||||
if (type.match(/[\]\)\};,]/)) return pass();
|
||||
return matchBrackets(type, pattern);
|
||||
}
|
||||
function patternmaybeop(type) {
|
||||
if (type == "op" && content == ".") return cont();
|
||||
if (content == "to") {cx.marked = "keyword"; return cont(pattern);}
|
||||
else return pass();
|
||||
}
|
||||
function altbody(type) {
|
||||
if (type == "{") return cont(pushlex("}", "alt"), altblock1, poplex);
|
||||
return pass();
|
||||
}
|
||||
function altblock1(type) {
|
||||
if (type == "}") return cont();
|
||||
if (type == "|") return cont(altblock1);
|
||||
if (content == "when") {cx.marked = "keyword"; return cont(expression, altblock2);}
|
||||
if (type.match(/[\]\);,]/)) return cont(altblock1);
|
||||
return pass(pattern, altblock2);
|
||||
}
|
||||
function altblock2(type) {
|
||||
if (type == "{") return cont(pushlex("}", "alt"), block, poplex, altblock1);
|
||||
else return pass(altblock1);
|
||||
}
|
||||
|
||||
function macro(type) {
|
||||
if (type.match(/[\[\(\{]/)) return matchBrackets(type, expression);
|
||||
return pass();
|
||||
}
|
||||
function matchBrackets(type, comb) {
|
||||
if (type == "[") return cont(pushlex("]"), commasep(comb, "]"), poplex);
|
||||
if (type == "(") return cont(pushlex(")"), commasep(comb, ")"), poplex);
|
||||
if (type == "{") return cont(pushlex("}"), commasep(comb, "}"), poplex);
|
||||
return cont();
|
||||
}
|
||||
|
||||
function parse(state, stream, style) {
|
||||
var cc = state.cc;
|
||||
// Communicate our context to the combinators.
|
||||
// (Less wasteful than consing up a hundred closures on every call.)
|
||||
cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
|
||||
|
||||
while (true) {
|
||||
var combinator = cc.length ? cc.pop() : block;
|
||||
if (combinator(tcat)) {
|
||||
while(cc.length && cc[cc.length - 1].lex)
|
||||
cc.pop()();
|
||||
return cx.marked || style;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
startState: function() {
|
||||
return {
|
||||
tokenize: tokenBase,
|
||||
cc: [],
|
||||
lexical: {indented: -indentUnit, column: 0, type: "top", align: false},
|
||||
keywords: valKeywords,
|
||||
indented: 0
|
||||
};
|
||||
},
|
||||
|
||||
token: function(stream, state) {
|
||||
if (stream.sol()) {
|
||||
if (!state.lexical.hasOwnProperty("align"))
|
||||
state.lexical.align = false;
|
||||
state.indented = stream.indentation();
|
||||
}
|
||||
if (stream.eatSpace()) return null;
|
||||
tcat = content = null;
|
||||
var style = state.tokenize(stream, state);
|
||||
if (style == "comment") return style;
|
||||
if (!state.lexical.hasOwnProperty("align"))
|
||||
state.lexical.align = true;
|
||||
if (tcat == "prefix") return style;
|
||||
if (!content) content = stream.current();
|
||||
return parse(state, stream, style);
|
||||
},
|
||||
|
||||
indent: function(state, textAfter) {
|
||||
if (state.tokenize != tokenBase) return 0;
|
||||
var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical,
|
||||
type = lexical.type, closing = firstChar == type;
|
||||
if (type == "stat") return lexical.indented + indentUnit;
|
||||
if (lexical.align) return lexical.column + (closing ? 0 : 1);
|
||||
return lexical.indented + (closing ? 0 : (lexical.info == "alt" ? altIndentUnit : indentUnit));
|
||||
},
|
||||
|
||||
electricChars: "{}"
|
||||
};
|
||||
});
|
||||
|
||||
CodeMirror.defineMIME("text/x-rustsrc", "rust");
|
@ -252,8 +252,6 @@ Markdown.prototype.toTree = function toTree( source, custom_root ) {
|
||||
Markdown.prototype.debug = function () {
|
||||
var args = Array.prototype.slice.call( arguments);
|
||||
args.unshift(this.debug_indent);
|
||||
if (typeof print !== "undefined")
|
||||
print.apply( print, args );
|
||||
if (typeof console !== "undefined" && typeof console.log !== "undefined")
|
||||
console.log.apply( null, args );
|
||||
}
|
||||
@ -1125,6 +1123,10 @@ Markdown.dialects.Maruku.block.definition_list = function definition_list( block
|
||||
return [ list ];
|
||||
}
|
||||
|
||||
Markdown.dialects.Maruku.block.html_paragraph = function html_paragraph( block, next ) {
|
||||
if (block.match(/^<\w/)) return [["RAW", block.toString()]];
|
||||
}
|
||||
|
||||
Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, out ) {
|
||||
if ( !out.length ) {
|
||||
return [ 2, "{:" ];
|
||||
@ -1297,6 +1299,7 @@ function render_tree( jsonml ) {
|
||||
var tag = jsonml.shift(),
|
||||
attributes = {},
|
||||
content = [];
|
||||
if (tag == "RAW") return jsonml[0];
|
||||
|
||||
if ( jsonml.length && typeof jsonml[ 0 ] === "object" && !( jsonml[ 0 ] instanceof Array ) ) {
|
||||
attributes = jsonml.shift();
|
||||
|
@ -1,7 +1,5 @@
|
||||
# Syntax Basics
|
||||
|
||||
FIXME: mention the module separator `::` somewhere
|
||||
|
||||
## Braces
|
||||
|
||||
Assuming you've programmed in any C-family language (C++, Java,
|
||||
@ -75,6 +73,19 @@ only have a trailing expression when you want to use their value for
|
||||
something—in which case you'll have embedded it in a bigger statement,
|
||||
like the `let x = ...` example above.
|
||||
|
||||
## Identifiers
|
||||
|
||||
Rust identifiers must start with an alphabetic character or an
|
||||
underscore, and after that may contain any alphanumeric character, and
|
||||
more underscores.
|
||||
|
||||
NOTE: The parser doesn't currently recognize non-ascii alphabetic
|
||||
characters. This is a bug that will eventually be fixed.
|
||||
|
||||
The double-colon (`::`) is used as a module separator, so
|
||||
`std::io::println` means 'the thing named `println` in the module
|
||||
named `io` in the module named `std`'.
|
||||
|
||||
## Types
|
||||
|
||||
The `-> bool` in the last example is the way a function's return type
|
||||
@ -158,10 +169,10 @@ Types can be given names with `type` declarations:
|
||||
This will provide a synonym, `monster_size`, for unsigned integers. It
|
||||
will not actually create a new type—`monster_size` and `uint` can be
|
||||
used interchangeably, and using one where the other is expected is not
|
||||
a type error. Read about [single-variant tags][svt] in the next
|
||||
section if you need to create a type name that's not just a synonym.
|
||||
a type error. Read about [single-variant tags][svt] further on if you
|
||||
need to create a type name that's not just a synonym.
|
||||
|
||||
[svt]: FIXME
|
||||
[svt]: data.html#single_variant_tag
|
||||
|
||||
## Literals
|
||||
|
||||
@ -211,4 +222,37 @@ following it, will not appear in the resulting string literal.
|
||||
|
||||
## Operators
|
||||
|
||||
FIXME recap C-style operators, ?:, explain `as`
|
||||
Rust's set of operators contains very few surprises. The main
|
||||
difference with C is that `++` and `--` are missing, and that the
|
||||
logical binary operators have higher precedence—in C, `x & 2 > 0`
|
||||
comes out as `x & (2 > 0)`, in Rust, it means `(x & 2) > 0`, which is
|
||||
more likely to be what you expect (unless you are a C veteran).
|
||||
|
||||
Thus, binary arithmetic is done with `*`, `/`, `%`, `+`, and `-`
|
||||
(multiply, divide, remainder, plus, minus). `-` is also a unary prefix
|
||||
operator (there are no unary postfix operators in Rust) that does
|
||||
negation.
|
||||
|
||||
Binary shifting is done with `>>` (shift right), `>>>` (arithmetic
|
||||
shift right), and `<<` (shift left). Logical bitwise operators are
|
||||
`&`, `|`, and `^` (and, or, and exclusive or), and unary `!` for
|
||||
bitwise negation (or boolean negation when applied to a boolean
|
||||
value).
|
||||
|
||||
The comparison operators are the traditional `==`, `!=`, `<`, `>`,
|
||||
`<=`, and `>=`. Short-circuiting (lazy) boolean operators are written
|
||||
`&&` (and) and `||` (or).
|
||||
|
||||
Rust has a ternary conditional operator `?:`, as in:
|
||||
|
||||
let message = badness < 10 ? "error" : "FATAL ERROR";
|
||||
|
||||
For type casting, Rust uses the binary `as` operator, which has a
|
||||
precedence between the bitwise combination operators (`&`, `|`, `^`)
|
||||
and the comparison operators. It takes an expression on the left side,
|
||||
and a type on the right side, and will, if a meaningful conversion
|
||||
exists, convert the result of the expression to the given type.
|
||||
|
||||
let x: float = 4.0;
|
||||
let y: uint = x as uint;
|
||||
assert y == 4u;
|
||||
|
19
doc/tutorial/web/default.css
Normal file
19
doc/tutorial/web/default.css
Normal file
@ -0,0 +1,19 @@
|
||||
.cm-s-default span.cm-keyword {color: #708;}
|
||||
.cm-s-default span.cm-atom {color: #219;}
|
||||
.cm-s-default span.cm-number {color: #164;}
|
||||
.cm-s-default span.cm-def {color: #00f;}
|
||||
.cm-s-default span.cm-variable {color: black;}
|
||||
.cm-s-default span.cm-variable-2 {color: #05a;}
|
||||
.cm-s-default span.cm-variable-3 {color: #0a5;}
|
||||
.cm-s-default span.cm-property {color: black;}
|
||||
.cm-s-default span.cm-operator {color: black;}
|
||||
.cm-s-default span.cm-comment {color: #a50;}
|
||||
.cm-s-default span.cm-string {color: #a11;}
|
||||
.cm-s-default span.cm-string-2 {color: #f50;}
|
||||
.cm-s-default span.cm-meta {color: #555;}
|
||||
.cm-s-default span.cm-error {color: #f00;}
|
||||
.cm-s-default span.cm-qualifier {color: #555;}
|
||||
.cm-s-default span.cm-builtin {color: #30a;}
|
||||
.cm-s-default span.cm-bracket {color: #cc7;}
|
||||
.cm-s-default span.cm-tag {color: #170;}
|
||||
.cm-s-default span.cm-attribute {color: #00c;}
|
@ -18,9 +18,8 @@ code {
|
||||
}
|
||||
|
||||
pre {
|
||||
margin: 1.1em 12px;
|
||||
border: 1px solid #CCCCCC;
|
||||
padding: .4em;
|
||||
margin: 1.1em 0;
|
||||
padding: .4em .4em .4em 1em;
|
||||
font-size: 120%;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user