#php #line-column #parser #token #representation #generate #offset

backyard-lexer

Generating tokens representation of PHP code

8 releases

0.1.10 Dec 17, 2024
0.1.9 Dec 12, 2024
0.1.5 Nov 29, 2024

#1603 in Development tools

Download history 287/week @ 2024-11-23 85/week @ 2024-11-30 568/week @ 2024-12-07 201/week @ 2024-12-14 10/week @ 2024-12-21

781 downloads per month
Used in 2 crates (via backyard-parser)

MIT license

53KB
1.5K SLoC

backyard-lexer

Generating tokens representation of PHP code.

features

  • Parse string to tokens (lex() & lex_eval())

usage

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

  let tokens = backyard_lexer::lex(&arena, code);
  println!("{:?}", tokens);
}

Resulting this:

Ok([
  Token { token_type: Function, value: "function", line: 2, column: 2, offset: 8 },
  Token { token_type: UnqualifiedName, value: "hello_world", line: 2, column: 11, offset: 17 },
  Token { token_type: LeftParenthesis, value: "(", line: 2, column: 22, offset: 28 },
  Token { token_type: Variable, value: "foo", line: 2, column: 23, offset: 29 },
  Token { token_type: RightParenthesis, value: ")", line: 2, column: 27, offset: 33 },
  Token { token_type: LeftCurlyBracket, value: "{", line: 2, column: 29, offset: 35 },
  Token { token_type: UnqualifiedName, value: "var_dump", line: 3, column: 4, offset: 41 },
  Token { token_type: LeftParenthesis, value: "(", line: 3, column: 12, offset: 49 },
  Token { token_type: Variable, value: "foo", line: 3, column: 13, offset: 50 },
  Token { token_type: RightParenthesis, value: ")", line: 3, column: 17, offset: 54 },
  Token { token_type: Semicolon, value: ";", line: 3, column: 18, offset: 55 },
  Token { token_type: RightCurlyBracket, value: "}", line: 4, column: 2, offset: 59 }
])

ecosystem

heavily inspired by

license

MIT

Dependencies

~1–1.8MB
~32K SLoC