1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Brainfuck token matcher. |
5
|
|
|
* |
6
|
|
|
* Auto-generated file, please don't edit manually. |
7
|
|
|
* Generated by UniLex. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Remorhaz\UniLex\Example\Brainfuck\Grammar; |
13
|
|
|
|
14
|
|
|
use Remorhaz\UniLex\IO\CharBufferInterface; |
15
|
|
|
use Remorhaz\UniLex\Lexer\TokenFactoryInterface; |
16
|
|
|
use Remorhaz\UniLex\Lexer\TokenMatcherTemplate; |
17
|
|
|
|
18
|
|
|
class TokenMatcher extends TokenMatcherTemplate |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
public function match(CharBufferInterface $buffer, TokenFactoryInterface $tokenFactory): bool |
22
|
|
|
{ |
23
|
|
|
$context = $this->createContext($buffer, $tokenFactory); |
24
|
|
|
$context->setRegExps( |
25
|
|
|
'>', |
26
|
|
|
'<', |
27
|
|
|
'\\+', |
28
|
|
|
'-', |
29
|
|
|
'\\.', |
30
|
|
|
',', |
31
|
|
|
'\\[', |
32
|
|
|
']' |
33
|
|
|
); |
34
|
|
|
goto state1; |
35
|
|
|
|
36
|
|
|
state1: |
37
|
|
|
if ($context->getBuffer()->isEnd()) { |
38
|
|
|
goto error; |
39
|
|
|
} |
40
|
|
|
$char = $context->getBuffer()->getSymbol(); |
41
|
|
|
if (0x3E == $char) { |
42
|
|
|
$context->getBuffer()->nextSymbol(); |
43
|
|
|
$context->allowRegExps('>'); |
44
|
|
|
goto state2; |
45
|
|
|
} |
46
|
|
|
if (0x3C == $char) { |
47
|
|
|
$context->getBuffer()->nextSymbol(); |
48
|
|
|
$context->allowRegExps('<'); |
49
|
|
|
goto state2; |
50
|
|
|
} |
51
|
|
|
if (0x2B == $char) { |
52
|
|
|
$context->getBuffer()->nextSymbol(); |
53
|
|
|
$context->allowRegExps('\\+'); |
54
|
|
|
goto state2; |
55
|
|
|
} |
56
|
|
|
if (0x2D == $char) { |
57
|
|
|
$context->getBuffer()->nextSymbol(); |
58
|
|
|
$context->allowRegExps('-'); |
59
|
|
|
goto state2; |
60
|
|
|
} |
61
|
|
|
if (0x2E == $char) { |
62
|
|
|
$context->getBuffer()->nextSymbol(); |
63
|
|
|
$context->allowRegExps('\\.'); |
64
|
|
|
goto state2; |
65
|
|
|
} |
66
|
|
|
if (0x2C == $char) { |
67
|
|
|
$context->getBuffer()->nextSymbol(); |
68
|
|
|
$context->allowRegExps(','); |
69
|
|
|
goto state2; |
70
|
|
|
} |
71
|
|
|
if (0x5B == $char) { |
72
|
|
|
$context->getBuffer()->nextSymbol(); |
73
|
|
|
$context->allowRegExps('\\['); |
74
|
|
|
goto state2; |
75
|
|
|
} |
76
|
|
|
if (0x5D == $char) { |
77
|
|
|
$context->getBuffer()->nextSymbol(); |
78
|
|
|
$context->allowRegExps(']'); |
79
|
|
|
goto state2; |
80
|
|
|
} |
81
|
|
|
goto error; |
82
|
|
|
|
83
|
|
|
state2: |
84
|
|
|
switch ($context->getRegExp()) { |
85
|
|
|
case '>': |
86
|
|
|
$context->setNewToken(TokenType::NEXT); |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
|
90
|
|
|
case '<': |
91
|
|
|
$context->setNewToken(TokenType::PREV); |
92
|
|
|
|
93
|
|
|
return true; |
94
|
|
|
|
95
|
|
|
case '\\+': |
96
|
|
|
$context->setNewToken(TokenType::INC); |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
|
100
|
|
|
case '-': |
101
|
|
|
$context->setNewToken(TokenType::DEC); |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
|
105
|
|
|
case '\\.': |
106
|
|
|
$context->setNewToken(TokenType::OUTPUT); |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
|
110
|
|
|
case ',': |
111
|
|
|
$context->setNewToken(TokenType::INPUT); |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
|
115
|
|
|
case '\\[': |
116
|
|
|
$context->setNewToken(TokenType::LOOP); |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
|
120
|
|
|
case ']': |
121
|
|
|
$context->setNewToken(TokenType::END_LOOP); |
122
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
|
125
|
|
|
default: |
126
|
|
|
goto error; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
error: |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|