Skip to content

Commit

Permalink
ParseError + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuqqu committed Feb 5, 2022
1 parent 4c33c35 commit 5bddc3b
Show file tree
Hide file tree
Showing 28 changed files with 575 additions and 94 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ $ast = $parser->parse();
$errs = $parser->getErrors();
```

Parser is able to recover itself if a parse error occurs, in this case it will continue parsing at the closest node it is able to recognise.
The resulting AST will be as full as possible, and you have to check `getErrors()` to see errors.

### Parsing single declarations
If you want, you may parse only a single declaration (e.g. a single function), instead of a fully defined Go program:
You may parse only a single declaration (e.g. a single function), instead of a fully defined Go program:
```php
$parser = new \GoParser\Parser(
'func main() { var x int }',
'func add(x, y int) { return x + y }',
mode: \GoParser\ParseMode::SingleDecl
);
$ast = $parser->parse();
$decl = $parser->parseSingleDecl();
```

Parser is able to recover itself if a parse error occurs, in this case it will continue parsing at the closest node it is able to recognise.

The resulting AST will be as full as possible, and you have to check `getErrors()` to see errors.


## Abstract Syntax Tree

Parsing results in an Abstract Syntax Tree result.
Parsing results in an Abstract Syntax Tree result. See `src/Ast`.

For the most part the AST nodes structure follows closely the official Golang [specification][1].

Mostly the AST nodes structure follows closely the official Golang [specification][1], but for the sake of simplicity there are few exceptions.
Some Nodes may also have a bit different name for the sake of brevity and consistency with others (e.g. `ExpressionList` vs `ExprList`), but for the most part the names are either the same or easily recognisable.
Some Nodes may also have a bit different name (e.g. `ExpressionList` vs `ExprList`), but mostly the names are either the same or easily recognisable.

## CLI
Package comes with a CLI command:
Expand Down
4 changes: 2 additions & 2 deletions src/Ast/FieldDecl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace GoParser\Ast;

use GoParser\Ast\Expr\Expr;
use GoParser\Ast\Expr\RawStringLit;
use GoParser\Ast\Expr\StringLit;
use GoParser\Ast\Expr\Type;

final class FieldDecl implements AstNode
{
public function __construct(
public readonly ?IdentList $identList,
public readonly Type $type,
public readonly ?Expr $type,
public readonly StringLit|RawStringLit|null $tag,
) {}
}
4 changes: 2 additions & 2 deletions src/Ast/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
final class File implements AstNode
{
/**
* @param ImportDecl[]|GroupSpec[] $imports
* @param Decl[]|GroupSpec[] $decls
* @param ImportDecl[] $imports
* @param Decl[] $decls
*/
public function __construct(
public readonly PackageClause $package,
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/IdentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GoParser\Ast;

use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\Expr\Expr;
use GoParser\Ast\Expr\Ident;
use GoParser\Ast\Expr\Type;
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/KeyedElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GoParser\Ast;

use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\Expr\Expr;

final class KeyedElement implements AstNode
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/Stmt/ConstDecl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace GoParser\Ast\Stmt;

use GoParser\Ast\ConstSpec;
use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\GroupSpec;
use GoParser\Ast\Keyword;
use GoParser\Ast\SpecType;
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/Stmt/IfStmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GoParser\Ast\Stmt;

use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\Expr\Expr;
use GoParser\Ast\Keyword;

Expand Down
2 changes: 1 addition & 1 deletion src/Ast/Stmt/ImportDecl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GoParser\Ast\Stmt;

use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\GroupSpec;
use GoParser\Ast\ImportSpec;
use GoParser\Ast\Keyword;
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/Stmt/TypeDecl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GoParser\Ast\Stmt;

use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\GroupSpec;
use GoParser\Ast\Keyword;
use GoParser\Ast\SpecType;
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/Stmt/VarDecl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GoParser\Ast\Stmt;

use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\GroupSpec;
use GoParser\Ast\Keyword;
use GoParser\Ast\SpecType;
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/VarSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GoParser\Ast;

use GoParser\Ast\Exception\InvalidArgument;
use GoParser\Exception\InvalidArgument;
use GoParser\Ast\Expr\Type;

final class VarSpec implements Spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace GoParser\Ast\Exception;
namespace GoParser\Exception;

final class InvalidArgument extends \InvalidArgumentException
{
Expand Down
19 changes: 19 additions & 0 deletions src/Exception/ParseModeError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace GoParser\Exception;

use GoParser\ParseMode;

final class ParseModeError extends \BadMethodCallException
{
public function __construct(ParseMode $expected, ParseMode $actual)
{
parent::__construct(\sprintf(
'Expected Parser to be initialised with Parse Mode "%s" , but got "%s"',
$expected->name,
$actual->name
));
}
}
Loading

0 comments on commit 5bddc3b

Please sign in to comment.