Skip to main content

luaparse_rs/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4extern crate alloc;
5use alloc::{string::String, vec::Vec};
6use thiserror::Error;
7
8/// The syntax tree types: statements, expressions, types, and the visitor.
9pub mod ast;
10/// The parser that turns source code into an [`Ast`].
11pub mod parser;
12/// The tokenizer that breaks source text into tokens.
13pub mod lexer;
14/// Lua version markers ([`Luau`], [`Lua51`], [`Lua52`], [`Lua53`], [`Lua54`]).
15pub mod marker;
16
17pub use ast::{Ast, AstWithTypes};
18pub use lexer::InterpolationPart;
19
20pub use marker::{Lua51, Lua52, Lua53, Lua54, LuaVersion, Luau};
21pub use parser::Parser;
22
23/// A byte range in the source code (`start..end`).
24///
25/// Every node in the syntax tree carries a span so you can trace it back to
26/// a position in the original source string.
27pub type Span = core::ops::Range<usize>;
28
29/// Something went wrong while parsing.
30///
31/// Each variant includes a [`Span`] pointing to where the problem is in the source.
32#[derive(Debug, Error, Clone)]
33pub enum ParseError {
34    /// Found a token the parser wasn't expecting.
35    #[error("unexpected token: expected {expected:?}, found {found}")]
36    UnexpectedToken {
37        /// What the parser was looking for.
38        expected: Vec<String>,
39        /// What it actually found.
40        found: String,
41        /// Where in the source.
42        span: Span,
43    },
44    /// The source ended before the parser finished.
45    #[error("unexpected end of file: expected {expected:?}")]
46    UnexpectedEof {
47        /// What the parser still needed.
48        expected: Vec<String>,
49        /// Points to the end of the source.
50        span: Span,
51    },
52    /// The code is structurally wrong.
53    #[error("{message}")]
54    InvalidSyntax {
55        /// What went wrong.
56        message: String,
57        /// Where in the source.
58        span: Span,
59        /// An optional suggestion for how to fix it.
60        help: Option<String>,
61    },
62    /// A feature was used that doesn't exist in the chosen Lua version.
63    ///
64    /// For example, using `continue` when parsing as Lua 5.1.
65    #[error("feature '{feature}' not supported in {version}")]
66    UnsupportedFeature {
67        /// Name of the unsupported feature.
68        feature: String,
69        /// Which Lua version was selected.
70        version: String,
71        /// Where in the source.
72        span: Span,
73    },
74}
75
76/// Something went wrong during lexing (tokenization).
77///
78/// These errors happen before parsing even starts. The source text couldn't
79/// be broken into valid tokens.
80#[derive(Debug, Error, Clone)]
81pub enum LexError {
82    /// A string literal was opened but never closed.
83    #[error("unterminated string literal")]
84    UnterminatedString { span: Span },
85    /// A number literal couldn't be understood.
86    #[error("invalid number literal")]
87    InvalidNumber { span: Span },
88    /// A block comment (`--[[ ... ]]`) was opened but never closed.
89    #[error("unterminated comment")]
90    UnterminatedComment { span: Span },
91    /// A string contains a backslash sequence that isn't valid.
92    #[error("invalid escape sequence: \\{escape}")]
93    InvalidEscape { escape: char, span: Span },
94}
95
96#[cfg(feature = "wasm")]
97pub mod wasm;