sprite-rs/src/lib.rs

74 lines
1.8 KiB
Rust
Raw Normal View History

2022-02-28 22:43:27 +00:00
// Copyright (c) 2022 Marceline Cramer
// SPDX-License-Identifier: GPL-3.0-or-later
2022-02-27 17:26:39 +00:00
use console::Style;
pub mod lexer;
2022-03-04 03:04:03 +00:00
pub mod namevec;
2022-03-03 19:14:39 +00:00
pub mod parse;
2022-03-04 03:04:03 +00:00
pub mod symbols;
2022-02-27 18:27:22 +00:00
use lexer::Token;
2022-02-27 17:26:39 +00:00
pub struct ColorTheme {
normal: Style,
keyword: Style,
literal: Style,
comment: Style,
error: Style,
}
impl ColorTheme {
pub fn new() -> Self {
Self {
normal: Style::default(),
keyword: Style::new().blue(),
literal: Style::new().cyan(),
comment: Style::new().white(),
error: Style::new().black().on_red().bold(),
}
}
pub fn token_style(&self, token: &Token) -> &Style {
use Token::*;
match token {
2022-03-01 17:27:41 +00:00
Struct | Function | For | If | Else | In | Let | Mut => &self.keyword,
2022-02-27 17:26:39 +00:00
BinaryInteger | OctalInteger | DecimalInteger => &self.literal,
SingleLineComment => &self.comment,
Error => &self.error,
_ => &self.normal,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lex_file() {
2022-03-02 19:30:47 +00:00
let source = include_str!("test/clock.fae");
let theme = ColorTheme::new();
let mut lex = lexer::Lexer::new(source);
while let Some(token) = lex.next() {
let style = theme.token_style(&token);
let styled = style.apply_to(format!("{:?}: {}", token, lex.slice()));
println!("{}", styled);
}
}
2022-02-27 18:27:22 +00:00
// TODO use spans to color-code instead of raw tokens, to show original whitespace
/*#[test]
2022-02-27 17:26:39 +00:00
fn color_file() {
2022-02-28 20:27:27 +00:00
let source = include_str!("test/example.fae");
2022-02-27 17:26:39 +00:00
let theme = ColorTheme::new();
let mut lex = Token::lexer(source);
while let Some(token) = lex.next() {
let style = theme.token_style(&token);
print!("{}", style.apply_to(lex.slice()));
}
2022-02-27 18:27:22 +00:00
}*/
2022-02-27 17:26:39 +00:00
}