From f7b61f9e0ef1b2085d4c4ad7ada721d4cec36be3 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 12 Apr 2024 15:43:34 +0200 Subject: [PATCH] lang: start working on parser that parses to events --- crates/lang/src/main.rs | 12 ++++-- crates/lang/src/parser/ast/lossless/parser.rs | 37 ++++++++++++++----- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/crates/lang/src/main.rs b/crates/lang/src/main.rs index 16ec4e4..203e1eb 100644 --- a/crates/lang/src/main.rs +++ b/crates/lang/src/main.rs @@ -3,7 +3,10 @@ use std::{fs, path::PathBuf}; use clap::Parser; use lang::{ err_reporting::ErrorCollector, - parser::ast::lossless::{lex, parser}, + parser::ast::lossless::{ + lex, + parser::{self, parser_to_events::to_events}, + }, }; #[derive(Parser)] @@ -17,8 +20,11 @@ fn main() { let n = args.file.clone(); let f = fs::read_to_string(n.clone()).expect("failed to read file"); println!("toks: {:?}", lex::lex(&f)); - let parse_res = parser::parse(&f); - println!("parse: {:?}", parse_res); + println!("evs: {:?}", to_events(&f)); + + // let parse_res = parser::parse(&f); + // println!("parse: {:?}", parse_res); + // dbg!(lex::lex(&f)); // let mut err_collector = ErrorCollector::new(vec![(n.to_str().unwrap(), &f)]); diff --git a/crates/lang/src/parser/ast/lossless/parser.rs b/crates/lang/src/parser/ast/lossless/parser.rs index 95bb7d7..b5f5e85 100644 --- a/crates/lang/src/parser/ast/lossless/parser.rs +++ b/crates/lang/src/parser/ast/lossless/parser.rs @@ -11,20 +11,37 @@ use crate::parser::{ use super::lex::{self, SyntaxKind}; -mod parsers { - use rowan::GreenNode; +pub mod parser_to_events { + use chumsky::Parser; - use crate::parser::ast::lossless::lex::SyntaxKind; + use crate::parser::ast::lossless::lex::SyntaxKind::{self, *}; - use super::SyntaxError; - - struct ParseResult { - green_node: GreenNode, - errors: Vec, + #[derive(Debug, PartialEq, Eq)] + pub(super) enum Event { + StartNode, + StartErr(SyntaxError), + EatToken, + FinishNode, + FinishErr, } - trait Parser { - fn parse<'src>(input: &[(SyntaxKind, &'src str)]) -> ParseResult; + #[derive(Debug, PartialEq, Eq)] + enum SyntaxError { + Expected(SyntaxKind), + AttrExpectedValue, + /// guessed if there's a newline and attr on next line without comma + /// should then suggest comma after attr + ExpectedCommaBetweenAttrs, + } + + pub fn to_events(src: &str) -> Vec { + let mut tokens = lex::lex(src); + parser().parse(tokens) + } + + pub fn parser() -> impl Parser<'static, SyntaxKind, Vec> { + let whitespace = just(WHITESPACE).or(NEWLINE).repeated().collect::>(); + whitespace } }