forked from wez/JLexPHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jlex.php
208 lines (183 loc) · 6.69 KB
/
jlex.php
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php # vim:ts=2:sw=2:et:
/*
Copyright 2006 Wez Furlong, OmniTI Computer Consulting, Inc.
Based on JLex which is:
JLEX COPYRIGHT NOTICE, LICENSE, AND DISCLAIMER
Copyright 1996-2000 by Elliot Joel Berk and C. Scott Ananian
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both the copyright notice and this permission notice and warranty
disclaimer appear in supporting documentation, and that the name of
the authors or their employers not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission.
The authors and their employers disclaim all warranties with regard to
this software, including all implied warranties of merchantability and
fitness. In no event shall the authors or their employers be liable
for any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether in an
action of contract, negligence or other tortious action, arising out
of or in connection with the use or performance of this software.
**************************************************************
*/
class JLexToken {
public $line;
public $col;
public $value;
public $type;
function __construct($type, $value = null, $line = null, $col = null) {
$this->line = $line;
$this->col = $col;
$this->value = $value;
$this->type = $type;
}
}
class JLexBase {
const YY_F = -1;
const YY_NO_STATE = -1;
const YY_NOT_ACCEPT = 0;
const YY_START = 1;
const YY_END = 2;
const YY_NO_ANCHOR = 4;
const YYEOF = -1;
protected $YY_BOL;
protected $YY_EOF;
protected $yy_reader;
protected $yy_buffer;
protected $yy_buffer_read;
protected $yy_buffer_index;
protected $yy_buffer_start;
protected $yy_buffer_end;
protected $yychar = 0;
protected $yycol = 0;
protected $yyline = 0;
protected $yy_at_bol;
protected $yy_lexical_state;
protected $yy_last_was_cr = false;
protected $yy_count_lines = false;
protected $yy_count_chars = false;
protected $yyfilename = null;
function __construct($stream) {
$this->yy_reader = $stream;
$meta = stream_get_meta_data($stream);
if (!isset($meta['uri'])) {
$this->yyfilename = '<<input>>';
} else {
$this->yyfilename = $meta['uri'];
}
$this->yy_buffer = "";
$this->yy_buffer_read = 0;
$this->yy_buffer_index = 0;
$this->yy_buffer_start = 0;
$this->yy_buffer_end = 0;
$this->yychar = 0;
$this->yyline = 1;
$this->yy_at_bol = true;
}
protected function yybegin($state) {
$this->yy_lexical_state = $state;
}
protected function yy_advance() {
if ($this->yy_buffer_index < $this->yy_buffer_read) {
if (!isset($this->yy_buffer[$this->yy_buffer_index])) {
return $this->YY_EOF;
}
return ord($this->yy_buffer[$this->yy_buffer_index++]);
}
if ($this->yy_buffer_start != 0) {
/* shunt */
$j = $this->yy_buffer_read - $this->yy_buffer_start;
$this->yy_buffer = substr($this->yy_buffer, $this->yy_buffer_start, $j);
$this->yy_buffer_end -= $this->yy_buffer_start;
$this->yy_buffer_start = 0;
$this->yy_buffer_read = $j;
$this->yy_buffer_index = $j;
$data = fread($this->yy_reader, 8192);
if ($data === false || !strlen($data)) return $this->YY_EOF;
$this->yy_buffer .= $data;
$this->yy_buffer_read += strlen($data);
}
while ($this->yy_buffer_index >= $this->yy_buffer_read) {
$data = fread($this->yy_reader, 8192);
if ($data === false || !strlen($data)) return $this->YY_EOF;
$this->yy_buffer .= $data;
$this->yy_buffer_read += strlen($data);
}
return ord($this->yy_buffer[$this->yy_buffer_index++]);
}
protected function yy_move_end() {
if ($this->yy_buffer_end > $this->yy_buffer_start &&
$this->yy_buffer[$this->yy_buffer_end-1] == "\n")
$this->yy_buffer_end--;
if ($this->yy_buffer_end > $this->yy_buffer_start &&
$this->yy_buffer[$this->yy_buffer_end-1] == "\r")
$this->yy_buffer_end--;
}
protected function yy_mark_start() {
if ($this->yy_count_lines || $this->yy_count_chars) {
if ($this->yy_count_lines) {
for ($i = $this->yy_buffer_start; $i < $this->yy_buffer_index; ++$i) {
if ("\n" == $this->yy_buffer[$i] && !$this->yy_last_was_cr) {
++$this->yyline;
$this->yycol = 0;
}
if ("\r" == $this->yy_buffer[$i]) {
++$yyline;
$this->yycol = 0;
$this->yy_last_was_cr = true;
} else {
$this->yy_last_was_cr = false;
}
}
}
if ($this->yy_count_chars) {
$this->yychar += $this->yy_buffer_index - $this->yy_buffer_start;
$this->yycol += $this->yy_buffer_index - $this->yy_buffer_start;
}
$this->yy_buffer_start = $this->yy_buffer_index;
}
}
protected function yy_mark_end() {
$this->yy_buffer_end = $this->yy_buffer_index;
}
protected function yy_to_mark() {
#echo "yy_to_mark: setting buffer index to ", $this->yy_buffer_end, "\n";
$this->yy_buffer_index = $this->yy_buffer_end;
$this->yy_at_bol = ($this->yy_buffer_end > $this->yy_buffer_start) &&
("\r" == $this->yy_buffer[$this->yy_buffer_end-1] ||
"\n" == $this->yy_buffer[$this->yy_buffer_end-1] ||
2028 /* unicode LS */ == $this->yy_buffer[$this->yy_buffer_end-1] ||
2029 /* unicode PS */ == $this->yy_buffer[$this->yy_buffer_end-1]);
}
protected function yytext() {
return substr($this->yy_buffer, $this->yy_buffer_start,
$this->yy_buffer_end - $this->yy_buffer_start);
}
protected function yylength() {
return $this->yy_buffer_end - $this->yy_buffer_start;
}
static $yy_error_string = array(
'INTERNAL' => "Error: internal error.\n",
'MATCH' => "Error: Unmatched input.\n"
);
protected function yy_error($code, $fatal) {
print self::$yy_error_string[$code];
flush();
if ($fatal) throw new Exception("JLex fatal error " . self::$yy_error_string[$code]);
}
/* creates an annotated token */
function createToken($type = null, $value = null) {
if ($type === null) $type = $this->yytext();
$tok = new JLexToken($type);
$this->annotateToken($tok, $value);
return $tok;
}
/* annotates a token with a value and source positioning */
function annotateToken(JLexToken $tok, $value = null) {
$tok->value = is_null($value) ? $this->yytext() : $value;
$tok->col = $this->yycol;
$tok->line = $this->yyline;
$tok->filename = $this->yyfilename;
}
}