#php #ast-node #parser #builder #walker

backyard-nodes

Nodes representing PHP code AST

7 releases

new 0.1.9 Dec 12, 2024
0.1.8 Dec 9, 2024
0.1.5 Nov 29, 2024

#5 in #walker

Download history 360/week @ 2024-11-27 347/week @ 2024-12-04

707 downloads per month
Used in 2 crates

MIT license

81KB
1.5K SLoC

backyard-nodes

Nodes representing PHP code AST, with simple builder and walker.

features

  • Ast Nodes
  • "builder" simplify building AST nodes
  • "walker" simple walker through AST nodes

usage

builder

This builder is behind the builder feature.

use backyard_nodes::builder::{ Builder, BlueprintBuildable };
fn main() {
  let arena = bumpalo::Bump::new();
  let b = Builder::new();
  let node = b
    .Program(&[b.Assignment(b.Variable(b.Identifier("a")), "=", b.Number("21"))])
    .build(&arena);
  println!("{node:?}");
}

Resulting this:

Node {
  node_type: Program,
  wrapper: Program(ProgramNode {
    children: [
      Node {
        node_type: Assignment,
        wrapper: Assignment(AssignmentNode {
          left: Node {
            node_type: Variable,
            wrapper: Variable(VariableNode {
              name: Node {
                node_type: Identifier,
                wrapper: Identifier(IdentifierNode { name: "a" }),
                loc: None, leadings: None, trailings: None
              }
            }),
            loc: None, leadings: None, trailings: None
          },
          operator: "=",
          right: Node {
            node_type: Number,
            wrapper: Number(NumberNode { value: "21" }),
            loc: None, leadings: None, trailings: None
          }
        }),
        loc: None, leadings: None, trailings: None
      }]
    }),
  loc: None, leadings: None, trailings: None
}

walker

This builder is behind the walker feature.

use backyard_nodes::{ builder::{ BlueprintBuildable, Builder }, walker::Walker, NodeType };

fn main() {
  let arena = bumpalo::Bump::new();
  let b = Builder::new();
  let node = b
    .Program(&[b.Assignment(b.Variable(b.Identifier("a")), "=", b.Number("21"))])
    .build(&arena);
  let mut walker = Walker::new(&*node).into_iter();

  assert_eq!(NodeType::Program, walker.next().unwrap().node_type);
  assert_eq!(NodeType::Assignment, walker.next().unwrap().node_type);
  assert_eq!(NodeType::Variable, walker.next().unwrap().node_type);
  assert_eq!(NodeType::Identifier, walker.next().unwrap().node_type);
  assert_eq!(NodeType::Number, walker.next().unwrap().node_type);
  assert!(walker.next().is_none());
}

ecosystem

heavily inspired by

license

MIT

Dependencies

~1.2–2.2MB
~43K SLoC