use super::{QuoteStyle, Token, TokenType};
use crate::Position;
pub(super) struct TokenProcessor;
impl TokenProcessor {
pub fn simple_token(token_type: TokenType, position: Position) -> Token {
Token::simple(token_type, position)
}
pub fn scalar_token(value: String, quote_style: QuoteStyle, position: Position) -> Token {
Token::new(TokenType::Scalar(value, quote_style), position, position)
}
pub fn anchor_token(name: String, position: Position) -> Token {
Token::new(TokenType::Anchor(name), position, position)
}
pub fn alias_token(name: String, position: Position) -> Token {
Token::new(TokenType::Alias(name), position, position)
}
pub fn tag_token(tag: String, position: Position) -> Token {
Token::new(TokenType::Tag(tag), position, position)
}
pub fn comment_token(comment: String, position: Position) -> Token {
Token::new(TokenType::Comment(comment), position, position)
}
pub fn literal_block_token(value: String, position: Position) -> Token {
Token::new(TokenType::BlockScalarLiteral(value), position, position)
}
pub fn folded_block_token(value: String, position: Position) -> Token {
Token::new(TokenType::BlockScalarFolded(value), position, position)
}
}
pub(super) struct CharClassifier;
impl CharClassifier {
pub fn is_flow_indicator(ch: char) -> bool {
matches!(ch, '[' | ']' | '{' | '}' | ',' | ':')
}
pub fn is_identifier_start(ch: char) -> bool {
ch.is_ascii_alphabetic() || ch == '_'
}
pub fn is_identifier_char(ch: char) -> bool {
ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_')
}
pub fn is_document_indicator(ch: char) -> bool {
matches!(ch, '-' | '.')
}
pub fn is_yaml_whitespace(ch: char) -> bool {
matches!(ch, ' ' | '\t')
}
pub fn is_line_break(ch: char) -> bool {
matches!(ch, '\n' | '\r')
}
pub fn is_printable_ascii(ch: char) -> bool {
ch.is_ascii() && !ch.is_ascii_control() || ch == '\t'
}
pub fn is_digit(ch: char) -> bool {
ch.is_ascii_digit()
}
pub fn is_hex_digit(ch: char) -> bool {
ch.is_ascii_hexdigit()
}
pub fn is_octal_digit(ch: char) -> bool {
matches!(ch, '0'..='7')
}
}
pub(super) struct PatternMatcher;
impl PatternMatcher {
pub fn is_document_start(input: &str, pos: usize) -> bool {
let chars: Vec<char> = input.chars().collect();
if pos + 2 >= chars.len() {
return false;
}
chars[pos] == '-'
&& chars[pos + 1] == '-'
&& chars[pos + 2] == '-'
&& (pos + 3 >= chars.len()
|| CharClassifier::is_yaml_whitespace(chars[pos + 3])
|| CharClassifier::is_line_break(chars[pos + 3]))
}
pub fn is_document_end(input: &str, pos: usize) -> bool {
let chars: Vec<char> = input.chars().collect();
if pos + 2 >= chars.len() {
return false;
}
chars[pos] == '.'
&& chars[pos + 1] == '.'
&& chars[pos + 2] == '.'
&& (pos + 3 >= chars.len()
|| CharClassifier::is_yaml_whitespace(chars[pos + 3])
|| CharClassifier::is_line_break(chars[pos + 3]))
}
pub fn is_tag_start(ch: char) -> bool {
ch == '!'
}
pub fn is_anchor_start(ch: char) -> bool {
ch == '&'
}
pub fn is_alias_start(ch: char) -> bool {
ch == '*'
}
pub fn is_comment_start(ch: char) -> bool {
ch == '#'
}
}