grabapl_syntax/
custom_syntax.rs

1pub mod example;
2mod example_with_ref;
3
4use crate::{MacroArgs, Span, Token};
5use chumsky::error::Rich;
6use chumsky::input::ValueInput;
7use chumsky::{Parser, extra};
8use grabapl::Semantics;
9use std::fmt;
10use std::fmt::Debug;
11
12pub trait CustomSyntax: Clone + Debug + 'static {
13
14    type AbstractNodeType: Clone + Debug + PartialEq;
15    type AbstractEdgeType: Clone + Debug + PartialEq;
16
17
18    fn get_node_type_parser<
19        'src: 'tokens,
20        'tokens,
21        I: ValueInput<'tokens, Token = Token<'src>, Span = Span>,
22    >()
23    -> impl Parser<'tokens, I, Self::AbstractNodeType, extra::Err<Rich<'tokens, Token<'src>, Span>>>
24    + Clone;
25    fn get_edge_type_parser<
26        'src: 'tokens,
27        'tokens,
28        I: ValueInput<'tokens, Token = Token<'src>, Span = Span>,
29    >()
30    -> impl Parser<'tokens, I, Self::AbstractEdgeType, extra::Err<Rich<'tokens, Token<'src>, Span>>>
31    + Clone;
32}
33
34// TODO: figure out how to remove Debug constraint? or is it ok if not?
35// TODO: actually, why does Builder::show_state need Debug? maybe lift that?
36pub trait SemanticsWithCustomSyntax:
37    Semantics<BuiltinOperation: Clone, BuiltinQuery: Clone, NodeAbstract: Debug, EdgeAbstract: Debug>
38{
39    type CS: CustomSyntax;
40
41    fn find_builtin_op(name: &str, args: Option<MacroArgs>) -> Option<Self::BuiltinOperation>;
42
43    fn find_builtin_query(name: &str, args: Option<MacroArgs>) -> Option<Self::BuiltinQuery>;
44
45    /// Returns an option so a more general CustomSyntax can be reused for multiple semantics.
46    fn convert_node_type(
47        syn_typ: <<Self as SemanticsWithCustomSyntax>::CS as CustomSyntax>::AbstractNodeType,
48    ) -> Option<Self::NodeAbstract>;
49    /// Returns an option so a more general CustomSyntax can be reused for multiple semantics.
50    fn convert_edge_type(
51        syn_typ: <<Self as SemanticsWithCustomSyntax>::CS as CustomSyntax>::AbstractEdgeType,
52    ) -> Option<Self::EdgeAbstract>;
53}