extern crate gramatica; use std::cmp::Ordering; use gramatica::{Associativity,EarleyKind,State,Parser,ParsingTablesTrait,AmbiguityInfo}; use std::rc::Rc; #[derive(Clone,Debug,PartialEq)] enum JsonValue{ Literal(String), Number(f64), Object(Vec<(String,JsonValue)>), Array(Vec), True, False, Null, } use std::io::{BufRead,Read}; fn main(){ let stdin=std::io::stdin(); let mut buf=String::new(); stdin.lock().read_to_string(&mut buf); match Parser::::parse(&buf,None) { Err(x) => println!("error parsing: {:?}" , x), Ok(x) => println!("parsed correctly: {:?}" , x), } ; } #[derive(Clone,Debug,PartialEq)] enum Token{DummyStart, True,False,Null,Number(f64),LitStr(String),LBrace,RBrace,LBracket,RBracket,Comma,Colon,Object(JsonValue),Members(Vec<(String,JsonValue)>),Pair(String,JsonValue),Array(Vec),Elements(Vec),Value(JsonValue),} impl Default for Token { fn default()->Self{Token::DummyStart} } struct ParsingTables { } impl ParsingTablesTrait for ParsingTables { fn initial()->usize { 12 } fn match_some(parser: &mut Parser) -> Option<(usize,Token)> { let source=&parser.source[parser.source_index..]; match { match parser.keyword("true",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::True)), }; match { match parser.keyword("false",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::False)), }; match { match parser.keyword("null",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::Null)), }; match { match parser.re("[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?",source) { None => None, Some((size,string)) => Some((size,string.parse::().unwrap() )) } } { None => (), Some((size,result)) => return Some((size,Token::Number(result))), }; { fn _match(parser:&mut Parser,source:& str)->Option<(usize,String)>{ let mut ret=None; let mut characters=source.chars(); if (characters.next()) != (Some('"')) {} else {let mut size=1; let mut r=String::from("\""); while true {match characters.next() { None => break, Some('"') => {ret = (Some((size + 1,r + &"\""))); break;}, Some('\\') => {match characters.next() { None => break, Some(c) => {r.push('\\'); r.push(c);}, } ; size += 2;}, Some(c) => {r.push(c); size += 1;}, } ;}} ret} match _match(parser,source) { None=>(), Some((size,result)) => return Some((size,Token::LitStr(result))), } }; match { match parser.re("\\{",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::LBrace)), }; match { match parser.re("\\}",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::RBrace)), }; match { match parser.re("\\[",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::LBracket)), }; match { match parser.re("\\]",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::RBracket)), }; match { match parser.re(",",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::Comma)), }; match { match parser.re(":",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::Colon)), }; match { match parser.re("\\s+|\n",source) { None => None, Some((size,_string)) => Some((size,())) } } { None => (), Some((size,_result)) => return Some((size,Token::DummyStart)), }; None }//match_some fn predict(parser:&mut Parser,index:usize,state_index:usize,token:usize) { match token { 12 => { parser.sets[index].predict(State{rule: 1 ,left: 12 ,right:vec![ 6,7 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 2 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 2 ,left: 12 ,right:vec![ 6,13,7 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 3 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); } 13 => { parser.sets[index].predict(State{rule: 3 ,left: 13 ,right:vec![ 14 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 4 ,left: 13 ,right:vec![ 13,10,14 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 3 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); } 14 => { parser.sets[index].predict(State{rule: 5 ,left: 14 ,right:vec![ 5,11,17 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 3 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); } 15 => { parser.sets[index].predict(State{rule: 6 ,left: 15 ,right:vec![ 8,9 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 2 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 7 ,left: 15 ,right:vec![ 8,16,9 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 3 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); } 16 => { parser.sets[index].predict(State{rule: 8 ,left: 16 ,right:vec![ 17 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 9 ,left: 16 ,right:vec![ 16,10,17 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 3 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); } 17 => { parser.sets[index].predict(State{rule: 10 ,left: 17 ,right:vec![ 5 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 11 ,left: 17 ,right:vec![ 4 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 12 ,left: 17 ,right:vec![ 12 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 13 ,left: 17 ,right:vec![ 15 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 14 ,left: 17 ,right:vec![ 1 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 15 ,left: 17 ,right:vec![ 2 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); parser.sets[index].predict(State{rule: 16 ,left: 17 ,right:vec![ 3 ],position:0,original_set:index,kind:EarleyKind::Predict(state_index),values:vec![Token::DummyStart; 1 ],computed_value:Token::DummyStart,ambiguity_info:AmbiguityInfo::default(),}); } _ => panic!(""), } }//predict fn compute_value(state:&mut State) { state.computed_value = match state.rule { 0 => state.values[0].clone(), 1 => match (&state.values[0],&state.values[1]) { (&Token::LBrace,&Token::RBrace) => Token::Object(JsonValue::Object(vec![])), _ => panic!(""), }, 2 => match (&state.values[0],&state.values[1],&state.values[2]) { (&Token::LBrace,&Token::Members(ref list),&Token::RBrace) => Token::Object(JsonValue::Object(list.clone())), _ => panic!(""), }, 3 => match &state.values[0] { &Token::Pair(ref s,ref value) => Token::Members(vec![(s . clone () , value . clone ())]), _ => panic!(""), }, 4 => match (&state.values[0],&state.values[1],&state.values[2]) { (&Token::Members(ref list),&Token::Comma,&Token::Pair(ref s,ref value)) => Token::Members({let mut new=(list.clone()); new.push((s.clone(),value.clone())); new}), _ => panic!(""), }, 5 => match (&state.values[0],&state.values[1],&state.values[2]) { (&Token::LitStr(ref s),&Token::Colon,&Token::Value(ref value)) => { let (x0,x1)=(s.clone(),value.clone()); Token::Pair(x0,x1) }, _ => panic!(""), }, 6 => match (&state.values[0],&state.values[1]) { (&Token::LBracket,&Token::RBracket) => Token::Array(vec![]), _ => panic!(""), }, 7 => match (&state.values[0],&state.values[1],&state.values[2]) { (&Token::LBracket,&Token::Elements(ref list),&Token::RBracket) => Token::Array(list.clone()), _ => panic!(""), }, 8 => match &state.values[0] { &Token::Value(ref value) => Token::Elements(vec![value . clone ()]), _ => panic!(""), }, 9 => match (&state.values[0],&state.values[1],&state.values[2]) { (&Token::Elements(ref list),&Token::Comma,&Token::Value(ref value)) => Token::Elements({let mut new=(list.clone()); new.push(value.clone()); new}), _ => panic!(""), }, 10 => match &state.values[0] { &Token::LitStr(ref s) => Token::Value(JsonValue::Literal(s.clone())), _ => panic!(""), }, 11 => match &state.values[0] { &Token::Number(v) => Token::Value(JsonValue::Number(v)), _ => panic!(""), }, 12 => match &state.values[0] { &Token::Object(ref value) => Token::Value(value.clone()), _ => panic!(""), }, 13 => match &state.values[0] { &Token::Array(ref list) => Token::Value(JsonValue::Array(list.clone())), _ => panic!(""), }, 14 => match &state.values[0] { &Token::True => Token::Value(JsonValue::True), _ => panic!(""), }, 15 => match &state.values[0] { &Token::False => Token::Value(JsonValue::False), _ => panic!(""), }, 16 => match &state.values[0] { &Token::Null => Token::Value(JsonValue::Null), _ => panic!(""), }, _ => panic!(""), } }//compute_value fn table_terminal(token_index:usize)->bool { match token_index { 1|2|3|4|5|6|7|8|9|10|11 => true, 0|12|13|14|15|16|17 => false, _ => panic!("table_terminal"), } }//table_terminal fn table_priority(a:usize, b:usize) -> Option { match (a,b) { _ => None, } }//table_priority fn table_associativity(rule:usize) -> Option { match rule { _ => None, } }//table_associativity fn to_usize(token:&Token) -> usize { match token { &Token::DummyStart => 0, &Token::True => 1, &Token::False => 2, &Token::Null => 3, &Token::Number(_) => 4, &Token::LitStr(_) => 5, &Token::LBrace => 6, &Token::RBrace => 7, &Token::LBracket => 8, &Token::RBracket => 9, &Token::Comma => 10, &Token::Colon => 11, &Token::Object(_) => 12, &Token::Members(_) => 13, &Token::Pair(_,_) => 14, &Token::Array(_) => 15, &Token::Elements(_) => 16, &Token::Value(_) => 17, } }//to_usize }//impl