Skip to content

Commit

Permalink
♻️ Rename 'de to 'ast
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Somfic committed Jan 5, 2025
1 parent 9f9992e commit 087d5f7
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 249 deletions.
32 changes: 16 additions & 16 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ pub use token::*;

use miette::{LabeledSpan, Result, SourceSpan};

pub struct Lexer<'de> {
whole: &'de str,
remainder: &'de str,
pub struct Lexer<'ast> {
whole: &'ast str,
remainder: &'ast str,
byte_offset: usize,
peeked: Option<Result<Token<'de>, miette::Error>>,
peeked: Option<Result<Token<'ast>, miette::Error>>,
}

impl<'de> Lexer<'de> {
pub fn new(input: &'de str) -> Self {
impl<'ast> Lexer<'ast> {
pub fn new(input: &'ast str) -> Self {
Self {
whole: input,
remainder: input,
Expand All @@ -24,7 +24,7 @@ impl<'de> Lexer<'de> {
&mut self,
expected: TokenKind,
unexpected: &str,
) -> Result<Token<'de>, miette::Error> {
) -> Result<Token<'ast>, miette::Error> {
match self.next() {
Some(Ok(token)) if expected == token.kind => Ok(token),
Some(Ok(token)) => Err(miette::miette! {
Expand All @@ -50,9 +50,9 @@ impl<'de> Lexer<'de> {

pub fn expect_where(
&mut self,
mut check: impl FnMut(&Token<'de>) -> bool,
mut check: impl FnMut(&Token<'ast>) -> bool,
unexpected: &str,
) -> Result<Token<'de>, miette::Error> {
) -> Result<Token<'ast>, miette::Error> {
match self.next() {
Some(Ok(token)) if check(&token) => Ok(token),
Some(Ok(token)) => Err(miette::miette! {
Expand All @@ -73,11 +73,11 @@ impl<'de> Lexer<'de> {
}
}

pub fn expect_any(&mut self, unexpected: &str) -> Result<Token<'de>, miette::Error> {
pub fn expect_any(&mut self, unexpected: &str) -> Result<Token<'ast>, miette::Error> {
self.expect_where(|_| true, unexpected)
}

pub fn peek(&mut self) -> Option<&Result<Token<'de>, miette::Error>> {
pub fn peek(&mut self) -> Option<&Result<Token<'ast>, miette::Error>> {
if self.peeked.is_some() {
return self.peeked.as_ref();
}
Expand All @@ -89,7 +89,7 @@ impl<'de> Lexer<'de> {
pub fn peek_expect(
&mut self,
expected: TokenKind,
) -> Option<&Result<Token<'de>, miette::Error>> {
) -> Option<&Result<Token<'ast>, miette::Error>> {
match self.peek() {
Some(Ok(token::Token { kind, .. })) => {
if *kind == expected {
Expand All @@ -107,7 +107,7 @@ impl<'de> Lexer<'de> {
single: TokenKind,
compound: TokenKind,
expected_char: char,
) -> Result<(TokenKind, TokenValue<'de>)> {
) -> Result<(TokenKind, TokenValue<'ast>)> {
if let Some(c) = self.remainder.chars().next() {
if c == expected_char {
self.remainder = &self.remainder[c.len_utf8()..];
Expand All @@ -122,8 +122,8 @@ impl<'de> Lexer<'de> {
}
}

impl<'de> Iterator for Lexer<'de> {
type Item = Result<Token<'de>>;
impl<'ast> Iterator for Lexer<'ast> {
type Item = Result<Token<'ast>>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(next) = self.peeked.take() {
Expand All @@ -138,7 +138,7 @@ impl<'de> Iterator for Lexer<'de> {
self.remainder = chars.as_str();
self.byte_offset += c.len_utf8();

let kind: Result<(TokenKind, TokenValue<'de>)> = match c {
let kind: Result<(TokenKind, TokenValue<'ast>)> = match c {
'(' => Ok((TokenKind::ParenOpen, TokenValue::None)),
')' => Ok((TokenKind::ParenClose, TokenValue::None)),
'{' => Ok((TokenKind::CurlyOpen, TokenValue::None)),
Expand Down
12 changes: 6 additions & 6 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{borrow::Cow, fmt::Display, hash::Hash};

#[derive(Debug, Clone, PartialEq)]
pub struct Token<'de> {
pub struct Token<'ast> {
pub kind: TokenKind,
pub value: TokenValue<'de>,
pub original: &'de str,
pub value: TokenValue<'ast>,
pub original: &'ast str,
pub span: miette::SourceSpan,
}

Expand All @@ -24,14 +24,14 @@ impl Display for Token<'_> {
}

#[derive(Debug, Clone, PartialEq)]
pub enum TokenValue<'de> {
pub enum TokenValue<'ast> {
None,
Boolean(bool),
Integer(i64),
Decimal(f64),
String(Cow<'de, str>),
String(Cow<'ast, str>),
Character(char),
Identifier(Cow<'de, str>),
Identifier(Cow<'ast, str>),
}

impl Display for TokenValue<'_> {
Expand Down
5 changes: 0 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ type HexCode = int;
fn main() {
let a = |a ~ HexCode| { 1 + 1; a };
let b = a;
let c = b(2);
}
";

fn main() {
Expand Down
48 changes: 26 additions & 22 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ pub mod typed;
pub mod untyped;

#[derive(Debug, Clone)]
pub struct Type<'de> {
pub value: TypeValue<'de>,
pub struct Type<'ast> {
pub value: TypeValue<'ast>,
pub span: SourceSpan,
pub original_span: Option<SourceSpan>,
}

impl<'de> Type<'de> {
impl<'ast> Type<'ast> {
pub fn label(&self, text: impl Into<String>) -> Vec<miette::LabeledSpan> {
let labels = vec![miette::LabeledSpan::at(self.span, text.into())];

Expand Down Expand Up @@ -74,31 +74,35 @@ impl<'de> Type<'de> {
}
}

pub fn symbol(span: SourceSpan, name: Cow<'de, str>) -> Self {
pub fn symbol(span: SourceSpan, name: Cow<'ast, str>) -> Self {
Self {
value: TypeValue::Symbol(name),
span,
original_span: None,
}
}

pub fn collection(span: SourceSpan, element: Type<'de>) -> Self {
pub fn collection(span: SourceSpan, element: Type<'ast>) -> Self {
Self {
value: TypeValue::Collection(Box::new(element)),
span,
original_span: None,
}
}

pub fn set(span: SourceSpan, element: Type<'de>) -> Self {
pub fn set(span: SourceSpan, element: Type<'ast>) -> Self {
Self {
value: TypeValue::Set(Box::new(element)),
span,
original_span: None,
}
}

pub fn function(span: SourceSpan, parameters: Vec<Type<'de>>, return_type: Type<'de>) -> Self {
pub fn function(
span: SourceSpan,
parameters: Vec<Type<'ast>>,
return_type: Type<'ast>,
) -> Self {
Self {
value: TypeValue::Function {
parameters,
Expand All @@ -109,7 +113,7 @@ impl<'de> Type<'de> {
}
}

pub fn alias(span: SourceSpan, name: Cow<'de, str>, alias: Type<'de>) -> Self {
pub fn alias(span: SourceSpan, name: Cow<'ast, str>, alias: Type<'ast>) -> Self {
Self {
value: TypeValue::Alias(name, Box::new(alias)),
span,
Expand Down Expand Up @@ -142,20 +146,20 @@ impl PartialEq for Type<'_> {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeValue<'de> {
pub enum TypeValue<'ast> {
Unit,
Boolean,
Integer,
Decimal,
Character,
String,
Alias(Cow<'de, str>, Box<Type<'de>>),
Symbol(Cow<'de, str>),
Collection(Box<Type<'de>>),
Set(Box<Type<'de>>),
Alias(Cow<'ast, str>, Box<Type<'ast>>),
Symbol(Cow<'ast, str>),
Collection(Box<Type<'ast>>),
Set(Box<Type<'ast>>),
Function {
parameters: Vec<Type<'de>>,
return_type: Box<Type<'de>>,
parameters: Vec<Type<'ast>>,
return_type: Box<Type<'ast>>,
},
}

Expand Down Expand Up @@ -202,7 +206,7 @@ impl Display for TypeValue<'_> {
}
}

pub trait Spannable<'de>: Sized {
pub trait Spannable<'ast>: Sized {
type Value;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self;
Expand Down Expand Up @@ -248,24 +252,24 @@ pub trait CombineSpan {

impl CombineSpan for SourceSpan {}

impl<'de> Spannable<'de> for untyped::Expression<'de> {
type Value = untyped::ExpressionValue<'de>;
impl<'ast> Spannable<'ast> for untyped::Expression<'ast> {
type Value = untyped::ExpressionValue<'ast>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}

impl<'de> Spannable<'de> for untyped::Statement<'de> {
type Value = untyped::StatementValue<'de>;
impl<'ast> Spannable<'ast> for untyped::Statement<'ast> {
type Value = untyped::StatementValue<'ast>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}

impl<'de> Spannable<'de> for Type<'de> {
type Value = TypeValue<'de>;
impl<'ast> Spannable<'ast> for Type<'ast> {
type Value = TypeValue<'ast>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self {
Expand Down
Loading

0 comments on commit 087d5f7

Please sign in to comment.