Skip to content

Commit fa3be9f

Browse files
committed
Changed expanderName definition
1 parent 1339c25 commit fa3be9f

File tree

4 files changed

+41
-67
lines changed

4 files changed

+41
-67
lines changed

src/Coduo/PHPMatcher/Lexer.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ class Lexer extends AbstractLexer
88
{
99
const T_NONE = 1;
1010
const T_EXPANDER_NAME = 2;
11-
const T_OPEN_PARENTHESIS = 3;
12-
const T_CLOSE_PARENTHESIS = 4;
13-
const T_STRING = 5;
14-
const T_NUMBER = 6;
15-
const T_BOOLEAN = 7;
16-
const T_NULL = 8;
17-
const T_COMMA = 9;
18-
const T_TYPE_PATTERN = 10;
11+
const T_CLOSE_PARENTHESIS = 3;
12+
const T_STRING = 4;
13+
const T_NUMBER = 5;
14+
const T_BOOLEAN = 6;
15+
const T_NULL = 7;
16+
const T_COMMA = 8;
17+
const T_TYPE_PATTERN = 9;
1918

2019
/**
2120
* Lexical catchable patterns.
@@ -25,11 +24,9 @@ class Lexer extends AbstractLexer
2524
protected function getCatchablePatterns()
2625
{
2726
return array(
28-
"\\.[a-zA-Z0-9_]+", // expander name
29-
"[a-zA-Z0-9_\\.]*", // none
30-
"\\-?[0-9.]+", // numbers
31-
"(true|false)", // boolean
32-
"null", // null
27+
"\\.?[a-zA-Z0-9_]+\\(", // expander name
28+
"[a-zA-Z0-9.]*", // words
29+
"\\-?[0-9]*\\.?[0-9]*", // numbers
3330
"'(?:[^']|'')*'", // string between ' character
3431
"\"(?:[^\"]|\"\")*\"", // string between " character,
3532
"@[a-zA-Z0-9\\*]+@", // type pattern
@@ -57,8 +54,6 @@ protected function getNonCatchablePatterns()
5754
protected function getType(&$value)
5855
{
5956
switch($value) {
60-
case '(':
61-
return self::T_OPEN_PARENTHESIS;
6257
case ')':
6358
return self::T_CLOSE_PARENTHESIS;
6459
case ',':
@@ -97,7 +92,7 @@ protected function getType(&$value)
9792
}
9893

9994
if ($this->isExpanderNameToken($value)) {
100-
$value = ltrim($value, '.');
95+
$value = rtrim(ltrim($value, '.'), '(');
10196
return self::T_EXPANDER_NAME;
10297
}
10398

@@ -146,7 +141,7 @@ protected function extractStringValue($value)
146141
*/
147142
protected function isExpanderNameToken($value)
148143
{
149-
return $value[0] === '.' && strlen($value) > 1;
144+
return substr($value, -1) === '(' && strlen($value) > 1;
150145
}
151146

152147
/**

src/Coduo/PHPMatcher/Parser.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,7 @@ private function getNextExpanderNode()
140140
return ;
141141
}
142142

143-
if ($this->lexer->lookahead['type'] !== Lexer::T_EXPANDER_NAME) {
144-
$this->unexpectedSyntaxError($this->lexer->lookahead, ".expanderName(args) definition");
145-
}
146-
$expander = new AST\Expander($this->lexer->lookahead['value']);
147-
148-
if (!$this->isNextOpenParenthesis()) {
149-
$this->unexpectedSyntaxError($this->lexer->lookahead, "(");
150-
}
143+
$expander = new AST\Expander($this->getExpanderName());
151144

152145
if ($this->endOfPattern()) {
153146
$this->unexpectedEndOfString(")");
@@ -166,6 +159,21 @@ private function getNextExpanderNode()
166159
return $expander;
167160
}
168161

162+
/**
163+
* @return mixed
164+
* @throws PatternException
165+
*/
166+
private function getExpanderName()
167+
{
168+
if ($this->lexer->lookahead['type'] !== Lexer::T_EXPANDER_NAME) {
169+
$this->unexpectedSyntaxError($this->lexer->lookahead, ".expanderName(args) definition");
170+
}
171+
$expander = $this->lexer->lookahead['value'];
172+
$this->lexer->moveNext();
173+
174+
return $expander;
175+
}
176+
169177
/**
170178
* Add arguments to expander
171179
*
@@ -175,7 +183,6 @@ private function addArgumentValues(AST\Expander $expander)
175183
{
176184
while(($argument = $this->getNextArgumentValue()) !== null) {
177185
$argument = ($argument === self::NULL_VALUE) ? null : $argument;
178-
179186
$expander->addArgument($argument);
180187
if (!$this->lexer->isNextToken(Lexer::T_COMMA)){
181188
break;
@@ -221,27 +228,15 @@ private function getNextArgumentValue()
221228
return $argument;
222229
}
223230

224-
/**
225-
* @return bool
226-
*/
227-
private function isNextOpenParenthesis()
228-
{
229-
$this->lexer->moveNext();
230-
$isOpenParenthesis = $this->lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS);
231-
$this->lexer->moveNext();
232-
233-
return $isOpenParenthesis;
234-
}
235-
236231
/**
237232
* @return bool
238233
*/
239234
private function isNextCloseParenthesis()
240235
{
241-
$isOpenParenthesis = $this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
236+
$isCloseParenthesis = $this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS);
242237
$this->lexer->moveNext();
243238

244-
return $isOpenParenthesis;
239+
return $isCloseParenthesis;
245240
}
246241

247242
/**

tests/Coduo/PHPMatcher/LexerTest.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function test_number_values($value, $expectedValue)
3636
$lexer->setInput($value);
3737
$lexer->moveNext();
3838
$this->assertEquals($lexer->lookahead['type'], Lexer::T_NUMBER);
39-
$this->assertEquals($lexer->lookahead['value'], $expectedValue);
39+
$this->assertEquals($expectedValue, $lexer->lookahead['value']);
4040
}
4141

4242
public static function validNumberValuesProvider()
@@ -118,14 +118,6 @@ public static function validNonTokenValuesProvider()
118118
);
119119
}
120120

121-
public function test_open_parenthesis()
122-
{
123-
$lexer = new Lexer();
124-
$lexer->setInput('(');
125-
$lexer->moveNext();
126-
$this->assertEquals($lexer->lookahead['type'], Lexer::T_OPEN_PARENTHESIS);
127-
}
128-
129121
public function test_close_parenthesis()
130122
{
131123
$lexer = new Lexer();
@@ -169,26 +161,27 @@ public static function validMatcherTypePatterns()
169161
/**
170162
* @dataProvider validExpanderNamesProvider
171163
*/
172-
public function test_expander_name($value)
164+
public function test_expander_name($value, $expectedTokenValue)
173165
{
174166
$lexer = new Lexer();
175167
$lexer->setInput($value);
176168
$lexer->moveNext();
177169
$this->assertEquals($lexer->lookahead['type'], Lexer::T_EXPANDER_NAME);
178-
$this->assertEquals($lexer->lookahead['value'], ltrim($value, "."));
170+
$this->assertEquals($lexer->lookahead['value'], $expectedTokenValue);
179171
}
180172

181173
public static function validExpanderNamesProvider()
182174
{
183175
return array(
184-
array(".expanderName"),
185-
array(".e")
176+
array("expanderName(", "expanderName"),
177+
array("e(", "e"),
178+
array(".e(", "e")
186179
);
187180
}
188181

189182
public function test_ignore_whitespaces_between_parenthesis()
190183
{
191-
$expectedTokens = array("type", "expander", "(", "arg1", ",", 2, ",", "arg3", ",", 4, ")");
184+
$expectedTokens = array("type", "expander", "arg1", ",", 2, ",", "arg3", ",", 4, ")");
192185
$lexer = new Lexer();
193186
$lexer->setInput("@[email protected]( 'arg1', 2 ,'arg3',4)");
194187

tests/Coduo/PHPMatcher/ParserTest.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function test_many_expanders()
7171

7272
/**
7373
* @expectedException \Coduo\PHPMatcher\Exception\PatternException
74-
* @expectedExceptionMessage [Syntax Error] line 0, col 0: Error: Expected "@type@ pattern", got "not_valid_type"
74+
* @expectedExceptionMessage [Syntax Error] line 0, col 0: Error: Expected "@type@ pattern", got "not"
7575
*/
7676
public function test_unexpected_statement_at_type_position()
7777
{
@@ -89,16 +89,7 @@ public function test_unexpected_statement_instead_of_expander()
8989

9090
/**
9191
* @expectedException \Coduo\PHPMatcher\Exception\PatternException
92-
* @expectedExceptionMessage [Syntax Error] line 0, col -1: Error: Expected "(", got ""
93-
*/
94-
public function test_missing_parenthesis_after_expander()
95-
{
96-
$this->parser->getAST("@[email protected]");
97-
}
98-
99-
/**
100-
* @expectedException \Coduo\PHPMatcher\Exception\PatternException
101-
* @expectedExceptionMessage [Syntax Error] line 0, col 16: Error: Expected ")", got end of string.end of string
92+
* @expectedExceptionMessage [Syntax Error] line 0, col 14: Error: Expected ")", got end of string.end of string
10293
*/
10394
public function test_end_of_string_after_opening_parenthesis()
10495
{
@@ -107,7 +98,7 @@ public function test_end_of_string_after_opening_parenthesis()
10798

10899
/**
109100
* @expectedException \Coduo\PHPMatcher\Exception\PatternException
110-
* @expectedExceptionMessage [Syntax Error] line 0, col 16: Error: Expected "string or number argument", got "not_argument"
101+
* @expectedExceptionMessage [Syntax Error] line 0, col 16: Error: Expected "string or number argument", got "not"
111102
*/
112103
public function test_not_argument_after_opening_parenthesis()
113104
{
@@ -143,7 +134,7 @@ public function test_missing_argument_after_comma()
143134

144135
/**
145136
* @expectedException \Coduo\PHPMatcher\Exception\PatternException
146-
* @expectedExceptionMessage [Syntax Error] line 0, col 25: Error: Expected "string or number argument", got "not_argument"
137+
* @expectedExceptionMessage [Syntax Error] line 0, col 25: Error: Expected "string or number argument", got "not"
147138
*/
148139
public function test_not_argument_after_comma()
149140
{

0 commit comments

Comments
 (0)