grabapl_syntax 0.0.4

Parsing support for grabapl.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
pub mod custom_syntax;
pub mod interpreter;

use crate::interpreter::{InterpreterResult, interpret};
use ariadne::{Color, Label, Report, ReportKind, sources};
use chumsky::input::SliceInput;
use chumsky::{input::ValueInput, prelude::*};
use custom_syntax::{CustomSyntax, SemanticsWithCustomSyntax};
use grabapl::operation::marker::SkipMarkers;
use grabapl::prelude::{OperationContext, OperationId};
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::fmt;
use std::fmt::{Debug, Display};
use std::io::BufWriter;
use std::ops::Range;

// A few type definitions to be used by our parsers below
pub type Span = SimpleSpan;
pub type Spanned<T> = (T, Span);

#[derive(Clone, Debug, PartialEq)]
pub enum Token<'src> {
    // do we want these?
    Bool(bool),
    // TODO: support parsing negative numbers
    Num(i32),
    Str(&'src str),
    // Op(&'src str),
    Arrow,
    // note: conflicts with call<-1>() ...
    // RevArrow
    ColonEq,
    Ctrl(char),
    Ident(&'src str),
    Fn,
    Return,
    Let,
    LetBang,
    If,
    Else,
    Shape,
    MacroArgs(&'src str),
    // NodeType(&'src str),
}

impl fmt::Display for Token<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Token::Bool(b) => write!(f, "{}", b),
            Token::Num(n) => write!(f, "{}", n),
            Token::Str(s) => write!(f, "\"{}\"", s),
            // Token::Op(op) => write!(f, "{}", op),
            Token::Arrow => write!(f, "->"),
            Token::ColonEq => write!(f, ":="),
            Token::Ctrl(c) => write!(f, "{}", c),
            Token::Ident(i) => write!(f, "{}", i),
            Token::Fn => write!(f, "fn"),
            Token::Return => write!(f, "return"),
            Token::Let => write!(f, "let"),
            Token::LetBang => write!(f, "let!"),
            Token::If => write!(f, "if"),
            Token::Else => write!(f, "else"),
            Token::Shape => write!(f, "shape"),
            Token::MacroArgs(s) => write!(f, "`{}`", s),
            // Token::NodeType(s) => write!(f, "{}", s),
        }
    }
}

pub fn lexer<'src>()
-> impl Parser<'src, &'src str, Vec<Spanned<Token<'src>>>, extra::Err<Rich<'src, char, Span>>> {
    // A parser for numbers
    let num = text::int(10).to_slice().from_str().unwrapped();

    // allow negative numbers
    let num = just('-')
        .ignore_then(num.clone())
        .map(|num: i32| -num)
        .or(num)
        .map(|num: i32| Token::Num(num))
        .boxed();

    // A parser for control characters (delimiters, semicolons, etc.)
    let ctrl = one_of("-()[]{};,?:*=/<>\"'.").map(Token::Ctrl);

    let arrow = just("->").to(Token::Arrow);
    let colon_eq = just(":=").to(Token::ColonEq);

    let let_bang = just("let!").to(Token::LetBang);

    // A parser for identifiers and keywords
    let ident = text::ascii::ident().map(|ident: &str| match ident {
        "fn" => Token::Fn,
        "let" => Token::Let,
        "if" => Token::If,
        "return" => Token::Return,
        "else" => Token::Else,
        "shape" => Token::Shape,
        "true" => Token::Bool(true),
        "false" => Token::Bool(false),
        _ => Token::Ident(ident),
    });

    // A parser for strings
    let str_ = just('"')
        .ignore_then(none_of('"').repeated().to_slice())
        .then_ignore(just('"'))
        .map(Token::Str);

    let macro_args = none_of("`")
        .repeated()
        .to_slice()
        .delimited_by(just('`'), just('`'))
        .map(Token::MacroArgs);

    let macro_args_opt2 = none_of("%")
        .repeated()
        .to_slice()
        .delimited_by(just('%'), just('%'))
        .map(Token::MacroArgs);

    let macro_args_opt3 = none_of("<>")
        .repeated()
        .to_slice()
        .delimited_by(just('<'), just('>'))
        .map(Token::MacroArgs);

    // A single token can be one of the above
    // (macro_args needs to be before ctrl, since ctrl has the same prefix)
    let token = let_bang
        .or(num)
        .or(arrow)
        .or(colon_eq)
        .or(macro_args)
        .or(macro_args_opt2)
        .or(macro_args_opt3)
        .or(str_)
        .or(ctrl)
        .or(ident)
        .boxed(); //.or(node_type_src);

    let comment = just("//")
        .then(any().and_is(just('\n').not()).repeated())
        .padded();

    let block_comment = just("/*")
        .then(any().and_is(just("*/").not()).repeated())
        .then_ignore(just("*/"))
        .padded();

    token
        .map_with(|tok, e| (tok, e.span()))
        .padded_by(comment.or(block_comment).repeated())
        .padded()
        // If we encounter an error, skip and attempt to lex the next character as a token instead
        .recover_with(skip_then_retry_until(any().ignored(), end()))
        .repeated()
        .collect()
}

