Skip to main content

treesitter_types_php/
lib.rs

1//! Strongly-typed AST types for PHP, auto-generated from
2//! [`tree-sitter-php`](https://docs.rs/tree-sitter-php)'s `node-types.json`.
3//!
4//! This crate is generated by [`treesitter-types`](https://docs.rs/treesitter-types) and is
5//! automatically kept up to date when a new version of the grammar crate is released.
6//!
7//! These types have been tested by parsing the
8//! [Laravel](https://github.com/laravel/framework) source code.
9//!
10//! See the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) project for more
11//! information about the underlying parser framework.
12//!
13//! # Example
14//!
15//! ```
16//! use treesitter_types_php::*;
17//!
18//! // A minimal PHP hello-world program.
19//! let src = b"\
20//! <?php
21//!
22//! function greet($name) {
23//!     echo \"Hello, $name!\";
24//! }
25//!
26//! greet(\"World\");
27//! ";
28//!
29//! // Parse the source with tree-sitter and convert into typed AST.
30//! let mut parser = tree_sitter::Parser::new();
31//! parser.set_language(&tree_sitter_php::LANGUAGE_PHP.into()).unwrap();
32//! let tree = parser.parse(src, None).unwrap();
33//! let program = Program::from_node(tree.root_node(), src).unwrap();
34//!
35//! // First child is the `<?php` tag, then our statements.
36//! assert!(program.children.len() >= 3);
37//!
38//! // The function definition — `function greet($name) { ... }`.
39//! let ProgramChildren::Statement(stmt) = &program.children[1] else {
40//!     panic!("expected a statement");
41//! };
42//! let Statement::FunctionDefinition(func) = stmt.as_ref() else {
43//!     panic!("expected a function definition");
44//! };
45//! assert_eq!(func.name.text(), "greet");
46//! assert_eq!(func.parameters.children.len(), 1); // one parameter: `$name`
47//! assert!(func.return_type.is_none());            // no return type declaration
48//! ```
49
50pub use tree_sitter_php;
51pub use treesitter_types::tree_sitter;
52pub use treesitter_types::{FromNode, LeafNode, ParseError, Span, Spanned};
53
54mod generated;
55pub use generated::*;