luaparse_rs/ast/mod.rs
1//! All the types that make up a parsed Lua syntax tree.
2//!
3//! The main entry point is [`Ast`], which holds the top level block of statements
4//! and any comments found in the source. For Luau code with type annotations,
5//! use [`AstWithTypes`] instead.
6
7use alloc::vec::Vec;
8
9/// Shared building blocks: identifiers, blocks, comments, parameters.
10pub mod common;
11/// Expression nodes (`1 + 2`, `foo()`, `if/then/else`, etc.).
12pub mod expr;
13/// Statement nodes (`local`, `if`, `while`, `return`, etc.).
14pub mod stmt;
15/// Luau type annotation nodes (`string`, `number?`, `{x: number}`, etc.).
16pub mod types;
17/// Tree traversal with the [`Visitor`](visitor::Visitor) and [`VisitorMut`](visitor::VisitorMut) traits.
18pub mod visitor;
19
20pub use common::*;
21pub use expr::*;
22pub use stmt::*;
23pub use types::*;
24
25/// A complete parsed Lua program.
26///
27/// Contains the top level block of statements and every comment found in the
28/// source. You get this from [`Parser::parse`](crate::Parser::parse).
29#[derive(Debug, Clone, PartialEq)]
30pub struct Ast {
31 /// The top level block containing all statements in the program.
32 pub block: Block,
33 /// Every comment found in the source, in order of appearance.
34 pub comments: Vec<Comment>,
35}
36
37impl Ast {
38 pub fn new(block: Block, comments: Vec<Comment>) -> Self {
39 Self { block, comments }
40 }
41}
42
43/// A parsed Luau program with type declarations pulled out separately.
44///
45/// You get this from [`Parser::parse_with_types`](crate::Parser::parse_with_types).
46/// The `type Foo = ...` and `export type Bar = ...` declarations live in
47/// `type_declarations` instead of being mixed into the statement list.
48#[derive(Debug, Clone, PartialEq)]
49pub struct AstWithTypes {
50 /// The syntax tree (statements and comments).
51 pub ast: Ast,
52 /// All `type` and `export type` declarations, with their full type expressions resolved.
53 pub type_declarations: Vec<TypeDeclarationFull>,
54}
55
56impl AstWithTypes {
57 pub fn new(ast: Ast, type_declarations: Vec<TypeDeclarationFull>) -> Self {
58 Self { ast, type_declarations }
59 }
60}