Skip to content

Commit 063ecf9

Browse files
committed
Injected Conveyor using. Parsing works except fields
1 parent c446aad commit 063ecf9

File tree

7 files changed

+167
-90
lines changed

7 files changed

+167
-90
lines changed

FieldModel.php-data

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace PHPDataGen\Model;
2+
3+
class FieldModel {
4+
5+
}
6+
/*
7+
addField(
8+
(new FieldBuilder())->
9+
setName('name')->
10+
setType('string')
11+
)->
12+
13+
addField(
14+
(new FieldBuilder())->
15+
setName('editable')->
16+
setType('bool')->
17+
setDefault('false')
18+
)->
19+
20+
addField(
21+
(new FieldBuilder())->
22+
setName('direct')->
23+
setType('bool')->
24+
setDefault('false')
25+
)->
26+
27+
addField(
28+
(new FieldBuilder())->
29+
setName('type')->
30+
setType('string')
31+
)->
32+
33+
addField(
34+
(new FieldBuilder())->
35+
setName('validators')->
36+
setType('array')
37+
)->
38+
39+
addField(
40+
(new FieldBuilder())->
41+
setName('hasDefault')->
42+
setType('bool')->
43+
setDefault('false')
44+
)->
45+
46+
addField(
47+
(new FieldBuilder())->
48+
setName('filterDefault')->
49+
setType('bool')->
50+
setDefault('true')
51+
)->
52+
53+
addField(
54+
(new FieldBuilder())->
55+
setName('default')
56+
);
57+
*/

Test.php-data

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Foo;
2+
3+
use Foo\Bar;
4+
use Foo\Baz as A;
5+
6+
class Test extends \B implements Bar, A {
7+
8+
}
9+
10+
class Test1 extends C {
11+
12+
}

