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

View File

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