Skip to content

Commit b0e2380

Browse files
committed
Embedded struct fields, field tag fix, code improvements
1 parent 0a6a7e3 commit b0e2380

Some content is hidden

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

73 files changed

+1442
-220
lines changed

.php-cs-fixer.dist.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44

55
const CONFIG = new PhpCsFixer\Config();
66
const RULES = [
7-
'@PSR12' => true,
7+
'@PER' => true,
88
'strict_param' => true,
9-
'braces' => false,
109
'ternary_operator_spaces' => false,
1110
'no_break_comment' => false,
1211
'array_syntax' => ['syntax' => 'short'],
12+
'no_unused_imports' => true,
13+
'single_line_empty_body' => true,
14+
'statement_indentation' => false,
15+
'global_namespace_import' => [
16+
'import_classes' => true,
17+
'import_constants' => true,
18+
'import_functions' => true,
19+
],
1320
];
1421

1522
CONFIG->setRules(RULES);

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# GoParser
2-
Golang (1.18) parser written in PHP 8.1
2+
Golang parser written in PHP 8.1
33

44
## Installation
55
To install this package, run:
@@ -27,15 +27,14 @@ $parser = new Parser($program);
2727
$ast = $parser->parse();
2828
$errs = $parser->getErrors();
2929
```
30-
Supported language level: Go 1.18 (generics).
3130

32-
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.
31+
The parser is capable of recovering itself if a parse error occurs. In such cases, it will continue parsing at the closest node it can recognise.
3332

34-
The resulting AST will be as full as possible, and you have to check `getErrors()` to see errors.
33+
The resulting Abstract Syntax Tree (AST) will be as complete as possible. You need to check `getErrors()` to identify any errors.
3534

3635
## Single declaration parsing
3736

38-
Parser can also handle a single declaration only (e.g. a single function), instead of a fully defined Go program:
37+
The parser can also handle a single declaration (e.g., a single function) instead of an entire Go program:
3938
```php
4039
use GoParser\{Parser, ParseMode};
4140

@@ -51,11 +50,11 @@ $decl = $parser->parseSingleDecl();
5150

5251
## Abstract Syntax Tree
5352

54-
Parsing results in an Abstract Syntax Tree result. See `src/Ast`.
53+
Parsing results in an Abstract Syntax Tree. Refer to `src/Ast` for details.
5554

56-
For the most part the AST nodes structure follows closely the official Golang [specification][1].
55+
For the most part, the structure of AST nodes closely follows the official Golang [specification][1].
5756

58-
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.
57+
Some nodes may have slightly different names (e.g., `ExpressionList` instead of `ExprList`), but in most cases, the names are the same or easily recognisable.
5958

6059
## CLI
6160
Package comes with a CLI command:

bin/go-parser

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function parse_argv(array $argv): array
8383
$mode = ParseMode::SingleDecl;
8484
break;
8585
case '--node-dumper':
86-
// default for now
86+
// default
8787
break;
8888
case '-d':
8989
case '--var-dump':

src/Ast/AstNode.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast;
66

7-
interface AstNode
8-
{
9-
}
7+
interface AstNode {}

src/Ast/CaseClause.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast;
66

7-
interface CaseClause extends AstNode
8-
{
9-
}
7+
interface CaseClause extends AstNode {}

src/Ast/CaseLabel.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast;
66

7-
interface CaseLabel extends AstNode
8-
{
9-
}
7+
interface CaseLabel extends AstNode {}

src/Ast/EmbeddedFieldDecl.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoParser\Ast;
6+
7+
use GoParser\Ast\Expr\PointerType;
8+
use GoParser\Ast\Expr\RawStringLit;
9+
use GoParser\Ast\Expr\StringLit;
10+
use GoParser\Ast\Expr\TypeName;
11+
12+
final class EmbeddedFieldDecl implements AstNode
13+
{
14+
public function __construct(
15+
public readonly TypeName|PointerType $type,
16+
public readonly StringLit|RawStringLit|null $tag,
17+
) {}
18+
}

src/Ast/Expr/Expr.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
use GoParser\Ast\AstNode;
88

9-
interface Expr extends AstNode
10-
{
11-
}
9+
interface Expr extends AstNode {}

src/Ast/Expr/Literal.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface Literal extends Operand
8-
{
9-
}
7+
interface Literal extends Operand {}

src/Ast/Expr/Operand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface Operand extends PrimaryExpr
8-
{
9-
}
7+
interface Operand extends PrimaryExpr {}

src/Ast/Expr/PrimaryExpr.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface PrimaryExpr extends Expr
8-
{
9-
}
7+
interface PrimaryExpr extends Expr {}

src/Ast/Expr/SliceExpr.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface SliceExpr extends PrimaryExpr
8-
{
9-
}
7+
interface SliceExpr extends PrimaryExpr {}

src/Ast/Expr/StructType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace GoParser\Ast\Expr;
66

7+
use GoParser\Ast\EmbeddedFieldDecl;
78
use GoParser\Ast\FieldDecl;
89
use GoParser\Ast\Keyword;
910
use GoParser\Ast\Punctuation;
1011

1112
final class StructType implements TypeLit
1213
{
1314
/**
14-
* @param FieldDecl[] $fieldDecls
15+
* @param list<FieldDecl|EmbeddedFieldDecl> $fieldDecls
1516
*/
1617
public function __construct(
1718
public readonly Keyword $keyword,

src/Ast/Expr/Type.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface Type extends TypeTerm, Expr
8-
{
9-
}
7+
interface Type extends TypeTerm, Expr {}

src/Ast/Expr/TypeLit.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface TypeLit extends Type
8-
{
9-
}
7+
interface TypeLit extends Type {}

src/Ast/Expr/TypeName.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface TypeName extends Type
8-
{
9-
}
7+
interface TypeName extends Type {}

src/Ast/Expr/TypeTerm.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Expr;
66

7-
interface TypeTerm
8-
{
9-
}
7+
interface TypeTerm {}

src/Ast/Expr/UnderlyingType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace GoParser\Ast\Expr;
66

77
use GoParser\Ast\Operator;
8-
use GoParser\Lexer\Position;
98

109
final class UnderlyingType implements TypeTerm, Expr
1110
{

src/Ast/FieldDecl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
final class FieldDecl implements AstNode
1212
{
1313
public function __construct(
14-
public readonly ?IdentList $identList,
15-
public readonly ?Type $type,
14+
public readonly IdentList $identList,
15+
public readonly Type $type,
1616
public readonly StringLit|RawStringLit|null $tag,
1717
) {}
1818
}

src/Ast/IdentList.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace GoParser\Ast;
66

7+
use GoParser\Ast\Expr\Type;
78
use GoParser\Exception\InvalidArgument;
89
use GoParser\Ast\Expr\Expr;
910
use GoParser\Ast\Expr\Ident;
10-
use GoParser\Ast\Expr\Type;
1111
use GoParser\Ast\Expr\SingleTypeName;
1212

13+
use function array_map;
14+
1315
final class IdentList implements AstNode
1416
{
1517
/**
@@ -21,7 +23,7 @@ public function __construct(
2123

2224
public static function fromExprList(ExprList $list): self
2325
{
24-
return new self(\array_map(
26+
return new self(array_map(
2527
static fn (Expr $expr): Ident => $expr instanceof Ident
2628
? $expr
2729
: throw new InvalidArgument('Cannot create IdentList from an arbitrary expression list'),
@@ -31,7 +33,7 @@ public static function fromExprList(ExprList $list): self
3133

3234
public static function fromTypeList(TypeList $list): self
3335
{
34-
return new self(\array_map(
36+
return new self(array_map(
3537
static fn (Type $type): Ident => $type instanceof SingleTypeName
3638
? $type->name
3739
: throw new InvalidArgument('Cannot create IdentList from an arbitrary type list'),

src/Ast/SpecType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace GoParser\Ast;
66

7-
enum SpecType implements \JsonSerializable
7+
use JsonSerializable;
8+
9+
enum SpecType implements JsonSerializable
810
{
911
case Import;
1012
case Var;

src/Ast/Stmt/ConstDecl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
use GoParser\Ast\Keyword;
1111
use GoParser\Ast\SpecType;
1212

13+
use function sprintf;
14+
1315
final class ConstDecl implements Decl
1416
{
1517
public function __construct(
1618
public readonly Keyword $keyword,
1719
public readonly GroupSpec|ConstSpec $spec,
1820
) {
1921
if ($spec->type() !== SpecType::Const) {
20-
throw new InvalidArgument(\sprintf('Cannot create a ConstDecl with Spec of type %s', $spec->type()->name));
22+
throw new InvalidArgument(sprintf('Cannot create a ConstDecl with Spec of type %s', $spec->type()->name));
2123
}
2224
}
2325
}

src/Ast/Stmt/Decl.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Stmt;
66

7-
interface Decl extends Stmt
8-
{
9-
}
7+
interface Decl extends Stmt {}

src/Ast/Stmt/IfStmt.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
final class IfStmt implements Stmt
1515
{
1616
public function __construct(
17-
public readonly Keyword $if,
17+
public readonly Keyword $keyword,
1818
public readonly ?SimpleStmt $init,
1919
public readonly Expr $condition,
2020
public readonly BlockStmt $ifBody,
21-
public readonly ?Keyword $else,
21+
public readonly ?Keyword $elseKeyword,
2222
public readonly BlockStmt|self|null $elseBody,
2323
) {
24-
if ((bool) $else !== (bool) $elseBody) {
24+
if ((bool) $elseKeyword !== (bool) $elseBody) {
2525
throw new InvalidArgument('Both "else" keyword and the body must be present or neither.');
2626
}
2727
}

src/Ast/Stmt/ImportDecl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
use GoParser\Ast\Keyword;
1111
use GoParser\Ast\SpecType;
1212

13+
use function sprintf;
14+
1315
final class ImportDecl implements Decl
1416
{
1517
public function __construct(
1618
public readonly Keyword $keyword,
1719
public readonly GroupSpec|ImportSpec $spec,
1820
) {
1921
if ($spec->type() !== SpecType::Import) {
20-
throw new InvalidArgument(\sprintf('Cannot create a ImportDecl with Spec of type %s', $spec->type()->name));
22+
throw new InvalidArgument(sprintf('Cannot create a ImportDecl with Spec of type %s', $spec->type()->name));
2123
}
2224
}
2325
}

src/Ast/Stmt/SimpleStmt.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Stmt;
66

7-
interface SimpleStmt extends Stmt
8-
{
9-
}
7+
interface SimpleStmt extends Stmt {}

src/Ast/Stmt/Stmt.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
use GoParser\Ast\AstNode;
88

9-
interface Stmt extends AstNode
10-
{
11-
}
9+
interface Stmt extends AstNode {}

src/Ast/Stmt/SwitchStmt.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace GoParser\Ast\Stmt;
66

7-
interface SwitchStmt extends Stmt
8-
{
9-
}
7+
interface SwitchStmt extends Stmt {}

src/Ast/Stmt/TypeDecl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
use GoParser\Ast\SpecType;
1111
use GoParser\Ast\TypeSpec;
1212

13+
use function sprintf;
14+
1315
final class TypeDecl implements Decl
1416
{
1517
public function __construct(
1618
public readonly Keyword $keyword,
1719
public readonly GroupSpec|TypeSpec $spec,
1820
) {
1921
if ($spec->type() !== SpecType::Type) {
20-
throw new InvalidArgument(\sprintf('Cannot create a TypeDecl with Spec of type %s', $spec->type()->name));
22+
throw new InvalidArgument(sprintf('Cannot create a TypeDecl with Spec of type %s', $spec->type()->name));
2123
}
2224
}
2325
}

src/Ast/Stmt/VarDecl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
use GoParser\Ast\SpecType;
1111
use GoParser\Ast\VarSpec;
1212

13+
use function sprintf;
14+
1315
final class VarDecl implements Decl
1416
{
1517
public function __construct(
1618
public readonly Keyword $keyword,
1719
public readonly GroupSpec|VarSpec $spec,
1820
) {
1921
if ($spec->type() !== SpecType::Var) {
20-
throw new InvalidArgument(\sprintf('Cannot create a VarDecl with Spec of type %s', $spec->type()->name));
22+
throw new InvalidArgument(sprintf('Cannot create a VarDecl with Spec of type %s', $spec->type()->name));
2123
}
2224
}
2325
}

0 commit comments

Comments
 (0)