flex_token_stream.php 934 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. abstract class flex_scanner {
  3. /*
  4. Let's face it: PHP is not up to lexical processing. GNU flex handles
  5. it well, so I've created a little protocol for delegating the work.
  6. Extend this class so that executable() gives a path to your lexical
  7. analyser program.
  8. */
  9. abstract function executable();
  10. function __construct($path) {
  11. if (!is_readable($path)) throw new Exception("$path is not readable.");
  12. putenv("PHP_LIME_SCAN_STDIN=$path");
  13. $scanner = $this->executable();
  14. $tokens = explode("\0", `$scanner < "\$PHP_LIME_SCAN_STDIN"`);
  15. array_pop($tokens);
  16. $this->tokens = $tokens;
  17. $this->lineno = 1;
  18. }
  19. function next() {
  20. if (list($key, $token) = each($this->tokens)) {
  21. list($this->lineno, $type, $text) = explode("\1", $token);
  22. return array($type, $text);
  23. }
  24. }
  25. function feed($parser) {
  26. while (list($type, $text) = $this->next()) {
  27. $parser->eat($type, $text);
  28. }
  29. return $parser->eat_eof();
  30. }
  31. }