forked from greyblue9/text-adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenizer.class.inc
158 lines (133 loc) · 4.06 KB
/
Tokenizer.class.inc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
require_once('Token.class.inc');
class Tokenizer
{
private $pos; // position of next character (unprocessed)
private $file;
private $len;
private $lineNum;
public $tokens; // token array
private $currentTokenIndex;
function __construct($pFile)
{
$this->file = $pFile;
$this->removeComments();
$this->len = strlen($this->file);
$this->tokenize();
$this->goToStart();
}
function goToStart()
{
$this->currentTokenIndex = 0;
}
function getNextToken()
{
if ($this->currentTokenIndex == count($this->tokens) - 1) return false;
return $this->tokens[$this->currentTokenIndex++];
}
function backUpOne()
{
$this->currentTokenIndex--;
}
// ____________
//_____/ private \__________________
private function tokenize()
{
$this->pos = 0;
$this->lineNum = 1;
$this->tokens = array();
$token = false;
do
{
$token = $this->fetchToken();
$this->tokens[] = $token;
} while ($token !== false);
}
/*
Returns the content of the next token (with surrounding whitespace removed)
*/
private function fetchTokenText()
{
// Start at next non-whitespace character
while ($this->pos != $this->len
&& self::isWhitespace($this->char()))
{
$this->checkNewLine();
$this->pos++;
}
if ($this->pos == strlen($this->file)) return false; // reached end of file
// Find end of token
$tokenStartPos = $this->pos;
if ($this->char() == '@')
{
// keyword token - find next whitespace
do
{
$this->checkNewLine();
$this->pos++;
} while ($this->pos != $this->len
&& !self::isWhitespace($this->file[$this->pos])
&& $this->char() != '@');
}
else
{
// string token - find start of next keyword
do
{
$this->checkNewLine();
$this->pos++;
} while ($this->pos != $this->len
&& !($this->char() == '@' && $this->file[$this->pos-1] != '\\'));
}
$tokenText = trim(substr($this->file, $tokenStartPos, $this->pos-$tokenStartPos));
$tokenText = str_replace('\\@', '@', $tokenText);
return $tokenText;
}
private function fetchToken()
{
$tokenText = $this->fetchTokenText();
if ($tokenText == false) return false;
return new Token($tokenText, $this->lineNum);
}
private static function isWhitespace($pChar)
{
return ($pChar == ' ' || $pChar == "\n" || $pChar == "\r" || $pChar == "\t");
}
private function char()
{
return $this->file[$this->pos];
}
private function checkNewLine()
{
if ($this->char() == "\n") $this->lineNum++;
}
/**
Removes comments and replaces \-escaped #s with a single #
**/
private function removeComments()
{
$commentPos = 0;
do
{
$commentPos = strpos($this->file, '#', $commentPos);
if ($commentPos !== false)
{
// normal # comment
if ($commentPos != 0 && $this->file[$commentPos-1] != '\\')
{
$newlinePos = strpos($this->file, "\n", $commentPos);
$this->file = substr($this->file, 0, $commentPos)
. substr($this->file, $newlinePos);
}
else
{
// escaped #
// remove escape character
$this->file = substr($this->file, 0, $commentPos-1)
. substr($this->file, $commentPos);
}
}
}
while ($commentPos !== false);
}
}