lime/flex_token_stream.php

42 lines
977 B
PHP
Raw Permalink Normal View History

2011-12-28 01:23:38 +04:00
<?php
2011-12-29 01:50:59 +04:00
/**
* Let's face it: PHP is not up to lexical processing. GNU flex handles
* it well, so I've created a little protocol for delegating the work.
* Extend this class so that executable() gives a path to your lexical
* analyser program.
*/
2011-12-28 01:23:38 +04:00
abstract class flex_scanner {
abstract function executable();
2011-12-29 01:50:59 +04:00
public function __construct($path) {
if (!is_readable($path)) {
throw new Exception("$path is not readable.");
}
2011-12-28 01:23:38 +04:00
putenv("PHP_LIME_SCAN_STDIN=$path");
2011-12-29 01:50:59 +04:00
2011-12-28 01:23:38 +04:00
$scanner = $this->executable();
$tokens = explode("\0", `$scanner < "\$PHP_LIME_SCAN_STDIN"`);
2011-12-29 01:50:59 +04:00
2011-12-28 01:23:38 +04:00
array_pop($tokens);
$this->tokens = $tokens;
$this->lineno = 1;
}
2011-12-29 01:50:59 +04:00
public function next() {
2011-12-28 01:23:38 +04:00
if (list($key, $token) = each($this->tokens)) {
list($this->lineno, $type, $text) = explode("\1", $token);
2011-12-29 01:50:59 +04:00
2011-12-28 01:23:38 +04:00
return array($type, $text);
}
}
2011-12-29 01:50:59 +04:00
public function feed($parser) {
2011-12-28 01:23:38 +04:00
while (list($type, $text) = $this->next()) {
$parser->eat($type, $text);
}
2011-12-29 01:50:59 +04:00
2011-12-28 01:23:38 +04:00
return $parser->eat_eof();
}
}