Enum jomini::TextToken

source ·
pub enum TextToken<'a> {
    Array {
        end: usize,
        mixed: bool,
    },
    Object {
        end: usize,
        mixed: bool,
    },
    MixedContainer,
    Unquoted(Scalar<'a>),
    Quoted(Scalar<'a>),
    Parameter(Scalar<'a>),
    UndefinedParameter(Scalar<'a>),
    Operator(Operator),
    End(usize),
    Header(Scalar<'a>),
}
Expand description

Represents a valid text value

Variants§

§

Array

Fields

§end: usize

The index of the TextToken::End for this array

§mixed: bool

If this array contains a MixedContainer token

Start of an array

§

Object

Fields

§end: usize

Index of the TextToken::End that signifies this objects’s termination

§mixed: bool

If this object contains a MixedContainer token

Start of an object

The value of a property typically immediately follows a key token. However, this is not guaranteed so always check if the end has been reached before trying to decode a value. There are two main situations where this is not guaranteed:

  • A non-equal operator (eg: a > b will be parsed to 3 instead of 2 tokens)
  • If an object switches to a mixed container that is both an array and object
§

MixedContainer

Denotes the start of where a homogenous object or array becomes heterogenous.

§

Unquoted(Scalar<'a>)

Extracted unquoted scalar value

§

Quoted(Scalar<'a>)

Extracted quoted scalar value

§

Parameter(Scalar<'a>)

A parameter scalar

Only seen so far in EU4. From the patch notes:

Scripted triggers or effects now support conditional compilation on arguments provided to them. You can now check for if an argument is defined or not and make the script look entirely different based on that. Syntax is [[var_name] code here ] for if variable is defined

generate_advisor = { [[scaled_skill] if = { } ] }
§

UndefinedParameter(Scalar<'a>)

An undefined parameter, see Parameter variant for more info.

Syntax for undefined variable:

[[!var_name] code here ]
§

Operator(Operator)

A present, but non-equal operator token

§

End(usize)

Index of the start of this object

§

Header(Scalar<'a>)

The header token of the subsequent scalar. For instance, given

color = rgb { 100 200 50 }

rgb would be a the header followed by a 3 element array

Implementations§

Returns the scalar if the token contains a scalar

use jomini::{Scalar, TextToken};
assert_eq!(TextToken::Unquoted(Scalar::new(b"abc")).as_scalar(), Some(Scalar::new(b"abc")));
assert_eq!(TextToken::Quoted(Scalar::new(b"abc")).as_scalar(), Some(Scalar::new(b"abc")));
assert_eq!(TextToken::Header(Scalar::new(b"rgb")).as_scalar(), Some(Scalar::new(b"rgb")));
assert_eq!((TextToken::Object { end: 2, mixed: false }).as_scalar(), None);
Examples found in repository?
src/text/reader.rs (line 695)
693
694
695
696
697
698
699
    pub fn read_scalar(&self) -> Result<Scalar<'data>, DeserializeError> {
        self.tokens[self.value_ind]
            .as_scalar()
            .ok_or_else(|| DeserializeError {
                kind: DeserializeErrorKind::Unsupported(String::from("not a scalar")),
            })
    }
More examples
Hide additional examples
src/text/tape.rs (line 920)
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
    pub fn parse(&mut self) -> Result<(), Error> {
        let mut data = self.data;
        let mut state = ParseState::Key;

        self.utf8_bom = data.get(..3).map_or(false, |x| x == [0xef, 0xbb, 0xbf]);
        if self.utf8_bom {
            data = &data[3..];
        }

        let mut mixed_mode = false;
        let mut parent_ind = 0;
        loop {
            let d = match self.skip_ws_t(data) {
                Some(d) => d,
                None => {
                    if state != ParseState::Key {
                        return Err(Error::eof());
                    }

                    if parent_ind == 0 {
                        return Ok(());
                    } else {
                        // Support for files that don't have enough closing brackets (ugh)
                        let grand_ind = match self.token_tape.get(parent_ind) {
                            Some(TextToken::Array { end, .. }) => *end,
                            Some(TextToken::Object { end, .. }) => *end,
                            _ => 0,
                        };

                        if grand_ind == 0 {
                            let end = self.token_tape.len();
                            self.token_tape.push(TextToken::End(parent_ind));
                            self.token_tape[parent_ind] = TextToken::Object { end, mixed: false };
                            return Ok(());
                        } else {
                            return Err(Error::eof());
                        }
                    }
                }
            };

            data = d;
            match state {
                ParseState::Key => {
                    match data[0] {
                        b'}' | b']' => {
                            let saved_mixed = mixed_mode;
                            let grand_ind = match self.token_tape.get(parent_ind) {
                                Some(TextToken::Array { end, .. }) => *end,
                                Some(TextToken::Object { end, .. }) => *end,
                                _ => 0,
                            };

                            match self.token_tape.get(grand_ind) {
                                Some(TextToken::Array { mixed, .. }) => {
                                    mixed_mode = *mixed;
                                    state = ParseState::ArrayValue;
                                }
                                Some(TextToken::Object { mixed, .. }) => {
                                    mixed_mode = *mixed;
                                    state = if mixed_mode {
                                        ParseState::ArrayValue
                                    } else {
                                        ParseState::Key
                                    }
                                }
                                _ => {
                                    mixed_mode = false;
                                    state = ParseState::Key;
                                }
                            };

                            let end_idx = self.token_tape.len();
                            if parent_ind == 0 && grand_ind == 0 {
                                // Allow extraneous close braces to support malformatted game files (ugh)
                                data = &data[1..];
                                continue;
                            }

                            self.token_tape.push(TextToken::End(parent_ind));
                            self.token_tape[parent_ind] = TextToken::Object {
                                end: end_idx,
                                mixed: saved_mixed,
                            };
                            parent_ind = grand_ind;
                            data = &data[1..];
                        }

                        // Empty object or token header
                        b'{' => {
                            data = self.skip_ws_t(&data[1..]).ok_or_else(Error::eof)?;
                            if data[0] == b'}' {
                                data = &data[1..];
                                continue;
                            }

                            if let Some(last) = self.token_tape.last_mut() {
                                if let TextToken::Unquoted(header) = last {
                                    *last = TextToken::Header(*header);
                                    self.token_tape.push(TextToken::Array {
                                        end: 0,
                                        mixed: false,
                                    });
                                    state = ParseState::ParseOpen;
                                    continue;
                                }
                            }

                            return Err(Error::new(ErrorKind::InvalidSyntax {
                                offset: self.offset(data),
                                msg: String::from("invalid syntax for token headers"),
                            }));
                        }

                        b'[' => {
                            data = self.parse_parameter_definition(
                                data,
                                &mut parent_ind,
                                &mut state,
                                false,
                            )?;
                        }

                        b'"' => {
                            data = self.parse_quote_scalar(data)?;
                            state = ParseState::KeyValueSeparator;
                        }

                        b'@' => {
                            data = self.parse_variable(data)?;
                            state = ParseState::KeyValueSeparator;
                        }

                        _ => {
                            data = self.parse_scalar(data);
                            state = ParseState::KeyValueSeparator;
                        }
                    }
                }
                ParseState::KeyValueSeparator => match data {
                    [b'<', b'=', ..] => {
                        self.token_tape
                            .push(TextToken::Operator(Operator::LessThanEqual));
                        data = &data[2..];
                        state = ParseState::ObjectValue;
                    }
                    [b'<', ..] => {
                        self.token_tape
                            .push(TextToken::Operator(Operator::LessThan));
                        data = &data[1..];
                        state = ParseState::ObjectValue;
                    }
                    [b'>', b'=', ..] => {
                        self.token_tape
                            .push(TextToken::Operator(Operator::GreaterThanEqual));
                        data = &data[2..];
                        state = ParseState::ObjectValue;
                    }
                    [b'>', ..] => {
                        self.token_tape
                            .push(TextToken::Operator(Operator::GreaterThan));
                        data = &data[1..];
                        state = ParseState::ObjectValue;
                    }
                    [b'!', b'=', ..] => {
                        self.token_tape
                            .push(TextToken::Operator(Operator::NotEqual));
                        data = &data[2..];
                        state = ParseState::ObjectValue;
                    }
                    [b'=', b'=', ..] => {
                        self.token_tape.push(TextToken::Operator(Operator::Exact));
                        data = &data[2..];
                        state = ParseState::ObjectValue;
                    }
                    [b'=', ..] if mixed_mode => {
                        self.token_tape.push(TextToken::Operator(Operator::Equal));
                        data = &data[1..];
                    }
                    [b'=', ..] => {
                        data = &data[1..];
                        state = ParseState::ObjectValue;
                    }
                    [b'{', ..] => {
                        state = ParseState::ObjectValue;
                    }
                    [b'}', ..] => {
                        self.token_tape
                            .insert(self.token_tape.len() - 1, TextToken::MixedContainer);
                        state = ParseState::ArrayValue;
                        mixed_mode = true;
                    }
                    _ => {
                        self.token_tape
                            .insert(self.token_tape.len() - 1, TextToken::MixedContainer);
                        state = ParseState::ArrayValue;
                        mixed_mode = true;
                    }
                },
                ParseState::ObjectValue => match data[0] {
                    b'{' => {
                        self.token_tape.push(TextToken::Array {
                            end: 0,
                            mixed: false,
                        });
                        state = ParseState::ParseOpen;
                        data = &data[1..];
                    }

                    b'}' => {
                        return Err(Error::new(ErrorKind::InvalidSyntax {
                            msg: String::from("encountered '}' for object value"),
                            offset: self.offset(data),
                        }));
                    }

                    b'"' => {
                        data = self.parse_quote_scalar(data)?;
                        state = ParseState::Key;
                    }
                    b'@' => {
                        data = self.parse_variable(data)?;
                        state = ParseState::Key;
                    }
                    _ => {
                        data = self.parse_scalar(data);
                        state = ParseState::Key;
                    }
                },
                ParseState::ParseOpen => {
                    match data[0] {
                        // Empty array
                        b'}' => {
                            let ind = self.token_tape.len() - 1;

                            match self.token_tape.get(parent_ind) {
                                Some(TextToken::Array { mixed, .. }) => {
                                    mixed_mode = *mixed;
                                    state = ParseState::ArrayValue;
                                }
                                Some(TextToken::Object { mixed, .. }) => {
                                    mixed_mode = *mixed;
                                    state = if mixed_mode {
                                        ParseState::ArrayValue
                                    } else {
                                        ParseState::Key
                                    }
                                }
                                _ => {
                                    mixed_mode = false;
                                    state = ParseState::Key;
                                }
                            };

                            self.token_tape[ind] = TextToken::Array {
                                end: ind + 1,
                                mixed: false,
                            };
                            self.token_tape.push(TextToken::End(ind));
                            data = &data[1..];
                            continue;
                        }

                        // start of a parameter definition
                        b'[' => {
                            if mixed_mode {
                                return Err(Error::new(ErrorKind::InvalidSyntax {
                                    msg: String::from(
                                        "mixed object and array container not expected",
                                    ),
                                    offset: self.offset(data),
                                }));
                            }

                            data = self.parse_parameter_definition(
                                data,
                                &mut parent_ind,
                                &mut state,
                                true,
                            )?;
                            continue;
                        }

                        // array of objects, another array
                        b'{' => {
                            let scratch = self.skip_ws_t(&data[1..]).ok_or_else(Error::eof)?;
                            if scratch[0] == b'}' {
                                data = &scratch[1..];
                                continue;
                            }

                            mixed_mode = false;
                            let ind = self.token_tape.len() - 1;
                            self.token_tape[ind] = TextToken::Array {
                                end: parent_ind,
                                mixed: mixed_mode,
                            };
                            parent_ind = ind;
                            state = ParseState::ArrayValue;
                            continue;
                        }
                        b'"' => {
                            data = self.parse_quote_scalar(data)?;
                        }
                        b'@' => {
                            data = self.parse_variable(data)?;
                        }
                        _ => {
                            data = self.parse_scalar(data);
                        }
                    }

                    if mixed_mode {
                        if let Some(
                            TextToken::Array { mixed, .. } | TextToken::Object { mixed, .. },
                        ) = self.token_tape.get_mut(parent_ind)
                        {
                            *mixed = mixed_mode;
                        }
                    }

                    mixed_mode = false;
                    data = self.skip_ws_t(data).ok_or_else(Error::eof)?;
                    match data[0] {
                        b'=' | b'>' | b'<' => {
                            let ind = self.token_tape.len() - 2;
                            self.token_tape[ind] = TextToken::Object {
                                end: parent_ind,
                                mixed: mixed_mode,
                            };
                            parent_ind = ind;
                            state = ParseState::KeyValueSeparator;
                        }
                        _ => {
                            let ind = self.token_tape.len() - 2;
                            self.token_tape[ind] = TextToken::Array {
                                end: parent_ind,
                                mixed: mixed_mode,
                            };
                            parent_ind = ind;
                            state = ParseState::ArrayValue;
                        }
                    }
                }
                ParseState::ArrayValue => match data[0] {
                    b'{' => {
                        self.token_tape.push(TextToken::Array {
                            end: 0,
                            mixed: false,
                        });
                        state = ParseState::ParseOpen;
                        data = &data[1..];
                    }
                    b'}' => {
                        let saved_mixed_mode = mixed_mode;
                        let (grand_ind, is_array) = match self.token_tape.get(parent_ind) {
                            Some(TextToken::Array { end, .. }) => (*end, true),
                            Some(TextToken::Object { end, .. }) => (*end, false),
                            _ => (0, false),
                        };

                        match self.token_tape.get(grand_ind) {
                            Some(TextToken::Array { mixed, .. }) => {
                                mixed_mode = *mixed;
                                state = ParseState::ArrayValue;
                            }
                            Some(TextToken::Object { mixed, .. }) => {
                                mixed_mode = *mixed;
                                state = if mixed_mode {
                                    ParseState::ArrayValue
                                } else {
                                    ParseState::Key
                                }
                            }
                            _ => {
                                mixed_mode = false;
                                state = ParseState::Key;
                            }
                        };

                        if parent_ind == 0 && grand_ind == 0 {
                            return Err(Error::new(ErrorKind::StackEmpty {
                                offset: self.offset(data),
                            }));
                        }

                        let end_idx = self.token_tape.len();
                        self.token_tape[parent_ind] = if is_array {
                            TextToken::Array {
                                end: end_idx,
                                mixed: saved_mixed_mode,
                            }
                        } else {
                            TextToken::Object {
                                end: end_idx,
                                mixed: saved_mixed_mode,
                            }
                        };

                        self.token_tape.push(TextToken::End(parent_ind));
                        parent_ind = grand_ind;
                        data = &data[1..];
                    }
                    b'"' => {
                        data = self.parse_quote_scalar(data)?;
                        state = ParseState::ArrayValue;
                    }
                    b'@' => {
                        data = self.parse_variable(data)?;
                        state = ParseState::ArrayValue;
                    }
                    b'<' | b'>' | b'!' | b'=' => {
                        if !mixed_mode {
                            if self.token_tape.last().and_then(|x| x.as_scalar()).is_some() {
                                self.token_tape
                                    .insert(self.token_tape.len() - 1, TextToken::MixedContainer);
                                mixed_mode = true;
                            } else {
                                return Err(Error::new(ErrorKind::InvalidSyntax {
                                    msg: String::from("expected a scalar to precede an operator"),
                                    offset: self.offset(data) - 1,
                                }));
                            }
                        }

                        match data {
                            [b'<', b'=', ..] => {
                                self.token_tape
                                    .push(TextToken::Operator(Operator::LessThanEqual));
                                data = &data[2..];
                            }
                            [b'<', ..] => {
                                self.token_tape
                                    .push(TextToken::Operator(Operator::LessThan));
                                data = &data[1..];
                            }
                            [b'>', b'=', ..] => {
                                self.token_tape
                                    .push(TextToken::Operator(Operator::GreaterThanEqual));
                                data = &data[2..];
                            }
                            [b'>', ..] => {
                                self.token_tape
                                    .push(TextToken::Operator(Operator::GreaterThan));
                                data = &data[1..];
                            }
                            [b'!', b'=', ..] => {
                                self.token_tape
                                    .push(TextToken::Operator(Operator::NotEqual));
                                data = &data[2..];
                            }
                            [b'=', b'=', ..] => {
                                self.token_tape.push(TextToken::Operator(Operator::Exact));
                                data = &data[2..];
                            }
                            [b'=', ..] => {
                                self.token_tape.push(TextToken::Operator(Operator::Equal));
                                data = &data[1..];
                            }
                            _ => {
                                return Err(Error::new(ErrorKind::InvalidSyntax {
                                    msg: String::from("unrecognized operator"),
                                    offset: self.offset(data) - 1,
                                }));
                            }
                        }
                    }
                    _ => {
                        data = self.parse_scalar(data);
                        state = ParseState::ArrayValue;
                    }
                },
            }
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.