app/Parsing/ClassState.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
* Class parsing state
3131
*/
32-
class ClassState extends State {
32+
class ClassState implements State {
3333

3434
/**
3535
* @var State Parent state
@@ -72,55 +72,51 @@ public function getBuilder(): ClassBuilder {
7272
return $this->builder;
7373
}
7474

75-
public function step(string &$next): State {
75+
public function step(Conveyor $conveyor): State {
7676
switch ($this->state) {
7777
case 0:
78-
$this->builder->setName(Parser::readClassname($next));
78+
$this->builder->setName($conveyor->readClassname());
7979

8080
$this->state = 1;
8181
return $this;
8282

8383
case 1:
84-
if (strpos($next, 'extends') === 0) {
84+
if ($conveyor->readOperator('extends')) {
8585
$this->state = 2;
8686

87-
$next = substr($next, 7);
8887
return $this;
8988
}
9089

91-
if (strpos($next, 'implements') === 0) {
90+
if ($conveyor->readOperator('implements')) {
9291
$this->state = 3;
9392

94-
$next = substr($next, 10);
9593
return $this;
9694
}
9795

98-
if (strpos($next, '{') === 0) {
96+
if ($conveyor->readOperator('{')) {
9997
$this->state = 5;
10098

101-
$next = substr($next, 1);
10299
return $this;
103100
}
104101

105102
throw new \Exception('Open bracket not found');
106103

107104
case 2:
108-
$this->builder->setExtends(Parser::readExtendedClassname($next));
105+
$this->builder->setExtends($conveyor->readExtendedClassname());
109106

110107
$this->state = 1;
111108
return $this;
112109

113110
case 3:
114-
$this->builder->addImplement(Parser::readExtendedClassname($next));
111+
$this->builder->addImplement($conveyor->readExtendedClassname());
115112

116113
$this->state = 4;
117114
return $this;
118115

119116
case 4:
120-
if (strpos($next, ',') === 0) {
117+
if ($conveyor->readOperator(',')) {
121118
$this->state = 3;
122119

123-
$next = substr($next, 1);
124120
return $this;
125121
}
126122

@@ -129,18 +125,18 @@ public function step(string &$next): State {
129125

130126
case 5:
131127
if (
132-
strpos($next, 'direct') === 0 ||
133-
strpos($next, 'val') === 0 ||
134-
strpos($next, 'var') === 0
128+
$conveyor->readOperator('direct') ||
129+
$conveyor->readOperator('val') ||
130+
$conveyor->readOperator('var')
135131
) {
136132
$this->state = 6;
137133

138134
$this->field = new FieldState($this);
139135
return $this->field;
140136
}
141137

142-
if (strpos($next, '}') === 0) {
143-
$next = substr($next, 1);
138+
if ($conveyor->readOperator('}')) {
139+
$this->parent->step($conveyor);
144140

145141
return $this->parent;
146142
}

app/Parsing/Conveyor.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class Conveyor {
3434
*/
3535
protected $next = null;
3636

37+
/**
38+
* @var int Next string length
39+
*/
40+
protected $length = 0;
41+
3742
/**
3843
* @var int Current cursor line
3944
*/
@@ -49,6 +54,16 @@ class Conveyor {
4954
*/
5055
public function __construct(string $source) {
5156
$this->next = $source;
57+
$this->length = strlen($source);
58+
}
59+
60+
/**
61+
* Returns next string length
62+
*
63+
* @return int
64+
*/
65+
public function length(): int {
66+
return $this->length;
5267
}
5368

5469
/**
@@ -72,6 +87,7 @@ public function move(int $offset) {
7287
}
7388

7489
$this->next = substr($this->next, $offset);
90+
$this->length -= $offset;
7591
}
7692

7793
/**
@@ -90,6 +106,7 @@ public function readOperator(string $operator, bool $required = false): bool {
90106
}
91107

92108
if ($required) {
109+
// TODO Parsing exception class
93110
throw new \Exception("$operator not found at {$this->line} line {$this->row} row");
94111
}
95112

@@ -100,7 +117,20 @@ public function readOperator(string $operator, bool $required = false): bool {
100117
* Moves conveyor to next not-space symbol
101118
*/
102119
public function skipSpaces() {
103-
if (preg_match('/^\s*/', $this->next, $matches) === 1) {
120+
if (preg_match('/^\\s*/', $this->next, $matches) === 1) {
121+
$this->move(strlen($matches[0]));
122+
}
123+
}
124+
125+
/**
126+
* Skips comment if exists
127+
*/
128+
public function skipComment() {
129+
if (preg_match('/^\\/\\*[\\s\\S]*\\*\\//', $this->next, $matches) === 1) {
130+
$this->move(strlen($matches[0]));
131+
}
132+
133+
if (preg_match('/^\\/\\/.*/', $this->next, $matches) === 1) {
104134
$this->move(strlen($matches[0]));
105135
}
106136
}
@@ -112,7 +142,7 @@ public function skipSpaces() {
112142
* @return string Namespace
113143
*/
114144
public function readNamespace(): string {
115-
if (preg_match('/^[a-z_][\w\\]*\w/i', $this->next, $matches) === 1) {
145+
if (preg_match('/^[a-z_][\\w\\\\]*\\w/i', $this->next, $matches) === 1) {
116146
$this->move(strlen($matches[0]));
117147

118148
return $matches[0];
@@ -128,7 +158,7 @@ public function readNamespace(): string {
128158
* @return string Class name
129159
*/
130160
public function readClassname(): string {
131-
if (preg_match('/^[a-z_]\w*/i', $this->next, $matches) === 1) {
161+
if (preg_match('/^[a-z_]\\w*/i', $this->next, $matches) === 1) {
132162
$this->move(strlen($matches[0]));
133163

134164
return $matches[0];
@@ -144,13 +174,13 @@ public function readClassname(): string {
144174
* @return string Extended class name
145175
*/
146176
public function readExtendedClassname(): string {
147-
if (preg_match('/^(\\[a-z_]\w*)+/i', $this->next, $matches) === 1) {
177+
if (preg_match('/^(\\\\[a-z_]\\w*)+/i', $this->next, $matches) === 1) {
148178
$this->move(strlen($matches[0]));
149179

150180
return $matches[0];
151181
}
152182

153-
return static::readClassname($next);
183+
return $this->readClassname();
154184
}
155185

156186
/**

app/Parsing/FileState.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
* File parsing state
3131
*/
32-
class FileState extends State {
32+
class FileState implements State {
3333

3434
/**
3535
* @var FileBuilder Builder
@@ -68,73 +68,70 @@ public function getBuilder(): FileBuilder {
6868
return $this->builder;
6969
}
7070

71-
public function step(string &$next): State {
71+
public function step(Conveyor $conveyor): State {
7272
switch ($this->state) {
7373
case 0:
74-
if (strpos($next, 'namespace') === 0) {
74+
if ($conveyor->readOperator('namespace')) {
7575
$this->state = 1;
7676

77-
$next = substr($next, 9);
7877
return $this;
7978
}
8079

81-
if (strpos($next, 'use') === 0) {
80+
if ($conveyor->readOperator('use')) {
8281
$this->state = 3;
8382

84-
$next = substr($next, 3);
8583
return $this;
8684
}
8785

88-
if (strpos($next, 'class') === 0) {
89-
$this->state = 6;
86+
if ($conveyor->readOperator('class')) {
9087
$this->class = new ClassState($this);
9188

92-
$next = substr($next, 5);
89+
$this->state = 6;
9390
return $this->class;
9491
}
9592

96-
return parent::step($next);
93+
return $this;
9794

9895
case 1:
99-
$this->builder->setNamespace(Parser::readNamespace($next));
96+
$this->builder->setNamespace($conveyor->readNamespace());
10097

10198
$this->state = 2;
10299
return $this;
103100

104101
case 2:
105-
Parser::readSemicolon($next);
102+
$conveyor->readSemicolon();
106103

107104
$this->state = 0;
108105
return $this;
109106

110107
case 3:
111-
$this->useBuffer = Parser::readNamespace($next);
108+
$this->useBuffer = $conveyor->readNamespace();
112109

113110
$this->state = 5;
114111
return $this;
115112

116113
case 4:
117-
$this->builder->addUse($this->useBuffer, Parser::readClassname($next));
114+
$this->builder->addUse($this->useBuffer, $conveyor->readClassname());
118115
$this->useBuffer = null;
119116

120117
$this->state = 5;
121118
return $this;
122119

123120
case 5:
124121
if (is_null($this->useBuffer)) {
125-
Parser::readSemicolon($next);
122+
$conveyor->readSemicolon();
123+
124+
$this->state = 0;
126125
return $this;
127126
}
128127

129-
if (strpos($next, 'as') === 0) {
128+
if ($conveyor->readOperator('as')) {
130129
$this->state = 4;
131-
132-
$next = substr($next, 2);
133130
} else {
134131
$this->builder->addUse($this->useBuffer);
132+
$this->useBuffer = null;
135133
}
136134

137-
Parser::readSemicolon($next);
138135
return $this;
139136

140137
case 6:

0 commit comments

Comments
 (0)