cl-parser: Move parser into its own crate

This commit is contained in:
John 2024-02-29 20:41:07 -06:00
parent 1afde9ce35
commit cc281fc6ab
11 changed files with 44 additions and 20 deletions

View File

@ -6,6 +6,7 @@ members = [
"cl-structures", "cl-structures",
"cl-token", "cl-token",
"cl-ast", "cl-ast",
"cl-parser",
] ]
resolver = "2" resolver = "2"

View File

@ -1,5 +1,5 @@
//! # The Abstract Syntax Tree //! # 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 //! # Notable nodes
//! - [Item] and [ItemKind]: Top-level constructs //! - [Item] and [ItemKind]: Top-level constructs

View File

@ -8,6 +8,10 @@ license.workspace = true
publish.workspace = true publish.workspace = true
[dependencies] [dependencies]
conlang = { path = "../libconlang" }
cl-ast = { path = "../cl-ast" } cl-ast = { path = "../cl-ast" }
cl-structures = { path = "../cl-structures" } cl-structures = { path = "../cl-structures" }
[dev-dependencies]
conlang = { path = "../libconlang" }
cl-parser = { path = "../cl-parser" }

View File

@ -1,7 +1,8 @@
#![allow(unused_imports)] #![allow(unused_imports)]
use crate::{env::Environment, temp_type_impl::ConValue, Interpret}; use crate::{env::Environment, temp_type_impl::ConValue, Interpret};
use cl_ast::*; use cl_ast::*;
use conlang::{lexer::Lexer, parser::Parser}; use cl_parser::Parser;
use conlang::lexer::Lexer;
pub use macros::*; pub use macros::*;
mod macros { mod macros {

14
cl-parser/Cargo.toml Normal file
View File

@ -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" }

View File

@ -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] //! For the full grammar, see [grammar.ebnf][1]
//! //!
//! [1]: https://git.soft.fish/j/Conlang/src/branch/main/grammar.ebnf //! [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, Error,
ErrorKind::{self, *}, ErrorKind::{self, *},
PResult, Parsing, PResult, Parsing,
}; };
use crate::lexer::{error::Error as LexError, Lexer};
use cl_ast::*; use cl_ast::*;
use cl_structures::span::*; use conlang::lexer::Lexer;
use cl_token::*;
pub mod error { pub mod error {
use std::fmt::Display;
use super::*; use super::*;
use conlang::lexer::error::{Error as LexError, Reason};
use std::fmt::Display;
pub type PResult<T> = Result<T, Error>; pub type PResult<T> = Result<T, Error>;
/// Contains information about [Parser] errors /// Contains information about [Parser] errors
@ -49,7 +52,6 @@ pub mod error {
} }
impl From<LexError> for ErrorKind { impl From<LexError> for ErrorKind {
fn from(value: LexError) -> Self { fn from(value: LexError) -> Self {
use crate::lexer::error::Reason;
match value.reason() { match value.reason() {
Reason::EndOfFile => Self::EndOfInput, Reason::EndOfFile => Self::EndOfInput,
_ => Self::Lexical(value), _ => Self::Lexical(value),
@ -130,9 +132,9 @@ pub mod error {
let Self { reason, while_parsing, loc } = self; let Self { reason, while_parsing, loc } = self;
match reason { match reason {
// TODO entries are debug-printed // 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 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}"), _ => 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> { pub struct Parser<'t> {
/// Lazy tokenizer /// Lazy tokenizer
lexer: Lexer<'t>, lexer: Lexer<'t>,

View File

@ -12,8 +12,9 @@ publish.workspace = true
[dependencies] [dependencies]
conlang = { path = "../libconlang" } conlang = { path = "../libconlang" }
cl-ast = { path = "../cl-ast" } cl-ast = { path = "../cl-ast" }
cl-interpret = { path = "../cl-interpret" }
cl-token = { path = "../cl-token" } cl-token = { path = "../cl-token" }
cl-parser = { path = "../cl-parser" }
cl-interpret = { path = "../cl-interpret" }
crossterm = "0.27.0" crossterm = "0.27.0"
[dev-dependencies] [dev-dependencies]

View File

@ -1,8 +1,9 @@
//! Collects identifiers into a list //! Collects identifiers into a list
use cl_parser::Parser;
use cl_repl::repline::Repline; use cl_repl::repline::Repline;
use cl_structures::span::Loc; use cl_structures::span::Loc;
use conlang::{lexer::Lexer, parser::Parser}; use conlang::lexer::Lexer;
use std::{ use std::{
collections::HashMap, collections::HashMap,
error::Error, error::Error,

View File

@ -74,10 +74,10 @@ pub mod program {
}; };
use cl_ast::{self as ast, ast_impl::format::Pretty}; use cl_ast::{self as ast, ast_impl::format::Pretty};
use cl_parser::{error::PResult, Parser};
use conlang::{ use conlang::{
// pretty_printer::{PrettyPrintable, Printer}, // pretty_printer::{PrettyPrintable, Printer},
lexer::Lexer, lexer::Lexer,
parser::{error::PResult, Parser},
resolver::{error::TyResult, Resolver}, resolver::{error::TyResult, Resolver},
}; };
use std::{fmt::Display, io::Write}; use std::{fmt::Display, io::Write};
@ -228,7 +228,7 @@ pub mod cli {
match (repl, path) { match (repl, path) {
(true, Some(path)) => { (true, Some(path)) => {
let prog = std::fs::read_to_string(path).unwrap(); 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() .file()
.unwrap(); .unwrap();
let mut env = cl_interpret::env::Environment::new(); let mut env = cl_interpret::env::Environment::new();

View File

@ -14,5 +14,5 @@ repository.workspace = true
[dependencies] [dependencies]
unicode-xid = "0.2.4" unicode-xid = "0.2.4"
cl-ast = { path = "../cl-ast" } cl-ast = { path = "../cl-ast" }
cl-structures = { path = "../cl-structures" }
cl-token = { path = "../cl-token" } cl-token = { path = "../cl-token" }
cl-structures = { path = "../cl-structures" }

View File

@ -4,8 +4,6 @@
pub mod lexer; pub mod lexer;
pub mod parser;
pub mod resolver; pub mod resolver;
#[cfg(test)] #[cfg(test)]