// TODO: we cannot really have this. Since both lib and custom might be able to parse the entire macro syntax,
//  this distinction needs to happen afterwards.
//  I suppose we could delay lib parsing and just store the tokens that are parsed into CS::MacroArgType for later consumption?
#[derive(Clone, Debug, PartialEq, Copy)]
pub struct MacroArgs<'src>(pub &'src str);

#[derive(Clone, Debug, PartialEq)]
pub struct FnCallExpr<'src> {
    pub name: Spanned<&'src str>,
    pub macro_args: Option<Spanned<MacroArgs<'src>>>,
    pub args: Vec<Spanned<NodeId<'src>>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct LetStmt<'src> {
    pub bang: bool,
    pub ident: Spanned<&'src str>,
    pub call: Spanned<FnCallExpr<'src>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ShapeNodeParam<'src, CS: CustomSyntax> {
    pub name: Spanned<NodeId<'src>>,
    pub node_type: Spanned<CS::AbstractNodeType>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ShapeEdgeParam<'src, CS: CustomSyntax> {
    pub src: Spanned<NodeId<'src>>,
    pub dst: Spanned<NodeId<'src>>,
    pub edge_type: Spanned<CS::AbstractEdgeType>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ShapeQueryParam<'src, CS: CustomSyntax> {
    Node(ShapeNodeParam<'src, CS>),
    Edge(ShapeEdgeParam<'src, CS>),
}

#[derive(Clone, Debug, PartialEq)]
pub struct ShapeQueryParams<'src, CS: CustomSyntax> {
    pub params: Vec<Spanned<ShapeQueryParam<'src, CS>>>,
    // TODO: spanned<>
    pub skip_markers: SkipMarkers,
}

#[derive(Clone, Debug, PartialEq)]
pub enum IfCond<'src, CS: CustomSyntax> {
    Query(Spanned<FnCallExpr<'src>>),
    Shape(Spanned<ShapeQueryParams<'src, CS>>),
}

#[derive(Clone, Debug, PartialEq)]
pub struct IfStmt<'src, CS: CustomSyntax> {
    pub cond: Spanned<IfCond<'src, CS>>,
    pub then_block: Spanned<Block<'src, CS>>,
    // if it doesn't exist, take empty span at the end of then_block
    pub else_block: Spanned<Block<'src, CS>>,
}

#[derive(Clone, derive_more::Debug, PartialEq, Copy)]
pub enum NodeId<'src> {
    #[debug("{_0}")]
    Single(&'src str),
    #[debug("{0}.{1}", _0.0, _1.0)]
    Output(Spanned<&'src str>, Spanned<&'src str>),
}

impl<'src> NodeId<'src> {
    pub fn must_single(&self) -> &'src str {
        match self {
            NodeId::Single(name) => name,
            _ => {
                panic!("NodeId must be a single node, but was: {:?}", self);
            }
        }
    }

    pub fn single(&self) -> Option<&'src str> {
        match self {
            NodeId::Single(name) => Some(name),
            _ => None,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum ReturnStmtMapping<'src, CS: CustomSyntax> {
    Node {
        /// The name of the output marker
        ret_name: Spanned<&'src str>,
        /// The node to return
        node: Spanned<NodeId<'src>>,
    },
    Edge {
        src: Spanned<NodeId<'src>>,
        dst: Spanned<NodeId<'src>>,
        edge_type: Spanned<CS::AbstractEdgeType>,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub struct ReturnStmt<'src, CS: CustomSyntax> {
    pub mapping: Vec<Spanned<ReturnStmtMapping<'src, CS>>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct RenameStmt<'src> {
    pub new_name: Spanned<&'src str>,
    pub src: Spanned<NodeId<'src>>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Statement<'src, CS: CustomSyntax> {
    Let(Spanned<LetStmt<'src>>),
    FnCall(Spanned<FnCallExpr<'src>>),
    If(Spanned<IfStmt<'src, CS>>),
    Return(Spanned<ReturnStmt<'src, CS>>),
    Rename(Spanned<RenameStmt<'src>>),
}

#[derive(Clone, Debug, PartialEq)]
pub struct Block<'src, CS: CustomSyntax> {
    pub statements: Vec<Spanned<Statement<'src, CS>>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FnNodeParam<'src, CS: CustomSyntax> {
    pub name: Spanned<&'src str>,
    pub node_type: Spanned<CS::AbstractNodeType>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct FnEdgeParam<'src, CS: CustomSyntax> {
    pub src: Spanned<&'src str>,
    pub dst: Spanned<&'src str>,
    pub edge_type: Spanned<CS::AbstractEdgeType>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum FnImplicitParam<'src, CS: CustomSyntax> {
    Node(FnNodeParam<'src, CS>),
    Edge(FnEdgeParam<'src, CS>),
}

#[derive(Clone, Debug, PartialEq)]
pub struct FnDef<'src, CS: CustomSyntax> {
    pub name: Spanned<&'src str>,
    pub explicit_params: Vec<Spanned<FnNodeParam<'src, CS>>>,
    pub implicit_params: Vec<Spanned<FnImplicitParam<'src, CS>>>,
    pub return_signature: Vec<Spanned<FnImplicitParam<'src, CS>>>,
    pub body: Spanned<Block<'src, CS>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Program<'src, CS: CustomSyntax> {
    // vec to preserve order. functions must be ordered according to their dependency order. no mutual recursion supported right now.
    // wrapper functions first, then their dependencies.
    pub functions: Vec<(&'src str, Spanned<FnDef<'src, CS>>)>,
}

pub fn program_parser<'tokens, 'src: 'tokens, I, CS: CustomSyntax>()
-> impl Parser<'tokens, I, Spanned<Program<'src, CS>>, extra::Err<Rich<'tokens, Token<'src>, Span>>>
+ Clone
where
    I: ValueInput<'tokens, Token = Token<'src>, Span = Span>
        + SliceInput<
            'tokens,
            Token = Token<'src>,
            Span = Span,
            Slice = &'tokens [Spanned<Token<'src>>],
        >,
{
    let ident_str = select! {
        Token::Ident(ident) => ident,
    }
    .map_with(|ident, e| (ident, e.span()))
    .labelled("identifier");

    let spanned_output_node_id = ident_str
        .then_ignore(just(Token::Ctrl('.')))
        .then(ident_str)
        .map_with(|(spanned_op, spanned_node), e| {
            (NodeId::Output(spanned_op, spanned_node), e.span())
        })
        .boxed();

    let spanned_node_id = spanned_output_node_id
        .or(ident_str.map(|(name, span)| (NodeId::Single(name), span)))
        .boxed()
        .labelled("node identifier");

    let spanned_fn_implicit_edge_param = ident_str
        .then_ignore(just(Token::Arrow))
        .then(ident_str)
        .then_ignore(just(Token::Ctrl(':')))
        .then(
            CS::get_edge_type_parser()
                .labelled("edge type")
                .map_with(|s, e| (s, e.span())),
        )
        .map_with(
            |(((src, src_span), (dst, dst_span)), (edge_type, edge_type_span)), overall_span| {
                (
                    FnImplicitParam::Edge(FnEdgeParam {
                        src: (src, src_span),
                        dst: (dst, dst_span),
                        edge_type: (edge_type, edge_type_span),
                    }),
                    overall_span.span(),
                )
            },
        )
        .boxed()
        .labelled("implicit edge function parameter");

    let spanned_fn_explicit_param = ident_str
        .then_ignore(just(Token::Ctrl(':')))
        .then(
            CS::get_node_type_parser()
                .labelled("node type")
                .map_with(|s, e| (s, e.span())),
        )
        .map_with(
            |((name, n_span), (node_type, node_type_span)), overall_span| {
                (
                    FnNodeParam {
                        name: (name, n_span),
                        node_type: (node_type, node_type_span),
                    },
                    overall_span.span(),
                )
            },
        )
        .boxed()
        .labelled("explicit function parameter");

    let spanned_fn_implicit_param = spanned_fn_explicit_param
        .clone()
        .map(|(explicit_param, span)| (FnImplicitParam::Node(explicit_param), span))
        .or(spanned_fn_implicit_edge_param)
        .boxed()
        .labelled("implicit function parameter");

    let fn_implicit_params = spanned_fn_implicit_param
        .separated_by(just(Token::Ctrl(',')))
        .allow_trailing()
        .collect::<Vec<_>>();

    let shape_node_param = spanned_node_id
        .clone()
        .then_ignore(just(Token::Ctrl(':')))
        .then(
            CS::get_node_type_parser()
                .labelled("node type")
                .map_with(|s, e| (s, e.span())),
        )
        .map(
            |((name, name_span), (node_type, node_type_span))| ShapeNodeParam {
                name: (name, name_span),
                node_type: (node_type, node_type_span),
            },
        )
        .boxed()
        .labelled("shape query node parameter");

    let shape_edge_param = spanned_node_id
        .clone()
        .then_ignore(just(Token::Arrow))
        .then(spanned_node_id.clone())
        .then_ignore(just(Token::Ctrl(':')))
        .then(
            CS::get_edge_type_parser()
                .labelled("edge type")
                .map_with(|s, e| (s, e.span())),
        )
        .map(
            |(((src, src_span), (dst, dst_span)), (edge_type, edge_type_span))| ShapeEdgeParam {
                src: (src, src_span),
                dst: (dst, dst_span),
                edge_type: (edge_type, edge_type_span),
            },
        )
        .boxed()
        .labelled("shape query edge parameter");

    let spanned_shape_param = shape_node_param
        .map(|node_param| ShapeQueryParam::Node(node_param))
        .or(shape_edge_param.map(|edge_param| ShapeQueryParam::Edge(edge_param)))
        .map_with(|s, e| (s, e.span()))
        .boxed();

    let shape_params = spanned_shape_param
        .separated_by(just(Token::Ctrl(',')))
        .allow_trailing()
        .collect::<Vec<_>>()
        .map(|params| ShapeQueryParams {
            params,
            skip_markers: SkipMarkers::default(),
        })
        .boxed();

    let block = recursive(|block| {
        let macro_args_str = select! {
            Token::MacroArgs(arg) => arg,
        };

        let spanned_macro_args = macro_args_str
            .map_with(|src, e| (MacroArgs(src), e.span()))
            .labelled("macro args")
            .boxed();

        let fn_call_expr = ident_str
            .then(spanned_macro_args.or_not())
            .then_ignore(just(Token::Ctrl('(')))
            .then(
                spanned_node_id
                    .clone()
                    .separated_by(just(Token::Ctrl(',')))
                    .allow_trailing()
                    .collect::<Vec<_>>(),
            )
            .then_ignore(just(Token::Ctrl(')')))
            .map_with(|((spanned_name, spanned_macro_args), args), e| {
                (
                    FnCallExpr {
                        name: spanned_name,
                        macro_args: spanned_macro_args,
                        args,
                    },
                    e.span(),
                )
            })
            .boxed();

        // TODO: add support for `skipping all` in addition to the current `skipping [...]` syntax.
        let optional_skipping_markers = select! {
            Token::Ident("skipping") => (),
        }
        .labelled("'skipping'")
        .ignore_then(just(Token::Ctrl('[')))
        .ignore_then(
            select! { Token::Str(s) => s }
                .labelled("marker literal (e.g., '\"visited\"')")
                .separated_by(just(Token::Ctrl(',')))
                .allow_trailing()
                .collect::<Vec<_>>(),
        )
        .then_ignore(just(Token::Ctrl(']')))
        .or_not()
        .map(|opt| {
            opt.map(|marker_names| SkipMarkers::new(marker_names))
                .unwrap_or(SkipMarkers::default())
        })
        .boxed()
        .labelled("skipping markers");

        let if_cond_shape = just(Token::Shape)
            .ignore_then(just(Token::Ctrl('[')))
            .ignore_then(shape_params.clone())
            .then_ignore(just(Token::Ctrl(']')))
            .then(optional_skipping_markers)
            .map(|(mut params, skip_markers)| {
                params.skip_markers = skip_markers;
                params
            })
            .map_with(|params, e| (params, e.span()))
            .map(IfCond::Shape);

        let if_cond_query = fn_call_expr
            .clone()
            .map(|spanned_call| IfCond::Query(spanned_call));

        let spanned_if_cond = if_cond_shape
            .or(if_cond_query)
            .map_with(|c, e| (c, e.span()))
            .labelled("if condition")
            .boxed();

        let if_stmt = recursive(|if_stmt| {
            let spanned_block_wrapped_stmts = block
                .clone()
                .delimited_by(just(Token::Ctrl('{')), just(Token::Ctrl('}')))
                .map_with(|block, e| (block, e.span()))
                .labelled("block");

            let if_stmt_as_block = if_stmt.clone().map_with(|if_stmt, e| Block {
                statements: vec![(if_stmt, e.span())],
            });

            let spanned_block_if_or_wrapped_stmts = spanned_block_wrapped_stmts
                .clone()
                .or(if_stmt_as_block.map_with(|if_stmt, e| (if_stmt, e.span())))
                .boxed();

            let optional_else_part = just(Token::Else)
                .ignore_then(spanned_block_if_or_wrapped_stmts)
                .or_not()
                .map_with(|opt, e| opt.unwrap_or((Block { statements: vec![] }, e.span())))
                .labelled("else")
                .boxed();

            let if_stmt = just(Token::If)
                .ignore_then(spanned_if_cond)
                .then_ignore(just(Token::Ctrl('{')))
                .then(block.clone().map_with(|block, e| (block, e.span())))
                .then_ignore(just(Token::Ctrl('}')))
                .then(optional_else_part)
                .map_with(|((cond, spanned_then_block), spanned_else_block), e| {
                    (
                        IfStmt {
                            cond,
                            then_block: spanned_then_block,
                            else_block: spanned_else_block,
                        },
                        e.span(),
                    )
                })
                .map(Statement::If)
                .labelled("if statement")
                .boxed();

            // TODO: continue with entire if else statements.
            if_stmt
        });

        let spanned_if_stmt = if_stmt
            .map_with(|if_stmt, e| (if_stmt, e.span()))
            .labelled("if statement");

        let fn_call_stmt = fn_call_expr
            .clone()
            .then_ignore(just(Token::Ctrl(';')))
            .map_with(|spanned_call, e| (Statement::FnCall(spanned_call), e.span()))
            .labelled("function call statement");

        let let_or_let_bang = select! {
            Token::Let => false,
            Token::LetBang => true,
        };

        let let_stmt = let_or_let_bang
            .then(ident_str)
            .then_ignore(just(Token::Ctrl('=')))
            .then(fn_call_expr)
            .then_ignore(just(Token::Ctrl(';')))
            .map_with(|((bang, (name, name_span)), (call, call_span)), e| {
                (
                    LetStmt {
                        bang,
                        ident: (name, name_span),
                        call: (call, call_span),
                    },
                    e.span(),
                )
            })
            .map(Statement::Let)
            .map_with(|let_stmt, e| (let_stmt, e.span()))
            .labelled("let statement");

        let spanned_return_node_mapping = ident_str
            .then_ignore(just(Token::Ctrl(':')))
            .then(spanned_node_id.clone())
            .map_with(|(ret_name, node_id), e| {
                (
                    ReturnStmtMapping::Node {
                        ret_name,
                        node: node_id,
                    },
                    e.span(),
                )
            });

        let spanned_return_edge_mapping = spanned_node_id
            .clone()
            .then_ignore(just(Token::Arrow))
            .then(spanned_node_id.clone())
            .then_ignore(just(Token::Ctrl(':')))
            .then(
                CS::get_edge_type_parser()
                    .labelled("edge type")
                    .map_with(|s, e| (s, e.span())),
            )
            .map_with(|((src, dst), edge_typ), e| {
                (
                    ReturnStmtMapping::Edge {
                        src,
                        dst,
                        edge_type: edge_typ,
                    },
                    e.span(),
                )
            })
            .boxed()
            .labelled("return edge mapping");

        let spanned_return_mappings = spanned_return_node_mapping
            .or(spanned_return_edge_mapping)
            .separated_by(just(Token::Ctrl(',')))
            .allow_trailing()
            .at_least(1)
            .collect::<Vec<_>>()
            .boxed();

        let spanned_return_stmt = just(Token::Return)
            .ignore_then(just(Token::Ctrl('(')))
            .ignore_then(spanned_return_mappings)
            .then_ignore(just(Token::Ctrl(')')))
            .then_ignore(just(Token::Ctrl(';')))
            .map_with(|mappings, e| (ReturnStmt { mapping: mappings }, e.span()))
            .map(Statement::Return)
            .map_with(|return_stmt, e| (return_stmt, e.span()))
            .labelled("return statement");

        let spanned_rename_stmt = ident_str
            .then_ignore(just(Token::ColonEq))
            .then(spanned_node_id.clone())
            .then_ignore(just(Token::Ctrl(';')))
            .map_with(|((new_name, new_name_span), (src, src_span)), e| {
                (
                    RenameStmt {
                        new_name: (new_name, new_name_span),
                        src: (src, src_span),
                    },
                    e.span(),
                )
            })
            .map(Statement::Rename)
            .map_with(|rename_stmt, e| (rename_stmt, e.span()))
            .labelled("rename statement");

        let spanned_stmt = let_stmt
            .or(fn_call_stmt)
            .or(spanned_if_stmt)
            .or(spanned_return_stmt)
            .or(spanned_rename_stmt)
            .labelled("statement")
            .boxed();

        let block_many_stmts = spanned_stmt
            .repeated()
            .collect()
            .map(|stmts| Block { statements: stmts })
            .labelled("block")
            .boxed();

        block_many_stmts
    });

    let fn_return_signature = fn_implicit_params.clone();

    let optional_fn_return_signature = (just(Token::Arrow)
        .ignore_then(just(Token::Ctrl('(')))
        .ignore_then(fn_return_signature)
        .then_ignore(just(Token::Ctrl(')'))))
    .or_not()
    .map(|opt| opt.unwrap_or_default())
    .boxed()
    .labelled("function return signature");

    let fn_explicit_params = spanned_fn_explicit_param
        .separated_by(just(Token::Ctrl(',')))
        .allow_trailing()
        .collect::<Vec<_>>();

    let optional_fn_implicit_param = (just(Token::Ctrl('['))
        .ignore_then(fn_implicit_params)
        .then_ignore(just(Token::Ctrl(']'))))
    .or_not()
    .map(|opt| opt.unwrap_or_default())
    .boxed()
    .labelled("implicit function parameters");

    let fn_def = just(Token::Fn)
        .ignore_then(ident_str)
        .then_ignore(just(Token::Ctrl('(')))
        .then(fn_explicit_params)
        .then_ignore(just(Token::Ctrl(')')))
        .then(optional_fn_implicit_param)
        .then(optional_fn_return_signature)
        .then_ignore(just(Token::Ctrl('{')))
        .then(block.map_with(|block, e| (block, e.span())))
        .then_ignore(just(Token::Ctrl('}')))
        .map(
            |(
                (((spanned_name, explicit_params), implicit_params), return_signature),
                spanned_body,
            )| FnDef {
                name: spanned_name,
                explicit_params,
                implicit_params,
                return_signature,
                body: spanned_body,
            },
        )
        .boxed()
        .labelled("function definition");

    let program = fn_def
        .map_with(|fn_def, e| (fn_def, e.span()))
        .repeated()
        .collect::<Vec<_>>()
        .validate(|functions_with_span, e, emitter| {
            let mut funcs_list = Vec::new();
            let mut seen_names = HashSet::new();
            for (func, func_span) in functions_with_span {
                if seen_names.contains(func.name.0) {
                    emitter.emit(Rich::custom(
                        func.name.1,
                        format!("Function `{}` is defined multiple times", func.name.0),
                    ));
                    continue;
                }
                seen_names.insert(func.name.0);
                funcs_list.push((func.name.0, (func, func_span)));
            }
            (
                Program {
                    functions: funcs_list,
                },
                e.span(),
            )
        })
        .labelled("program");

    program
}

/// Important syntax note: mutually recursive functions are not supported.
/// Function definitions must be ordered in reverse C/C++ order, i.e.,
/// if function `foo` calls `bar`, then `bar` must be defined after `foo` in the source.
// TODO: rework this function. terrible.
pub fn parse_to_op_ctx_and_map<'src, S: SemanticsWithCustomSyntax>(
    src: &'src str,
) -> (OperationContext<S>, HashMap<&'src str, OperationId>) {
    match try_parse_to_op_ctx_and_map::<S>(src, true).op_ctx_and_map {
        Ok((op_ctx, fn_map)) => (op_ctx, fn_map),
        Err(WithLineColSpans { value: output, .. }) => {
            panic!("Failed to parse the input source code:\n{output}")
        }
    }
}

pub fn try_parse_to_op_ctx_and_map<'src, S: SemanticsWithCustomSyntax>(
    src: &'src str,
    color_enabled: bool,
) -> InterpreterResult<'src, S, WithLineColSpans<String>> {
    let filename = "input".to_string();
    let (tokens, errs) = lexer().parse(src).into_output_errors();

    fn string_of_report(
        filename: String,
        src: &str,
        report: Report<(String, Range<usize>)>,
    ) -> String {
        let mut output_buf = BufWriter::new(Vec::new());
        report
            .write(sources([(filename, src)]), &mut output_buf)
            .unwrap();
        String::from_utf8(output_buf.into_inner().unwrap()).unwrap()
    }

    // println!("Tokens: {tokens:?}");

    let parse_errs = if let Some(tokens) = &tokens {
        let (ast, parse_errs) = program_parser::<_, S::CS>()
            .map_with(|ast, e| (ast, e.span()))
            .parse(
                tokens
                    .as_slice()
                    .map((src.len()..src.len()).into(), |(t, s)| (t, s)),
            )
            .into_output_errors();

        // Note: if we wanted to also proceed with a error-recovered AST, this filter predicate needs to be changed, and the errors would still need
        // to be propagated somehow.
        if let Some((program, file_span)) = ast.filter(|_| errs.len() + parse_errs.len() == 0) {
            let res = interpret::<S>(program);
            match res.op_ctx_and_map {
                Ok((op_ctx, fns_to_ids)) => {
                    return InterpreterResult {
                        op_ctx_and_map: Ok((op_ctx, fns_to_ids)),
                        state_map: res.state_map,
                    };
                }
                Err(e) => {
                    let detailed_message = format!("{:?}", e);
                    let error_span = e.current_context().span;

                    let line_col_span = span_into_line_col_start_and_line_col_end(&error_span, src);

                    // the amount of spaces depends on the printed line number of the error.
                    // this is just for prettier formatting below of the pipe prefix.
                    // ariadne tracking issue: https://github.com/zesterer/ariadne/issues/68
                    let line_num = line_col_span.line_end;
                    let num_spaces = format!("{line_num}").len();
                    let spaces = " ".repeat(num_spaces);

                    // TODO: need to consider here if color is enabled or not.
                    let detailed_message_pipe_mapped = detailed_message
                        .lines()
                        .map(|line| format!("  {spaces}│  {line}"))
                        .collect::<Vec<_>>()
                        .join("\n");

                    // build report with error
                    let err_string = string_of_report(
                        filename.clone(),
                        src,
                        Report::build(
                            ReportKind::Error,
                            (filename.clone(), error_span.into_range()),
                        )
                        .with_config(
                            ariadne::Config::new()
                                .with_index_type(ariadne::IndexType::Byte)
                                .with_color(color_enabled),
                        )
                        .with_message(e.to_string())
                        .with_label(
                            Label::new((filename.clone(), error_span.into_range()))
                                .with_message(format!(
                                    "detailed message:\n{detailed_message_pipe_mapped}"
                                ))
                                .with_color(Color::Red),
                        )
                        .finish(),
                    );

                    let err = Err(WithLineColSpans {
                        value: err_string,
                        spans: vec![line_col_span],
                    });
                    return InterpreterResult {
                        op_ctx_and_map: err,
                        state_map: res.state_map,
                    };
                }
            }
        }

        parse_errs
    } else {
        Vec::new()
    };

    let mut output_buf = BufWriter::new(Vec::new());

    let mut line_col_spans = Vec::new();

    errs.into_iter()
        .map(|e| e.map_token(|c| c.to_string()))
        .chain(
            parse_errs
                .into_iter()
                .map(|e| e.map_token(|tok| tok.to_string())),
        )
        .for_each(|e| {
            line_col_spans.push(span_into_line_col_start_and_line_col_end(&e.span(), src));
            Report::build(ReportKind::Error, (filename.clone(), e.span().into_range()))
                .with_config(
                    ariadne::Config::new()
                        .with_index_type(ariadne::IndexType::Byte)
                        .with_color(color_enabled),
                )
                .with_message(e.to_string())
                .with_label(
                    Label::new((filename.clone(), e.span().into_range()))
                        .with_message(e.reason().to_string())
                        .with_color(Color::Red),
                )
                .with_labels(e.contexts().map(|(label, span)| {
                    Label::new((filename.clone(), span.into_range()))
                        .with_message(format!("while parsing this {label}"))
                        .with_color(Color::Yellow)
                }))
                .finish()
                .write(sources([(filename.clone(), src)]), &mut output_buf)
                .unwrap()
        });

    let output = String::from_utf8(output_buf.into_inner().unwrap()).unwrap();
    InterpreterResult {
        op_ctx_and_map: Err(WithLineColSpans {
            value: output,
            spans: line_col_spans,
        }),
        state_map: HashMap::new(),
    }
}

#[derive(Clone)]
pub struct WithLineColSpans<T> {
    pub value: T,
    pub spans: Vec<LineColSpan>,
}

impl<T: Display> Debug for WithLineColSpans<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "WithLineColSpans {{ value: \n{}\n, spans: {:?} }}",
            self.value, self.spans
        )
    }
}

#[derive(Clone, Debug)]
pub struct LineColSpan {
    pub line_start: usize,
    pub col_start: usize,
    pub line_end: usize,
    pub col_end: usize,
}

fn span_into_line_col_start_and_line_col_end(span: &Span, src: &str) -> LineColSpan {
    let start = span.start();
    let end = span.end();
    let mut line_start = 1;
    let mut col_start = 1;
    let mut line_end = 1;
    let mut col_end = 1;

    for (i, c) in src.bytes().enumerate() {
        if i < start {
            if c == b'\n' {
                line_start += 1;
                col_start = 1;
            } else {
                col_start += 1;
            }
            // line_end and col_end must be at least this.
            line_end = line_start;
            col_end = col_start;
        } else if i < end {
            if c == b'\n' {
                line_end += 1;
                col_end = 1;
            } else {
                col_end += 1;
            }
        } else {
            break;
        }
    }

    LineColSpan {
        line_start,
        col_start,
        line_end,
        col_end,
    }
}

/// Compared to the syntax_macro, this will only parse at runtime. The syntax_macro will parse at runtime as well,
/// but will compile-error if the syntax is invalid.
#[macro_export]
macro_rules! grabapl_parse {
    ($semantics:ty, $($t:tt)*) => {$crate::parse_to_op_ctx_and_map::<$semantics>(stringify!($($t)*))};
}

#[macro_export]
macro_rules! grabapl_defs {
    ($fn_name:ident, $semantics:ty, $($t:tt)*) => {
        fn $fn_name() -> (OperationContext<$semantics>, std::collections::HashMap<&'static str, grabapl::prelude::OperationId>) {
            $crate::grabapl_parse!($semantics, $($t)*)
        }
    };
}

#[cfg(test)]
mod tests {
    use grabapl::semantics::example_with_ref::ExampleWithRefSemantics;

    #[test]
    fn negative_token() {
        let _ = grabapl_parse!(ExampleWithRefSemantics,
        fn foo(x: int) {
            add_node<int,-1>();
        }
        );
    }
}