corewars_parser/
lib.rs

1//! This module is used for parsing a Redcode program.
2//! It operates in multiple phases, which are found in the [phase](phase/index.html)
3//! module. Each phase passes its result to the next phase.
4
5pub use error::{Error, Warning};
6pub use result::Result;
7
8mod error;
9mod grammar;
10mod phase;
11mod result;
12
13use std::convert::TryFrom;
14
15use corewars_core::load_file::Warrior;
16
17use phase::{CommentsRemoved, Evaluated, Expanded, Output, Phase, Raw};
18
19/// Parse a given input string into a [`Result`](Result). If successful the
20/// `Result` will contain a `Warrior`, otherwise it will contain an error. In
21/// either case, one or more [`Warning`](error::Warning)s may be generated with
22/// the `Warrior`.
23pub fn parse(input: &str) -> Result<Warrior> {
24    parse_impl(input).into()
25}
26
27fn parse_impl(input: &str) -> std::result::Result<Warrior, Error> {
28    let raw = Phase::<Raw>::from(input);
29
30    let cleaned = Phase::<CommentsRemoved>::from(raw);
31
32    let expanded = Phase::<Expanded>::from(cleaned);
33
34    let evaluated = Phase::<Evaluated>::try_from(expanded)?;
35
36    let output = Phase::<Output>::from(evaluated);
37
38    Ok(output.state.warrior)
39}