Move lexer into parse::

This commit is contained in:
mars 2022-03-25 16:07:03 -06:00
parent 15687c337c
commit 24414dcdd1
4 changed files with 72 additions and 75 deletions

View File

@ -4,73 +4,6 @@
#[macro_use]
extern crate pest_derive;
use console::Style;
pub mod lexer;
pub mod namevec;
pub mod parse;
pub mod symbols;
use lexer::Token;
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 {
Struct | Function | For | If | Else | In | Let | Mut => &self.keyword,
BinaryInteger | OctalInteger | DecimalInteger => &self.literal,
SingleLineComment => &self.comment,
Error => &self.error,
_ => &self.normal,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lex_file() {
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);
}
}
// TODO use spans to color-code instead of raw tokens, to show original whitespace
/*#[test]
fn color_file() {
let source = include_str!("test/example.fae");
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()));
}
}*/
}

View File

@ -200,3 +200,66 @@ impl<'a> Iterator for Lexer<'a> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::Style;
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 {
Struct | Function | For | If | Else | In | Let | Mut => &self.keyword,
BinaryInteger | OctalInteger | DecimalInteger => &self.literal,
SingleLineComment => &self.comment,
Error => &self.error,
_ => &self.normal,
}
}
}
#[test]
fn lex_file() {
let source = include_str!("../test/clock.fae");
let theme = ColorTheme::new();
let mut lex = 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);
}
}
// TODO use spans to color-code instead of raw tokens, to show original whitespace
/*#[test]
fn color_file() {
let source = include_str!("test/example.fae");
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()));
}
}*/
}

View File

@ -1,10 +1,11 @@
// Copyright (c) 2022 Marceline Cramer
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::lexer::{Lexer, Token};
pub mod lexer;
pub mod pest;
use lexer::{Lexer, Token};
#[derive(Debug)]
pub struct ParseTree<'a> {
pub defs: Vec<Definition<'a>>,
@ -659,7 +660,7 @@ mod tests {
#[test]
fn function() {
parse(include_str!("test/function.fae"));
parse(include_str!("../test/function.fae"));
}
#[test]
@ -669,22 +670,22 @@ mod tests {
#[test]
fn structure() {
parse(include_str!("test/structure.fae"));
parse(include_str!("../test/structure.fae"));
}
#[test]
fn interface() {
parse(include_str!("test/interface.fae"));
parse(include_str!("../test/interface.fae"));
}
#[test]
fn example() {
parse(include_str!("test/example.fae"));
parse(include_str!("../test/example.fae"));
}
#[test]
fn clock() {
parse(include_str!("test/clock.fae"));
parse(include_str!("../test/clock.fae"));
}
mod expr {

View File

@ -151,7 +151,7 @@ mod tests {
use super::*;
fn from_source(source: &str) -> SymbolTable {
let mut lex = crate::lexer::Lexer::new(source);
let mut lex = parse::lexer::Lexer::new(source);
let pt = parse::ParseTree::build(&mut lex);
let symtab = SymbolTable::from_parse_tree(&pt);
println!("{:#?}", symtab);