Skip to content

Commit

Permalink
allow empty strings and numbers as parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Dec 9, 2018
1 parent c976e8e commit 93eecd5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
42 changes: 24 additions & 18 deletions src/Type/InnerParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,53 @@ public function __construct()
'skip' => '\s+',
'parenthesis_' => '<',
'_parenthesis' => '>',
'empty_string' => '""|\'\'',
'number' => '(\+|\-)?(0|[1-9]\d*)(\.\d+)?',
'comma' => ',',
'name' => '(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*',
'quote_:quoted_string' => '"',
'apostrophe_:apostrophed_string' => '\'',
],
'quoted_string' => [
'quoted_string' => '(?:[^"]|"")+',
'quoted_string' => '[^"]+',
'_quote:default' => '"',
],
'apostrophed_string' => [
'apostrophed_string' => '(?:[^\']|\'\')+',
'apostrophed_string' => '[^\']+',
'_apostrophe:default' => '\'',
],
],
[
'type' => new Choice('type', ['simple_type', 'compound_type'], null),
1 => new Token(1, 'name', null, -1, true),
2 => new Concatenation(2, [1], '#simple_type'),
3 => new Token(3, 'quote_', null, -1, false),
4 => new Token(4, 'quoted_string', null, -1, true),
5 => new Token(5, '_quote', null, -1, false),
6 => new Concatenation(6, [3, 4, 5], '#simple_type'),
7 => new Token(7, 'apostrophe_', null, -1, false),
8 => new Token(8, 'apostrophed_string', null, -1, true),
9 => new Token(9, '_apostrophe', null, -1, false),
3 => new Token(3, 'number', null, -1, true),
4 => new Concatenation(4, [3], '#simple_type'),
5 => new Token(5, 'empty_string', null, -1, true),
6 => new Concatenation(6, [5], '#simple_type'),
7 => new Token(7, 'quote_', null, -1, false),
8 => new Token(8, 'quoted_string', null, -1, true),
9 => new Token(9, '_quote', null, -1, false),
10 => new Concatenation(10, [7, 8, 9], '#simple_type'),
'simple_type' => new Choice('simple_type', [2, 6, 10], null),
12 => new Token(12, 'name', null, -1, true),
13 => new Token(13, 'parenthesis_', null, -1, false),
14 => new Token(14, 'comma', null, -1, false),
15 => new Concatenation(15, [14, 'type'], '#compound_type'),
16 => new Repetition(16, 0, -1, 15, null),
17 => new Token(17, '_parenthesis', null, -1, false),
'compound_type' => new Concatenation('compound_type', [12, 13, 'type', 16, 17], null),
11 => new Token(11, 'apostrophe_', null, -1, false),
12 => new Token(12, 'apostrophed_string', null, -1, true),
13 => new Token(13, '_apostrophe', null, -1, false),
14 => new Concatenation(14, [11, 12, 13], '#simple_type'),
'simple_type' => new Choice('simple_type', [2, 4, 6, 10, 14], null),
16 => new Token(16, 'name', null, -1, true),
17 => new Token(17, 'parenthesis_', null, -1, false),
18 => new Token(18, 'comma', null, -1, false),
19 => new Concatenation(19, [18, 'type'], '#compound_type'),
20 => new Repetition(20, 0, -1, 19, null),
21 => new Token(21, '_parenthesis', null, -1, false),
'compound_type' => new Concatenation('compound_type', [16, 17, 'type', 20, 21], null),
],
[]
);

$this->getRule('type')->setPPRepresentation(' simple_type() | compound_type()');
$this->getRule('simple_type')->setDefaultId('#simple_type');
$this->getRule('simple_type')->setPPRepresentation(' <name> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe::');
$this->getRule('simple_type')->setPPRepresentation(' <name> | <number> | <empty_string> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe::');
$this->getRule('compound_type')->setDefaultId('#compound_type');
$this->getRule('compound_type')->setPPRepresentation(' <name> ::parenthesis_:: type() ( ::comma:: type() )* ::_parenthesis::');
}
Expand Down
8 changes: 8 additions & 0 deletions src/Type/TypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ private function visitSimpleType(TreeNode $element)
return ['name' => $value, 'params' => []];
}

if ('empty_string' === $token) {
return '';
}

if ('number' === $token) {
return false === strpos($value, '.') ? intval($value) : floatval($value);
}

$escapeChar = 'quoted_string' === $token ? '"' : "'";

if (false === strpos($value, $escapeChar)) {
Expand Down
8 changes: 6 additions & 2 deletions src/Type/grammar.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

%token parenthesis_ <
%token _parenthesis >
%token empty_string ""|''
%token number (\+|\-)?(0|[1-9]\d*)(\.\d+)?
%token comma ,
%token name (?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

%token quote_ " -> quoted_string
%token quoted_string:quoted_string (?:[^"]|"")+
%token quoted_string:quoted_string [^"]+
%token quoted_string:_quote " -> default
%token apostrophe_ ' -> apostrophed_string
%token apostrophed_string:apostrophed_string (?:[^']|'')+
%token apostrophed_string:apostrophed_string [^']+
%token apostrophed_string:_apostrophe ' -> default
type:
simple_type() | compound_type()
#simple_type:
<name>
| <number>
| <empty_string>
| ::quote_:: <quoted_string> ::_quote::
| ::apostrophe_:: <apostrophed_string> ::_apostrophe::
Expand Down
28 changes: 20 additions & 8 deletions tests/Serializer/Type/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ public function validTypesProvider(): iterable
'array<Foo>',
$type('array', [['name' => 'Foo', 'params' => []]]),
];
yield [
'Foo<\'a\'>',
$type('Foo', ['a']),
];
yield [
'Foo<5>',
$type('Foo', [5]),
];
yield [
'Foo<5.5>',
$type('Foo', [5.5]),
];
yield [
'Foo<\'a\',\'b\',\'c\'>',
$type('Foo', ['a', 'b', 'c']),
];
yield [
'Foo<\'a\',\'\'>',
$type('Foo', ['a', '']),
];
yield [
'array<Foo,Bar>',
$type('array', [['name' => 'Foo', 'params' => []], ['name' => 'Bar', 'params' => []]]),
Expand All @@ -72,14 +92,6 @@ public function validTypesProvider(): iterable
'Foo<"asdf asdf">',
$type('Foo', ['asdf asdf']),
];
yield [
'Foo<"""bar""">',
$type('Foo', ['"bar"']),
];
yield [
"Foo<'a''b'>",
$type('Foo', ["a'b"]),
];
}

public function testEmptyString(): void
Expand Down

0 comments on commit 93eecd5

Please sign in to comment.