Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes some problems with syntax #12

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions ebnf.pp2
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
%token T_TABLE_SETTING_INCREMENT (?<=\b)increment\b
%token T_TABLE_SETTING_DEFAULT (?<=\b)default\b

%token T_TABLE_COLUMN_SIZE \([0-9\,]+\)

%token T_TABLE_SETTING_NOT_NULL (?<=\b)not\snull\b

%token T_REF_ACTION_CASCADE (?<=\b)cascade\b
Expand All @@ -35,6 +37,7 @@
%token T_REF_ACTION_SET_DEFAULT (?<=\b)set\default\b
%token T_REF_ACTION_NO_ACTION (?<=\b)no\saction\b

%token T_REF_ACTION_DESTROY (?<=\b)destroy\b
%token T_REF_ACTION_DELETE (?<=\b)delete\b
%token T_REF_ACTION_UPDATE (?<=\b)update\b

Expand All @@ -47,7 +50,7 @@
* --------------------------------------------------------------------------
*/
%token T_FLOAT [0-9]+\.[0-9]+ // 123.123
%token T_INT [0-9]+ // 123
%token T_INT ^[0-9]+$ // 123

/**
* --------------------------------------------------------------------------
Expand Down Expand Up @@ -229,9 +232,13 @@
<T_WORD>
;

#TableColumnTypeSize
#TableColumnTypeSize -> {
return new \Butschster\Dbml\Ast\Table\Column\SizeNode(
$token->getOffset(), $children->getValue()
);
}
:
::T_LPAREN:: Int() ::T_RPAREN::
<T_TABLE_COLUMN_SIZE>
;

TableColumnSettings
Expand Down Expand Up @@ -507,7 +514,10 @@ OnDeleteAction -> {
);
}
:
<T_REF_ACTION_DELETE>
(
<T_REF_ACTION_DELETE> |
<T_REF_ACTION_DESTROY>
)
::T_COLON::
RefAction()
;
Expand Down
24 changes: 24 additions & 0 deletions src/Ast/Table/Column/SizeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Butschster\Dbml\Ast\Table\Column;

class SizeNode
{
public function __construct(
private int $offset,
private string $value,
) {
}

public function getOffset(): int
{
return $this->offset;
}

public function getValue(): array
{
return array_map('intval', explode(',', substr($this->value, 1, -1)));
}
}
20 changes: 14 additions & 6 deletions src/Ast/Table/Column/TypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
namespace Butschster\Dbml\Ast\Table\Column;

use Butschster\Dbml\Ast\Table\NameNode;
use Butschster\Dbml\Ast\Values\IntNode;

class TypeNode
{
private string $name;
private ?int $size = null;
private ?array $size = null;

public function __construct(
private int $offset, NameNode $type, ?IntNode $size = null
)
{
private int $offset,
NameNode $type,
?SizeNode $size = null,
) {
$this->name = $type->getValue();
$this->size = $size ? $size->getValue() : null;
}
Expand All @@ -37,6 +37,14 @@ public function getName(): string
*/
public function getSize(): ?int
{
return $this->size;
return $this->size[0] ?? null;
}

/**
* Get max size as array
*/
public function getSizeArray(): array
{
return $this->size ?? [];
}
}
22 changes: 20 additions & 2 deletions src/Ast/Values/StringNode.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Butschster\Dbml\Ast\Values;
Expand All @@ -12,10 +13,27 @@ public function __construct(int $offset, Token $token)
{
$value = match ($token->getName()) {
'T_WORD' => $token->getValue(),
'T_QUOTED_STRING' => $this->unquoteTokenValue($token->getValue())
'T_QUOTED_STRING' => $this->unquoteTokenValue($token->getValue()),
};

parent::__construct($offset, $value);
parent::__construct($offset, $this->convertType($value));
}

private function convertType(string $value): mixed
{
if (ctype_digit($value)) {
return (int)$value;
}

if (is_numeric($value)) {
return (float)$value;
}

if (in_array(strtolower($value), ['true', 'false'])) {
return strtolower($value) === 'true';
}

return $value;
}

private function unquoteTokenValue(string $value): string
Expand Down
470 changes: 239 additions & 231 deletions src/grammar.php

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions tests/Ast/Table/Column/SizeNodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Butschster\Tests\Ast\Table\Column;

use Butschster\Dbml\Ast\Table\Column\SizeNode;
use Butschster\Tests\Ast\TestCase;

class SizeNodeTest extends TestCase
{
function test_gets_offset()
{
$node = new SizeNode(123, '(1)');
$this->assertEquals(123, $node->getOffset());
}

function test_gets_value()
{
$node = new SizeNode(123, '(1)');
$this->assertSame([1], $node->getValue());
}

function test_gets_coma_separated_value()
{
$node = new SizeNode(123, '(8,2)');
$this->assertSame([8,2], $node->getValue());
}
}
11 changes: 7 additions & 4 deletions tests/Parsers/EnumParserTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Butschster\Tests\Parsers;
Expand All @@ -10,15 +11,17 @@ class EnumParserTest extends TestCase
*/
function test_enum_should_be_parsed(string $name)
{
$this->assertAst(<<<DBML
$this->assertAst(
<<<DBML
{$name} job_status {
created [note: 'Waiting to be processed']
running
done
failure
}
DBML
, <<<AST
,
<<<AST
<Schema offset="0">
<Enum offset="0">
<EnumName offset="5">
Expand All @@ -43,15 +46,15 @@ function test_enum_should_be_parsed(string $name)
</EnumValue>
</Enum>
</Schema>
AST
AST,
);
}

public function enumKeyNames()
{
return [
['enum'],
['Enum']
['Enum'],
];
}
}
5 changes: 3 additions & 2 deletions tests/Parsers/ForeignKeysParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ function test_column_many_to_one_relation_should_be_parsed()
);
}


function test_column_relation_with_composite_key_should_be_parsed()
{
$this->assertAst(<<<DBML
Expand Down Expand Up @@ -324,7 +323,7 @@ function test_relationship_long_form_with_name_should_be_parsed()
function test_relationship_with_settings()
{
$this->assertAst(<<<DBML
Ref: products.merchant_id > merchants.id [delete: cascade, update: no action]
Ref: products.merchant_id > merchants.id [delete: cascade, update: no action, destroy: cascade]
DBML
, <<<AST
<Schema offset="0">
Expand All @@ -350,6 +349,8 @@ function test_relationship_with_settings()
<T_REF_ACTION_CASCADE offset="50">cascade</T_REF_ACTION_CASCADE>
<T_REF_ACTION_UPDATE offset="59">update</T_REF_ACTION_UPDATE>
<T_REF_ACTION_NO_ACTION offset="67">no action</T_REF_ACTION_NO_ACTION>
<T_REF_ACTION_DESTROY offset="78">destroy</T_REF_ACTION_DESTROY>
<T_REF_ACTION_CASCADE offset="87">cascade</T_REF_ACTION_CASCADE>
</Ref>
</Schema>
AST
Expand Down
12 changes: 6 additions & 6 deletions tests/Parsers/FullSchemaParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ function test_parse_schema()
<T_TABLE_SETTING_NOT_NULL offset="242">not null</T_TABLE_SETTING_NOT_NULL>
<T_TABLE_SETTING_UNIQUE offset="252">unique</T_TABLE_SETTING_UNIQUE>
<T_TABLE_SETTING_DEFAULT offset="260">default</T_TABLE_SETTING_DEFAULT>
<Int offset="269">
<T_INT offset="269">1</T_INT>
</Int>
<String offset="269">
<T_WORD offset="269">1</T_WORD>
</String>
</TableColumn>
<TableColumn offset="274">
<TableColumnName offset="274">
Expand Down Expand Up @@ -244,9 +244,9 @@ function test_parse_schema()
</TableColumnTypeName>
</TableColumnType>
<T_TABLE_SETTING_DEFAULT offset="859">default</T_TABLE_SETTING_DEFAULT>
<Int offset="868">
<T_INT offset="868">1</T_INT>
</Int>
<String offset="868">
<T_WORD offset="868">1</T_WORD>
</String>
</TableColumn>
</Table>
<Ref offset="891">
Expand Down
Loading
Loading