Menu

[r46]: / trunk / python / python.tokenize.php  Maximize  Restore  History

Download this file

205 lines (169 with data), 4.4 kB

  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
<?php
require_once __DIR__.'/python.token.php';
require_once __DIR__.'/python.literal.php';
require_once __DIR__.'/python.interface.php';
class lang_python_tokenize extends lang_python_interface {
public static function python_tokenize($code) {
$c = rtrim($code);
$o = array();
$i = 0;
$l = 1;
$z = 0;
$g = array();
do {
$v = static::python_tokenize_newline($c);
if($v>0){
$c=substr($c,$v);
$l++;
}
} while($v>0);
while (strlen($c) && $z < 100) {
$n = 0;
do {
$v = static::python_tokenize_newline($c);
if ($v > 0) {
$c = substr($c,$v);
$n+= $v;
$l++;
}
} while ($v>0);
if ($n > 0) {
$o[] = new lang_python_token('NEWLINE');
}
$w = static::python_tokenize_whitespace($c);
if ($w > 0) {
$s = substr($c, 0, $w);
$c = substr($c, $w);
} else {
$s = '';
}
if ($n > 0) {
$e = static::python_tokenize_compute_space($s);
if ($e != $i) {
if ($e > $i) {
$o[] = new lang_python_token('INDENT');
$g[] = $e;
$i = $e;
} else if ($e==0 || in_array($e,$g)) {
do {
$h = array_pop($g);
if ($h==$e) {break;}
$o[] = new lang_python_token('DEDENT');
} while (count($g)>0);
$g[] = $e;
$i = $e;
} else {
$o[] = new lang_python_token('FAIL');
}
}
}
$t = static::python_tokenize_next($c);
$o[] = $t;
$c = substr($c,$t->length);
$z++;
}
$o[] = new lang_python_token('NEWLINE');
while (count($g)>0) {
$o[] = new lang_python_token('DEDENT');
$h = array_pop($g);
}
$o[] = new lang_python_token('ENDMARKER');
echo '<pre>';
foreach($o as $t) {
echo "$t->name: ".$t->getValue()."\n";
}
echo '</pre>';
return $o;
}
##
public static function python_tokenize_newline($c) {
$l = 0;
$k = false;
if(isset($c[$l])){if(ord($c[$l])==13){$l++;$k=true;}}
if(isset($c[$l])){if(ord($c[$l])==10){$l++;$k=true;}}
if($k){return $l;}
if (re('/(^[ \t]+)/',$c,$s)) {
$l = strlen($s[0]);
$k = false;
if(isset($c[$l])){if(ord($c[$l])==13){$l++;$k=true;}}
if(isset($c[$l])){if(ord($c[$l])==10){$l++;$k=true;}}
if($k){return $l;}
}
return 0;
}
##
public static function python_tokenize_whitespace($c) {
$l = 0;
if (re('/(^[ \t]+)/',$c,$s)) {
$l = strlen($s[0]);
return $l;
}
return 0;
}
##
public static function python_tokenize_next($c) {
$h = array(
'/^,/' => "COMMA",
'/^\./' => "DOT",
'/^:/' => "COLON",
'/^;/' => "SEMICOLON",
'/^=/' => "EQ",
'/^\+/' => "PLUS",
'/^%/' => "MOD",
'/^==/' => "EQUAL",
'/^>/' => "GREAT",
'/^</' => "LESS",
'/^>=/' => "GREATEQUAL",
'/^<=/' => "LESSEQUAL",
'/^\(/' => "RBO",
'/^\)/' => "RBC",
'/^\[/' => "SBO",
'/^\]/' => "SBC",
'/^{/' => "BO",
'/^}/' => "BC",
'/^print/' => "PRINT",
'/^import/' => "IMPORT",
'/^from/' => "FROM",
'/^class/' => "CLASS",
'/^def/' => "DEF",
'/^return/' => "RETURN",
'/^while/' => "WHILE",
'/^if/' => 'IF',
'/^True/' => 'TRUE',
'/^False/' => 'FALSE',
'/^None/' => 'NONE',
'/^\.\.\./' => 'TRIEPLEDOT',
);
foreach($h as $r=>$t) {
if (re($r,$c,$s)) {
return new lang_python_token($t,$s[0],strlen($s[0]));
}
}
if (re('|^#.*|',$c,$s)) {
return python_parse_comment($s[0]);
} else if (re('/^[0-9]+(\.[0-9]*)?/',$c,$s)) {
return new lang_python_token("NUMBER",(int)$s[0],strlen($s[0]));
} else if (re('/^"""(.*)"""/',$c,$s)) {
return new lang_python_token("STRING",$s[1],strlen($s[0]));
} else if (re('/^"([^"]+((\\\\")*[^"]+))"/',$c,$s)) {
return new lang_python_token("STRING",(string)$s[1],strlen($s[0]));
} else if (re('/^\'([^"]+((\\\\\')*[^\']+))\'/',$c,$s)) {
return new lang_python_token("STRING",(string)$s[1],strlen($s[0]));
} else if (re('|^[A-Za-z_][A-Za-z0-1_]*|',$c,$s)) {
return new lang_python_token("NAME",new lang_python_literal($s[0]),strlen($s[0]));
}
return new lang_python_token('FAIL');
}
public static function python_tokenize_compute_space($s) {
$l = 0;
for($i=0;$i<strlen($s);$i++) {
$c = $s[$i];
if ($c==" ") {
$l=$l+1;
} else if ($c=="\t") {
$l=$l+8;
}
}
return $l;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.