Skip to content

Commit

Permalink
Improve parser documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Jun 30, 2024
1 parent bee677f commit 5dc9257
Showing 1 changed file with 29 additions and 130 deletions.
159 changes: 29 additions & 130 deletions Writerside/topics/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,38 @@ information and grammar checking.
To create a parser instance, the `TypeLang\Parser\Parser` class is used.
To run code analysis, you should use the `parse()` method.

<deflist>
<def title="Method signature">
```php
$parser = new TypeLang\Parser\Parser();

$result = $parser->parse('example');
```

```php
TypeLang\Parser\Node\Stmt\NamedTypeNode {
+offset: 0
+name: TypeLang\Parser\Node\Name {
+offset: 0
-parts: array:1 [
0 => TypeLang\Parser\Node\Identifier {
+offset: 0
+value: "example"
}
]
}
+arguments: null
+fields: null
}
```
{collapsible="true" collapsed-title="Result"}

### Parser Arguments

The first argument of the `parse()` method corresponds to the source code data
The first argument of the `TypeLang\Parser\Parser::parse(<source>)` method corresponds to the source code data
and can be of the following types:

<deflist>
<def title="Method signature">

<tabs>
<tab title="string">

Expand Down Expand Up @@ -66,130 +92,3 @@ and can be of the following types:
</tabs>
</def>
</deflist>

```php
$parser = new TypeLang\Parser\Parser();

$result = $parser->parse(<<<'PHP'
array<array-key, object{
key: int<0, max>,
...
}>
PHP);

var_dump($result);
```
{prompt="1"}


```php
TypeLang\Parser\Node\Stmt\NamedTypeNode {
+offset: 0
+name: TypeLang\Parser\Node\Name {
+offset: 0
-parts: array:1 [
0 => TypeLang\Parser\Node\Identifier {
+offset: 0
+value: "array"
}
]
}
+arguments: TypeLang\Parser\Node\Stmt\Template\ArgumentsListNode {
+offset: 5
+items: array:2 [
0 => TypeLang\Parser\Node\Stmt\Template\ArgumentNode {
+offset: 6
+value: TypeLang\Parser\Node\Stmt\NamedTypeNode {
+offset: 6
+name: TypeLang\Parser\Node\Name {
+offset: 6
-parts: array:1 [
0 => TypeLang\Parser\Node\Identifier {
+offset: 6
+value: "array-key"
}
]
}
+arguments: null
+fields: null
}
}
1 => TypeLang\Parser\Node\Stmt\Template\ArgumentNode {
+offset: 17
+value: TypeLang\Parser\Node\Stmt\NamedTypeNode {
+offset: 17
+name: TypeLang\Parser\Node\Name {
+offset: 17
-parts: array:1 [
0 => TypeLang\Parser\Node\Identifier {
+offset: 17
+value: "object"
}
]
}
+arguments: null
+fields: TypeLang\Parser\Node\Stmt\Shape\FieldsListNode {
+offset: 27
+items: array:1 [
0 => TypeLang\Parser\Node\Stmt\Shape\NamedFieldNode {
+offset: 27
+type: TypeLang\Parser\Node\Stmt\NamedTypeNode {
+offset: 32
+name: TypeLang\Parser\Node\Name {
+offset: 32
-parts: array:1 [
0 => TypeLang\Parser\Node\Identifier {
+offset: 32
+value: "int"
}
]
}
+arguments: TypeLang\Parser\Node\Stmt\Template\ArgumentsListNode {
+offset: 35
+items: array:2 [
0 => TypeLang\Parser\Node\Stmt\Template\ArgumentNode {
+offset: 36
+value: TypeLang\Parser\Node\Literal\IntLiteralNode {
+offset: 36
+raw: "0"
+value: 0
}
}
1 => TypeLang\Parser\Node\Stmt\Template\ArgumentNode {
+offset: 39
+value: TypeLang\Parser\Node\Stmt\NamedTypeNode {
+offset: 39
+name: TypeLang\Parser\Node\Name {
+offset: 39
-parts: array:1 [
0 => TypeLang\Parser\Node\Identifier {
+offset: 39
+value: "max"
}
]
}
+arguments: null
+fields: null
}
}
]
}
+fields: null
}
+optional: false
+key: TypeLang\Parser\Node\Identifier {
+offset: 27
+value: "key"
}
}
]
+sealed: false
}
}
}
]
}
+fields: null
}
```
{collapsible="true" collapsed-title="Result"}

0 comments on commit 5dc9257

Please sign in to comment.