From 303661f75f7fd3ab5be83fd0ec5ebf5cf7453ec6 Mon Sep 17 00:00:00 2001 From: Kubat <maelle.martin@proton.me> Date: Mon, 28 Apr 2025 21:53:39 +0200 Subject: [PATCH] Should be enaugh to parse the expressions, only need to implement some functions in the parser::utils module --- grimoire/src/ast/expr.rs | 2 +- grimoire/src/parser/expr.rs | 20 +++++++++----------- grimoire/src/parser/utils.rs | 20 ++++++++++++++++++-- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/grimoire/src/ast/expr.rs b/grimoire/src/ast/expr.rs index 7c4028c..623b087 100644 --- a/grimoire/src/ast/expr.rs +++ b/grimoire/src/ast/expr.rs @@ -5,8 +5,8 @@ pub enum VarOrConstant { Var(String), Int(i32), Flt(f32), + Str(String), Bool(bool), - String(String), } #[derive(Debug)] diff --git a/grimoire/src/parser/expr.rs b/grimoire/src/parser/expr.rs index ee6c32d..78bf3ca 100644 --- a/grimoire/src/parser/expr.rs +++ b/grimoire/src/parser/expr.rs @@ -4,23 +4,21 @@ use crate::{ }; use nom::{ - Parser, - branch::alt, - bytes::complete::tag, - character::complete::{digit1, multispace0}, - combinator::map, - multi::many0, - sequence::preceded, + Parser, branch::alt, bytes::complete::tag, character::complete::multispace0, combinator::map, + multi::many0, sequence::preceded, }; fn var_or_constant(s: Span) -> ParserResult<Expression> { utils::map_with_locaiton( alt(( - map(digit1, |digits| todo!()), - map(utils::identifier("true"), |_| todo!()), - map(utils::identifier("false"), |_| todo!()), + map(utils::number, VarOrConstant::Int), + map(utils::float, VarOrConstant::Flt), + map(utils::string, VarOrConstant::Str), + map(utils::keyword("true"), |_| VarOrConstant::Bool(true)), + map(utils::keyword("false"), |_| VarOrConstant::Bool(false)), + map(utils::identifier, VarOrConstant::Var), )), - |begin, end, var_or_constant| todo!(), + Expression::Leaf, ) .parse(s) } diff --git a/grimoire/src/parser/utils.rs b/grimoire/src/parser/utils.rs index 075a319..ee5d028 100644 --- a/grimoire/src/parser/utils.rs +++ b/grimoire/src/parser/utils.rs @@ -5,7 +5,7 @@ use crate::{ use nom::{ Parser, bytes::complete::tag, - character::complete::multispace0, + character::complete::{multispace0, multispace1}, sequence::{delimited, preceded}, }; @@ -39,8 +39,24 @@ where } } -pub fn identifier<'a>( +pub fn keyword<'a>( ident: &'static str, ) -> impl Parser<Span<'a>, Output = Span<'a>, Error = ParserError> { + delimited(multispace1, tag(ident), multispace1) +} + +pub fn float(s: Span) -> ParserResult<f32> { + todo!() +} + +pub fn string(s: Span) -> ParserResult<String> { todo!() } + +pub fn identifier(s: Span) -> ParserResult<String> { + todo!() +} + +pub fn number(s: Span) -> ParserResult<i32> { + todo!() +} -- GitLab