Created
October 26, 2012 03:10
-
-
Save dckc/3956652 to your computer and use it in GitHub Desktop.
struct matching problem (compile with rustc structmatch.rs --test)
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
extern mod std; // else --test goes wonky | |
extern mod rparse; // https://github.com/jesse99/rparse or: cargo install rparse | |
use rparse::{Parser, Combinators, ParseFailed, match0 }; | |
pub fn space() -> Parser<@~str> { match0(|ch| ws_char.contains_char(ch)) } | |
const ws_char: &str = &" \t\r\n\x0A"; | |
#[cfg(test)] | |
mod test_preliminaries { | |
// This shows ParseFailed is correctly imported. | |
fn mk_failed() -> ParseFailed { | |
ParseFailed{file: @~"", line: 1, col: 2, mesg: @~"hello"} | |
} | |
#[test] | |
fn match_problemr() { | |
let actual = space() | |
.parse(@~"f", @"$( x \u0000 is not allowed $)"); | |
match actual { | |
Ok(_) => fail, | |
Err(ParseFailed{file: f, line: y, col: x, mesg: txt}) => assert x == 6, | |
// The with the line below in place of the one above, this compiles. | |
//Err(_) => () | |
} | |
} | |
} |
Author
dckc
commented
Oct 26, 2012
Meanwhile, this parses:
extern mod std; // else --test goes wonky
struct ParseFailed {
file: @~str,
line: uint,
col: uint,
mesg: @~str
}
type ParseResult = Result<int, ParseFailed>;
fn match_problem() {
let actual: ParseResult = Ok(1);
match actual {
Ok(_) => fail,
Err(ParseFailed{file: f, line: y, col: x, mesg: txt}) => assert x == 6,
}
}
er... compiles, rather
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment