This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListParser.php
More file actions
84 lines (68 loc) · 2.2 KB
/
ListParser.php
File metadata and controls
84 lines (68 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Icecave\Dialekt\Parser;
use Icecave\Dialekt\AST\EmptyExpression;
use Icecave\Dialekt\AST\LogicalAnd;
use Icecave\Dialekt\AST\Tag;
use Icecave\Dialekt\Parser\Exception\ParseException;
/**
* Parses a list of tags.
*
* The expression must be a space-separated list of tags. The result is
* either EmptyExpression, a single Tag node, or a LogicalAnd node
* containing only Tag nodes.
*/
class ListParser extends AbstractParser
{
protected function parseExpression()
{
$this->startExpression();
$expression = null;
while ($this->currentToken) {
$this->expectToken(Token::STRING);
if (strpos($this->currentToken->value, $this->wildcardString()) !== false) {
throw new ParseException(
'Unexpected wildcard string "' . $this->wildcardString() . '", in tag "' . $this->currentToken->value . '".'
);
}
$tag = new Tag($this->currentToken->value);
$this->startExpression();
$this->nextToken();
$this->endExpression($tag);
if ($expression) {
if ($expression instanceof Tag) {
$expression = new LogicalAnd($expression);
}
$expression->add($tag);
} else {
$expression = $tag;
}
}
$this->endExpression($expression);
return $expression;
}
/**
* Parse a list of tags into an array.
*
* The expression must be a space-separated list of tags. The result is
* an array of strings.
*
* @param string $expression The tag list to parse.
*
* @return array<string> The tags in the list.
* @throws ParseException if the tag list is invalid.
*/
public function parseAsArray($expression)
{
$result = $this->parse($expression);
if ($result instanceof EmptyExpression) {
return array();
} elseif ($result instanceof Tag) {
return array($result->name());
}
$tags = array();
foreach ($result->children() as $node) {
$tags[] = $node->name();
}
return $tags;
}
}