1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Parser_Statement} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Parser |
7
|
|
|
* @see Mailcode_Parser_Statement |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
use AppUtils\OperationResult; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Mailcode statement parser: parses arbitrary statements |
18
|
|
|
* to check for validation issues. |
19
|
|
|
* |
20
|
|
|
* @package Mailcode |
21
|
|
|
* @subpackage Parser |
22
|
|
|
* @author Sebastian Mordziol <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Mailcode_Parser_Statement |
25
|
|
|
{ |
26
|
|
|
const ERROR_TOKENIZE_METHOD_MISSING = 48901; |
27
|
|
|
|
28
|
|
|
const VALIDATION_EMPTY = 48801; |
29
|
|
|
|
30
|
|
|
const VALIDATION_UNQUOTED_STRING_LITERALS = 48802; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $statement; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var OperationResult |
39
|
|
|
*/ |
40
|
|
|
protected $result; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer|NULL |
44
|
|
|
*/ |
45
|
|
|
protected $tokenizer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Mailcode_Parser_Statement_Info|NULL |
49
|
|
|
*/ |
50
|
|
|
protected $info; |
51
|
|
|
|
52
|
|
|
public function __construct(string $statement) |
53
|
|
|
{ |
54
|
|
|
$this->statement = $statement; |
55
|
|
|
$this->result = new OperationResult($this); |
56
|
|
|
$this->tokenizer = new Mailcode_Parser_Statement_Tokenizer($this); |
57
|
|
|
|
58
|
|
|
$this->validate(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getStatementString() : string |
62
|
|
|
{ |
63
|
|
|
return $this->statement; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isValid() : bool |
67
|
|
|
{ |
68
|
|
|
return $this->result->isValid(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getValidationResult() : OperationResult |
72
|
|
|
{ |
73
|
|
|
return $this->result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getInfo() : Mailcode_Parser_Statement_Info |
77
|
|
|
{ |
78
|
|
|
if(isset($this->info)) |
79
|
|
|
{ |
80
|
|
|
return $this->info; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->info = new Mailcode_Parser_Statement_Info($this->tokenizer); |
|
|
|
|
84
|
|
|
|
85
|
|
|
return $this->info; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function validate() : void |
89
|
|
|
{ |
90
|
|
|
if(!$this->tokenizer->hasTokens()) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$this->result->makeError( |
93
|
|
|
t('Empty statement'), |
94
|
|
|
self::VALIDATION_EMPTY |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$unknown = $this->tokenizer->getFirstUnknown(); |
101
|
|
|
|
102
|
|
|
if($unknown) |
103
|
|
|
{ |
104
|
|
|
$this->result->makeError( |
105
|
|
|
t('Unquoted string literal found:').' ('.$unknown->getMatchedText().')', |
106
|
|
|
self::VALIDATION_UNQUOTED_STRING_LITERALS |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getNormalized() : string |
112
|
|
|
{ |
113
|
|
|
return $this->tokenizer->getNormalized(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|