| 148 |
lars |
1 |
# Tokenizer
|
|
|
2 |
|
|
|
3 |
A small library for converting tokenized PHP source code into XML.
|
|
|
4 |
|
|
|
5 |
[](https://github.com/theseer/tokenizer/actions/workflows/ci.yml)
|
|
|
6 |
[](https://scrutinizer-ci.com/g/theseer/tokenizer/?branch=master)
|
|
|
7 |
[](https://scrutinizer-ci.com/g/theseer/tokenizer/?branch=master)
|
|
|
8 |
[](https://scrutinizer-ci.com/g/theseer/tokenizer/build-status/master)
|
|
|
9 |
|
|
|
10 |
## Installation
|
|
|
11 |
|
|
|
12 |
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
|
|
|
13 |
|
|
|
14 |
composer require theseer/tokenizer
|
|
|
15 |
|
|
|
16 |
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
|
|
|
17 |
|
|
|
18 |
composer require --dev theseer/tokenizer
|
|
|
19 |
|
|
|
20 |
## Usage examples
|
|
|
21 |
|
|
|
22 |
```php
|
|
|
23 |
$tokenizer = new TheSeer\Tokenizer\Tokenizer();
|
|
|
24 |
$tokens = $tokenizer->parse(file_get_contents(__DIR__ . '/src/XMLSerializer.php'));
|
|
|
25 |
|
|
|
26 |
$serializer = new TheSeer\Tokenizer\XMLSerializer();
|
|
|
27 |
$xml = $serializer->toXML($tokens);
|
|
|
28 |
|
|
|
29 |
echo $xml;
|
|
|
30 |
```
|
|
|
31 |
|
|
|
32 |
The generated XML structure looks something like this:
|
|
|
33 |
|
|
|
34 |
```xml
|
|
|
35 |
<?xml version="1.0"?>
|
|
|
36 |
<source xmlns="https://github.com/theseer/tokenizer">
|
|
|
37 |
<line no="1">
|
|
|
38 |
<token name="T_OPEN_TAG"><?php </token>
|
|
|
39 |
<token name="T_DECLARE">declare</token>
|
|
|
40 |
<token name="T_OPEN_BRACKET">(</token>
|
|
|
41 |
<token name="T_STRING">strict_types</token>
|
|
|
42 |
<token name="T_WHITESPACE"> </token>
|
|
|
43 |
<token name="T_EQUAL">=</token>
|
|
|
44 |
<token name="T_WHITESPACE"> </token>
|
|
|
45 |
<token name="T_LNUMBER">1</token>
|
|
|
46 |
<token name="T_CLOSE_BRACKET">)</token>
|
|
|
47 |
<token name="T_SEMICOLON">;</token>
|
|
|
48 |
</line>
|
|
|
49 |
</source>
|
|
|
50 |
```
|