backyard-lexer 0.1.3

Generating tokens representation of PHP code.
Documentation

backyard-lexer

Generating tokens representation of PHP code.

features

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

usage

import { lex } from "@alzera/backyard";

const code = `<?php
function hello_world($foo) {
  var_dump($foo);
}`;

const tokens = lex(code);
console.log(JSON.stringify(tokens, null, 2));

Resulting this json:

[
  {
    "token_type": "Function",
    "value": "function",
    "line": 2,
    "column": 0,
    "offset": 6
  },
  {
    "token_type": "Identifier",
    "value": "hello_world",
    "line": 2,
    "column": 9,
    "offset": 15
  },
  {
    "token_type": "LeftParenthesis",
    "value": "(",
    "line": 2,
    "column": 20,
    "offset": 26
  },
  {
    "token_type": "Variable",
    "value": "foo",
    "line": 2,
    "column": 21,
    "offset": 27
  },
  {
    "token_type": "RightParenthesis",
    "value": ")",
    "line": 2,
    "column": 25,
    "offset": 31
  },
  {
    "token_type": "LeftCurlyBracket",
    "value": "{",
    "line": 2,
    "column": 27,
    "offset": 33
  },
  {
    "token_type": "Identifier",
    "value": "var_dump",
    "line": 3,
    "column": 2,
    "offset": 37
  },
  {
    "token_type": "LeftParenthesis",
    "value": "(",
    "line": 3,
    "column": 10,
    "offset": 45
  },
  {
    "token_type": "Variable",
    "value": "foo",
    "line": 3,
    "column": 11,
    "offset": 46
  },
  {
    "token_type": "RightParenthesis",
    "value": ")",
    "line": 3,
    "column": 15,
    "offset": 50
  },
  {
    "token_type": "Semicolon",
    "value": ";",
    "line": 3,
    "column": 16,
    "offset": 51
  },
  {
    "token_type": "RightCurlyBracket",
    "value": "}",
    "line": 4,
    "column": 0,
    "offset": 53
  }
]

heavily inspired by

license

MIT