span: Break out into its own crate, to make room for future expansion

This commit is contained in:
John 2024-02-29 18:31:41 -06:00
parent 362817e512
commit 69f5035a8b
13 changed files with 72 additions and 53 deletions

View File

@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["libconlang", "cl-repl", "cl-interpret"] members = ["libconlang", "cl-repl", "cl-interpret", "cl-structures"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]

View File

@ -9,3 +9,4 @@ publish.workspace = true
[dependencies] [dependencies]
conlang = { path = "../libconlang" } conlang = { path = "../libconlang" }
cl-structures = { path = "../cl-structures"}

View File

@ -517,7 +517,7 @@ pub mod error {
//! The [Error] type represents any error thrown by the [Environment](super::Environment) //! The [Error] type represents any error thrown by the [Environment](super::Environment)
use super::temp_type_impl::ConValue; use super::temp_type_impl::ConValue;
use conlang::common::Loc; use cl_structures::span::Loc;
pub type IResult<T> = Result<T, Error>; pub type IResult<T> = Result<T, Error>;

View File

@ -13,3 +13,6 @@ publish.workspace = true
conlang = { path = "../libconlang" } conlang = { path = "../libconlang" }
cl-interpret = { path = "../cl-interpret" } cl-interpret = { path = "../cl-interpret" }
crossterm = "0.27.0" crossterm = "0.27.0"
[dev-dependencies]
cl-structures = { path = "../cl-structures" }

View File

@ -1,7 +1,8 @@
//! Collects identifiers into a list //! Collects identifiers into a list
use cl_repl::repline::Repline; 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::{ use std::{
collections::HashMap, collections::HashMap,
error::Error, error::Error,

10
cl-structures/Cargo.toml Normal file
View File

@ -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]

44
cl-structures/src/lib.rs Normal file
View File

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

View File

@ -13,3 +13,4 @@ repository.workspace = true
[dependencies] [dependencies]
unicode-xid = "0.2.4" unicode-xid = "0.2.4"
cl-structures = { path = "../cl-structures" }

View File

@ -9,7 +9,7 @@
//! - [AssignKind], [BinaryKind], and [UnaryKind] operators //! - [AssignKind], [BinaryKind], and [UnaryKind] operators
//! - [Ty] and [TyKind]: Type qualifiers //! - [Ty] and [TyKind]: Type qualifiers
//! - [Path]: Path expressions //! - [Path]: Path expressions
use crate::common::*; use cl_structures::span::*;
pub mod ast_impl; pub mod ast_impl;

View File

@ -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())
}
}

View File

@ -1,5 +1,6 @@
//! Converts a text file into tokens //! Converts a text file into tokens
use crate::token::preamble::*; use crate::token::preamble::*;
use cl_structures::span::Loc;
use std::{ use std::{
iter::Peekable, iter::Peekable,
str::{Chars, FromStr}, 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}; use error::{Error, LResult, Reason};
pub mod error { pub mod error {
//! [Error] type for the [Lexer](super::Lexer) //! [Error] type for the [Lexer](super::Lexer)

View File

@ -2,8 +2,6 @@
#![warn(clippy::all)] #![warn(clippy::all)]
#![feature(decl_macro)] #![feature(decl_macro)]
pub mod common;
pub mod token; pub mod token;
pub mod ast; pub mod ast;

View File

@ -11,7 +11,6 @@ use self::error::{
}; };
use crate::{ use crate::{
ast::*, ast::*,
common::*,
lexer::{error::Error as LexError, Lexer}, lexer::{error::Error as LexError, Lexer},
token::{ token::{
token_data::Data, token_data::Data,
@ -19,6 +18,7 @@ use crate::{
Token, Token,
}, },
}; };
use cl_structures::span::*;
pub mod error { pub mod error {
use std::fmt::Display; use std::fmt::Display;