I can see the light

This commit is contained in:
2025-05-02 18:18:13 -04:00
parent d7222cc7a4
commit 57c46b653e
35 changed files with 1247 additions and 1000 deletions
+15 -36
View File
@@ -1,4 +1,6 @@
use super::{CharCursor, MaybeParsable, ParserCtx, CompilerMsg, Symbol, Token};
use crate::parser::{Parsable, ParseResult};
use super::{PString, ParserCtx, Symbol, Token};
use std::fmt::Debug;
#[derive(Clone, PartialEq, Eq)]
@@ -16,26 +18,29 @@ pub struct PNumber {
pub ty: Option<String>,
}
impl MaybeParsable for PLiteral {
fn maybe_parse(ctx: &mut ParserCtx) -> Result<Option<Self>, CompilerMsg> {
impl Parsable for Option<PLiteral> {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self> {
let inst = ctx.expect_peek()?;
Ok(Some(match &inst.token {
ParseResult::Ok(Some(match &inst.token {
Token::Symbol(Symbol::SingleQuote) => {
let chars = ctx.chars();
let c = chars.expect_next()?;
chars.expect('\'')?;
ctx.next();
Self::Char(c)
PLiteral::Char(c)
}
Token::Symbol(Symbol::DoubleQuote) => {
let res = Self::String(string_from(ctx.chars())?);
ctx.next();
res
let s = ctx.parse::<PString>()?;
return match s.inner {
Some(s) => ParseResult::Ok(Some(PLiteral::String(s.0))),
None => ParseResult::SubErr,
};
}
Token::Word(text) => {
let first = text.chars().next().unwrap();
if !first.is_ascii_digit() {
return Ok(None);
return ParseResult::Ok(None);
}
let (whole, ty) = parse_whole_num(text);
let mut num = PNumber {
@@ -57,9 +62,9 @@ impl MaybeParsable for PLiteral {
}
}
}
Self::Number(num)
PLiteral::Number(num)
}
_ => return Ok(None),
_ => return ParseResult::Ok(None),
}))
}
}
@@ -81,32 +86,6 @@ pub fn parse_whole_num(text: &str) -> (String, Option<String>) {
(whole, if ty.is_empty() { None } else { Some(ty) })
}
pub fn string_from(cursor: &mut CharCursor) -> Result<String, CompilerMsg> {
let mut str = String::new();
loop {
let c = cursor.expect_next()?;
if c == '"' {
return Ok(str);
}
str.push(match c {
'\\' => {
let next = cursor.expect_next()?;
match next {
'"' => '"',
'\'' => '\'',
't' => '\t',
'n' => '\n',
'0' => '\0',
_ => {
todo!();
}
}
}
_ => c,
})
}
}
impl Debug for PLiteral {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {