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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
/*
Copyright (c) 2023 Michał Wilczek, Michał Margos

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use geo_aid_derive::Parse;
use num_traits::Zero;

use crate::script::builtins;
use std::{
    fmt::{Debug, Display},
    iter::Peekable,
    marker::PhantomData,
};
use std::fmt::Formatter;
use crate::script::token::number::ProcNum;

use crate::span;

use super::{
    token::{
        number::CompExponent, Ampersant, Asterisk, At, Caret, Colon, Comma, Dollar, Dot, Eq,
        Exclamation, Gt, Gteq, Ident, LBrace, LParen, LSquare, Let, Lt, Lteq, Minus, NamedIdent,
        Number, Plus, Question, RBrace, RParen, RSquare, Semi, Slash, Span, StrLit, TokInteger,
        Token,
    },
    unit, ComplexUnit, Error,
};

pub trait Parse {
    type FirstToken: CheckParses;

    /// # Errors
    /// Returns an error if parsing was unsuccessful.
    fn parse<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &mut InputStream<'t, I>,
    ) -> Result<Self, Error>
    where
        Self: Sized;

    fn get_span(&self) -> Span;
}

pub trait CheckParses {
    fn check_parses<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &InputStream<'t, I>,
    ) -> Option<bool>;
}

impl<T: Parse> CheckParses for T {
    fn check_parses<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &InputStream<'t, I>,
    ) -> Option<bool> {
        Some(input.clone().parse::<Self>().is_ok())
    }
}

#[derive(Debug)]
pub struct TokenOr<T, U> {
    phantom_t: PhantomData<T>,
    phantom_u: PhantomData<U>,
}

impl<T: CheckParses, U: CheckParses> CheckParses for TokenOr<T, U> {
    fn check_parses<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &InputStream<'t, I>,
    ) -> Option<bool> {
        T::check_parses(input).or_else(|| U::check_parses(input))
    }
}

#[derive(Debug)]
pub struct Maybe<T> {
    phantom_t: PhantomData<T>,
}

impl<T: CheckParses> CheckParses for Maybe<T> {
    fn check_parses<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &InputStream<'t, I>,
    ) -> Option<bool> {
        T::check_parses(input).and_then(|x| if x { Some(x) } else { None })
    }
}

impl<T: Parse> Parse for Vec<T> {
    type FirstToken = Maybe<T::FirstToken>;

    fn parse<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &mut InputStream<'t, I>,
    ) -> Result<Self, Error>
    where
        Self: Sized,
    {
        let mut parsed = Self::new();

        while let Ok(Some(v)) = input.parse() {
            parsed.push(v);
        }

        Ok(parsed)
    }

    fn get_span(&self) -> Span {
        self.first().map_or(Span::empty(), |x| {
            x.get_span().join(self.last().unwrap().get_span())
        })
    }
}

impl<T: Parse, U: Parse> Parse for (T, U) {
    type FirstToken = T::FirstToken;

    fn parse<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &mut InputStream<'t, I>,
    ) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Ok((input.parse()?, input.parse()?))
    }

    fn get_span(&self) -> Span {
        self.0.get_span().join(self.1.get_span())
    }
}

impl<T: Parse> Parse for Option<T> {
    type FirstToken = Maybe<T::FirstToken>;

    fn parse<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &mut InputStream<'t, I>,
    ) -> Result<Self, Error>
    where
        Self: Sized,
    {
        if T::FirstToken::check_parses(input) == Some(false) {
            Ok(None)
        } else {
            Ok(Some(input.parse()?))
        }
    }

    fn get_span(&self) -> Span {
        match self {
            Some(v) => v.get_span(),
            None => span!(0, 0, 0, 0),
        }
    }
}

#[derive(Debug, Clone)]
pub struct InputStream<'t, I: Iterator<Item = &'t Token> + Clone> {
    it: Peekable<I>,
}

impl<'t, I: Iterator<Item = &'t Token> + Clone> InputStream<'t, I> {
    #[must_use]
    pub fn new<It: IntoIterator<IntoIter = I>>(it: It) -> Self {
        Self {
            it: it.into_iter().peekable(),
        }
    }

    /// # Errors
    /// Returns an error if failed to parse
    pub fn parse<P: Parse>(&mut self) -> Result<P, Error> {
        P::parse(self)
    }

    /// # Errors
    /// Returns an EOF error if there is no next token.
    pub fn get_token(&mut self) -> Result<&'t Token, Error> {
        self.it.next().ok_or(Error::EndOfInput)
    }

    /// # Errors
    /// Returns an EOF error if there is no next token. Does not advance the inner iterator.
    pub fn expect_token(&mut self) -> Result<(), Error> {
        if self.eof() {
            Err(Error::EndOfInput)
        } else {
            Ok(())
        }
    }

    #[must_use]
    pub fn eof(&mut self) -> bool {
        self.it.peek().is_none()
    }
}

/// A binary operator, like `+`, `-`, `*` or `/`.
#[derive(Debug, Parse)]
pub enum BinaryOperator {
    /// Addition
    Add(Plus),
    /// Subtraction
    Sub(Minus),
    /// Multiplication
    Mul(Asterisk),
    /// Division
    Div(Slash),
}

impl Display for BinaryOperator {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            BinaryOperator::Add(_) => write!(f, "+"),
            BinaryOperator::Sub(_) => write!(f, "-"),
            BinaryOperator::Mul(_) => write!(f, "*"),
            BinaryOperator::Div(_) => write!(f, "/"),
        }
    }
}

#[derive(Debug, Parse)]
#[parse(first_token = Ampersant)]
pub struct PointCollectionConstructor {
    pub ampersant: Ampersant,
    pub left_paren: LParen,
    pub points: Punctuated<Expression<false>, Comma>,
    pub right_paren: RParen,
}

/// Punctuated expressions.
#[derive(Debug)]
pub struct ImplicitIterator<const ITER: bool> {
    pub exprs: Punctuated<SimpleExpression, Comma>,
}

impl<const ITER: bool> ImplicitIterator<ITER> {
    #[must_use]
    pub fn get(&self, index: usize) -> Option<&SimpleExpression> {
        if ITER {
            self.exprs.get(index)
        } else {
            Some(&self.exprs.first)
        }
    }
}

impl<const ITER: bool> Parse for ImplicitIterator<ITER> {
    type FirstToken = <SimpleExpression as Parse>::FirstToken;

    fn parse<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &mut InputStream<'t, I>,
    ) -> Result<Self, Error>
    where
        Self: Sized,
    {
        if ITER {
            Ok(Self {
                exprs: input.parse()?,
            })
        } else {
            Ok(Self {
                exprs: Punctuated {
                    first: input.parse()?,
                    collection: Vec::new(),
                },
            })
        }
    }

    fn get_span(&self) -> Span {
        self.exprs.get_span()
    }
}

/// $id(a, b, ...).
#[derive(Debug)]
pub struct ExplicitIterator {
    pub dollar: Dollar,
    pub id_token: TokInteger,
    pub id: u8,
    pub left_paren: LParen,
    pub exprs: Punctuated<Expression<false>, Comma>,
    pub right_paren: RParen,
}

impl ExplicitIterator {
    #[must_use]
    pub fn get(&self, index: usize) -> Option<&Expression<false>> {
        self.exprs.get(index)
    }
}

impl Parse for ExplicitIterator {
    type FirstToken = Dollar;

    fn parse<'r, I: Iterator<Item = &'r Token> + Clone>(
        input: &mut InputStream<'r, I>,
    ) -> Result<Self, Error> {
        let mut parsed = Self {
            dollar: input.parse()?,
            id_token: input.parse()?,
            id: 0,
            left_paren: input.parse()?,
            exprs: input.parse()?,
            right_paren: input.parse()?,
        };

        parsed.id = parsed
            .id_token
            .parsed
            .parse()
            .map_err(|_| Error::NumberTooLarge {
                error_span: parsed.id_token.get_span(),
            })?;

        if parsed.exprs.len() == 1 {
            return Err(Error::SingleVariantExplicitIterator {
                error_span: parsed.dollar.span.join(parsed.right_paren.span),
            });
        }

        Ok(parsed)
    }

    fn get_span(&self) -> Span {
        self.dollar.span.join(self.right_paren.span)
    }
}

/// A parsed expression.
#[derive(Debug)]
pub enum Expression<const ITER: bool> {
    /// Simple values separated by a comma.
    ImplicitIterator(ImplicitIterator<ITER>),
    /// A binary operator expression.
    Binop(ExprBinop<ITER>),
}

impl<const ITER: bool> Parse for Expression<ITER> {
    type FirstToken = <SimpleExpression as Parse>::FirstToken;

    fn parse<'r, I: Iterator<Item = &'r Token> + Clone>(
        input: &mut InputStream<'r, I>,
    ) -> Result<Self, Error> {
        let mut expr = Expression::ImplicitIterator(input.parse()?);

        while let Ok(Some(op)) = input.parse() {
            let rhs = Expression::ImplicitIterator(input.parse()?);

            expr = dispatch_order(expr, op, rhs);
        }

        Ok(expr)
    }

    fn get_span(&self) -> Span {
        match self {
            Expression::ImplicitIterator(it) => it.get_span(),
            Expression::Binop(e) => e.lhs.get_span().join(e.rhs.get_span()),
        }
    }
}

/// A rational exponent.
#[derive(Debug)]
pub struct RationalExponent {
    pub lparen: LParen,
    pub nom: TokInteger,
    pub slash: Slash,
    pub denom: TokInteger,
    pub rparen: RParen,
}

impl Parse for RationalExponent {
    type FirstToken = LParen;

    fn parse<'r, I: Iterator<Item = &'r Token> + Clone>(
        input: &mut InputStream<'r, I>,
    ) -> Result<Self, Error> {
        let parsed = Self {
            lparen: input.parse()?,
            nom: input.parse()?,
            slash: input.parse()?,
            denom: input.parse()?,
            rparen: input.parse()?,
        };

        if parsed.denom.parsed.is_zero() {
            return Err(Error::ZeroDenominator {
                error_span: parsed.denom.span,
            });
        }

        Ok(parsed)
    }

    fn get_span(&self) -> Span {
        self.lparen.span.join(self.rparen.span)
    }
}

/// An integer or a ratio.
#[derive(Debug, Parse)]
pub enum Exponent {
    Simple(TokInteger),
    Parenthesized(RationalExponent),
}

impl Exponent {
    /// # Errors
    /// Returns an error if the numbers can't fit.
    pub fn as_comp(&self) -> Result<CompExponent, Error> {
        match self {
            Self::Simple(i) => Ok(CompExponent::new(
                i.parsed
                    .parse()
                    .map_err(|_| Error::NumberTooLarge { error_span: i.span })?,
                1,
            )),
            Self::Parenthesized(exp) => Ok(CompExponent::new(
                exp.nom.parsed.parse().map_err(|_| Error::NumberTooLarge {
                    error_span: exp.nom.span,
                })?,
                exp.denom
                    .parsed
                    .parse()
                    .map_err(|_| Error::NumberTooLarge {
                        error_span: exp.denom.span,
                    })?,
            )),
        }
    }
}

/// A value being raised to a rational power.
#[derive(Debug, Parse)]
pub struct Exponentiation {
    /// Caret token
    pub caret: Caret,
    /// Possible negation
    pub minus: Option<Minus>,
    /// The exponent.
    pub exponent: Exponent,
}

#[derive(Debug, Parse)]
pub struct FieldIndex {
    /// The indexed thing
    pub name: Box<Name>,
    /// The dot
    pub dot: Dot,
    /// The field
    pub field: Ident,
}

/// A name (field, method or variable).
#[derive(Debug)]
pub enum Name {
    // Variant order matters, as it defines the parsing priority
    Call(ExprCall),
    FieldIndex(FieldIndex),
    Ident(Ident),
    Expression(ExprParenthesised),
}

impl Parse for Name {
    type FirstToken = TokenOr<Maybe<Ident>, LParen>;

    fn parse<'t, I: Iterator<Item = &'t Token> + Clone>(
        input: &mut InputStream<'t, I>,
    ) -> Result<Self, Error>
    where
        Self: Sized,
    {
        let mut name = if let Some(expr) = input.parse()? {
            Self::Expression(expr)
        } else {
            Self::Ident(input.parse()?)
        };

        loop {
            name = if let Some(dot) = input.parse::<Option<Dot>>()? {
                Self::FieldIndex(FieldIndex {
                    name: Box::new(name),
                    dot,
                    field: input.parse()?,
                })
            } else if let Some(lparen) = input.parse::<Option<LParen>>()? {
                Self::Call(ExprCall {
                    name: Box::new(name),
                    lparen,
                    params: input.parse()?,
                    rparen: input.parse()?,
                })
            } else {
                break Ok(name);
            };
        }
    }

    fn get_span(&self) -> Span {
        match self {
            Self::Call(v) => v.get_span(),
            Self::FieldIndex(v) => v.get_span(),
            Self::Expression(v) => v.get_span(),
            Self::Ident(v) => v.get_span(),
        }
    }
}

/// A parsed simple expression.
#[derive(Debug, Parse)]
pub struct SimpleExpression {
    /// Possible minus for negation.
    pub minus: Option<Minus>,
    /// The kind of the expression.
    pub kind: SimpleExpressionKind,
    /// Possible exponentiation.
    pub exponent: Option<Exponentiation>,
    /// The additional display information.
    pub display: Option<DisplayProperties>,
}

/// A parsed simple expression.
#[derive(Debug, Parse)]
pub enum SimpleExpressionKind {
    /// A named (variable, field or function call)
    Name(Name),
    /// A raw number
    Number(Number),
    /// An explicit iterator.
    ExplicitIterator(ExplicitIterator),
    /// A point collection construction
    PointCollection(PointCollectionConstructor),
}

/// A parsed function call
#[derive(Debug, Parse)]
pub struct ExprCall {
    /// The called thing.
    pub name: Box<Name>,
    /// The `(` token.
    pub lparen: LParen,
    /// Punctuated params. `None` if no params are given.
    pub params: Option<Punctuated<Expression<false>, Comma>>,
    /// The `)` token.
    pub rparen: RParen,
}

/// A parsed parenthesised expression
#[derive(Debug, Parse)]
pub struct ExprParenthesised {
    /// The `(` token.
    pub lparen: LParen,
    /// The contained `Expression`.
    pub content: Box<Expression<true>>,
    /// The `)` token.
    pub rparen: RParen,
}

/// A parsed binary operator expression.
#[derive(Debug, Parse)]
pub struct ExprBinop<const ITER: bool> {
    /// Left hand side
    pub lhs: Box<Expression<ITER>>,
    /// The operator
    pub operator: BinaryOperator,
    /// Right hand side.
    pub rhs: Box<Expression<ITER>>,
}

impl BinaryOperator {
    fn index(&self) -> u8 {
        match self {
            BinaryOperator::Add(_) | BinaryOperator::Sub(_) => 1,
            BinaryOperator::Mul(_) | BinaryOperator::Div(_) => 2,
        }
    }
}

/// Inserts an operator with an rhs into an operator series, considering the order of operations.
fn dispatch_order<const ITER: bool>(
    lhs: Expression<ITER>,
    op: BinaryOperator,
    rhs: Expression<ITER>, // We have to trust, that it is a valid expression.
) -> Expression<ITER> {
    match lhs {
        // if lhs is simple, there is no order to consider.
        lhs @ Expression::ImplicitIterator(_) => Expression::Binop(ExprBinop {
            lhs: Box::new(lhs),
            operator: op,
            rhs: Box::new(rhs),
        }),
        // Otherwise we compare indices of the operators and act accordingly.
        Expression::Binop(lhs) => {
            if op.index() > lhs.operator.index() {
                Expression::Binop(ExprBinop {
                    lhs: lhs.lhs,
                    operator: lhs.operator,
                    rhs: Box::new(dispatch_order(*lhs.rhs, op, rhs)),
                })
            } else {
                Expression::Binop(ExprBinop {
                    lhs: Box::new(Expression::Binop(lhs)),
                    operator: op,
                    rhs: Box::new(rhs),
                })
            }
        }
    }
}

/// A builtin rule operator
#[derive(Debug, Parse)]
pub enum PredefinedRuleOperator {
    /// Equality
    Eq(Eq),
    /// Less than
    Lt(Lt),
    /// Greater than
    Gt(Gt),
    /// Less than or equal
    Lteq(Lteq),
    /// Greater than or equal
    Gteq(Gteq),
}

/// A rule operator.
#[derive(Debug, Parse)]
pub enum RuleOperator {
    /// An inverted rule operator (!op)
    Inverted(InvertedRuleOperator),
    Predefined(PredefinedRuleOperator),
    Defined(NamedIdent),
}

/// An inverted rule operator.
#[derive(Debug, Parse)]
#[parse(first_token = Exclamation)]
pub struct InvertedRuleOperator {
    /// The `!` token
    pub exclamation: Exclamation,
    /// The operator.
    pub operator: Box<RuleOperator>,
}

/// Defines the first half of a flag statement.
#[derive(Debug, Parse)]
pub struct FlagName {
    pub at: At,
    pub name: Punctuated<NamedIdent, Dot>,
    pub colon: Colon,
}

/// A set of flags.
#[derive(Debug, Parse)]
#[parse(first_token = LBrace)]
pub struct FlagSet {
    pub lbrace: LBrace,
    pub flags: Vec<FlagStatement>,
    pub rbrace: RBrace,
}

/// Defines the second half of a flag statement.
#[derive(Debug, Parse)]
pub enum FlagValue {
    Ident(NamedIdent),
    Set(FlagSet),
    Number(Number),
}

/// Defines a compiler flag or flagset.
#[derive(Debug, Parse)]
pub struct FlagStatement {
    pub name: FlagName,
    pub value: FlagValue,
}

/// A single variable definition. Contains its name and optional display properties
#[derive(Debug, Clone, Parse)]
pub struct VariableDefinition {
    /// Name of the variable.
    pub name: Ident,
    /// Display properties.
    pub display_properties: Option<DisplayProperties>,
}

/// `let <something> = <something else>`.
/// Defines variables and possibly adds rules to them.
#[derive(Debug, Parse)]
pub struct LetStatement {
    /// The `let` token.
    pub let_token: Let,
    /// The lhs ident iterator.
    pub ident: Punctuated<VariableDefinition, Comma>,
    /// The `=` token.
    pub eq: Eq,
    /// The rhs expression.
    pub expr: Expression<true>,
    /// The rules after the rhs expression.
    pub rules: Vec<(RuleOperator, Expression<true>)>,
    /// The ending semicolon.
    pub semi: Semi,
}

/// `lhs ruleop rhs`.
/// Defines a rule.
#[derive(Debug, Parse)]
pub struct RuleStatement {
    /// Display properties.
    pub display: Option<DisplayProperties>,
    /// Left hand side
    pub lhs: Expression<true>,
    /// Rule operator
    pub op: RuleOperator,
    /// Right hand side
    pub rhs: Expression<true>,
    /// The ending semicolon.
    pub semi: Semi,
}

/// `?expr`
#[derive(Debug, Parse)]
pub struct RefStatement {
    /// Display properties.
    pub display: Option<DisplayProperties>,
    /// The starting question mark.
    pub question: Question,
    /// Operand.
    pub operand: Expression<true>,
    /// The ending semicolon.
    pub semi: Semi,
}

/// A general statement.
#[derive(Debug, Parse)]
pub enum Statement {
    /// No operation
    Noop(Semi),
    /// let
    Let(LetStatement),
    /// Flag
    Flag(FlagStatement),
    /// Reference
    Ref(RefStatement),
    /// rule
    Rule(RuleStatement),
}

impl Statement {
    #[must_use]
    pub fn as_flag(&self) -> Option<&FlagStatement> {
        if let Self::Flag(v) = self {
            Some(v)
        } else {
            None
        }
    }
}

/// A utility struct for collections of parsed items with punctuators between them.
#[derive(Debug, Clone, Parse)]
pub struct Punctuated<T: Parse, P: Parse> {
    /// The first parsed item.
    pub first: Box<T>,
    /// The next items with punctuators.
    pub collection: Vec<(P, T)>,
}

impl<T: Parse, P: Parse> Punctuated<T, P> {
    /// Creates a new instance of `Punctuated`.
    #[must_use]
    pub fn new(first: T) -> Punctuated<T, P> {
        Self {
            first: Box::new(first),
            collection: Vec::new(),
        }
    }

    /// Turns the punctuated into an iterator on the items.
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        vec![self.first.as_ref()]
            .into_iter()
            .chain(self.collection.iter().map(|x| &x.1))
    }

    /// Turns the punctuated into an iterator on the items.
    pub fn into_parsed_iter(self) -> impl Iterator<Item = T> {
        vec![*self.first]
            .into_iter()
            .chain(self.collection.into_iter().map(|x| x.1))
    }

    /// Gets the item count.
    #[must_use]
    pub fn len(&self) -> usize {
        self.collection.len() + 1
    }

    /// Checks if there are no items (always false).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        false
    }

    /// Tries to get the element on `index`.
    #[must_use]
    pub fn get(&self, index: usize) -> Option<&T> {
        match index {
            0 => Some(&self.first),
            _ => self.collection.get(index - 1).map(|x| &x.1),
        }
    }
}

impl Parse for TokInteger {
    type FirstToken = Number;

    fn parse<'r, I: Iterator<Item = &'r Token> + Clone>(
        input: &mut InputStream<'r, I>,
    ) -> Result<Self, Error> {
        match input.get_token()? {
            Token::Number(Number::Integer(tok)) => Ok(tok.clone()),
            t => Err(Error::InvalidToken { token: t.clone() }),
        }
    }

    fn get_span(&self) -> Span {
        self.span
    }
}

impl Parse for NamedIdent {
    type FirstToken = Ident;

    fn parse<'r, I: Iterator<Item = &'r Token> + Clone>(
        input: &mut InputStream<'r, I>,
    ) -> Result<Self, Error> {
        match input.get_token()? {
            Token::Ident(Ident::Named(named)) => Ok(named.clone()),
            t => Err(Error::InvalidToken { token: t.clone() }),
        }
    }

    fn get_span(&self) -> Span {
        self.span
    }
}

impl<T: Parse> Parse for Box<T> {
    type FirstToken = T::FirstToken;

    fn parse<'r, I: Iterator<Item = &'r Token> + Clone>(
        input: &mut InputStream<'r, I>,
    ) -> Result<Self, Error> {
        Ok(Box::new(input.parse()?))
    }

    fn get_span(&self) -> Span {
        (**self).get_span()
    }
}

/// A builtin type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Type {
    /// A point
    Point,
    /// A line
    Line,
    /// A scalar of a certain unit.
    Scalar(Option<ComplexUnit>),
    /// A point collection.
    PointCollection(usize),
    /// A circle
    Circle,
    /// A bundle type.
    Bundle(&'static str),
    /// Marks unknown type. Unknown type pretends to be valid, but isn't really.
    Unknown,
}

impl Type {
    #[must_use]
    pub fn as_scalar(&self) -> Option<&Option<ComplexUnit>> {
        if let Self::Scalar(v) = self {
            Some(v)
        } else {
            None
        }
    }
}

/// A user-defined type.
pub struct DefinedType {
    /// The type's name.
    pub name: String,
}

impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Point => write!(f, "Point"),
            Self::Line => write!(f, "Line"),
            Self::Scalar(unit) => match unit {
                Some(unit) => write!(f, "Scalar ({unit})"),
                None => write!(f, "Scalar (no unit)"),
            },
            Self::PointCollection(l) => write!(f, "Point collection ({l})"),
            Self::Circle => write!(f, "Circle"),
            Self::Bundle(name) => write!(f, "{name}"),
            Type::Unknown => write!(f, "undefined"),
        }
    }
}

impl Type {
    /// Whether `self` can be cast to `into`.
    #[must_use]
    pub fn can_cast(&self, into: &Type) -> bool {
        match self {
            // A point can only be cast into another point or a point collection with length one.
            Type::Point => matches!(into, Type::Point | Type::PointCollection(1)),
            // A line can only be cast into another line.
            Type::Line => matches!(into, Type::Line),
            // A scalar with a defined unit can only be cast into another scalar with the same unit.
            Type::Scalar(Some(unit1)) => {
                if let Type::Scalar(Some(unit2)) = into {
                    unit1 == unit2
                } else {
                    false
                }
            }
            // A scalar with no defined unit can be cast into any other scalar, except angle.
            Type::Scalar(None) => match into {
                Type::Scalar(unit) => match unit {
                    Some(unit) => unit.0[1].is_zero(), // no angle
                    None => true,
                },
                _ => false,
            },
            Type::PointCollection(l) => match into {
                Type::Point => *l == 1,
                Type::Line => *l == 2,
                Type::Scalar(Some(u)) => *u == unit::DISTANCE && *l == 2,
                Type::PointCollection(v) => v == l || *v == 0,
                _ => false,
            },
            Type::Circle => matches!(into, Type::Circle),
            Type::Bundle(name) => {
                if into == self {
                    true
                } else if let Type::PointCollection(count) = into {
                    builtins::get_bundle_pc(name) == *count
                } else {
                    false
                }
            }
            Type::Unknown => false,
        }
    }
}

/// A property
#[derive(Debug, Clone, Parse)]
pub struct Property {
    /// Property name.
    pub name: NamedIdent,
    /// '='
    pub eq: Eq,
    /// Property value.
    pub value: PropertyValue,
}

/// A property's value
#[derive(Debug, Clone, Parse)]
pub enum PropertyValue {
    Number(Number),
    Ident(Ident),
    RawString(RawString),
    String(StrLit),
}

impl Display for PropertyValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Number(n) => write!(f, "{n}"),
            Self::Ident(i) => write!(f, "{i}"),
            Self::RawString(s) => write!(f, "!{}", s.lit),
            Self::String(s) => write!(f, "{s}"),
        }
    }
}

#[derive(Debug, Clone, Parse)]
pub struct RawString {
    pub excl: Exclamation,
    pub lit: StrLit,
}

pub trait FromProperty: Sized {
    /// # Errors
    /// Causes an error if the value is not properly convertible.
    fn from_property(property: PropertyValue) -> Result<Self, Error>;
}

impl<T: FromProperty> FromProperty for Result<T, Error> {
    fn from_property(property: PropertyValue) -> Result<Self, Error> {
        Ok(T::from_property(property))
    }
}

impl FromProperty for bool {
    fn from_property(property: PropertyValue) -> Result<Self, Error> {
        match property {
            PropertyValue::Ident(ident) => match ident {
                Ident::Named(ident) => match ident.ident.as_str() {
                    "enabled" | "on" | "true" | "yes" => Ok(true),
                    "disabled" | "off" | "false" | "no" => Ok(false),
                    _ => Err(Error::BooleanExpected {
                        error_span: ident.get_span(),
                    }),
                },
                Ident::Collection(_) => Err(Error::BooleanExpected {
                    error_span: ident.get_span(),
                }),
            },
            PropertyValue::Number(num) => match num {
                Number::Integer(i) => match i.parsed.parse::<u8>() {
                    Ok(0) => Ok(false),
                    Ok(1) => Ok(true),
                    _ => Err(Error::BooleanExpected { error_span: i.span }),
                },
                Number::Float(f) => Err(Error::BooleanExpected { error_span: f.span }),
            },
            PropertyValue::String(s) => match s.content.as_str() {
                "enabled" | "on" | "true" | "yes" => Ok(true),
                "disabled" | "off" | "false" | "no" => Ok(false),
                _ => Err(Error::BooleanExpected {
                    error_span: s.get_span(),
                }),
            },
            PropertyValue::RawString(s) => Err(Error::BooleanExpected {
                error_span: s.get_span(),
            }),
        }
    }
}

impl FromProperty for String {
    fn from_property(property: PropertyValue) -> Result<String, Error> {
        match property {
            PropertyValue::Ident(ident) => Ok(ident.to_string()),
            PropertyValue::Number(num) => Err(Error::StringExpected {
                error_span: num.get_span(),
            }),
            PropertyValue::RawString(s) => Ok(s.lit.content),
            PropertyValue::String(s) => Ok(s.content),
        }
    }
}

impl FromProperty for ProcNum {
    fn from_property(property: PropertyValue) -> Result<Self, Error> {
        match property {
            PropertyValue::Number(num) => Ok(ProcNum::from(&num)),
            PropertyValue::RawString(s) => Err(Error::NumberExpected {
                error_span: s.get_span(),
            }),
            PropertyValue::String(s) => Err(Error::NumberExpected {
                error_span: s.get_span(),
            }),
            PropertyValue::Ident(ident) => Err(Error::NumberExpected {
                error_span: ident.get_span(),
            })
        }
    }
}

/// Properties related to displaying things.
#[derive(Debug, Clone, Parse)]
pub struct DisplayProperties {
    /// '['
    pub lsquare: LSquare,
    /// Properties
    pub properties: Punctuated<Property, Semi>,
    /// ']'
    pub rsquare: RSquare,
}