Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Last active August 29, 2015 14:05
Show Gist options
  • Save shadowmint/19e62fcc44005bf244bd to your computer and use it in GitHub Desktop.
Save shadowmint/19e62fcc44005bf244bd to your computer and use it in GitHub Desktop.
boo
Fresh array v0.1.4 (https://github.com/shadowmint/rust-array?ref=0.1.5#f574f1a3)
Compiling txt v0.1.0 (file:///Users/doug/dev/rust-txt)
src/parser/parser.rs:63:18: 63:35 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
src/parser/parser.rs:63 for token in ast.tokens.iter() {
^~~~~~~~~~~~~~~~~
src/parser/parser.rs:62:3: 83:4 note: consider using an explicit lifetime parameter as shown: fn phase_pattern_match(&'a self, ast: &'a mut Ast<'a, T>)
src/parser/parser.rs:62 fn phase_pattern_match(&'a self, ast:&mut Ast<'a, T>) {
src/parser/parser.rs:63 for token in ast.tokens.iter() {
src/parser/parser.rs:64 let mut matched = false;
src/parser/parser.rs:65 self.patterns.iter().all(|pattern| -> bool {
src/parser/parser.rs:66 match pattern.is_match(token) {
src/parser/parser.rs:67 Some(symbol) => {
...
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
src/parser/parser.rs:63:18: 63:35 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
src/parser/parser.rs:63 for token in ast.tokens.iter() {
^~~~~~~~~~~~~~~~~
src/parser/parser.rs:62:3: 83:4 note: consider using an explicit lifetime parameter as shown: fn phase_pattern_match(&'a self, ast: &'a mut Ast<'a, T>)
src/parser/parser.rs:62 fn phase_pattern_match(&'a self, ast:&mut Ast<'a, T>) {
src/parser/parser.rs:63 for token in ast.tokens.iter() {
src/parser/parser.rs:64 let mut matched = false;
src/parser/parser.rs:65 self.patterns.iter().all(|pattern| -> bool {
src/parser/parser.rs:66 match pattern.is_match(token) {
src/parser/parser.rs:67 Some(symbol) => {
...
error: aborting due to previous error
Could not compile `txt`.
use regex::Regex;
use parser::tokenizer::Tokenizer;
use parser::pattern::Pattern;
use parser::pattern::Symbol;
use string::String;
/// Generic parser
pub struct Parser<'a, T> {
tokenizer:Tokenizer,
patterns:Vec<Pattern<T>>,
}
/// The AST result of using Parser
pub struct Ast<'a, T> {
root:String<'a>,
parser:&'a Parser<'a, T>,
tokens:Vec<String<'a>>,
symbols:Vec<Symbol<'a, T>>,
bad_tokens:Vec<String<'a>>
}
impl<'a, T> Parser<'a, T> {
...
/// Parse phase 1: pattern match
fn phase_pattern_match(&self, ast:&mut Ast<'a, T>) {
for token in ast.tokens.iter() {
let mut matched = false;
self.patterns.iter().all(|pattern| -> bool {
match pattern.is_match(token) {
Some(symbol) => {
trace!("Symbol!");
ast.symbols.push(symbol);
matched = true;
return false;
},
None => {}
}
return true;
});
if !matched {
trace!("Bad match");
ast.bad_tokens.push(token.clone());
}
}
}
}
extern crate regex;
use string::String;
use self::regex::Regex;
/// Any root pattern in the AST, eg. "[0-9abc]*Hello!"
pub struct Pattern<T> {
id:T,
regex:Option<Regex>
}
/// An instance of a pattern
pub struct Symbol<'a, T> {
pattern:&'a Pattern<T>,
value:&'a String<'a>
}
impl<T> Pattern<T> {
/// Create a new pattern node
pub fn new(id:T, pattern:&str) -> Pattern<T> {
let r = match Regex::new(pattern) {
Ok(r) => Some(r),
Err(_) => None
};
return Pattern {
id:id,
regex: r
};
}
/// Create a new pattern node
pub fn from_regex(id:T, regex:Regex) -> Pattern<T> {
return Pattern {
id:id,
regex: Some(regex)
};
}
/// Check if this pattern if valid
pub fn is_valid(&self) -> bool {
return self.regex.is_some();
}
/// Check if the pattern matches, return a Symbol if it does
pub fn is_match<'a>(&'a self, target:&'a String<'a>) -> Option<Symbol<'a, T>> {
if !self.is_valid() {
return None;
}
let mut matches = false;
{
match target.find(self.regex.as_ref().unwrap()) {
Some(_) => { matches = true; }
None => {},
}
}
if matches {
return Some(Symbol {
pattern: self,
value: target
});
}
return None;
}
}
impl<'a, T: Copy> Symbol<'a, T> {
/// Return the type of this symbol
pub fn id(&self) -> T {
return self.pattern.id;
}
/// Borrow the inner value of this symbol
pub fn value(&'a self) -> String<'a> {
return self.value.substr(0, self.value.len()).unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment