Skip to content

Commit 73f31dc

Browse files
Merge pull request #6 from vojtech-dobes/improvements
Improvements
2 parents d4feb16 + 008f611 commit 73f31dc

36 files changed

+1096
-57
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ trim_trailing_whitespace = true
99
[*.{md,neon,yml}]
1010
indent_size = 2
1111
indent_style = space
12+
13+
[phpstan-baseline.neon]
14+
indent_style = tab

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"require-dev": {
3636
"nette/tester": "^2.5.4",
3737
"phpstan/phpstan": "^2.1.12",
38-
"spaze/phpstan-disallowed-calls": "^4.5.0"
38+
"spaze/phpstan-disallowed-calls": "^4.5.0",
39+
"tracy/tracy": "^2.10.9"
3940
},
4041
"scripts": {
4142
"phpstan": "phpstan analyse",

composer.lock

Lines changed: 76 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan-baseline.neon

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,16 @@ parameters:
2828
message: '#^Parameter \$column of class Vojtechdobes\\GrammarProcessing\\Location constructor expects int\<0, max\>, int given\.$#'
2929
identifier: argument.type
3030
count: 1
31-
path: src/GrammarProcessing/LexicalGrammar.php
31+
path: src/GrammarProcessing/LocationGetter.php
32+
33+
-
34+
message: '#^Parameter \#1 \$source of method Vojtechdobes\\GrammarProcessing\\Ebnf\\Parser\:\:parseGrammarFromSource\(\) expects string, string\|false given\.$#'
35+
identifier: argument.type
36+
count: 1
37+
path: tests/cases/Grammars/ebnfGrammar.Test.php
38+
39+
-
40+
message: '#^Parameter \#1 \$source of method Vojtechdobes\\GrammarProcessing\\Grammar\:\:parseSource\(\) expects string, string\|false given\.$#'
41+
identifier: argument.type
42+
count: 2
43+
path: tests/cases/Grammars/ebnfGrammar.Test.php
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Vojtechdobes\GrammarProcessing\Ebnf;
4+
5+
use Vojtechdobes\GrammarProcessing;
6+
7+
8+
final class GrammarFactory
9+
{
10+
11+
public function createGrammar(): GrammarProcessing\Grammar
12+
{
13+
$lexicalSymbols = [
14+
'letter' => new GrammarProcessing\Vocabulary\Regexp('[a-zA-Z]'),
15+
'digit' => new GrammarProcessing\Vocabulary\Regexp('[0-9]'),
16+
'symbol' => new GrammarProcessing\Vocabulary\OneOf([
17+
new GrammarProcessing\Vocabulary\Literal('['),
18+
new GrammarProcessing\Vocabulary\Literal(']'),
19+
new GrammarProcessing\Vocabulary\Literal('{'),
20+
new GrammarProcessing\Vocabulary\Literal('}'),
21+
new GrammarProcessing\Vocabulary\Literal('('),
22+
new GrammarProcessing\Vocabulary\Literal(')'),
23+
new GrammarProcessing\Vocabulary\Literal('<'),
24+
new GrammarProcessing\Vocabulary\Literal('>'),
25+
new GrammarProcessing\Vocabulary\Literal("'"),
26+
new GrammarProcessing\Vocabulary\Literal('"'),
27+
new GrammarProcessing\Vocabulary\Literal('='),
28+
new GrammarProcessing\Vocabulary\Literal('|'),
29+
new GrammarProcessing\Vocabulary\Literal('.'),
30+
new GrammarProcessing\Vocabulary\Literal(','),
31+
new GrammarProcessing\Vocabulary\Literal(';'),
32+
new GrammarProcessing\Vocabulary\Literal('-'),
33+
new GrammarProcessing\Vocabulary\Literal('+'),
34+
new GrammarProcessing\Vocabulary\Literal('*'),
35+
new GrammarProcessing\Vocabulary\Literal('?'),
36+
new GrammarProcessing\Vocabulary\Literal('\\'),
37+
]),
38+
'character' => new GrammarProcessing\Vocabulary\OneOf([
39+
new GrammarProcessing\Vocabulary\Nonterminal('letter'),
40+
new GrammarProcessing\Vocabulary\Nonterminal('digit'),
41+
new GrammarProcessing\Vocabulary\Nonterminal('symbol'),
42+
new GrammarProcessing\Vocabulary\Literal('_'),
43+
new GrammarProcessing\Vocabulary\Literal(' '),
44+
]),
45+
'identifier' => new GrammarProcessing\Vocabulary\Sequence([
46+
new GrammarProcessing\Vocabulary\Nonterminal('letter'),
47+
new GrammarProcessing\Vocabulary\Repeat(
48+
new GrammarProcessing\Vocabulary\OneOf([
49+
new GrammarProcessing\Vocabulary\Nonterminal('letter'),
50+
new GrammarProcessing\Vocabulary\Nonterminal('digit'),
51+
new GrammarProcessing\Vocabulary\Literal('_'),
52+
]),
53+
0,
54+
null,
55+
),
56+
]),
57+
'terminal' => new GrammarProcessing\Vocabulary\OneOf([
58+
new GrammarProcessing\Vocabulary\Sequence([
59+
new GrammarProcessing\Vocabulary\Literal("'"),
60+
new GrammarProcessing\Vocabulary\Repeat(
61+
new GrammarProcessing\Vocabulary\Subtract(
62+
new GrammarProcessing\Vocabulary\Nonterminal('character'),
63+
new GrammarProcessing\Vocabulary\Literal("'"),
64+
),
65+
1,
66+
null,
67+
),
68+
new GrammarProcessing\Vocabulary\Literal("'"),
69+
]),
70+
new GrammarProcessing\Vocabulary\Sequence([
71+
new GrammarProcessing\Vocabulary\Literal('"'),
72+
new GrammarProcessing\Vocabulary\Repeat(
73+
new GrammarProcessing\Vocabulary\Subtract(
74+
new GrammarProcessing\Vocabulary\Nonterminal('character'),
75+
new GrammarProcessing\Vocabulary\Literal('"'),
76+
),
77+
1,
78+
null,
79+
),
80+
new GrammarProcessing\Vocabulary\Literal('"'),
81+
]),
82+
]),
83+
'S' => new GrammarProcessing\Vocabulary\Repeat(
84+
new GrammarProcessing\Vocabulary\OneOf([
85+
new GrammarProcessing\Vocabulary\Literal(' '),
86+
new GrammarProcessing\Vocabulary\Regexp('\n'),
87+
new GrammarProcessing\Vocabulary\Regexp('\t'),
88+
new GrammarProcessing\Vocabulary\Regexp('\r'),
89+
new GrammarProcessing\Vocabulary\Regexp('\f'),
90+
new GrammarProcessing\Vocabulary\Regexp('\b'),
91+
]),
92+
1,
93+
null,
94+
),
95+
];
96+
97+
$syntacticSymbols = [
98+
'opt_S' => new GrammarProcessing\Vocabulary\Repeat(
99+
new GrammarProcessing\Vocabulary\Nonterminal('S'),
100+
0,
101+
null,
102+
),
103+
'terminator' => new GrammarProcessing\Vocabulary\OneOf([
104+
new GrammarProcessing\Vocabulary\Literal(';'),
105+
new GrammarProcessing\Vocabulary\Literal('.'),
106+
]),
107+
'term' => new GrammarProcessing\Vocabulary\OneOf([
108+
new GrammarProcessing\Vocabulary\Sequence([
109+
new GrammarProcessing\Vocabulary\Literal('('),
110+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
111+
new GrammarProcessing\Vocabulary\Nonterminal('rhs'),
112+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
113+
new GrammarProcessing\Vocabulary\Literal(')'),
114+
]),
115+
new GrammarProcessing\Vocabulary\Sequence([
116+
new GrammarProcessing\Vocabulary\Literal('['),
117+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
118+
new GrammarProcessing\Vocabulary\Nonterminal('rhs'),
119+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
120+
new GrammarProcessing\Vocabulary\Literal(']'),
121+
]),
122+
new GrammarProcessing\Vocabulary\Sequence([
123+
new GrammarProcessing\Vocabulary\Literal('{'),
124+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
125+
new GrammarProcessing\Vocabulary\Nonterminal('rhs'),
126+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
127+
new GrammarProcessing\Vocabulary\Literal('}'),
128+
]),
129+
new GrammarProcessing\Vocabulary\Nonterminal('terminal'),
130+
new GrammarProcessing\Vocabulary\Nonterminal('identifier'),
131+
]),
132+
'factor' => new GrammarProcessing\Vocabulary\OneOf([
133+
new GrammarProcessing\Vocabulary\Sequence([
134+
new GrammarProcessing\Vocabulary\Nonterminal('term'),
135+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
136+
new GrammarProcessing\Vocabulary\Literal('?'),
137+
]),
138+
new GrammarProcessing\Vocabulary\Sequence([
139+
new GrammarProcessing\Vocabulary\Nonterminal('term'),
140+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
141+
new GrammarProcessing\Vocabulary\Literal('*'),
142+
]),
143+
new GrammarProcessing\Vocabulary\Sequence([
144+
new GrammarProcessing\Vocabulary\Nonterminal('term'),
145+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
146+
new GrammarProcessing\Vocabulary\Literal('+'),
147+
]),
148+
new GrammarProcessing\Vocabulary\Sequence([
149+
new GrammarProcessing\Vocabulary\Nonterminal('term'),
150+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
151+
new GrammarProcessing\Vocabulary\Literal('-'),
152+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
153+
new GrammarProcessing\Vocabulary\Nonterminal('term'),
154+
]),
155+
new GrammarProcessing\Vocabulary\Sequence([
156+
new GrammarProcessing\Vocabulary\Nonterminal('term'),
157+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
158+
]),
159+
]),
160+
'concatenation' => new GrammarProcessing\Vocabulary\Sequence([
161+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
162+
new GrammarProcessing\Vocabulary\Nonterminal('factor'),
163+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
164+
new GrammarProcessing\Vocabulary\Repeat(
165+
new GrammarProcessing\Vocabulary\Sequence([
166+
new GrammarProcessing\Vocabulary\Literal(','),
167+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
168+
new GrammarProcessing\Vocabulary\Nonterminal('factor'),
169+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
170+
]),
171+
0,
172+
null,
173+
),
174+
]),
175+
'alternation' => new GrammarProcessing\Vocabulary\Sequence([
176+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
177+
new GrammarProcessing\Vocabulary\Nonterminal('concatenation'),
178+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
179+
new GrammarProcessing\Vocabulary\Repeat(
180+
new GrammarProcessing\Vocabulary\Sequence([
181+
new GrammarProcessing\Vocabulary\Literal('|'),
182+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
183+
new GrammarProcessing\Vocabulary\Nonterminal('concatenation'),
184+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
185+
]),
186+
0,
187+
null,
188+
),
189+
]),
190+
'rhs' => new GrammarProcessing\Vocabulary\Nonterminal('alternation'),
191+
'lhs' => new GrammarProcessing\Vocabulary\Nonterminal('identifier'),
192+
'rule' => new GrammarProcessing\Vocabulary\Sequence([
193+
new GrammarProcessing\Vocabulary\Nonterminal('lhs'),
194+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
195+
new GrammarProcessing\Vocabulary\Literal('='),
196+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
197+
new GrammarProcessing\Vocabulary\Nonterminal('rhs'),
198+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
199+
new GrammarProcessing\Vocabulary\Nonterminal('terminator'),
200+
]),
201+
'grammar' => new GrammarProcessing\Vocabulary\Repeat(
202+
new GrammarProcessing\Vocabulary\Sequence([
203+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
204+
new GrammarProcessing\Vocabulary\Nonterminal('rule'),
205+
new GrammarProcessing\Vocabulary\Nonterminal('opt_S'),
206+
]),
207+
0,
208+
null,
209+
),
210+
];
211+
212+
return new GrammarProcessing\Grammar(
213+
lexicalSymbols: $lexicalSymbols,
214+
syntaxTokenSymbols: [
215+
'identifier',
216+
'terminal',
217+
'symbol',
218+
'S',
219+
],
220+
ignoredTokenSymbols: [],
221+
syntacticSymbols: $syntacticSymbols,
222+
);
223+
}
224+
225+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Vojtechdobes\GrammarProcessing\Ebnf;
4+
5+
use Vojtechdobes\GrammarProcessing;
6+
7+
8+
final class InterpretationFactory
9+
{
10+
11+
/**
12+
* @template TLexicalSymbol of string
13+
* @template TSyntaxTokenSymbol of TLexicalSymbol
14+
* @template TIgnoredTokenSymbol of TLexicalSymbol
15+
* @param non-empty-list<TLexicalSymbol> $lexicalSymbols
16+
* @param non-empty-list<TSyntaxTokenSymbol> $syntaxTokenSymbols
17+
* @param list<TIgnoredTokenSymbol> $ignoredTokenSymbols
18+
*/
19+
public function createInterpretation(
20+
array $lexicalSymbols,
21+
array $syntaxTokenSymbols,
22+
array $ignoredTokenSymbols,
23+
): GrammarProcessing\Interpretation
24+
{
25+
return new GrammarProcessing\Interpretation([
26+
'alternation' => new Nodes\Alternation(),
27+
'concatenation' => new Nodes\Concatenation(),
28+
'factor' => new Nodes\Factor(),
29+
'grammar' => new Nodes\Grammar(
30+
lexicalSymbols: $lexicalSymbols,
31+
syntaxTokenSymbols: $syntaxTokenSymbols,
32+
ignoredTokenSymbols: $ignoredTokenSymbols,
33+
),
34+
'identifier' => new Nodes\Identifier(),
35+
'lhs' => new Nodes\Lhs(),
36+
'rhs' => new Nodes\Rhs(),
37+
'rule' => new Nodes\Rule(),
38+
'term' => new Nodes\Term(),
39+
'terminal' => new Nodes\Terminal(),
40+
]);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)