eventql_parser/token.rs
1//! Token types for lexical analysis.
2//!
3//! This module defines the types used during tokenization (lexical analysis)
4//! of EventQL queries. Tokens are the atomic units produced by the lexer
5//! and consumed by the parser.
6//!
7//! # Core Types
8//!
9//! - [`Token`] - A token with position information
10//! - [`Sym`] - The symbol/category of a token
11//! - [`Operator`] - Arithmetic, comparison, and logical operators
12//! - [`Symbol`] - Structural symbols (parentheses, brackets, etc.)
13
14use nom_locate::LocatedSpan;
15use serde::{Deserialize, Serialize};
16use std::fmt::{Display, Formatter};
17
18/// Symbol type representing the category and value of a token.
19///
20/// This enum encompasses all the different types of tokens that can appear
21/// in an EventQL query, from keywords and operators to literals and symbols.
22#[derive(Clone, Debug, Copy, Serialize)]
23pub enum Sym<'a> {
24 /// Identifier (variable names, keywords not yet classified)
25 Id(&'a str),
26 /// String literal
27 String(&'a str),
28 /// Numeric literal
29 Number(f64),
30 /// Keyword (FROM, WHERE, etc.)
31 Keyword(&'a str),
32 /// Operator (+, -, ==, AND, etc.)
33 Operator(Operator),
34 /// Structural symbol (parentheses, brackets, etc.)
35 Symbol(Symbol),
36 /// End of file marker
37 Eof,
38}
39
40impl Display for Sym<'_> {
41 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42 match self {
43 Sym::Id(id) => write!(f, "{}", id),
44 Sym::String(s) => write!(f, "\"{}\"", s),
45 Sym::Number(n) => write!(f, "{}", n),
46 Sym::Keyword(kw) => write!(f, "{}", kw.to_uppercase()),
47 Sym::Operator(op) => write!(f, "{}", op),
48 Sym::Symbol(sym) => write!(f, "{}", sym),
49 Sym::Eof => write!(f, "<eof>"),
50 }
51 }
52}
53
54/// Operator types for expressions.
55///
56/// This enum represents all the operators supported in EventQL, including
57/// arithmetic, comparison, and logical operators.
58///
59/// # Operator Precedence
60///
61/// From highest to lowest precedence:
62/// 1. Unary: `+`, `-`, `NOT`
63/// 2. Multiplicative: `*`, `/`
64/// 3. Additive: `+`, `-`
65/// 4. Comparison: `<`, `<=`, `>`, `>=`, `==`, `!=`
66/// 5. Logical: `AND`, `OR`, `XOR`
67#[derive(Clone, Debug, Copy, Serialize)]
68pub enum Operator {
69 /// Addition operator `+`
70 Add,
71 /// Subtraction operator `-`
72 Sub,
73 /// Multiplication operator `*`
74 Mul,
75 /// Division operator `/`
76 Div,
77 /// Equality operator `==`
78 Eq,
79 /// Inequality operator `!=`
80 Neq,
81 /// Less than operator `<`
82 Lt,
83 /// Less than or equal operator `<=`
84 Lte,
85 /// Greater than operator `>`
86 Gt,
87 /// Greater than or equal operator `>=`
88 Gte,
89 /// Logical AND operator
90 And,
91 /// Logical OR operator
92 Or,
93 /// Logical XOR operator
94 Xor,
95 /// Logical NOT operator
96 Not,
97}
98
99impl Display for Operator {
100 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
101 match self {
102 Operator::Add => write!(f, "+"),
103 Operator::Sub => write!(f, "-"),
104 Operator::Mul => write!(f, "*"),
105 Operator::Div => write!(f, "/"),
106 Operator::Eq => write!(f, "=="),
107 Operator::Neq => write!(f, "!="),
108 Operator::Lt => write!(f, "<"),
109 Operator::Lte => write!(f, "<="),
110 Operator::Gt => write!(f, ">"),
111 Operator::Gte => write!(f, ">="),
112 Operator::And => write!(f, "AND"),
113 Operator::Or => write!(f, "OR"),
114 Operator::Xor => write!(f, "XOR"),
115 Operator::Not => write!(f, "NOT"),
116 }
117 }
118}
119
120/// Structural symbols used in EventQL syntax.
121///
122/// These symbols define the structure of expressions and queries,
123/// such as parentheses for grouping, brackets for arrays, and braces for records.
124#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
125pub enum Symbol {
126 /// Opening parenthesis `(`
127 OpenParen,
128 /// Closing parenthesis `)`
129 CloseParen,
130 /// Dot `.` (for field access)
131 Dot,
132 /// Comma `,` (for separating list items)
133 Comma,
134 /// Colon `:` (for record field definitions)
135 Colon,
136 /// Opening bracket `[`
137 OpenBracket,
138 /// Closing bracket `]`
139 CloseBracket,
140 /// Opening brace `{`
141 OpenBrace,
142 /// Closing brace `}`
143 CloseBrace,
144}
145
146impl Display for Symbol {
147 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
148 match self {
149 Symbol::OpenParen => write!(f, "("),
150 Symbol::CloseParen => write!(f, ")"),
151 Symbol::Dot => write!(f, "."),
152 Symbol::Comma => write!(f, ","),
153 Symbol::Colon => write!(f, ":"),
154 Symbol::OpenBracket => write!(f, "["),
155 Symbol::CloseBracket => write!(f, "]"),
156 Symbol::OpenBrace => write!(f, "{{"),
157 Symbol::CloseBrace => write!(f, "}}"),
158 }
159 }
160}
161
162/// Type alias for text with position tracking.
163///
164/// Used internally by the lexer for tracking positions during tokenization.
165pub type Text<'a> = LocatedSpan<&'a str>;
166
167/// A token with position information.
168///
169/// Tokens are the output of lexical analysis and the input to syntactic analysis.
170/// Each token contains a symbol (its type and value) along with its position
171/// in the source code.
172#[derive(Debug, Clone, Copy, Serialize)]
173pub struct Token<'a> {
174 /// The symbol (type and value) of this token
175 pub sym: Sym<'a>,
176 /// Line number where this token appears (1-indexed)
177 pub line: u32,
178 /// Column number where this token appears (1-indexed)
179 pub col: u32,
180}