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
|
|
|
goto state1; |
25
|
|
|
|
26
|
|
|
state1: |
27
|
|
|
if ($context->getBuffer()->isEnd()) { |
28
|
|
|
goto error; |
29
|
|
|
} |
30
|
|
|
$char = $context->getBuffer()->getSymbol(); |
31
|
|
|
if (0x3E == $char) { |
32
|
|
|
$context->getBuffer()->nextSymbol(); |
33
|
|
|
// > |
34
|
|
|
$context->setNewToken(TokenType::NEXT); |
35
|
|
|
|
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
if (0x3C == $char) { |
39
|
|
|
$context->getBuffer()->nextSymbol(); |
40
|
|
|
// < |
41
|
|
|
$context->setNewToken(TokenType::PREV); |
42
|
|
|
|
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
if (0x2B == $char) { |
46
|
|
|
$context->getBuffer()->nextSymbol(); |
47
|
|
|
// \+ |
48
|
|
|
$context->setNewToken(TokenType::INC); |
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
if (0x2D == $char) { |
53
|
|
|
$context->getBuffer()->nextSymbol(); |
54
|
|
|
// - |
55
|
|
|
$context->setNewToken(TokenType::DEC); |
56
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
if (0x2E == $char) { |
60
|
|
|
$context->getBuffer()->nextSymbol(); |
61
|
|
|
// \. |
62
|
|
|
$context->setNewToken(TokenType::OUTPUT); |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
if (0x2C == $char) { |
67
|
|
|
$context->getBuffer()->nextSymbol(); |
68
|
|
|
// , |
69
|
|
|
$context->setNewToken(TokenType::INPUT); |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
if (0x5B == $char) { |
74
|
|
|
$context->getBuffer()->nextSymbol(); |
75
|
|
|
// \[ |
76
|
|
|
$context->setNewToken(TokenType::LOOP); |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
if (0x5D == $char) { |
81
|
|
|
$context->getBuffer()->nextSymbol(); |
82
|
|
|
// ] |
83
|
|
|
$context->setNewToken(TokenType::END_LOOP); |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
goto error; |
88
|
|
|
|
89
|
|
|
error: |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|