cvs: functable /lib proto2xml.php
| From: | Hartmut Holzgraefe | Date: | Tue, 28 May 2002 11:32:08 +0000 |
| Subject: | cvs: functable /lib proto2xml.php | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-12138@lists.php.net to get a copy of this message | ||
hholzgra Tue May 28 07:32:08 2002 EDT
Added files:
/functable/lib proto2xml.php
Log:
initial checkin
Index: functable/lib/proto2xml.php
+++ functable/lib/proto2xml.php
<?php
// all known php types
$types =
array("void","bool","boolean","int","float","string","array","object","resource","mixed");
// return true if argument is type name
function is_type($name) {
$name = strtolower($name);
if($name=="double") $name=$float;
if(in_array($name,$GLOBALS['types'])) {
return true;
}
return false;
}
// return true if argument is valid name
function is_name($name) {
if(ereg("&?[a-zA-Z0-9_]",$name)) {
if(is_type($name)) return false;
// TODO keywords
return true;
}
return false;
}
// simple tokenize input line
function tokenize($text) {
$len=strlen($text);
$name="";
$tokens=array();
for($n=0;$n<$len;$n++) {
$char = $text{$n};
if(ereg("[a-zA-Z0-9_]",$char)) {
$name.=$char;
} else {
if($name) $tokens[]=$name;
$name="";
if(trim($char)) $tokens[]=$char;
}
}
if($name) $tokens[]=$name;
return $tokens;
}
function gen_proto($line) {
$tokens = tokenize($line);
$n=0;
$opts=0;
$params=array();
$return_type = is_type($tokens[$n]) ? $tokens[$n++] : "void";
if(! is_name($tokens[$n])) die("$tokens[$n] is not a valid function name");
$function_name = $tokens[$n++];
if($tokens[$n]!='(') die("'(' expected instead of
'$tokens[$n]'");
if($tokens[++$n]!=')') {
for($param=0;$tokens[$n];$n++,$param++) {
if($tokens[$n]=='[') {
$params[$param]['optional']=true;
$opts++;
$n++;
if($param>0) {
if ($tokens[$n]!=',') die("',' expected after '[' instead of
$token[$n]");
$n++;
}
}
if(!is_type($tokens[$n])) die("type name expected instead of $tokens[$n]");
$params[$param]['type']=$tokens[$n];
$n++;
if(is_name($tokens[$n])) {
$params[$param]['name']=str_replace("&","&",$tokens[$n]);
$n++;
}
if($tokens[$n]=='[') {
$n--;
continue;
}
if($tokens[$n]==',') continue;
if($tokens[$n]==']') break;
if($tokens[$n]==')') break;
}
}
$numopts=$opts;
while($tokens[$n]==']') {
$n++;
$opts--;
}
if($opts!=0) die ("'[' / ']' count mismatch");
if($tokens[$n] != ')') die ("')' expected instead of $tokens[$n]");
$proto = array("name"=>$function_name
,"returns"=>$return_type
,"params"=>$params
,"optional"=>$numopts
,"desc"=>trim(substr(strstr($line,")"),1))
);
// PHP documentation stubs
$function_refentries = "";
$params = "";
if(isset($proto['params'])) {
foreach($proto['params'] as $key => $param) {
if($param['type']=="void") {
$params .= " <void/>\n";
} else{
$params .= " <paramdef>$param[type] <parameter>";
if(isset($param['optional'])) $params.="<optional>";
$params .= isset($param['name']) ? $param['name'] : "par_$key";
if(isset($param['optional'])) $params.="</optional>";
$params .= "</parameter></paramdef>\n";
}
}
}
if($params=="") {
$params = " <void/>";
} else {
$params = rtrim($params);
}
$function_refentries .= "
<refentry
id='function.".str_replace("_","-",$proto['name'])."'>
<refnamediv>
<refname>$proto[name]</refname>
<refpurpose>
".(!empty($proto['desc'])?$proto['desc']:"No description given
yet")."
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<funcsynopsis>
<funcprototype>
<funcdef>$proto[returns] <function>$proto[name]</function></funcdef>
$params
</funcprototype>
</funcsynopsis>
<para>
&warn.undocumented.func;
</para>
</refsect1>
</refentry>
";
return $function_refentries;
}
mysql_connect("localhost","phpdoc","phpdoc");
mysql_select_db("phpdoc");
header("Content-type: text/plain");
if(is_array($function)) {
$function = "function IN
('".join("','",$function)."')";
} else {
$function = "function = '$function'";
}
$query="SELECT proto
, proto_desc
FROM proto4
WHERE $function
";
$res=mysql_query($query);
if(mysql_numrows($res)) {
while($row=mysql_fetch_assoc($res))
echo gen_proto("$row[proto] $row[proto_desc]")."\n\n";
} else {
echo "no proto available\n\n$query";
print_r($row);
}
mysql_free_result($res);
?>