6 releases

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

#311 in Programming languages

Download history 257/week @ 2024-11-27 331/week @ 2024-12-04

588 downloads per month
Used in backyard-generator

MIT license

325KB
9K SLoC

backyard-parser

Parse PHP code to AST node.

features

  • Parse string to AST (parse() & parse_eval())

usage

fn main() {
  let arena = bumpalo::Bump::new();
  let code = r#"<?php
  function hello_world($foo) {
    var_dump($foo);
  }"#;

  let parsed = backyard_parse::parse(&arena, code);
  println!("{:?}", parsed);
}

Resulting this:

Ok(Node {
  leadings: [],
  trailings: [],
  node_type: Program,
  node: Program(ProgramNode {
    children: [
      Node {
        leadings: [],
        trailings: [],
        node_type: Function,
        node: Function(FunctionNode {
          is_ref: false,
          name: Node {
            leadings: [],
            trailings: [],
            node_type: Identifier,
            node: Identifier(IdentifierNode { name: "hello_world" }),
            loc: Some(RangeLocation {
              start: Location {
                line: 2,
                column: 13,
                offset: 19
              },
              end: Location {
                line: 2,
                column: 24,
                offset: 30
              }
            })
          },
          parameters: [
            Node {
              leadings: [],
              trailings: [],
              node_type: Parameter,
              node: Parameter(ParameterNode {
                variable_type: None,
                is_ref: false,
                is_ellipsis: false,
                name: Node {
                  leadings: [],
                  trailings: [],
                  node_type: Identifier,
                  node: Identifier(IdentifierNode { name: "foo" }),
                  loc: Some(RangeLocation {
                    start: Location {
                      line: 2,
                      column: 25,
                      offset: 31
                    },
                    end: Location {
                      line: 2,
                      column: 28,
                      offset: 34
                    }
                  })
                },
                value: None
              }),
              loc: Some(RangeLocation {
                start: Location {
                  line: 2,
                  column: 25,
                  offset: 31
                },
                end: Location {
                  line: 2,
                  column: 25,
                  offset: 31
                }
              })
            }
          ],
          return_type: None,
          body: Some(Node {
            leadings: [],
            trailings: [],
            node_type: Block,
            node: Block(BlockNode {
              statements: [
                Node {
                  leadings: [],
                  trailings: [],
                  node_type: Call,
                  node: Call(CallNode {
                    name: Node {
                      leadings: [],
                      trailings: [],
                      node_type: Identifier,
                      node: Identifier(IdentifierNode { name: "var_dump" }),
                      loc: Some(RangeLocation {
                        start: Location {
                          line: 3,
                          column: 6,
                          offset: 45
                        },
                        end: Location {
                          line: 3,
                          column: 14,
                          offset: 53
                        }
                      })
                    },
                    arguments: [
                      Node {
                        leadings: [],
                        trailings: [],
                        node_type: CallArgument,
                        node: CallArgument(CallArgumentNode {
                          name: None,
                          value: Node {
                            leadings: [],
                            trailings: [],
                            node_type: Variable,
                            node: Variable(VariableNode {
                              name: Node {
                                leadings: [],
                                trailings: [],
                                node_type: Identifier,
                                node: Identifier(IdentifierNode { name: "foo" }),
                                loc: Some(RangeLocation {
                                  start: Location {
                                    line: 3,
                                    column: 15,
                                    offset: 54
                                  },
                                  end: Location {
                                    line: 3,
                                    column: 18,
                                    offset: 57
                                  }
                                })
                              }
                            }),
                            loc: Some(RangeLocation {
                              start: Location {
                                line: 3,
                                column: 15,
                                offset: 54
                              },
                              end: Location {
                                line: 3,
                                column: 18,
                                offset: 57
                              }
                            })
                          }
                        }),
                        loc: Some(RangeLocation {
                          start: Location {
                            line: 3,
                            column: 15,
                            offset: 54
                          },
                          end: Location {
                            line: 3,
                            column: 15,
                            offset: 54
                          }
                        })
                      }
                    ]
                  }),
                  loc: Some(RangeLocation {
                    start: Location {
                      line: 3,
                      column: 14,
                      offset: 53
                    },
                    end: Location {
                      line: 3,
                      column: 19,
                      offset: 58
                    }
                  })
                }
              ]
            }),
            loc: Some(RangeLocation {
              start: Location {
                line: 2,
                column: 31,
                offset: 37
              },
              end: Location {
                line: 4,
                column: 4,
                offset: 65
              }
            })
          })
        }),
        loc: Some(RangeLocation {
          start: Location {
            line: 2,
            column: 4,
            offset: 10
          },
          end: Location {
            line: 4,
            column: 4,
            offset: 65
          }
        })
      }
    ]
  }),
  loc: Some(RangeLocation {
    start: Location {
      line: 1,
      column: 0,
      offset: 0
    },
    end: Location {
      line: 4,
      column: 4,
      offset: 65
    }
  })
})

ecosystem

heavily inspired by

license

MIT

Dependencies

~1.5–2.6MB
~52K SLoC