Last active
March 5, 2018 13:29
-
-
Save okunokentaro/ad27ef3eeed48d20a7af649ffaeec2b5 to your computer and use it in GitHub Desktop.
WIP
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
#[derive(Debug)] | |
struct Parser { | |
cursor: Cursor, | |
tokens: Vec<String>, | |
} | |
impl Parser { | |
pub fn new(cursor: Cursor) -> Parser { | |
Parser { cursor, tokens: Vec::new()} | |
} | |
pub fn update_cursor(mut self, mode: String, n: usize) -> Parser { | |
self.cursor = self.cursor.update(mode, n); | |
self | |
} | |
pub fn add_token(mut self, token: String) -> Parser { | |
self.tokens.push(token); | |
self | |
} | |
} | |
#[derive(Debug)] | |
struct Cursor { | |
mode: String, | |
n: usize, | |
} | |
impl Cursor { | |
pub fn update(mut self, mode: String, n: usize) -> Cursor { | |
self.mode = mode; | |
self.n = n; | |
self | |
} | |
} | |
fn get_string(vec_chars: &Vec<char>, begin: usize, end: usize) -> String { | |
vec_chars[begin..end].iter().cloned().collect::<String>() | |
} | |
fn main() { | |
let string = "class_implements 'Foo' and includes 'Bar' then error"; | |
let vec_chars = string.chars().collect::<Vec<char>>(); | |
let default_cursor = Cursor {mode: "statement".to_string(), n: 0}; | |
let parser = Parser::new(default_cursor); | |
let result = vec_chars | |
.clone() | |
.into_iter() | |
.enumerate() | |
.fold(parser, |mut parser, (i, v)| { | |
match parser.cursor.mode.as_ref() { | |
"statement" => { | |
match v { | |
'c' => { | |
parser.update_cursor("indicator".to_string(), i) | |
} | |
_ => { | |
println!("error 100"); | |
parser | |
} | |
} | |
} | |
"indicator" => { | |
match v { | |
' ' => { | |
let token = get_string(&vec_chars, parser.cursor.n, i); | |
parser = parser.add_token(token); | |
parser.update_cursor("start_identifier".to_string(), i) | |
} | |
_ => parser | |
} | |
} | |
"start_identifier" => { | |
match v { | |
'\'' => { | |
parser.update_cursor("identifier".to_string(), i + 1) | |
} | |
_ => parser | |
} | |
} | |
"identifier" => { | |
match v { | |
'\'' => { | |
let token = get_string(&vec_chars, parser.cursor.n, i); | |
parser = parser.add_token(token); | |
parser.update_cursor("end_identifier".to_string(), i + 1) | |
} | |
_ => parser | |
} | |
} | |
"end_identifier" => { | |
match v { | |
' ' => { | |
parser.update_cursor("and_then_statement".to_string(), i + 1) | |
} | |
_ => parser | |
} | |
} | |
"and_then_statement" => { | |
match v { | |
// and | |
'a' => { | |
parser.update_cursor("and_indicator".to_string(), i) | |
}, | |
// then | |
't' => { | |
parser.update_cursor("then_indicator".to_string(), i) | |
}, | |
_ => parser | |
} | |
} | |
"and_statement" => { | |
match v { | |
// includes, import | |
'i' => { | |
parser.update_cursor("and_indicator".to_string(), i) | |
} | |
// not | |
'n' => { | |
match get_string(&vec_chars, parser.cursor.n, i).as_ref() { | |
"not" => { | |
parser.update_cursor("and_indicator".to_string(), i) | |
} | |
"name_matches" => { | |
parser.update_cursor("and_statement".to_string(), i) | |
} | |
_ => parser | |
} | |
}, | |
'\'' => { | |
parser.update_cursor("identifier".to_string(), i + 1) | |
} | |
' ' => { | |
parser.update_cursor("and_statement".to_string(), i + 1) | |
} | |
_ => parser | |
} | |
} | |
"and_indicator" => { | |
match v { | |
' ' => { | |
let token = get_string(&vec_chars, parser.cursor.n, i); | |
parser = parser.add_token(token); | |
parser.update_cursor("and_statement".to_string(), i + 1) | |
} | |
_ => parser | |
} | |
}, | |
"then_statement" => { | |
match v { | |
// error | |
'e' => { | |
parser.update_cursor("then_indicator".to_string(), i) | |
} | |
// warn | |
'w' => { | |
parser.update_cursor("then_indicator".to_string(), i) | |
}, | |
_ => parser | |
} | |
}, | |
"then_indicator" => { | |
if vec_chars.len() - 1 == i { | |
return match get_string(&vec_chars, parser.cursor.n, i + 1).as_ref() { | |
"error" => { | |
let token = get_string(&vec_chars, parser.cursor.n, i + 1); | |
parser = parser.add_token(token); | |
parser.update_cursor("statement".to_string(), i) | |
} | |
"warn" => { | |
let token = get_string(&vec_chars, parser.cursor.n, i + 1); | |
parser = parser.add_token(token); | |
parser.update_cursor("statement".to_string(), i) | |
} | |
_ => parser | |
}; | |
} | |
match v { | |
' ' => { | |
parser.update_cursor("then_statement".to_string(), i + 1) | |
} | |
_ => parser | |
} | |
} | |
_ => { | |
println!("error 9000"); | |
parser | |
} | |
} | |
}); | |
println!("{:?}", result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment