span: Break out into its own crate, to make room for future expansion
This commit is contained in:
parent
362817e512
commit
69f5035a8b
@ -1,5 +1,5 @@
|
||||
[workspace]
|
||||
members = ["libconlang", "cl-repl", "cl-interpret"]
|
||||
members = ["libconlang", "cl-repl", "cl-interpret", "cl-structures"]
|
||||
resolver = "2"
|
||||
|
||||
[workspace.package]
|
||||
|
@ -9,3 +9,4 @@ publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
conlang = { path = "../libconlang" }
|
||||
cl-structures = { path = "../cl-structures"}
|
||||
|
@ -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<T> = Result<T, Error>;
|
||||
|
||||
|
@ -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" }
|
||||
|
@ -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,
|
||||
|
10
cl-structures/Cargo.toml
Normal file
10
cl-structures/Cargo.toml
Normal 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
44
cl-structures/src/lib.rs
Normal 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}:")
|
||||
}
|
||||
}
|
||||
}
|
@ -13,3 +13,4 @@ repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
unicode-xid = "0.2.4"
|
||||
cl-structures = { path = "../cl-structures" }
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
@ -2,8 +2,6 @@
|
||||
#![warn(clippy::all)]
|
||||
#![feature(decl_macro)]
|
||||
|
||||
pub mod common;
|
||||
|
||||
pub mod token;
|
||||
|
||||
pub mod ast;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user