Skip to content

Commit

Permalink
Fix for embedded struct field parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tuqqu committed Sep 9, 2023
1 parent b0e2380 commit 9897007
Show file tree
Hide file tree
Showing 38 changed files with 677 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.github/ export-ignore

/.php-cs-fixer.php export-ignore
/psalm.xml export-ignore
/phpunit.xml export-ignore
/tests/ export-ignore
/tests/ export-ignore
File renamed without changes.
3 changes: 3 additions & 0 deletions src/Ast/FromLexeme.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace GoParser\Ast;

use GoParser\Lexer\Lexeme;
use GoParser\Lexer\Position;

trait FromLexeme
{
public static function fromLexeme(Lexeme $lexeme): static
{
return new static($lexeme->pos, $lexeme->literal ?? $lexeme->token->value);
}

abstract public function __construct(Position $pos, string $literal);
}
7 changes: 3 additions & 4 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace GoParser;

interface Error
{
public function __toString(): string;
}
use Stringable;

interface Error extends Stringable {}
2 changes: 1 addition & 1 deletion src/Exception/ParseModeError.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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 Parser to be initialised with Parse Mode "%s", but got "%s"',
$expected->name,
$actual->name
));
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ private function parseStructType(): StructType
$typeOrIdent = $this->parseType();
$tag = $this->tryParseTag();

if ($this->match(Token::Semicolon)) {
if ($this->matchAny(Token::Semicolon, Token::RightBrace)) {
if (
!$typeOrIdent instanceof TypeName
&& ($typeOrIdent instanceof PointerType && (!$typeOrIdent->type instanceof TypeName))
Expand Down
36 changes: 24 additions & 12 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
use GoParser\Ast\Stmt\VarDecl;
use GoParser\Parser;
use PHPUnit\Framework\TestCase;
use RuntimeException;

use function json_encode;
use function glob;
use function sprintf;
use function basename;
use function file_get_contents;
use function glob;
use function json_encode;
use function sprintf;

use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
Expand Down Expand Up @@ -82,15 +83,26 @@ private static function dataFiles(): iterable

private static function fileContents(string $dir): iterable
{
$path = __DIR__ . '/data/';
$files = glob(sprintf($path . '/src/%s/*.go', $dir));

foreach ($files as $file) {
$goProgram = file_get_contents($file);
$jsonPath = sprintf($path . '/ast/%s/%s.json', $dir, basename($file, '.go'));
$parsedJson = file_get_contents($jsonPath);

yield $file => [$goProgram, $parsedJson];
$path = __DIR__ . '/data';
$goFiles = glob(sprintf($path . '/%s/*.go', $dir));
$astFiles = glob(sprintf($path . '/%s/*.json', $dir));

foreach ($goFiles as $i => $goFile) {
$goFilename = basename($goFile, '.go');
$astFilename = basename($astFiles[$i], '.json');

if ($goFilename !== $astFilename) {
throw new RuntimeException(sprintf(
'Filename mismatch: %s vs %s',
$goFilename,
$astFilename
));
}

$goProgram = file_get_contents($goFile);
$ast = file_get_contents($astFiles[$i]);

yield $goFilename => [$goProgram, $ast];
}
}
}
5 changes: 2 additions & 3 deletions tests/data/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
## Directory structure

* `./src/core` Go test files
* `./src/sample` Go files from various open source projects
* `./ast` Parsed AST in JSON
* `./core` Go test files
* `./sample` Go files from various open source projects
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions tests/data/src/core/struct.go → tests/data/core/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,28 @@ type a struct {
time.Time "tag"
*time.Duration
}

type b struct {
string
int
}

type name = string
type age int

type person struct {
name
age
}

func test() {
var a struct {
uint
string
} = struct {
uint
string
}{uint: 100}

var b struct{ string } = struct{ string }{}
}
Loading

0 comments on commit 9897007

Please sign in to comment.