1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
mod header;
pub mod notes;
pub mod obj;
mod random;
pub mod rng;
use std::ops::ControlFlow;
use self::{header::Header, notes::Notes, random::RandomParser, rng::Rng};
use crate::lex::{command::ObjId, token::TokenStream};
#[derive(Debug, Clone)]
pub enum ParseError {
SyntaxError(String),
BpmParseError(String),
UndefinedObject(ObjId),
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParseError::SyntaxError(mes) => write!(f, "syntax error: {}", mes),
ParseError::BpmParseError(bpm) => write!(f, "not a number bpm: {}", bpm),
ParseError::UndefinedObject(id) => write!(f, "undefined object: {:?}", id),
}
}
}
impl std::error::Error for ParseError {}
pub type Result<T> = std::result::Result<T, ParseError>;
#[derive(Debug)]
pub struct Bms {
pub header: Header,
pub notes: Notes,
}
impl Bms {
pub fn from_token_stream(token_stream: &TokenStream, rng: impl Rng) -> Result<Self> {
let mut random_parser = RandomParser::new(rng);
let mut notes = Notes::default();
let mut header = Header::default();
for token in token_stream.iter() {
match random_parser.parse(token) {
ControlFlow::Continue(_) => {}
ControlFlow::Break(Ok(_)) => continue,
ControlFlow::Break(Err(e)) => return Err(e),
}
notes.parse(token)?;
header.parse(token)?;
}
Ok(Self { header, notes })
}
}