Created
April 6, 2012 22:35
-
-
Save jamorton/2323598 to your computer and use it in GitHub Desktop.
Pygments Test File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Comment | |
Thing / * * / | |
*/ /* */ | |
// /* test | |
// TEST | |
// \\ / Comment \/ // */ | |
// Number Literals | |
123; // type int | |
123u; // type uint | |
123_u; // type uint | |
0xff00; // type int | |
0xff_u8; // type u8 | |
0b1111_1111_1001_0000_i32; // type i32 | |
123.0; // type float | |
0.1; // type float | |
3f; // type float | |
0.1f32; // type f32 | |
12E+99_f64; // type f64 | |
// good | |
3u16; | |
12_25_u32; | |
0xFu32; | |
0.1123e52f32; | |
0b110011u; | |
0xFFFu; | |
0xFu16; | |
// errors | |
5u33; | |
0x; | |
12e+u32; | |
// Character Literals | |
'a'; | |
'\x4a'; | |
'\x66'; | |
'\Ua92Ff8a0'; | |
'\uFFFF'; | |
'\n'; | |
'\r'; | |
'\t'; | |
'T'; | |
'\\'; | |
// error | |
'\Ua8'; | |
'\x4'; | |
'\?'; | |
'\xj5'; | |
// String Literals | |
"Test"; | |
"Test 2 \\ \uFFFF \n abc \r abc \t"; | |
"\UDEADBEEF \xAF \n"; | |
type t = map::hashmap<int,str>; // Type arguments used in a type expression | |
let x = id::<int>(10); // Type arguments used in a call expression | |
x; | |
x::y::z; | |
// CRATE FILE | |
// Linkage attributes | |
#[ link(name = "projx" | |
vers = "2.5", | |
uuid = "9cccc5d5-aceb-4af5-8285-811211826b82") ]; | |
// Additional metadata attributes | |
#[ desc = "Project X", | |
license = "BSD" | |
author = "Jane Doe" ]; | |
// Import a module. | |
use std (ver = "1.0"); | |
// Define some modules. | |
#[path = "foo.rs"] | |
mod foo; | |
mod bar { | |
#[path = "quux.rs"] | |
mod quux; | |
} | |
// Testing attributes | |
#[inline] | |
fn bar() { } | |
#[doc = "Derp ] "] | |
fn bar2() { } | |
#[ comment = "Long | |
Multiline | |
Thingy"] | |
#unzip_literals[5, 6, 7]; | |
// Nested macro | |
#macro([#zip_literals[[x, ...], [y, ...], [[x, y], ...]]]); | |
#macro([#unzip_literals[[x, y], ...], [[x, ...], [y, ...]]]); | |
#macro(#do_stuff[[x, y], | |
[y, x]]); | |
#foo(thing]); | |
// Macro Ends | |
// Real doc | |
#[doc = " | |
Convert to the `either` type | |
`ok` result variants are converted to `either::right` variants, `err` | |
result variants are converted to `either::left`. | |
"] | |
pure fn to_either<T: copy, U: copy>(res: result<U, T>) -> either<T, U> { | |
alt res { | |
ok(res) { either::right(res) } | |
err(fail_) { either::left(fail_) } | |
} | |
} | |
// module | |
mod math { | |
type complex = (f64, f64); | |
fn sin(f: f64) -> f64 { | |
// ... | |
} | |
fn cos(f: f64) -> f64 { | |
// ... | |
} | |
fn tan(f: f64) -> f64 { | |
// ... | |
} | |
} | |
// import | |
import foo = core::info; | |
import core::float::sin; | |
import core::str::{slice, hash}; | |
import core::option::some; | |
// Generics | |
fn iter<T>(seq: [T], f: fn(T)) { | |
for seq.each {|elt| f(elt); } | |
} | |
fn map<T, U>(seq: [T], f: fn(T) -> U) -> [U] { | |
let mut acc = []; | |
for seq.each {|elt| acc += [f(elt)]; } | |
acc | |
} | |
// Logging | |
// Full version, logging a value. | |
log(core::error, "file not found: " + filename); | |
// Log-level abbreviated, since core::* is imported by default. | |
log(error, "file not found: " + filename); | |
// Formatting the message using a format-string and #fmt | |
log(error, #fmt("file not found: %s", filename)); | |
// Using the #error macro, that expands to the previous call. | |
#error("file not found: %s", filename); | |
export EXIT_FAILURE, EXIT_SUCCESS, RAND_MAX, | |
EOF, SEEK_SET, SEEK_CUR, SEEK_END, _IOFBF, _IONBF, _IOLBF, | |
BUFSIZ, FOPEN_MAX, FILENAME_MAX, L_tmpnam, TMP_MAX, | |
O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_EXCL, O_TRUNC, | |
S_IFIFO, S_IFCHR, S_IFBLK, S_IFDIR, S_IFREG, S_IFMT, S_IEXEC, | |
S_IWRITE, S_IREAD, S_IRWXU, S_IXUSR, S_IWUSR, S_IRUSR, F_OK, R_OK, | |
W_OK, X_OK, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO; | |
// Code samples | |
fn split_inner(s: str, sepfn: fn(cc: char) -> bool, count: uint, | |
allow_empty: bool) -> [str] unsafe { | |
let l = len(s); | |
let mut result = [], i = 0u, start = 0u, done = 0u; | |
while i < l && done < count { | |
let {ch, next} = char_range_at(s, i); | |
if sepfn(ch) { | |
if allow_empty || start < i { | |
result += [unsafe::slice_bytes(s, start, i)]; | |
} | |
start = next; | |
done += 1u; | |
} | |
i = next; | |
} | |
if allow_empty || start < l { | |
result += [unsafe::slice_bytes(s, start, l)]; | |
} | |
result | |
} | |
fn vec_equal<T>(v: [T], u: [T], element_equality_test: fn@(&&T, &&T) -> bool) -> | |
bool { | |
let Lv = vec::len(v); | |
if Lv != vec::len(u) { ret false; } | |
let i = 0u; | |
while i < Lv { | |
if !element_equality_test(v[i], u[i]) { ret false; } | |
i += 1u; | |
} | |
ret true; | |
} | |
fn next_token_inner(rdr: reader) -> token::token { | |
let mut accum_str = ""; | |
let mut c = rdr.curr; | |
if (c >= 'a' && c <= 'z') | |
|| (c >= 'A' && c <= 'Z') | |
|| c == '_' | |
|| (c > 'z' && char::is_XID_start(c)) { | |
while (c >= 'a' && c <= 'z') | |
|| (c >= 'A' && c <= 'Z') | |
|| (c >= '0' && c <= '9') | |
|| c == '_' | |
|| (c > 'z' && char::is_XID_continue(c)) { | |
str::push_char(accum_str, c); | |
rdr.bump(); | |
c = rdr.curr; | |
} | |
if str::eq(accum_str, "_") { ret token::UNDERSCORE; } | |
let is_mod_name = c == ':' && rdr.next() == ':'; | |
// FIXME: perform NFKC normalization here. | |
ret token::IDENT(interner::intern::<str>(*rdr.interner, | |
accum_str), is_mod_name); | |
} | |
if is_dec_digit(c) { | |
ret scan_number(c, rdr); | |
} | |
fn binop(rdr: reader, op: token::binop) -> token::token { | |
rdr.bump(); | |
if rdr.curr == '=' { | |
rdr.bump(); | |
ret token::BINOPEQ(op); | |
} else { ret token::BINOP(op); } | |
} | |
alt c { | |
// One-byte tokens. | |
';' { rdr.bump(); ret token::SEMI; } | |
',' { rdr.bump(); ret token::COMMA; } | |
'.' { | |
rdr.bump(); | |
if rdr.curr == '.' && rdr.next() == '.' { | |
rdr.bump(); | |
rdr.bump(); | |
ret token::ELLIPSIS; | |
} | |
ret token::DOT; | |
} | |
'(' { rdr.bump(); ret token::LPAREN; } | |
')' { rdr.bump(); ret token::RPAREN; } | |
'{' { rdr.bump(); ret token::LBRACE; } | |
'}' { rdr.bump(); ret token::RBRACE; } | |
'[' { rdr.bump(); ret token::LBRACKET; } | |
']' { rdr.bump(); ret token::RBRACKET; } | |
'@' { rdr.bump(); ret token::AT; } | |
'#' { | |
rdr.bump(); | |
if rdr.curr == '<' { rdr.bump(); ret token::POUND_LT; } | |
if rdr.curr == '{' { rdr.bump(); ret token::POUND_LBRACE; } | |
ret token::POUND; | |
} | |
'~' { rdr.bump(); ret token::TILDE; } | |
':' { | |
rdr.bump(); | |
if rdr.curr == ':' { | |
rdr.bump(); | |
ret token::MOD_SEP; | |
} else { ret token::COLON; } | |
} | |
'$' { | |
rdr.bump(); | |
if is_dec_digit(rdr.curr) { | |
let mut val = dec_digit_val(rdr.curr) as uint; | |
while is_dec_digit(rdr.next()) { | |
rdr.bump(); | |
val = val * 10u + (dec_digit_val(rdr.curr) as uint); | |
} | |
rdr.bump(); | |
ret token::DOLLAR_NUM(val); | |
} else if rdr.curr == '(' { | |
rdr.bump(); | |
ret token::DOLLAR_LPAREN; | |
} else { | |
rdr.fatal("expected digit"); | |
} | |
} | |
// Multi-byte tokens. | |
'=' { | |
rdr.bump(); | |
if rdr.curr == '=' { | |
rdr.bump(); | |
ret token::EQEQ; | |
} else { ret token::EQ; } | |
} | |
'!' { | |
rdr.bump(); | |
if rdr.curr == '=' { | |
rdr.bump(); | |
ret token::NE; | |
} else { ret token::NOT; } | |
} | |
'<' { | |
rdr.bump(); | |
alt rdr.curr { | |
'=' { rdr.bump(); ret token::LE; } | |
'<' { ret binop(rdr, token::LSL); } | |
'-' { | |
rdr.bump(); | |
alt rdr.curr { | |
'>' { rdr.bump(); ret token::DARROW; } | |
_ { ret token::LARROW; } | |
} | |
} | |
_ { ret token::LT; } | |
} | |
} | |
'>' { | |
rdr.bump(); | |
alt rdr.curr { | |
'=' { rdr.bump(); ret token::GE; } | |
'>' { | |
if rdr.next() == '>' { | |
rdr.bump(); | |
ret binop(rdr, token::ASR); | |
} else { ret binop(rdr, token::LSR); } | |
} | |
_ { ret token::GT; } | |
} | |
} | |
'\'' { | |
rdr.bump(); | |
let mut c2 = rdr.curr; | |
rdr.bump(); | |
if c2 == '\\' { | |
let escaped = rdr.curr; | |
rdr.bump(); | |
alt escaped { | |
'n' { c2 = '\n'; } | |
'r' { c2 = '\r'; } | |
't' { c2 = '\t'; } | |
'\\' { c2 = '\\'; } | |
'\'' { c2 = '\''; } | |
'x' { c2 = scan_numeric_escape(rdr, 2u); } | |
'u' { c2 = scan_numeric_escape(rdr, 4u); } | |
'U' { c2 = scan_numeric_escape(rdr, 8u); } | |
c2 { | |
rdr.fatal(#fmt["unknown character escape: %d", c2 as int]); | |
} | |
} | |
} | |
if rdr.curr != '\'' { | |
rdr.fatal("unterminated character constant"); | |
} | |
rdr.bump(); // advance curr past token | |
ret token::LIT_INT(c2 as i64, ast::ty_char); | |
} | |
'"' { | |
let n = rdr.chpos; | |
rdr.bump(); | |
while rdr.curr != '"' { | |
if rdr.is_eof() { | |
rdr.fatal(#fmt["unterminated double quote string: %s", | |
rdr.get_str_from(n)]); | |
} | |
let ch = rdr.curr; | |
rdr.bump(); | |
alt ch { | |
'\\' { | |
let escaped = rdr.curr; | |
rdr.bump(); | |
alt escaped { | |
'n' { str::push_char(accum_str, '\n'); } | |
'r' { str::push_char(accum_str, '\r'); } | |
't' { str::push_char(accum_str, '\t'); } | |
'\\' { str::push_char(accum_str, '\\'); } | |
'"' { str::push_char(accum_str, '"'); } | |
'\n' { consume_whitespace(rdr); } | |
'x' { | |
str::push_char(accum_str, scan_numeric_escape(rdr, 2u)); | |
} | |
'u' { | |
str::push_char(accum_str, scan_numeric_escape(rdr, 4u)); | |
} | |
'U' { | |
str::push_char(accum_str, scan_numeric_escape(rdr, 8u)); | |
} | |
c2 { | |
rdr.fatal(#fmt["unknown string escape: %d", c2 as int]); | |
} | |
} | |
} | |
_ { str::push_char(accum_str, ch); } | |
} | |
} | |
rdr.bump(); | |
ret token::LIT_STR(interner::intern::<str>(*rdr.interner, | |
accum_str)); | |
} | |
'-' { | |
if rdr.next() == '>' { | |
rdr.bump(); | |
rdr.bump(); | |
ret token::RARROW; | |
} else { ret binop(rdr, token::MINUS); } | |
} | |
'&' { | |
if rdr.next() == '&' { | |
rdr.bump(); | |
rdr.bump(); | |
ret token::ANDAND; | |
} else { ret binop(rdr, token::AND); } | |
} | |
'|' { | |
alt rdr.next() { | |
'|' { rdr.bump(); rdr.bump(); ret token::OROR; } | |
_ { ret binop(rdr, token::OR); } | |
} | |
} | |
'+' { ret binop(rdr, token::PLUS); } | |
'*' { ret binop(rdr, token::STAR); } | |
'/' { ret binop(rdr, token::SLASH); } | |
'^' { ret binop(rdr, token::CARET); } | |
'%' { ret binop(rdr, token::PERCENT); } | |
c { rdr.fatal(#fmt["unknown start of token: %d", c as int]); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment