parser/trunk_parser
2022-08-22 19:05:06 +01:00
..
src parser: Visitor trait for traversal 2022-08-22 19:05:06 +01:00
Cargo.toml phpast: output as json 2022-07-19 16:56:55 +01:00
README.md parser: README 2022-08-11 14:54:32 +01:00

Trunk Parser

This crate provides a handwritten recursive descent parser targeting versions of PHP >=8.0.

It produces an abstract syntax tree containing Statement and Expression types which can be traversed to analyse code, compile into a lower level language, or reconstructed back into PHP code.

Alongside the regular PHP mode, there will be an additional Trunk compatibility mode that introduces some extra niceties to help support the development of the Trunk project. This will include things such as:

  • Variable type declarations using a familiar var [type] $name syntax.
  • Optionally enforce type declarations where appropriate (class properties, function parameters, return types).

Usage

use trunk_lexer::*;
use trunk_parser::*;

let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(&source_code[..]).unwrap();

let mut parser = Parser::new(None);
let ast = parser.parse(tokens).unwrap();

The resulting ast is a Vec<trunk_parser::Statement> and can easily be iterated or converted into a dedicated iterator type.