diff --git a/Cargo.toml b/Cargo.toml index 74264b9..8cef1ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "cl-structures", "cl-token", "cl-ast", + "cl-parser", ] resolver = "2" diff --git a/cl-ast/src/lib.rs b/cl-ast/src/lib.rs index f4c3cc3..3786e84 100644 --- a/cl-ast/src/lib.rs +++ b/cl-ast/src/lib.rs @@ -1,5 +1,5 @@ //! # The Abstract Syntax Tree -//! Contains definitions of AST Nodes, to be derived by a [parser](super::parser). +//! Contains definitions of Conlang AST Nodes. //! //! # Notable nodes //! - [Item] and [ItemKind]: Top-level constructs diff --git a/cl-interpret/Cargo.toml b/cl-interpret/Cargo.toml index df93b35..f66c36f 100644 --- a/cl-interpret/Cargo.toml +++ b/cl-interpret/Cargo.toml @@ -8,6 +8,10 @@ license.workspace = true publish.workspace = true [dependencies] -conlang = { path = "../libconlang" } cl-ast = { path = "../cl-ast" } cl-structures = { path = "../cl-structures" } + + +[dev-dependencies] +conlang = { path = "../libconlang" } +cl-parser = { path = "../cl-parser" } diff --git a/cl-interpret/src/tests.rs b/cl-interpret/src/tests.rs index f603103..c0d6659 100644 --- a/cl-interpret/src/tests.rs +++ b/cl-interpret/src/tests.rs @@ -1,7 +1,8 @@ #![allow(unused_imports)] use crate::{env::Environment, temp_type_impl::ConValue, Interpret}; use cl_ast::*; -use conlang::{lexer::Lexer, parser::Parser}; +use cl_parser::Parser; +use conlang::lexer::Lexer; pub use macros::*; mod macros { diff --git a/cl-parser/Cargo.toml b/cl-parser/Cargo.toml new file mode 100644 index 0000000..5adb138 --- /dev/null +++ b/cl-parser/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "cl-parser" +repository.workspace = true +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] +cl-ast = { path = "../cl-ast" } +cl-token = { path = "../cl-token" } +cl-structures = { path = "../cl-structures" } +conlang = { path = "../libconlang" } diff --git a/libconlang/src/parser.rs b/cl-parser/src/lib.rs similarity index 99% rename from libconlang/src/parser.rs rename to cl-parser/src/lib.rs index 33f8107..ea0206e 100644 --- a/libconlang/src/parser.rs +++ b/cl-parser/src/lib.rs @@ -1,23 +1,26 @@ -//! Parses [tokens](super::token) into an [AST](super::ast) +//! Parses [tokens](cl_token::token) into an [AST](cl_ast) //! //! For the full grammar, see [grammar.ebnf][1] //! //! [1]: https://git.soft.fish/j/Conlang/src/branch/main/grammar.ebnf +#![feature(decl_macro)] -use self::error::{ +use cl_structures::span::*; +use cl_token::*; + +use crate::error::{ Error, ErrorKind::{self, *}, PResult, Parsing, }; -use crate::lexer::{error::Error as LexError, Lexer}; use cl_ast::*; -use cl_structures::span::*; -use cl_token::*; +use conlang::lexer::Lexer; pub mod error { - use std::fmt::Display; - use super::*; + + use conlang::lexer::error::{Error as LexError, Reason}; + use std::fmt::Display; pub type PResult = Result; /// Contains information about [Parser] errors @@ -49,7 +52,6 @@ pub mod error { } impl From for ErrorKind { fn from(value: LexError) -> Self { - use crate::lexer::error::Reason; match value.reason() { Reason::EndOfFile => Self::EndOfInput, _ => Self::Lexical(value), @@ -130,9 +132,9 @@ pub mod error { let Self { reason, while_parsing, loc } = self; match reason { // TODO entries are debug-printed - Todo => write!(f, "{loc} {reason} {while_parsing:?}"), + ErrorKind::Todo => write!(f, "{loc} {reason} {while_parsing:?}"), // lexical errors print their own higher-resolution loc info - Lexical(e) => write!(f, "{e} (while parsing {while_parsing})"), + ErrorKind::Lexical(e) => write!(f, "{e} (while parsing {while_parsing})"), _ => write!(f, "{loc} {reason} while parsing {while_parsing}"), } } @@ -226,6 +228,8 @@ pub mod error { } } + +/// Parses a sequence of [Tokens](Token) into an [AST](cl_ast) pub struct Parser<'t> { /// Lazy tokenizer lexer: Lexer<'t>, diff --git a/cl-repl/Cargo.toml b/cl-repl/Cargo.toml index bd20a02..89ca1fa 100644 --- a/cl-repl/Cargo.toml +++ b/cl-repl/Cargo.toml @@ -12,8 +12,9 @@ publish.workspace = true [dependencies] conlang = { path = "../libconlang" } cl-ast = { path = "../cl-ast" } -cl-interpret = { path = "../cl-interpret" } cl-token = { path = "../cl-token" } +cl-parser = { path = "../cl-parser" } +cl-interpret = { path = "../cl-interpret" } crossterm = "0.27.0" [dev-dependencies] diff --git a/cl-repl/examples/collect-identifiers.rs b/cl-repl/examples/collect-identifiers.rs index 6ced9bf..b73f754 100644 --- a/cl-repl/examples/collect-identifiers.rs +++ b/cl-repl/examples/collect-identifiers.rs @@ -1,8 +1,9 @@ //! Collects identifiers into a list +use cl_parser::Parser; use cl_repl::repline::Repline; use cl_structures::span::Loc; -use conlang::{lexer::Lexer, parser::Parser}; +use conlang::lexer::Lexer; use std::{ collections::HashMap, error::Error, diff --git a/cl-repl/src/lib.rs b/cl-repl/src/lib.rs index 15bd23b..744155c 100644 --- a/cl-repl/src/lib.rs +++ b/cl-repl/src/lib.rs @@ -74,10 +74,10 @@ pub mod program { }; use cl_ast::{self as ast, ast_impl::format::Pretty}; + use cl_parser::{error::PResult, Parser}; use conlang::{ // pretty_printer::{PrettyPrintable, Printer}, lexer::Lexer, - parser::{error::PResult, Parser}, resolver::{error::TyResult, Resolver}, }; use std::{fmt::Display, io::Write}; @@ -228,7 +228,7 @@ pub mod cli { match (repl, path) { (true, Some(path)) => { let prog = std::fs::read_to_string(path).unwrap(); - let code = conlang::parser::Parser::new(conlang::lexer::Lexer::new(&prog)) + let code = cl_parser::Parser::new(conlang::lexer::Lexer::new(&prog)) .file() .unwrap(); let mut env = cl_interpret::env::Environment::new(); diff --git a/libconlang/Cargo.toml b/libconlang/Cargo.toml index d88cead..143c33a 100644 --- a/libconlang/Cargo.toml +++ b/libconlang/Cargo.toml @@ -14,5 +14,5 @@ repository.workspace = true [dependencies] unicode-xid = "0.2.4" cl-ast = { path = "../cl-ast" } -cl-structures = { path = "../cl-structures" } cl-token = { path = "../cl-token" } +cl-structures = { path = "../cl-structures" } diff --git a/libconlang/src/lib.rs b/libconlang/src/lib.rs index 7cc7af3..cc7fc9d 100644 --- a/libconlang/src/lib.rs +++ b/libconlang/src/lib.rs @@ -4,8 +4,6 @@ pub mod lexer; -pub mod parser; - pub mod resolver; #[cfg(test)]