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
use crate::token::Token;

#[derive(Debug)]
pub struct CanonicalFormatScanner {
	state: State,
}

#[derive(Debug, PartialEq, Eq)]
pub enum Recommendation {
	Recommended,
	Required,
	PrependSpace,
	PrependSpaceRequired,
	PrependNewline,
	PrependTwoNewlines,
	SkipToken,
}

#[derive(Debug, PartialEq, Eq)]
enum State {
	MultiNewlineOrInitial,
	Hashed,
	SingleNewline,
	Identifier,
	Number,
	Comment,
	Error,
	Other,
	ColonOrComma,
}

impl CanonicalFormatScanner {
	#[must_use]
	pub fn new() -> Self {
		Self {
			state: State::MultiNewlineOrInitial,
		}
	}
}

impl Default for CanonicalFormatScanner {
	fn default() -> Self {
		Self::new()
	}
}

impl CanonicalFormatScanner {
	/// # Panics
	///
	/// This function panics in some cases where the input could not have been created by correctly parsing a text file.
	pub fn next<Position>(&mut self, token: &Token<Position>) -> Recommendation {
		#[allow(clippy::match_same_arms)]
		let recommendation = match (&self.state, token) {
			(State::Error, _) | (_, Token::Error) => Recommendation::PrependSpaceRequired,

			(State::Comment, Token::Newline) => Recommendation::Required,
			(State::Comment, _) => {
				panic!("Invalid token sequence: Comments can only be followed by newlines")
			}

			(State::MultiNewlineOrInitial, Token::Newline) => Recommendation::SkipToken,
			(State::SingleNewline, Token::Newline) => Recommendation::Recommended,
			(_, Token::Newline) => Recommendation::Required,

			(State::MultiNewlineOrInitial, Token::HeadingHashes(_)) => Recommendation::Required,
			(State::SingleNewline, Token::HeadingHashes(_)) => Recommendation::PrependNewline,
			(_, Token::HeadingHashes(_)) => Recommendation::PrependTwoNewlines,
			(State::Hashed, _) => Recommendation::PrependSpace,

			(State::Identifier, Token::Identifier(_)) => Recommendation::PrependSpaceRequired,

			(
				State::Number,
				Token::Decimal(_)
				| Token::Integer(_)
				| Token::InvalidZeroPrefixedDecimal(_)
				| Token::InvalidZeroPrefixedInteger(_),
			) => Recommendation::PrependSpaceRequired,

			(State::SingleNewline | State::MultiNewlineOrInitial, Token::Comment(_)) => {
				Recommendation::Required
			}
			(_, Token::Comment(_)) => Recommendation::PrependSpace,

			(State::ColonOrComma, _) => Recommendation::PrependSpace,

			(_, _) => Recommendation::Required,
		};

		self.state = match token {
			Token::HeadingHashes(_) => State::Hashed,
			Token::Newline
				if self.state == State::MultiNewlineOrInitial
					|| self.state == State::SingleNewline =>
			{
				State::MultiNewlineOrInitial
			}
			Token::Newline => State::SingleNewline,
			Token::Comment(_) => State::Comment,
			Token::Decimal(_)
			| Token::Integer(_)
			| Token::InvalidZeroPrefixedDecimal(_)
			| Token::InvalidZeroPrefixedInteger(_) => State::Number,
			Token::Identifier(_) | Token::InvalidIdentifierWithVerbatimCarriageReturn(_) => {
				State::Identifier
			}
			Token::Colon | Token::Comma => State::ColonOrComma,
			Token::Error => State::Error,

			// Intentionally not `_` so that this fails to compile when tokens are added.
			// When adding match arms here, also add them above if necessary.
			Token::Brac
			| Token::Ket
			| Token::Bra
			| Token::Ce
			| Token::Paren
			| Token::Thesis
			| Token::Period
			| Token::String(_)
			| Token::InvalidStringWithVerbatimCarriageReturn(_)
			| Token::DataLiteral(_)
			| Token::InvalidDataLiteralWithVerbatimCarriageReturn(_) => State::Other,
		};

		recommendation
	}
}

//TODO: Test stability