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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
//! Lexical analyzer of BMS format.
pub mod command;
mod cursor;
pub mod token;
use thiserror::Error;
use self::{
cursor::Cursor,
token::{Token, TokenStream},
};
/// An error occurred when lexical analysis.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
pub enum LexError {
/// An unknown command detected.
#[error("unknown command found at line {line}, col {col}")]
UnknownCommand {
/// The line number of the command detected.
line: usize,
/// The column number of the command detected.
col: usize,
},
/// The token was expected but not found.
#[error("expected {message}, but not found at line {line}, col {col}")]
ExpectedToken {
/// The line number of the token expected.
line: usize,
/// The column number of the token expected.
col: usize,
/// What the expected is.
message: &'static str,
},
}
/// An error occurred when lexical analyzing the BMS format file.
pub type Result<T> = std::result::Result<T, LexError>;
/// Analyzes and converts the BMS format text into [`TokenStream`].
pub fn parse(source: &str) -> Result<TokenStream> {
let mut cursor = Cursor::new(source);
let mut tokens = vec![];
while !cursor.is_end() {
tokens.push(Token::parse(&mut cursor)?);
}
Ok(TokenStream::from_tokens(tokens))
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::{command::*, parse, token::Token::*};
#[test]
fn simple() {
const SRC: &str = r"
#PLAYER 1
#GENRE FUGA
#TITLE BAR(^^)
#ARTIST MikuroXina
#BPM 120
#PLAYLEVEL 6
#RANK 2
#BACKBMP boon.jpg
#WAV01 hoge.WAV
#WAV02 foo.WAV
#WAV03 bar.WAV
#00211:0303030303
#00211:0303000303
#00211:010101
#00211:00020202
";
let ts = parse(SRC).expect("SRC must be parsed");
let id1 = 1.try_into().unwrap();
let id2 = 2.try_into().unwrap();
let id3 = 3.try_into().unwrap();
let tokens: Vec<_> = ts.into_iter().collect();
assert_eq!(
tokens,
vec![
Player(PlayerMode::Single),
Genre("FUGA"),
Title("BAR(^^)"),
Artist("MikuroXina"),
Bpm("120"),
PlayLevel(6),
Rank(JudgeLevel::Normal),
BackBmp(Path::new("boon.jpg")),
Wav(id1, Path::new("hoge.WAV")),
Wav(id2, Path::new("foo.WAV")),
Wav(id3, Path::new("bar.WAV")),
Message {
track: Track(2),
channel: Channel::Note {
kind: NoteKind::Visible,
is_player1: true,
key: Key::Key1,
},
message: "0303030303",
},
Message {
track: Track(2),
channel: Channel::Note {
kind: NoteKind::Visible,
is_player1: true,
key: Key::Key1,
},
message: "0303000303",
},
Message {
track: Track(2),
channel: Channel::Note {
kind: NoteKind::Visible,
is_player1: true,
key: Key::Key1,
},
message: "010101",
},
Message {
track: Track(2),
channel: Channel::Note {
kind: NoteKind::Visible,
is_player1: true,
key: Key::Key1,
},
message: "00020202",
},
]
);
}
}