sqparse/parser/context.rs
1/// Context information that can be attached to a [`ParseError`].
2///
3/// Essentially, if an error has a context, it means that the parser knows that the error is inside
4/// some specific syntactical construct.
5///
6/// This is intended to hint at the state of the parser to make syntax errors and parser debugging a
7/// bit easier.
8///
9/// Implements [`std::fmt::Display`] to write a useful description of the context.
10///
11/// [`ParseError`]: crate::ParseError
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum ContextType {
14 /// A span of something. This should generally be replaced with a more specific context.
15 Span,
16
17 /// An expression with a value and an operator.
18 ///
19 /// # Example
20 /// ```text
21 /// (1 + 5) * 3--
22 /// ^^^^^^^ ^^^ expression
23 /// ^^^^^^^^^^^^^ expression
24 /// ```
25 Expression,
26
27 /// Literal defining a table.
28 ///
29 /// # Example
30 /// ```text
31 /// local ages = SumAges({ Charlie = 28, Maeve = 23 })
32 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ table literal
33 /// ```
34 TableLiteral,
35
36 /// Literal defining an array.
37 ///
38 /// # Example
39 /// ```text
40 /// local cities = ["Adelaide", "Sydney"].join(", ")
41 /// ^^^^^^^^^^^^^^^^^^^^^^ array literal
42 /// ```
43 ArrayLiteral,
44
45 /// Literal defining a 3D vector.
46 ///
47 /// # Example
48 /// ```text
49 /// player.LookAt(< -5.2, 0.0, 10.0 >)
50 /// ^^^^^^^^^^^^^^^^^^^ vector literal
51 /// ```
52 VectorLiteral,
53
54 /// Literal defining an anonymous function.
55 ///
56 /// # Example
57 /// ```text
58 /// local writeVal = function() { }
59 /// ^^^^^^^^^^^^^^ function literal
60 /// ```
61 FunctionLiteral,
62
63 /// Literal defining an anonymous class.
64 ///
65 /// # Example
66 /// ```text
67 /// local ageManager = class { Ages = ages }
68 /// ^^^^^^^^^^^^^^^^^^^^^ class literal
69 /// ```
70 ClassLiteral,
71
72 /// List of arguments in a function call.
73 ///
74 /// # Example
75 /// ```text
76 /// player.SayTo(friend, "Hello there!")
77 /// ^^^^^^^^^^^^^^^^^^^^^^ call arguments
78 /// ```
79 CallArgumentList,
80
81 /// A statement in a program or block.
82 ///
83 /// # Example
84 /// ```text
85 /// local name = "squirrel"
86 /// ^^^^^^^^^^^^^^^^^^^^^^^ statement
87 /// ```
88 Statement,
89
90 /// A block statement, containing multiple sub-statements.
91 ///
92 /// # Example
93 /// ```text
94 /// local count = 6;
95 /// {
96 /// _^
97 /// | log("hello");
98 /// | }
99 /// |_^ block statement
100 /// log("world");
101 /// ```
102 BlockStatement,
103
104 /// All of an `if` statement.
105 ///
106 /// # Example
107 /// ```text
108 /// local isTimedOut = player.IsTimedOut()
109 /// if (isTimedOut && player.IsAlive()) {
110 /// _^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111 /// | player.Kill()
112 /// | }
113 /// |_^ if statement
114 /// ```text
115 IfStatement,
116
117 /// The condition in an `if` statement.
118 ///
119 /// # Example
120 /// ```text
121 /// if ( IsLoggedIn() ) LogOut()
122 /// ^^^^^^^^^^^^^^^^ if statement condition
123 /// ```
124 IfStatementCondition,
125
126 /// All of a `while` statement.
127 ///
128 /// # Example
129 /// ```text
130 /// while (!IsDay()) {
131 /// _^^^^^^^^^^^^^^^^^^
132 /// | Sleep();
133 /// | }
134 /// |_^ while statement
135 /// WakeUp();
136 /// ```
137 WhileStatement,
138
139 /// The condition of a `while` statement.
140 ///
141 /// # Example
142 /// ```text
143 /// while (!isDone) Continue()
144 /// ^^^^^^^^^ while statement condition
145 /// ```
146 WhileStatementCondition,
147
148 /// All of a `do while` statement.
149 ///
150 /// # Example
151 /// ```text
152 /// do {
153 /// _^^^^
154 /// | Shoot();
155 /// | } while ( IsButtonDown() )
156 /// |_^^^^^^^^^^^^^^^^^^^^^^^^^^ do while statement
157 /// ```
158 DoWhileStatement,
159
160 /// The condition of a `do while` statement.
161 ///
162 /// # Example
163 /// ```text
164 /// do {
165 /// train()
166 /// } while (fitness < 10)
167 /// ^^^^^^^^^^^^^^ do while statement condition
168 ///
169 /// ```
170 DoWhileStatementCondition,
171
172 /// All of a `switch` statement.
173 ///
174 /// # Example
175 /// ```text
176 /// switch (state) {
177 /// _^^^^^^^^^^^^^^^^
178 /// | case "playing": play(); break;
179 /// | cause "paused": pause(); break;
180 /// | }
181 /// |_^ switch statement
182 /// ```
183 SwitchStatement,
184
185 /// The condition of a `switch` statement.
186 ///
187 /// # Example
188 /// ```text
189 /// switch ( GameMode() ) {
190 /// ^^^^^^^^^^^^^^ switch statement condition
191 /// case "slayer":
192 /// return;
193 /// case "creative":
194 /// SetHealth(100);
195 /// break;
196 /// }
197 /// ```
198 SwitchStatementCondition,
199
200 /// All of a `for` statement.
201 ///
202 /// # Example
203 /// ```text
204 /// for (local i = 0; i < 10; i++) {
205 /// _^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
206 /// | println(i)
207 /// | }
208 /// |_^ for statement
209 /// ```
210 ForStatement,
211
212 /// The condition of a `for` statement.
213 ///
214 /// # Example
215 /// ```text
216 /// for (local i = 0; i < players.len; i++) {
217 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ for statement condition
218 /// players[i].say("hello")
219 /// }
220 /// ```
221 ForStatementCondition,
222
223 /// All of a `foreach` statement.
224 ///
225 /// # Example
226 /// ```text
227 /// foreach (i, video in videos) {
228 /// _^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
229 /// | println("playing " + i)
230 /// | play(video)
231 /// | }
232 /// |_^
233 /// ```
234 ForeachStatement,
235
236 /// The condition of a `foreach` statement.
237 ///
238 /// # Example
239 /// ```text
240 /// foreach (Map map in maps) {
241 /// ^^^^^^^^^^^^^^^^^ foreach statement condition
242 /// map.load()
243 /// println("loaded map: " + map.name)
244 /// }
245 /// ```
246 ForeachStatementCondition,
247
248 /// All of a `try catch` statement.
249 ///
250 /// # Example
251 /// ```text
252 /// try {
253 /// _^^^^^
254 /// | FetchData()
255 /// | } catch (error) {
256 /// | println("could not fetch data: " + error)
257 /// | }
258 /// |_^ try catch statement
259 /// ```
260 TryCatchStatement,
261
262 /// The catch binding in a `try catch` statement.
263 ///
264 /// # Example
265 /// ```text
266 /// try {
267 /// money += withdraw(500)
268 /// } catch (error) {
269 /// ^^^^^^^ try catch statement catch name
270 /// println(error)
271 /// }
272 /// ```
273 TryCatchStatementCatchName,
274
275 /// A `return` statement in a function.
276 ///
277 /// # Example
278 /// ```text
279 /// return rnd(0, 10)
280 /// ^^^^^^^^^^^^^^^^^ return statement
281 /// ```
282 ReturnStatement,
283
284 /// A `yield` statement in a generator function.
285 ///
286 /// # Example
287 /// ```text
288 /// foreach (player in players) {
289 /// yield player.health
290 /// ^^^^^^^^^^^^^^^^^^^ yield statement
291 /// }
292 /// ```
293 YieldStatement,
294
295 /// A `throw` statement.
296 ///
297 /// # Example
298 /// ```text
299 /// if ( !request.success ) {
300 /// throw "Request Failed"
301 /// ^^^^^^^^^^^^^^^^^^^^^^ throw statement
302 /// }
303 /// ```
304 ThrowStatement,
305
306 /// A `thread` statement.
307 ///
308 /// # Example
309 /// ```text
310 /// thread PostmatchScreen()
311 /// ^^^^^^^^^^^^^^^^^^^^^^^^ thread statement
312 /// ```
313 ThreadStatement,
314
315 /// A `delaythread` statement.
316 ///
317 /// # Example
318 /// ```text
319 /// delaythread(10) alert("times up!")
320 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ delay thread statement
321 /// ```
322 DelayThreadStatement,
323
324 /// A `waitthread` statement.
325 ///
326 /// # Example
327 /// ```text
328 /// waitthread readAllFiles()
329 /// ^^^^^^^^^^^^^^^^^^^^^^^^^ wait thread statement
330 /// ```
331 WaitThreadStatement,
332
333 /// A `waitthreadsolo` statement.
334 ///
335 /// # Example
336 /// ```text
337 /// waitthreadsolo WaitForPlayerExitButtonPressed( player )
338 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ wait thread solo statement
339 /// ```
340 WaitThreadSoloStatement,
341
342 /// A `wait` statement.
343 ///
344 /// # Example
345 /// ```text
346 /// println("3")
347 /// wait 1
348 /// ^^^^^^ wait statement
349 /// println("2")
350 /// ```
351 WaitStatement,
352
353 /// A `global` statement.
354 ///
355 /// # Example
356 /// ```text
357 /// global MaxThreads = 10
358 /// ^^^^^^^^^^^^^^^^^^^^^^ global statement
359 /// ```
360 GlobalStatement,
361
362 /// Class definition.
363 ///
364 /// # Example
365 /// ```text
366 /// class Person {
367 /// _^^^^^^^^^^^^^^
368 /// | alive = true
369 /// | constructor(name) {
370 /// | this.name <- name
371 /// | }
372 /// | }
373 /// |_^ class definition
374 /// ```
375 ClassDefinition,
376
377 /// Enum definition.
378 ///
379 /// # Example
380 /// ```text
381 /// enum GameMode {
382 /// _^^^^^^^^^^^^^^^
383 /// | SURVIVAL,
384 /// | CREATIVE,
385 /// | ADVENTURE,
386 /// | }
387 /// |_^ enum definition
388 /// ```
389 EnumDefinition,
390
391 /// Function definition.
392 ///
393 /// # Example
394 /// ```text
395 /// void function Damage( entity player ) {
396 /// _^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
397 /// | player.health -= 5
398 /// | }
399 /// |_^ function definition
400 /// Damage( me )
401 /// ```
402 FunctionDefinition,
403
404 /// Constant value definition.
405 ///
406 /// # Example
407 /// ```text
408 /// const MaxHealth = 10;
409 /// ^^^^^^^^^^^^^^^^^^^^^ const definition
410 /// player.health = MaxHealth
411 /// ```
412 ConstDefinition,
413
414 /// Variable definition.
415 ///
416 /// # Example
417 /// ```text
418 /// local cities = ["Adelaide", "Sydney"]
419 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ variable definition
420 /// ```
421 VarDefinition,
422
423 /// Struct definition.
424 ///
425 /// # Example
426 /// ```text
427 /// struct Waypoint {
428 /// _^^^^^^^^^^^^^^^^^
429 /// | float x,
430 /// | float y,
431 /// | string name
432 /// | }
433 /// |_^ struct definition
434 /// ```
435 StructDefinition,
436
437 /// Type definition.
438 ///
439 /// # Example
440 /// ```text
441 /// typedef PlayerMap table<string, player>
442 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type definition
443 /// ```
444 TypeDefinition,
445
446 /// Property in a class, table, or struct.
447 ///
448 /// # Example
449 /// ```text
450 /// class Player {
451 /// isPlaying = true
452 /// ^^^^^^^^^^^^^^^^ class property
453 /// }
454 /// ```
455 Property,
456
457 /// Constructor in a class or table.
458 ///
459 /// # Example
460 /// ```text
461 /// class FunMachine() {
462 /// constructor() {
463 /// _____^^^^^^^^^^^^^^^
464 /// | this.funLevel <- 11
465 /// | }
466 /// |_____^ class constructor
467 /// }
468 /// ```
469 Constructor,
470
471 /// Method in a class or table.
472 ///
473 /// # Example
474 /// ```text
475 /// class FooCalculator {
476 /// int function calculateFoo() {
477 /// _____^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
478 /// | return 4
479 /// | }
480 /// | }
481 /// |_^ class method
482 /// ```
483 Method,
484
485 /// Function definition's parameter list.
486 ///
487 /// # Example
488 /// ```text
489 /// function foobar(int a, string b = "no", ...) {
490 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function param list
491 /// }
492 /// ```
493 FunctionParamList,
494
495 /// Function environment definition.
496 ///
497 /// # Example
498 /// ```text
499 /// local person = { age = 32 }
500 /// function printPersonDetails[person]() {
501 /// ^^^^^^^^ function environment
502 /// println(this.age)
503 /// }
504 /// ```
505 FunctionEnvironment,
506
507 /// Function free variable/capture list.
508 ///
509 /// # Example
510 /// ```text
511 /// filterFunc = function(val) : (max) {
512 /// ^^^^^ function capture list
513 /// return val <= max
514 /// }
515 /// ```
516 FunctionCaptureList,
517
518 /// A type with an optional number of type modifiers.
519 ///
520 /// # Example
521 /// ```text
522 /// table<string, person&> ornull
523 /// ^^^^^ ^^^^^^ ^^^^^^ type
524 /// ^^^^^^^ type
525 /// ^^^^^^^^^^^^^^^^^^^^^^ type
526 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type
527 /// ```
528 Type,
529
530 /// List of arguments in a generic type.
531 ///
532 /// # Example
533 /// ```text
534 /// table<string, country>
535 /// ^^^^^^^^^^^^^^^^^ generic argument list
536 /// ```
537 GenericArgumentList,
538}
539
540impl std::fmt::Display for ContextType {
541 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
542 match self {
543 ContextType::Span => write!(f, "span"),
544 ContextType::Expression => write!(f, "expression"),
545 ContextType::TableLiteral => write!(f, "table literal"),
546 ContextType::ArrayLiteral => write!(f, "array literal"),
547 ContextType::VectorLiteral => write!(f, "vector literal"),
548 ContextType::FunctionLiteral => write!(f, "function literal"),
549 ContextType::ClassLiteral => write!(f, "class literal"),
550 ContextType::CallArgumentList => write!(f, "argument list"),
551 ContextType::Statement => write!(f, "statement"),
552 ContextType::BlockStatement => write!(f, "block statement"),
553 ContextType::IfStatement => write!(f, "`if` statement"),
554 ContextType::IfStatementCondition => write!(f, "`if` statement condition"),
555 ContextType::WhileStatement => write!(f, "`while` loop"),
556 ContextType::WhileStatementCondition => write!(f, "`while` loop condition"),
557 ContextType::DoWhileStatement => write!(f, "`do while` loop"),
558 ContextType::DoWhileStatementCondition => write!(f, "`do while` loop condition"),
559 ContextType::SwitchStatement => write!(f, "`switch` statement"),
560 ContextType::SwitchStatementCondition => write!(f, "`switch` statement condition"),
561 ContextType::ForStatement => write!(f, "`for` loop"),
562 ContextType::ForStatementCondition => write!(f, "`for` loop condition"),
563 ContextType::ForeachStatement => write!(f, "`foreach` loop"),
564 ContextType::ForeachStatementCondition => write!(f, "`foreach` loop condition"),
565 ContextType::TryCatchStatement => write!(f, "`try` statement"),
566 ContextType::TryCatchStatementCatchName => write!(f, "`try catch` name"),
567 ContextType::ReturnStatement => write!(f, "`return` statement"),
568 ContextType::YieldStatement => write!(f, "`yield` statement"),
569 ContextType::ThrowStatement => write!(f, "`throw` statement"),
570 ContextType::ThreadStatement => write!(f, "`thread` statement"),
571 ContextType::DelayThreadStatement => write!(f, "`delaythread` statement"),
572 ContextType::WaitThreadStatement => write!(f, "`waitthread` statement"),
573 ContextType::WaitThreadSoloStatement => write!(f, "`waitthreadsolo` statement"),
574 ContextType::WaitStatement => write!(f, "`wait` statement"),
575 ContextType::GlobalStatement => write!(f, "`global` statement"),
576 ContextType::ClassDefinition => write!(f, "`class` definition"),
577 ContextType::EnumDefinition => write!(f, "`enum` definition"),
578 ContextType::FunctionDefinition => write!(f, "`function` definition"),
579 ContextType::ConstDefinition => write!(f, "`const` definition"),
580 ContextType::VarDefinition => write!(f, "variable definition"),
581 ContextType::StructDefinition => write!(f, "`struct` definition"),
582 ContextType::TypeDefinition => write!(f, "type definition"),
583 ContextType::Property => write!(f, "property"),
584 ContextType::Constructor => write!(f, "constructor"),
585 ContextType::Method => write!(f, "method"),
586 ContextType::FunctionParamList => write!(f, "parameter list"),
587 ContextType::FunctionEnvironment => write!(f, "function environment"),
588 ContextType::FunctionCaptureList => write!(f, "function capture list"),
589 ContextType::Type => write!(f, "type"),
590 ContextType::GenericArgumentList => write!(f, "generic type list"),
591 }
592 }
593}