Lua.Parser (Lua v1.0.0-rc.1)
View SourceHand-written recursive descent parser for Lua 5.3.
Uses Pratt parsing for operator precedence in expressions.
Summary
Functions
Parses Lua source code into an AST.
Parses a chunk (top-level block) from a token list.
Parses an expression with minimum precedence.
Parses Lua source code and returns raw error information.
Types
@type token() :: Lua.Lexer.token()
Functions
@spec parse(String.t()) :: {:ok, Lua.AST.Chunk.t()} | {:error, String.t()}
Parses Lua source code into an AST.
Returns {:ok, chunk} on success or {:error, formatted_error} on failure.
The error is a beautifully formatted string with context and suggestions.
Examples
iex> Lua.Parser.parse("local x = 42")
{:ok, %Lua.AST.Chunk{...}}
iex> {:error, error_msg} = Lua.Parser.parse("if x then")
iex> String.contains?(error_msg, "Parse Error")
true
@spec parse_chunk([token()]) :: {:ok, Lua.AST.Chunk.t()} | {:error, term()}
Parses a chunk (top-level block) from a token list.
@spec parse_expr([token()], non_neg_integer()) :: parse_result(Lua.AST.Expr.t())
Parses an expression with minimum precedence.
@spec parse_raw(String.t()) :: {:ok, Lua.AST.Chunk.t()} | {:error, term()}
Parses Lua source code and returns raw error information.
Use this when you want to handle errors programmatically instead of displaying them to users.