Skip to content
This repository was archived by the owner on Nov 15, 2019. It is now read-only.

Commit 2baacc1

Browse files
committed
Fix parser helpers autoloading
1 parent 6675691 commit 2baacc1

30 files changed

+286
-380
lines changed

.github/CONTRIBUTING.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
.idea/
22
vendor/
3-
composer.phar
43
.DS_Store
54
Thumbs.db
5+
composer.phar
6+
composer.lock
67
*.phpt.out
78
*.phpt.exp
89
*.phpt.diff

README.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
</p>
44

55
<p align="center">
6-
<a href="https://travis-ci.org/railt/parser"><img src="https://travis-ci.org/railt/parser.svg?branch=master" alt="Travis CI" /></a>
7-
<a href="https://scrutinizer-ci.com/g/railt/parser/?branch=master"><img src="https://scrutinizer-ci.com/g/railt/parser/badges/quality-score.png?b=master" alt="Scrutinizer CI" /></a>
6+
<a href="https://travis-ci.org/railt/parser"><img src="https://travis-ci.org/railt/parser.svg?branch=1.3.x" alt="Travis CI" /></a>
7+
<a href="https://scrutinizer-ci.com/g/railt/parser/?branch=1.3.x"><img src="https://scrutinizer-ci.com/g/railt/parser/badges/quality-score.png?b=master" alt="Scrutinizer CI" /></a>
88
<a href="https://packagist.org/packages/railt/parser"><img src="https://poser.pugx.org/railt/parser/version" alt="Latest Stable Version"></a>
99
<a href="https://packagist.org/packages/railt/parser"><img src="https://poser.pugx.org/railt/parser/v/unstable" alt="Latest Unstable Version"></a>
1010
<a href="https://raw.githubusercontent.com/railt/parser/master/LICENSE.md"><img src="https://poser.pugx.org/railt/parser/license" alt="License MIT"></a>
@@ -295,8 +295,73 @@ And now, as an **operation** rule, we get the instance of `Operation` class:
295295
```php
296296
$ast = (new Parser($lexer, $grammar))->parse(File::fromSources('2 + 2'));
297297

298-
$operation = $ast->getChild(0); // Operation::class
298+
$operation = $ast->first('operation'); // Operation::class
299299

300300
$operation->isPlus(); // true
301301
$operation->isMinus(); // false
302302
```
303+
304+
### Environment
305+
306+
You can also cast data and the external environment (or context)
307+
inside each rule using the environment class:
308+
309+
```php
310+
$sources = File::fromSources('...');
311+
312+
$parser->env(File::class, $source); // Share environment variable
313+
314+
class DelegateExample extends Rule
315+
{
316+
public function getFile(): File
317+
{
318+
return $this->env(File::class);
319+
}
320+
}
321+
```
322+
323+
## Finder
324+
325+
For a convenient search by AST structure, we can take
326+
advantage of the capabilities of the `Finder`. This class provides a
327+
quick lazy API for querying and finding the right data
328+
inside an Abstract Syntax Tree.
329+
330+
Each rule already has the ability to execute the requested
331+
query using the `find` method:
332+
333+
```php
334+
$ast = $parser->parse(File::fromSources('2 + (3 * 4 + (42 - 10))'));
335+
336+
echo $ast->find('T_DIGIT')->first();
337+
// T_DIGIT "2"
338+
339+
foreach ($ast->find('group T_DIGIT') as $digit) {
340+
echo $digit;
341+
// T_DIGIT "3"
342+
// T_DIGIT "4"
343+
// T_DIGIT "42"
344+
// T_DIGIT "10"
345+
}
346+
347+
348+
echo $ast->find('group > group operation')->first();
349+
// T_MINUS "-"
350+
351+
352+
foreach ($ast->find('group > T_DIGIT') as $digit) {
353+
echo $digit;
354+
// T_DIGIT "3"
355+
// T_DIGIT "4"
356+
}
357+
```
358+
359+
Allowed expressions:
360+
361+
- `name` - Defines any node with the specified name.
362+
- `:name` - Defines only tokens (`LeafInterface`) with the specified name.
363+
- `#name` - Defines only rules (`RuleInterface`) with the specified name.
364+
- `*` - Any rule
365+
- ` ` - Whitespace indicates that the next rule can be at any nested depth.
366+
- `>` - Indicates that the next rule can be strictly within the specified.
367+
- `(N)` - Indicates that the following rule may be strictly within the rule with the N (digit) nesting.

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "railt/parser",
33
"license": "MIT",
44
"type": "library",
5+
"version": "1.3",
56
"homepage": "http://railt.org",
67
"description": "Syntactic LL(k) parser-analyzer implementation",
78
"keywords": [
@@ -37,13 +38,16 @@
3738
"ext-spl": "*",
3839
"ext-pcre": "*",
3940
"ext-mbstring": "*",
40-
"railt/io": "1.4.*|1.4.x-dev",
41-
"railt/lexer": "1.4.*|1.4.x-dev"
41+
"railt/io": "1.3.*|dev-master",
42+
"railt/lexer": "1.3.*|dev-master"
4243
},
4344
"autoload": {
4445
"psr-4": {
4546
"Railt\\": "src/"
46-
}
47+
},
48+
"files": [
49+
"src/Parser/helpers.php"
50+
]
4751
},
4852
"require-dev": {
4953
"phpunit/phpunit": "^6.5"

phpunit.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false">
10+
stopOnFailure="false"
11+
syntaxCheck="true">
1112
<testsuites>
1213
<testsuite name="Test Suite">
1314
<directory suffix="TestCase.php">./tests</directory>

src/Parser/Ast/Builder.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of compiler package.
3+
* This file is part of Railt package.
44
*
55
* For the full copyright and license information, please view the LICENSE
66
* file that was distributed with this source code.
@@ -44,7 +44,6 @@ public function __construct(array $trace, GrammarInterface $grammar)
4444
/**
4545
* @return RuleInterface
4646
* @throws InternalException
47-
* @throws \LogicException
4847
*/
4948
public function build(): RuleInterface
5049
{
@@ -64,7 +63,6 @@ public function build(): RuleInterface
6463
* @param int $i Current trace index.
6564
* @param array &$children Collected children.
6665
* @return Node|int
67-
* @throws \LogicException
6866
*/
6967
protected function buildTree(int $i = 0, array &$children = [])
7068
{
@@ -137,7 +135,7 @@ protected function buildTree(int $i = 0, array &$children = [])
137135
continue;
138136
}
139137

140-
$children[] = new Leaf($trace->getToken());
138+
$children[] = $this->leaf($trace);
141139
++$i;
142140
}
143141
}
@@ -149,18 +147,22 @@ protected function buildTree(int $i = 0, array &$children = [])
149147
* @param string $name
150148
* @param array $children
151149
* @param int $offset
152-
* @return Rule|mixed
153-
* @throws \LogicException
150+
* @return RuleInterface
154151
*/
155-
protected function rule(string $name, array $children, int $offset)
152+
private function rule(string $name, array $children, int $offset): RuleInterface
156153
{
157-
$delegate = $this->grammar->delegate($name) ?? Rule::class;
154+
/** @var Rule $class */
155+
$class = $this->grammar->delegate($name) ?? Rule::class;
158156

159-
try {
160-
return new $delegate($name, $children, $offset);
161-
} catch (\TypeError $e) {
162-
$error = \sprintf('Error while %s initialization: %s', $delegate, $e->getMessage());
163-
throw new \LogicException($error);
164-
}
157+
return new $class($name, $children, $offset);
158+
}
159+
160+
/**
161+
* @param Token $token
162+
* @return LeafInterface
163+
*/
164+
private function leaf(Token $token): LeafInterface
165+
{
166+
return new Leaf($token->getToken());
165167
}
166168
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
declare(strict_types=1);
99

10-
namespace Railt\Parser\Dumper;
10+
namespace Railt\Parser\Ast\Dumper;
1111

1212
use Railt\Parser\Ast\LeafInterface;
1313
use Railt\Parser\Ast\NodeInterface;

src/Parser/Dumper/NodeDumperInterface.php renamed to src/Parser/Ast/Dumper/NodeDumperInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
declare(strict_types=1);
99

10-
namespace Railt\Parser\Dumper;
10+
namespace Railt\Parser\Ast\Dumper;
1111

1212
/**
1313
* Interface Dumper

0 commit comments

Comments
 (0)