Skip to content

Commit 738295d

Browse files
committed
Update README.md.
1 parent 83b5cee commit 738295d

1 file changed

Lines changed: 8 additions & 129 deletions

File tree

README.md

Lines changed: 8 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,14 @@
11
# ANTLR4 Runtime for PHP
2-
[![Travis CI](https://api.travis-ci.org/antlr/antlr-php-runtime.svg?branch=master)](https://travis-ci.org/antlr/antlr-php-runtime)
3-
[![Latest Stable Version](https://poser.pugx.org/antlr/antlr4-php-runtime/v/stable)](https://packagist.org/packages/antlr/antlr4-php-runtime)
4-
[![License](https://poser.pugx.org/antlr/antlr4-php-runtime/license)](https://packagist.org/packages/antlr/antlr4-php-runtime)
52

6-
### First steps
3+
[![Travis CI](https://api.travis-ci.org/SetBased/antlr-php-runtime.svg?branch=master)](https://travis-ci.org/SetBased/antlr-php-runtime)
4+
[![Latest Stable Version](https://poser.pugx.org/SetBased/antlr4-php-runtime/v/stable)](https://packagist.org/packages/SetBased/antlr4-php-runtime)
5+
[![License](https://poser.pugx.org/SetBased/antlr4-php-runtime/license)](https://packagist.org/packages/SetBased/antlr4-php-runtime)
6+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/SetBased/antlr-php-runtime/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/SetBased/antlr-php-runtime/?branch=master)
7+
[![Code Coverage](https://scrutinizer-ci.com/g/SetBased/antlr-php-runtime/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/SetBased/antlr-php-runtime/?branch=master)
78

8-
#### 1. Install ANTLR4
9+
This repository is a fork of [antlr-php-runtime](https://github.com/antlr/antlr-php-runtime). The (hopefully short-lived) mission of this repository is to release an ANTLR runtime library for PHP 8.x.
910

10-
[The getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md)
11-
should get you started.
12-
13-
#### 2. Install the PHP ANTLR runtime
14-
15-
Each target language for ANTLR has a runtime package for running parser
16-
generated by ANTLR4. The runtime provides a common set of tools for using your parser.
17-
18-
Install the runtime with Composer:
19-
20-
```bash
21-
composer require antlr/antlr4-php-runtime
22-
```
23-
24-
#### 3. Generate your parser
25-
26-
You use the ANTLR4 "tool" to generate a parser. These will reference the ANTLR
27-
runtime, installed above.
28-
29-
Suppose you're using a UNIX system and have set up an alias for the ANTLR4 tool
30-
as described in [the getting started guide](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md).
31-
To generate your PHP parser, run the following command:
32-
33-
```bash
34-
antlr4 -Dlanguage=PHP MyGrammar.g4
35-
```
36-
37-
For a full list of antlr4 tool options, please visit the
38-
[tool documentation page](https://github.com/antlr/antlr4/blob/master/doc/tool-options.md).
39-
40-
### Complete example
41-
42-
Suppose you're using the JSON grammar from https://github.com/antlr/grammars-v4/tree/master/json.
43-
44-
Then, invoke `antlr4 -Dlanguage=PHP JSON.g4`. The result of this is a
45-
collection of `.php` files in the `parser` directory including:
46-
```
47-
JsonParser.php
48-
JsonBaseListener.php
49-
JsonLexer.php
50-
JsonListener.php
51-
```
52-
53-
Another common option to the ANTLR tool is `-visitor`, which generates a parse
54-
tree visitor, but we won't be doing that here. For a full list of antlr4 tool
55-
options, please visit the [tool documentation page](tool-options.md).
56-
57-
We'll write a small main func to call the generated parser/lexer
58-
(assuming they are separate). This one writes out the encountered
59-
`ParseTreeContext`'s:
60-
61-
```php
62-
<?php
63-
64-
namespace JsonParser;
65-
66-
use Antlr\Antlr4\Runtime\CommonTokenStream;
67-
use Antlr\Antlr4\Runtime\Error\Listeners\DiagnosticErrorListener;
68-
use Antlr\Antlr4\Runtime\InputStream;
69-
use Antlr\Antlr4\Runtime\ParserRuleContext;
70-
use Antlr\Antlr4\Runtime\Tree\ErrorNode;
71-
use Antlr\Antlr4\Runtime\Tree\ParseTreeListener;
72-
use Antlr\Antlr4\Runtime\Tree\ParseTreeWalker;
73-
use Antlr\Antlr4\Runtime\Tree\TerminalNode;
74-
75-
final class TreeShapeListener implements ParseTreeListener {
76-
public function visitTerminal(TerminalNode $node) : void {}
77-
public function visitErrorNode(ErrorNode $node) : void {}
78-
public function exitEveryRule(ParserRuleContext $ctx) : void {}
79-
80-
public function enterEveryRule(ParserRuleContext $ctx) : void {
81-
echo $ctx->getText();
82-
}
83-
}
84-
85-
$input = InputStream::fromPath($argv[1]);
86-
$lexer = new JSONLexer($input);
87-
$tokens = new CommonTokenStream($lexer);
88-
$parser = new JSONParser($tokens);
89-
$parser->addErrorListener(new DiagnosticErrorListener());
90-
$parser->setBuildParseTree(true);
91-
$tree = $parser->json();
92-
93-
ParseTreeWalker::default()->walk(new TreeShapeListener(), $tree);
11+
Install this package with [Composer](https://getcomposer.org):
9412
```
95-
96-
Create a `example.json` file:
97-
```json
98-
{"a":1}
13+
composer require SetBased/antlr4-php-runtime
9914
```
100-
101-
Parse the input file:
102-
103-
```
104-
php json.php example.json
105-
```
106-
107-
The expected output is:
108-
109-
```
110-
{"a":1}
111-
{"a":1}
112-
"a":1
113-
1
114-
```
115-
116-
### Useful information
117-
118-
* [Official site](http://www.antlr.org/)
119-
* [Documentation](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/index.md)
120-
* [Getting started with v4](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/getting-started.md)
121-
* [PHPStan Exension](https://github.com/antlr/antlr-php-runtime-phpstan)
122-
* [FAQ](https://github.com/tunnelvisionlabs/antlr4/blob/master/doc/faq/index.md)
123-
124-
### The Definitive ANTLR 4 Reference
125-
126-
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.
127-
128-
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).
129-
130-
You will find the [Book source code](http://pragprog.com/titles/tpantlr2/source_code) useful.
131-
132-
### Additional grammars
133-
134-
[This repository](https://github.com/antlr/grammars-v4) is a collection of grammars without actions where the
135-
root directory name is the all-lowercase name of the language parsed by the grammar.

0 commit comments

Comments
 (0)