lehit 0.2.0

Embeddable Lehit language frontend and Cranelift JIT runtime
Documentation
use chumsky::prelude::*;

use crate::parsing::{
    Span,
    ast::{
        ContractDeclaration, ContractDefinition, ContractDefinitionContent, GenericDeclarationList,
        GenericList, IdentName, ParsingState, PubVisible, StringValueExpression, ValueType,
        contract_declaration_parser, contract_definition_parser, generic_declaration_list_parser,
        ident_name_parser, pub_visible_parser, string_value_expression_parser,
    },
    lexer::{Token, TokensData},
};

#[derive(Debug, Clone)]
pub struct ContractAccessDeclaration {
    pub span: Span,
    pub pub_visible: Option<PubVisible>,
    pub ident: IdentName,
    pub generic_declaration_list: Option<GenericDeclarationList>,
    pub source: ContractSource,
}

#[derive(Debug, Clone)]
pub enum ContractSource {
    Access(Box<ContractAccessPath>),
    Declaration(Box<ContractDeclaration>),
    Definition(Box<ContractDefinition>),
    File(Box<StringValueExpression>),
}

impl ContractSource {
    pub fn span(&self) -> Span {
        match self {
            Self::Access(cap) => cap.span,
            Self::Declaration(cd) => cd.span,
            Self::Definition(definition) => definition.span,
            Self::File(sve) => sve.span,
        }
    }
}

#[derive(Debug, Clone)]
pub struct ContractAccessPath {
    pub span: Span,
    pub parts: (ContractAccessPathPart, Vec<ContractAccessPathPart>),
}

#[derive(Debug, Clone)]
pub struct ContractAccessPathPart {
    pub span: Span,
    pub ident: IdentName,
    pub generic_list: Option<GenericList>,
}

pub(crate) fn contract_access_declaration_parser<'src, I, GLP, VTP, CDCP>(
    tokens_data: &'src TokensData,
    state: &'src ParsingState,
    generic_list_parser: GLP,
    value_type_parser: VTP,
    contract_definition_content_parser: CDCP,
) -> impl Parser<'src, I, ContractAccessDeclaration, extra::Err<Rich<'src, Token, Span>>> + Clone
where
    I: Input<'src, Token = Token, Span = Span>,
    GLP: Parser<'src, I, GenericList, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
    VTP: Parser<'src, I, ValueType, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
    CDCP: Parser<'src, I, ContractDefinitionContent, extra::Err<Rich<'src, Token, Span>>>
        + Clone
        + 'src,
{
    pub_visible_parser()
        .or_not()
        .then_ignore(just(Token::AcsKw))
        .then(ident_name_parser(tokens_data, state).or(
            crate::parsing::ast::empty_parentheses_parser().map(|span| IdentName {
                span,
                string: state.get_string_id("()"),
            }),
        ))
        .then(
            generic_declaration_list_parser(tokens_data, state, generic_list_parser.clone())
                .or_not(),
        )
        .then_ignore(just(Token::Colon))
        .then(contract_source_parser(
            tokens_data,
            state,
            generic_list_parser,
            value_type_parser,
            contract_definition_content_parser,
        ))
        .map_with(|(((pv, ident), gdl), src), ex| ContractAccessDeclaration {
            span: ex.span(),
            pub_visible: pv,
            ident: ident,
            generic_declaration_list: gdl,
            source: src,
        })
}

pub(crate) fn contract_source_parser<'src, I, GLP, VTP, CDCP>(
    tokens_data: &'src TokensData,
    state: &'src ParsingState,
    generic_list_parser: GLP,
    value_type_parser: VTP,
    contract_definition_content_parser: CDCP,
) -> impl Parser<'src, I, ContractSource, extra::Err<Rich<'src, Token, Span>>> + Clone
where
    I: Input<'src, Token = Token, Span = Span>,
    GLP: Parser<'src, I, GenericList, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
    VTP: Parser<'src, I, ValueType, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
    CDCP: Parser<'src, I, ContractDefinitionContent, extra::Err<Rich<'src, Token, Span>>>
        + Clone
        + 'src,
{
    contract_access_path_parser(tokens_data, state, generic_list_parser.clone())
        .map(|cap| ContractSource::Access(Box::new(cap)))
        .or(contract_declaration_parser(
            tokens_data,
            state,
            generic_list_parser,
            value_type_parser,
            contract_definition_content_parser.clone(),
        )
        .map(|cd| ContractSource::Declaration(Box::new(cd))))
        .or(
            contract_definition_parser(contract_definition_content_parser)
                .map(|definition| ContractSource::Definition(Box::new(definition))),
        )
        .or(string_value_expression_parser(tokens_data, state)
            .map(|sve| ContractSource::File(Box::new(sve))))
}

pub(crate) fn contract_access_path_parser<'src, I, GLP>(
    tokens_data: &'src TokensData,
    state: &'src ParsingState,
    generic_list_parser: GLP,
) -> impl Parser<'src, I, ContractAccessPath, extra::Err<Rich<'src, Token, Span>>> + Clone
where
    I: Input<'src, Token = Token, Span = Span>,
    GLP: Parser<'src, I, GenericList, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
{
    contract_access_path_part_parser(tokens_data, state, generic_list_parser.clone())
        .then(
            just(Token::Dot)
                .ignore_then(contract_access_path_part_parser(
                    tokens_data,
                    state,
                    generic_list_parser,
                ))
                .repeated()
                .collect::<Vec<_>>(),
        )
        .map_with(|(first, rest), ex| ContractAccessPath {
            span: ex.span(),
            parts: (first, rest),
        })
}

pub(crate) fn contract_access_path_part_parser<'src, I, GLP>(
    tokens_data: &'src TokensData,
    state: &'src ParsingState,
    generic_list_parser: GLP,
) -> impl Parser<'src, I, ContractAccessPathPart, extra::Err<Rich<'src, Token, Span>>> + Clone
where
    I: Input<'src, Token = Token, Span = Span>,
    GLP: Parser<'src, I, GenericList, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
{
    ident_name_parser(tokens_data, state)
        .or(select! {
            Token::BigSelfKw => "Self",
            Token::LowSelfKw => "self",
        }
        .map_with(|name, extra| IdentName {
            span: extra.span(),
            string: state.get_string_id(name),
        }))
        .then(generic_list_parser.or_not())
        .map_with(|(ident, gl), ex| ContractAccessPathPart {
            span: ex.span(),
            ident: ident,
            generic_list: gl,
        })
}