Skip to content

Commit 9e1b759

Browse files
committed
Initial commit
0 parents  commit 9e1b759

File tree

137 files changed

+20074
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+20074
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.cache
2+
/composer.lock
3+
/vendor
4+
/phpcs.xml
5+
/phpstan.neon
6+
/phpunit.xml

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2019, Antlr Project
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# ANTLR4 Runtime for PHP
2+
3+
### First steps
4+
5+
#### 1. Install ANTLR4
6+
7+
[The getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md)
8+
should get you started.
9+
10+
#### 2. Install the PHP ANTLR runtime
11+
12+
Each target language for ANTLR has a runtime package for running parser
13+
generated by ANTLR4. The runtime provides a common set of tools for using your parser.
14+
15+
Install the runtime with Composer:
16+
17+
```bash
18+
composer install antlr/antlr4
19+
```
20+
21+
#### 3. Generate your parser
22+
23+
You use the ANTLR4 "tool" to generate a parser. These will reference the ANTLR
24+
runtime, installed above.
25+
26+
Suppose you're using a UNIX system and have set up an alias for the ANTLR4 tool
27+
as described in [the getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md).
28+
To generate your PHP parser, run the following command:
29+
30+
```bash
31+
antlr4 -Dlanguage=PHP MyGrammar.g4
32+
```
33+
34+
For a full list of antlr4 tool options, please visit the
35+
[tool documentation page](https://github.com/antlr/antlr4/blob/master/doc/tool-options.md).
36+
37+
### Complete example
38+
39+
Suppose you're using the JSON grammar from https://github.com/antlr/grammars-v4/tree/master/json.
40+
41+
Then, invoke `antlr4 -Dlanguage=PHP JSON.g4`. The result of this is a
42+
collection of `.php` files in the `parser` directory including:
43+
```
44+
JsonParser.php
45+
JsonBaseListener.php
46+
JsonLexer.php
47+
JsonListener.php
48+
```
49+
50+
Another common option to the ANTLR tool is `-visitor`, which generates a parse
51+
tree visitor, but we won't be doing that here. For a full list of antlr4 tool
52+
options, please visit the [tool documentation page](tool-options.md).
53+
54+
We'll write a small main func to call the generated parser/lexer
55+
(assuming they are separate). This one writes out the encountered
56+
`ParseTreeContext`'s:
57+
58+
```php
59+
<?php
60+
61+
namespace JsonParser;
62+
63+
use Antlr\Antlr4\Runtime\CommonTokenStream;
64+
use Antlr\Antlr4\Runtime\Error\Listeners\DiagnosticErrorListener;
65+
use Antlr\Antlr4\Runtime\InputStream;
66+
use Antlr\Antlr4\Runtime\ParserRuleContext;
67+
use Antlr\Antlr4\Runtime\Tree\ErrorNode;
68+
use Antlr\Antlr4\Runtime\Tree\ParseTreeListener;
69+
use Antlr\Antlr4\Runtime\Tree\ParseTreeWalker;
70+
use Antlr\Antlr4\Runtime\Tree\TerminalNode;
71+
72+
final class TreeShapeListener implements ParseTreeListener {
73+
public function visitTerminal(TerminalNode $node) : void {}
74+
public function visitErrorNode(ErrorNode $node) : void {}
75+
public function exitEveryRule(ParserRuleContext $ctx) : void {}
76+
77+
public function enterEveryRule(ParserRuleContext $ctx) : void {
78+
echo $ctx->getText();
79+
}
80+
}
81+
82+
$input = InputStream::fromPath($argv[1]);
83+
$lexer = new JSONLexer($input);
84+
$tokens = new CommonTokenStream($lexer);
85+
$parser = new JSONParser($tokens);
86+
$parser->addErrorListener(new DiagnosticErrorListener());
87+
$parser->setBuildParseTree(true);
88+
$tree = $parser->json();
89+
90+
ParseTreeWalker::default()->walk(new TreeShapeListener(), $tree);
91+
```
92+
93+
Create a `example.json` file:
94+
```json
95+
{"a":1}
96+
```
97+
98+
Parse the input file:
99+
100+
```
101+
php json.php example.json
102+
```
103+
104+
The expected output is:
105+
106+
```
107+
{"a":1}
108+
{"a":1}
109+
"a":1
110+
1
111+
```
112+
113+
### Useful information
114+
115+
* [Official site](http://www.antlr.org/)
116+
* [Documentation](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/index.md)
117+
* [Getting started with v4](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/getting-started.md)
118+
* [FAQ](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/faq/index.md)
119+
120+
### The Definitive ANTLR 4 Reference
121+
122+
Programmers run into parsing problems all the time. Whether it’s a data format like JSON, a network protocol like SMTP, a server configuration file for Apache, a PostScript/PDF file, or a simple spreadsheet macro language—ANTLR v4 and this book will demystify the process. ANTLR v4 has been rewritten from scratch to make it easier than ever to build parsers and the language applications built on top. This completely rewritten new edition of the bestselling Definitive ANTLR Reference shows you how to take advantage of these new features.
123+
124+
You can buy the book [The Definitive ANTLR 4 Reference](http://amzn.com/1934356999) at amazon or an [electronic version at the publisher's site](https://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference).
125+
126+
You will find the [Book source code](http://pragprog.com/titles/tpantlr2/source_code) useful.
127+
128+
### Additional grammars
129+
130+
[This repository](https://github.com/antlr/grammars-v4) is a collection of grammars without actions where the
131+
root directory name is the all-lowercase name of the language parsed by the grammar.

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "antlr/antlr4-php-runtime",
3+
"type": "library",
4+
"description": "PHP 7 runtime for ANTLR 4",
5+
"license": [
6+
"BSD-3-Clause"
7+
],
8+
"minimum-stability": "dev",
9+
"prefer-stable": true,
10+
"require": {
11+
"php": "^7.2",
12+
"ext-mbstring": "*"
13+
},
14+
"require-dev": {
15+
"phpstan/phpstan": "^0.11.4",
16+
"phpunit/phpunit": "^8.0.4",
17+
"slevomat/coding-standard": "^5.0.4",
18+
"squizlabs/php_codesniffer": "^3.4.1",
19+
"phpstan/extension-installer": "^1.0"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Antlr\\Antlr4\\Runtime\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Antlr\\Antlr4\\Runtime\\Tests\\": "tests/"
29+
}
30+
},
31+
"extra": {
32+
"branch-alias": {
33+
"dev-master": "0.2-dev"
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)