use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TokenType {
Variable,
Number,
StringSq,
StringDq,
HereStringSq,
HereStringDq,
Generic,
Keyword,
Parameter,
Operator,
Pipe,
Amp,
Semicolon,
Comma,
Dot,
DoubleColon,
LParen,
RParen,
LBrace,
RBrace,
LBracket,
RBracket,
DollarParen,
AtParen,
AtBrace,
Redirect,
Newline,
Comment,
Eof,
Unknown,
}
impl fmt::Display for TokenType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub const NAMED_OPERATORS: &[&str] = &[
"eq",
"ne",
"gt",
"ge",
"lt",
"le",
"like",
"notlike",
"match",
"notmatch",
"replace",
"contains",
"notcontains",
"in",
"notin",
"is",
"isnot",
"as",
"and",
"or",
"xor",
"not",
"band",
"bor",
"bxor",
"bnot",
"shl",
"shr",
"join",
"split",
"f",
"ceq",
"cne",
"cgt",
"cge",
"clt",
"cle",
"clike",
"cnotlike",
"cmatch",
"cnotmatch",
"creplace",
"ccontains",
"cnotcontains",
"ieq",
"ine",
"igt",
"ige",
"ilt",
"ile",
"ilike",
"inotlike",
"imatch",
"inotmatch",
"ireplace",
];
pub const KEYWORDS: &[&str] = &[
"if",
"elseif",
"else",
"switch",
"while",
"for",
"foreach",
"do",
"until",
"break",
"continue",
"function",
"filter",
"workflow",
"return",
"throw",
"trap",
"try",
"catch",
"finally",
"param",
"begin",
"process",
"end",
"dynamicparam",
"data",
"class",
"enum",
"using",
"in",
"exit",
];
#[derive(Debug, Clone)]
pub struct Token {
pub ty: TokenType,
pub value: String,
pub line: u32,
pub col: u32,
pub pos: usize,
pub text: Option<String>,
pub scope: Option<String>,
pub splat: bool,
}
impl Token {
pub fn new(ty: TokenType, value: impl Into<String>, line: u32, col: u32, pos: usize) -> Self {
Token {
ty,
value: value.into(),
line,
col,
pos,
text: None,
scope: None,
splat: false,
}
}
pub fn text_or_value(&self) -> &str {
self.text.as_deref().unwrap_or(&self.value)
}
}