From 9c415417129a5ca4dee630087ae8741083550a1d Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sun, 11 Aug 2024 19:02:18 +0300 Subject: [PATCH] refactor: lexer types, start parser --- src/daleth/format.rs | 3 ++- src/daleth/lexer/mod.rs | 7 +++++-- src/daleth/lexer/types.rs | 11 ----------- src/daleth/mod.rs | 2 ++ src/daleth/parser/mod.rs | 24 ++++++++++++++++++++++++ src/daleth/parser/types.rs | 8 ++++++++ src/daleth/types.rs | 4 ++++ 7 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 src/daleth/parser/mod.rs create mode 100644 src/daleth/parser/types.rs create mode 100644 src/daleth/types.rs diff --git a/src/daleth/format.rs b/src/daleth/format.rs index 6df0511..bd81691 100644 --- a/src/daleth/format.rs +++ b/src/daleth/format.rs @@ -1,5 +1,6 @@ use super::{ - lexer::types::{Spanned, Token}, + lexer::types::Token, + types::Spanned, utils::{prepend_indent, set_indent}, }; diff --git a/src/daleth/lexer/mod.rs b/src/daleth/lexer/mod.rs index 3d74435..9d9e49e 100644 --- a/src/daleth/lexer/mod.rs +++ b/src/daleth/lexer/mod.rs @@ -1,7 +1,10 @@ -use chumsky::prelude::*; -use types::{Span, Spanned, Token}; pub mod types; +use chumsky::prelude::*; +use types::*; + +use super::types::{Span, Spanned}; + pub fn lexer<'src>( ) -> impl Parser<'src, &'src str, Vec>>, extra::Err>> { let token = choice((symbol(), tag(), argument(), textual())); diff --git a/src/daleth/lexer/types.rs b/src/daleth/lexer/types.rs index 0aa9eb2..1c81d08 100644 --- a/src/daleth/lexer/types.rs +++ b/src/daleth/lexer/types.rs @@ -1,8 +1,3 @@ -use chumsky::prelude::*; - -pub type Span = SimpleSpan; -pub type Spanned = (T, Span); - #[derive(Clone, Debug, PartialEq)] pub enum Token<'src> { // Symbols @@ -69,9 +64,3 @@ pub enum Token<'src> { Pre, Meta, } - -impl<'src> From>> for Token<'src> { - fn from(value: Spanned>) -> Self { - value.0 - } -} diff --git a/src/daleth/mod.rs b/src/daleth/mod.rs index f0f0692..dbf90d9 100644 --- a/src/daleth/mod.rs +++ b/src/daleth/mod.rs @@ -1,3 +1,5 @@ pub mod format; pub mod lexer; +pub mod parser; +pub mod types; pub mod utils; diff --git a/src/daleth/parser/mod.rs b/src/daleth/parser/mod.rs new file mode 100644 index 0000000..e3d8eee --- /dev/null +++ b/src/daleth/parser/mod.rs @@ -0,0 +1,24 @@ +pub mod types; + +use super::{ + lexer::types::Token, + types::{Span, Spanned}, +}; +use crate::typed::{Page, Tag::*}; +use chumsky::prelude::*; +use types::*; + +pub fn parser<'tokens, 'src: 'tokens>() -> impl Parser< + 'tokens, + ParserInput<'tokens, 'src>, + Spanned, + extra::Err, Span>>, +> { + let br = just(Token::Br).to(Br); + + let tag = br; + + tag.repeated() + .collect() + .map_with(|t, e| (Page { data: t }, e.span())) +} diff --git a/src/daleth/parser/types.rs b/src/daleth/parser/types.rs new file mode 100644 index 0000000..b873aee --- /dev/null +++ b/src/daleth/parser/types.rs @@ -0,0 +1,8 @@ +use crate::daleth::{ + lexer::types::*, + types::{Span, Spanned}, +}; +use chumsky::input::SpannedInput; + +pub type ParserInput<'tokens, 'src> = + SpannedInput, Span, &'tokens [Spanned>]>; diff --git a/src/daleth/types.rs b/src/daleth/types.rs new file mode 100644 index 0000000..c1fd68f --- /dev/null +++ b/src/daleth/types.rs @@ -0,0 +1,4 @@ +use chumsky::span::SimpleSpan; + +pub type Span = SimpleSpan; +pub type Spanned = (T, Span);