-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.php
156 lines (142 loc) · 3.73 KB
/
Lexer.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
<?php
/**
* PHP 5.3 or higher is required.
*
* base : http://d.hatena.ne.jp/amachang/20080502/1209732467
* @license MIT License
*/
class Lexer
{
const ENC = 'UTF-8';
protected static $ignore = false;
protected static $ignoreToken = array();
protected static $lex = array(
/**
* def :
* array(
* <string>, // token name
* <string> // regex
* );
*
* rule :
* array(
* <string>, // token name
* <string>, // regex
* <string> or <null>, // start state : <STRING> the STRING start condition
* <string> or <null>, // end state : %s Start Conditions (default is 'INITIAL')
* <boolean> or <null> // isLiteral
* )
*/
'defs' => array(
array('num', '[0-9]+|[0-9]*\.[0-9]+'),
array('intnum', '[0-9]+'),
),
'rules' => array(
array('INTEGER', '{intnum}'),
array('FLOATTOKEN', '{num}'),
)
);
/**
* create rules
*
* in :
* array(
* 'defs' => array(array('intnum', '[0-9]+')),
* 'rules' => array(array('INTEGER', '{intnum}'))
* );
*
* out :
* array(
* array('INTEGER', '(?:[0-9]+)')
* );
*
* @return Array
*/
protected static function createRules()
{
$result = array();
$defs = static::$lex['defs'];
$count = count($defs);
for ($i = 0; $i < $count; $i++) {
$tmp = $defs[$i][1];
for ($j = 0; $j < $count; $j++) {
$tmp = str_replace('{'.$defs[$j][0].'}', '(?:'.$defs[$j][1].')', $tmp);
}
$defs[$i][1] = $tmp;
}
foreach (static::$lex['rules'] as $n => $rule) {
$tmp = $rule[1];
foreach ($defs as $def) {
$tmp = str_replace('{'.$def[0].'}', '(?:'.$def[1].')', $tmp);
}
$result[$n] = array(
$rule[0], // token name
$tmp, // regex
isset($rule[2]) ? $rule[2] : null, // start state
isset($rule[3]) ? $rule[3] : null, // end state
isset($rule[4]) ? $rule[4] : null, // isLiteral
);
}
return $result;
}
/**
* lexical analyze
*
* @param string $source source
*
* @return array($match, $yylval, $lexbuf)
*/
public static function lex($source)
{
static $rules,$state = 'INITIAL'; // $state : default : INITIAL
if ($rules === null) $rules = self::createRules();
$result = $_matches = array();
foreach ($rules as $rule) {
if ($rule[2] === null || ($rule[2] !== null && $state === $rule[2])) {
if (preg_match('/^('.$rule[1].')/i', $source, $matches)) {
// token name, token strlen, token, rule
$_matches[] = array($rule[0], mb_strlen($matches[0], self::ENC), $matches[0], $rule);
}
}
}
$_state = null;
$_literal = false;
$length = 0;
if (count($_matches) !== 0) {
foreach ($_matches as $match) {
if ($length < $match[1]) {
$length = $match[1];
$result = $match;
$_state = $match[3][3];
$_literal = $match[3][4];
}
}
if ($_state !== null) $state = $_state; // use next
// ignore
if (static::$ignore === true && in_array($result[0], static::$ignoreToken, true)) {
$source = mb_substr($source, $result[1], mb_strlen($source, self::ENC) - 1, self::ENC);
return array(0, $result[2], $source);
}
if ($_literal === true) {
$char = mb_substr($source, 0, 1, self::ENC);
$result = array(ord($char), 1, $char, null);
}
} else {
$char = mb_substr($source, 0, 1, self::ENC);
$result = array(ord($char), 1, $char, null);
}
$source = mb_substr($source, $result[1], mb_strlen($source, self::ENC) - 1, self::ENC);
return array($result[0], $result[2], $source);
}
/**
* set static::$ignore. igonore tokenlist
*
* @param mix $isIgnore mix cast (boolean)
*
* @return boolean
*/
public static function setIgnore($isIgnore)
{
return static::$ignore = (boolean) $isIgnore;
}
}