rework parser

This commit is contained in:
Volodymyr Patuta 2020-11-08 19:17:57 +01:00
parent e4331a6523
commit 6e55705184
3 changed files with 69 additions and 73 deletions

View File

@ -1,3 +1,5 @@
world = { ASCII_DIGIT+ ~ " " ~ ASCII_DIGIT+ } World = { HeaderWorld ~ NEWLINE+ ~ Robot+ }
robot_init = { ASCII_DIGIT+ ~ " " ~ ASCII_DIGIT+ ~ " " ~ ("S" | "N" | "W" | "E") } HeaderWorld = { ASCII_DIGIT+ ~ " " ~ ASCII_DIGIT+ }
robot_instructions = { ASCII_ALPHA_UPPER+ } Robot = { HeaderRobot ~ NEWLINE ~ Instructions ~ NEWLINE+ }
HeaderRobot = { ASCII_DIGIT+ ~ " " ~ ASCII_DIGIT+ ~ " " ~ ("S" | "N" | "W" | "E") }
Instructions = { ("F" | "L" | "R")+ }

View File

@ -75,77 +75,71 @@ fn gen_random_instructions() -> String {
/// Parse the config file, generate the world and robot pool. /// Parse the config file, generate the world and robot pool.
fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, String> { fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, String> {
let mut lines: Vec<&str> = conf.split('\n').collect(); let pairs = match ConfParser::parse(Rule::World, &conf) {
let raw_world = match ConfParser::parse(Rule::world, lines.remove(0)) { Ok(p) => p,
Ok(s) => s.as_str(), Err(_) => return Err(String::from("Config is broken.")),
Err(_) => return Err(String::from("World config is broken.")),
}; };
let mut world = world::World { x: 0, y: 0 };
let mut id = 1;
for pair in pairs {
for inner in pair.into_inner() {
match inner.as_rule() {
Rule::HeaderWorld => {
let mut w: Vec<i32> = Vec::with_capacity(2); let mut w: Vec<i32> = Vec::with_capacity(2);
for n in raw_world.split_whitespace() { for n in inner.as_str().split_whitespace() {
let v: i32 = n.parse::<i32>().unwrap(); let v: i32 = n.parse::<i32>().unwrap();
w.push(v); w.push(v);
} }
let world = world::World { x: w[0], y: w[1] }; world = world::World { x: w[0], y: w[1] };
lines.remove(0);
let mut r_id: u32 = 0;
loop {
r_id += 1;
if lines.len() == 0 {
break;
} }
let raw_setup = match ConfParser::parse(Rule::robot_init, lines.remove(0)) { Rule::Robot => {
Ok(s) => s.as_str(), let mut vpos: Vec<robot::Position> = Vec::new();
Err(_) => return Err(String::from("Robot setup is broken.")), let mut vor: Vec<robot::Orientation> = Vec::new();
}; let mut vinst: Vec<Vec<robot::Instruction>> = Vec::new();
for inner_robot in inner.into_inner() {
let rand_instructions = gen_random_instructions(); match inner_robot.as_rule() {
let l = lines.remove(0); Rule::HeaderRobot => {
let mut setup = inner_robot.as_str().split_whitespace();
let instructions = match ConfParser::parse(Rule::robot_instructions, l) { let rx = setup.next().unwrap();
Ok(s) => s.as_str(), let ry = setup.next().unwrap();
Err(_) => rand_instructions.as_str(), let ro = setup.next().unwrap();
}; vpos.push(robot::Position {
x: rx.parse::<i32>().unwrap(),
let mut setup = raw_setup.split_whitespace(); y: ry.parse::<i32>().unwrap(),
let pos_x = setup.next().unwrap(); });
let pos_y = setup.next().unwrap(); vor.push(match ro {
let orientation = setup.next().unwrap();
// Convert values of the setup line
let r_x = pos_x.parse::<i32>().unwrap();
let r_y = pos_y.parse::<i32>().unwrap();
let r_o = match orientation {
"N" => robot::Orientation::N, "N" => robot::Orientation::N,
"E" => robot::Orientation::E, "E" => robot::Orientation::E,
"S" => robot::Orientation::S, "S" => robot::Orientation::S,
"W" => robot::Orientation::W, _ => robot::Orientation::W,
_ => { })
return Err(String::from(
"The third token of the setup line do not match any orientations !",
))
} }
}; Rule::Instructions => {
if !robot::is_instructions(instructions) { let instructions = inner_robot.as_str();
return Err(String::from("Invalid instructions !")); vinst.push(robot::instructions_from_string(
} instructions.chars().rev().collect::<String>(),
)?);
let inst = robot::instructions_from_string(instructions.chars().rev().collect::<String>())?; let r = robot::Robot::new(
id,
let r = robot::Robot::new(r_id, r_o, robot::Position { x: r_x, y: r_y }, inst); vor.pop().unwrap(),
// Load robot inside the pool. vpos.pop().unwrap(),
vinst.pop().unwrap(),
);
match check_map(&r, &world) { match check_map(&r, &world) {
Ok(()) => pool.push(r), Ok(()) => pool.push(r),
Err(err) => return Err(err), Err(err) => return Err(err),
} }
id += 1;
if lines.len() == 0 {
break;
} }
if l.len() == 0 { _ => unreachable!(),
continue; }
}
}
_ => unreachable!(),
};
} }
lines.remove(0);
} }
Ok(world) Ok(world)
} }

View File

@ -9,7 +9,7 @@ pub struct Robot {
pub id: u32, pub id: u32,
pub o: Orientation, pub o: Orientation,
pub p: Position, pub p: Position,
i: Vec<Instruction>, pub i: Vec<Instruction>,
} }
impl Robot { impl Robot {