Crate analisar[][src]

Analisar

A Lua parser for Rust

Usage

This crate provides 3 different APIs for parsing lua.

Parser

The first is a fairly standard parser over a fairly standard AST which provides no context at all about whitespace or the position of punctuation or keywords. The provided AST is designed to represent the intent of the program over anything else

This kind of parser could be used to build a tree walking interpreter. Here is an example:

use analisar::{Parser, ast::{Statement, Expression}};
 
let lua = "print('hello world')";
let mut p = Parser::new(lua.as_bytes());
let stmt = p.next().unwrap().unwrap();
assert!(matches!(stmt, Statement::Expression(Expression::FuncCall(_))));

TokenBufferParser

This is one is a bit of a hybrid of the other two parsers provided by this crate. It provides both a tree of Statement/Expressions but also the raw tokens represented by a given Statement.

Here is an example of this kind of parser:

use analisar::{TokenBufferParser, ast::{Statement, Expression}};
 
let lua = "
-- print out hello world to stdout
print(--[[ why!!! ]]'hello world' --[[seriously???]])";
let mut p = TokenBufferParser::new(lua.as_bytes());
let (tokens, stmt) = p.next().unwrap().unwrap();
assert_eq!(tokens.len(), 7);
assert!(matches!(stmt, Statement::Expression(Expression::FuncCall(_))));

As you can see the output of the Statement::Expression is exactly the same as before, however there is also a Vec of tokens provided.

aware::Parser

The final parser that this crate provides is a fully context aware parser. Let's look at an example of that the output of this one looks like.

use analisar::aware::{Parser, ast::{Statement, Expression}};
 
let lua = "print(('hello world')) -- print the string 'hello world' to stdout";
let mut p = Parser::new(lua.as_bytes());
let stmt = p.next().unwrap().unwrap();
assert!(matches!(stmt.statement, Statement::Expression(Expression::FuncCall(_))));

Notice this one looks quite a bit different from the other two. First of all the function call's name has an associated Span which represents to byte offsets for the token in the original string, you'll notice similar spans across each entry in this tree. Another thing it provides is a Parened expression, for representing when an expression has been put in parentheses. Finally we see the comments that apply to this statement are also provided. With all 3 of these additions it would be possible to fully reconstruct the tokens into the order they appeared originally, which would be handy it you were building a code formatter or a document generator.

Re-exports

pub use error::Error;

Modules

ast
aware

A module for whitespace & comment aware parsing

error

Structs

Parser

This parser will provide an AST without any whitespace or comment context provided.

TokenBufferParser

Similar to the Parser, on a call to next will return a vector of the raw tokens from lex_lua along with the parsed Statement