diff --git a/Cargo.toml b/Cargo.toml index ddf9553..62902bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["libconlang", "cl-repl", "cl-interpret"] +members = ["libconlang", "cl-repl", "cl-interpret", "cl-structures"] resolver = "2" [workspace.package] diff --git a/cl-interpret/Cargo.toml b/cl-interpret/Cargo.toml index 07d0b9d..fd09019 100644 --- a/cl-interpret/Cargo.toml +++ b/cl-interpret/Cargo.toml @@ -9,3 +9,4 @@ publish.workspace = true [dependencies] conlang = { path = "../libconlang" } +cl-structures = { path = "../cl-structures"} diff --git a/cl-interpret/src/lib.rs b/cl-interpret/src/lib.rs index b5e1e82..9ddae23 100644 --- a/cl-interpret/src/lib.rs +++ b/cl-interpret/src/lib.rs @@ -517,7 +517,7 @@ pub mod error { //! The [Error] type represents any error thrown by the [Environment](super::Environment) use super::temp_type_impl::ConValue; - use conlang::common::Loc; + use cl_structures::span::Loc; pub type IResult = Result; diff --git a/cl-repl/Cargo.toml b/cl-repl/Cargo.toml index 239aeb9..940b145 100644 --- a/cl-repl/Cargo.toml +++ b/cl-repl/Cargo.toml @@ -13,3 +13,6 @@ publish.workspace = true conlang = { path = "../libconlang" } cl-interpret = { path = "../cl-interpret" } crossterm = "0.27.0" + +[dev-dependencies] +cl-structures = { path = "../cl-structures" } diff --git a/cl-repl/examples/collect-identifiers.rs b/cl-repl/examples/collect-identifiers.rs index a6158ca..ebc6d13 100644 --- a/cl-repl/examples/collect-identifiers.rs +++ b/cl-repl/examples/collect-identifiers.rs @@ -1,7 +1,8 @@ //! Collects identifiers into a list use cl_repl::repline::Repline; -use conlang::{common::Loc, lexer::Lexer, parser::Parser}; +use cl_structures::span::Loc; +use conlang::{lexer::Lexer, parser::Parser}; use std::{ collections::HashMap, error::Error, diff --git a/cl-structures/Cargo.toml b/cl-structures/Cargo.toml new file mode 100644 index 0000000..b8e7ea9 --- /dev/null +++ b/cl-structures/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "cl-structures" +repository.workspace = true +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true +publish.workspace = true + +[dependencies] diff --git a/cl-structures/src/lib.rs b/cl-structures/src/lib.rs new file mode 100644 index 0000000..11b63fa --- /dev/null +++ b/cl-structures/src/lib.rs @@ -0,0 +1,44 @@ +//! # Universally useful structures +//! - [Span](struct@span::Span): Stores a start and end [Loc](struct@span::Loc) +//! - [Loc](struct@span::Loc): Stores the index in a stream + +pub mod span { + //! - [struct@Span]: Stores the start and end [struct@Loc] of a notable AST node + //! - [struct@Loc]: Stores the line/column of a notable AST node + #![allow(non_snake_case)] + + /// Stores the start and end [locations](struct@Loc) within the token stream + #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct Span { + pub head: Loc, + pub tail: Loc, + } + pub fn Span(head: Loc, tail: Loc) -> Span { + Span { head, tail } + } + + /// Stores a read-only (line, column) location in a token stream + #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct Loc { + line: u32, + col: u32, + } + pub fn Loc(line: u32, col: u32) -> Loc { + Loc { line, col } + } + impl Loc { + pub fn line(self) -> u32 { + self.line + } + pub fn col(self) -> u32 { + self.col + } + } + + impl std::fmt::Display for Loc { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let Loc { line, col } = self; + write!(f, "{line}:{col}:") + } + } +} diff --git a/libconlang/Cargo.toml b/libconlang/Cargo.toml index 3ce19d0..ea0bf6b 100644 --- a/libconlang/Cargo.toml +++ b/libconlang/Cargo.toml @@ -13,3 +13,4 @@ repository.workspace = true [dependencies] unicode-xid = "0.2.4" +cl-structures = { path = "../cl-structures" } diff --git a/libconlang/src/ast.rs b/libconlang/src/ast.rs index 3335fec..e221d7f 100644 --- a/libconlang/src/ast.rs +++ b/libconlang/src/ast.rs @@ -9,7 +9,7 @@ //! - [AssignKind], [BinaryKind], and [UnaryKind] operators //! - [Ty] and [TyKind]: Type qualifiers //! - [Path]: Path expressions -use crate::common::*; +use cl_structures::span::*; pub mod ast_impl; diff --git a/libconlang/src/common.rs b/libconlang/src/common.rs deleted file mode 100644 index 6382f3f..0000000 --- a/libconlang/src/common.rs +++ /dev/null @@ -1,46 +0,0 @@ -//! # Universally useful structures -//! - [struct@Span]: Stores the start and end [struct@Loc] of a notable AST node -//! - [struct@Loc]: Stores the line/column of a notable AST node -#![allow(non_snake_case)] -use crate::lexer::Lexer; - -/// Stores the start and end [locations](struct@Loc) within the token stream -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Span { - pub head: Loc, - pub tail: Loc, -} -pub fn Span(head: Loc, tail: Loc) -> Span { - Span { head, tail } -} - -/// Stores a read-only (line, column) location in a token stream -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Loc { - line: u32, - col: u32, -} -pub fn Loc(line: u32, col: u32) -> Loc { - Loc { line, col } -} -impl Loc { - pub fn line(self) -> u32 { - self.line - } - pub fn col(self) -> u32 { - self.col - } -} - -impl std::fmt::Display for Loc { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let Loc { line, col } = self; - write!(f, "{line}:{col}:") - } -} - -impl<'t> From<&Lexer<'t>> for Loc { - fn from(value: &Lexer<'t>) -> Self { - Loc(value.line(), value.col()) - } -} diff --git a/libconlang/src/lexer.rs b/libconlang/src/lexer.rs index 5193539..a668ecf 100644 --- a/libconlang/src/lexer.rs +++ b/libconlang/src/lexer.rs @@ -1,5 +1,6 @@ //! Converts a text file into tokens use crate::token::preamble::*; +use cl_structures::span::Loc; use std::{ iter::Peekable, str::{Chars, FromStr}, @@ -445,6 +446,12 @@ impl<'t> Lexer<'t> { } } +impl<'t> From<&Lexer<'t>> for Loc { + fn from(value: &Lexer<'t>) -> Self { + Loc(value.line(), value.col()) + } +} + use error::{Error, LResult, Reason}; pub mod error { //! [Error] type for the [Lexer](super::Lexer) diff --git a/libconlang/src/lib.rs b/libconlang/src/lib.rs index 0f36fba..cea409a 100644 --- a/libconlang/src/lib.rs +++ b/libconlang/src/lib.rs @@ -2,8 +2,6 @@ #![warn(clippy::all)] #![feature(decl_macro)] -pub mod common; - pub mod token; pub mod ast; diff --git a/libconlang/src/parser.rs b/libconlang/src/parser.rs index c8b67a2..88ff585 100644 --- a/libconlang/src/parser.rs +++ b/libconlang/src/parser.rs @@ -11,7 +11,6 @@ use self::error::{ }; use crate::{ ast::*, - common::*, lexer::{error::Error as LexError, Lexer}, token::{ token_data::Data, @@ -19,6 +18,7 @@ use crate::{ Token, }, }; +use cl_structures::span::*; pub mod error { use std::fmt::Display;