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

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

View File

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

View File

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

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
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)

View File

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

View File

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