use chumsky::prelude::*; use crate::{ ast, parser::{annotation, error::ParseError, token::Token, utils}, }; pub fn parser() -> impl Parser { utils::public() .or_not() .then_ignore(just(Token::Const)) .then(select! {Token::Name{name} => name}) .then( just(Token::Colon) .ignore_then(annotation::parser()) .or_not(), ) .then_ignore(just(Token::Equal)) .then(value()) .map_with_span(|(((public, name), annotation), value), span| { ast::UntypedDefinition::ModuleConstant(ast::ModuleConstant { doc: None, location: span, public: public.is_some(), name, annotation, value: Box::new(value), tipo: (), }) }) } pub fn value() -> impl Parser { let constant_string_parser = select! {Token::String {value} => value}.map_with_span(|value, span| { ast::Constant::String { location: span, value, } }); let constant_int_parser = select! {Token::Int {value, base} => (value, base)}.map_with_span(|(value, base), span| { ast::Constant::Int { location: span, value, base, } }); let constant_bytearray_parser = utils::bytearray().map_with_span(|(preferred_format, bytes), span| { ast::Constant::ByteArray { location: span, bytes, preferred_format, } }); choice(( constant_string_parser, constant_int_parser, constant_bytearray_parser, )) }