Menu

[r27]: / trunk / python / python.lib.php  Maximize  Restore  History

Download this file

103 lines (79 with data), 2.2 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
<?php
$_PYTHON = array(
"DEBUG" => array(
"DUMP_TOKEN" => false
),
"PARSER" => null,
);
function python_start() {
global $_PYTHON;
require_once("lime/parse_engine.php");
require_once("python.class");
require_once("python-tokenizer.php");
require_once("python-interface.php");
$_PYTHON["PARSER"] = new parse_engine(new python());
}
function python_eval($pycode) {
global $_PYTHON;
$parser = &$_PYTHON["PARSER"];
$codeln = 1;
if (!strlen($pycode)) return;
try {
$parser-> reset();
$tokens = python_tokenize($pycode);
$alltok = count($tokens);
$cursor = 0;
while($cursor < $alltok) {
$token0 = $tokens[$cursor];
$token1 = isset($tokens[$cursor+1]);
if ($_PYTHON["DEBUG"]["DUMP_TOKEN"]) python_dump_token($token0);
$parser->eat($token0["name"], $token0["value"]);
if ($token0["name"] == "NEWLINE") {
$codeln++;
}
$cursor++;
}
$parser->eat_eof();
} catch (parse_error $e) {
echo '<pre>';
echo $e->getMessage(), "\nLine: $codeln;", "\n";
echo '</pre>';
}
}
function python_file($filename) {
$code = file_get_contents($filename);
python_eval($code);
}
function python_exec($filename) {
$code = file_get_contents($filename);
python_eval($code);
}
function python_build() {
require_once("lime/lime.php");
$code = parse_lime_grammar(dirname(__FILE__)."/python.lime");
file_put_contents(dirname(__FILE__)."/python.class","<?php \n".$code."\n?>");
}
function python_sanitize($code) {
return $code;
}
function re($regex,$input,&$output) {
return preg_match($regex,$input,&$output);
}
function token($name,$value=null) {
return array(
"name" => $name,
"value" => $value
);
}
function python_parse_comment($comment) {
global $_PYTHON;
if (re("/^#\{([a-zA-Z_][a-zA-Z0-9_]*)\}:(.*)/",$comment,$r)){
$_PYTHON["DEBUG"][$r[1]] = trim($r[2]);
#var_dump($_PYTHON["DEBUG"]);
}
}
function python_dump_token($token) {
echo '<span style="margin:0 2px 0 0;display:inline-block;padding:2px 4px;font-family:arial;font-size:10px;background:yellow;color:#323232;border:1px solid #323232;">';
echo $token["name"].'('.$token["value"].')';
echo '</span>';
}
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.