PHP_LexerGenerator
[ class tree: PHP_LexerGenerator ] [ index: PHP_LexerGenerator ] [ all elements ]

Source for file longestfirst.php

Documentation is available at longestfirst.php

  1. <?php
  2. class CodeGen_PECL_Tools_ProtoParser
  3. {
  4.     const PAR_OPEN = 1;
  5.     const PAR_CLOSE = 2;
  6.     const EQ = 3;
  7.     const COMMA = 4;
  8.     const SEMICOLON = 5;
  9.     const VOID = 6;
  10.     const BOOL = 7;
  11.     const NUMVAL = 8;
  12.     const STRVAL = 9;
  13. }
  14.  
  15. class CodeGen_PECL_Tools_ProtoLexer
  16. {
  17.     private $data;
  18.     public $token;
  19.     public $value;
  20.     private $line;
  21.     private $count;
  22.  
  23.     function __construct($data)
  24.     {
  25.         $this->data  $data;
  26.         $this->count = 0;
  27.         $this->line  = 1;
  28.     }
  29.  
  30.  
  31.     private $_yy_state = 1;
  32.     private $_yy_stack = array();
  33.  
  34.     function yylex()
  35.     {
  36.         return $this->{'yylex' $this->_yy_state}();
  37.     }
  38.  
  39.     function yypushstate($state)
  40.     {
  41.         array_push($this->_yy_stack$this->_yy_state);
  42.         $this->_yy_state $state;
  43.     }
  44.  
  45.     function yypopstate()
  46.     {
  47.         $this->_yy_state array_pop($this->_yy_stack);
  48.     }
  49.  
  50.     function yybegin($state)
  51.     {
  52.         $this->_yy_state $state;
  53.     }
  54.  
  55.  
  56.  
  57.  
  58.     function yylex1()
  59.     {
  60.         do {
  61.             $rules = array(
  62.                 '/^[ \t\n]+/',
  63.                 '/^\\(/',
  64.                 '/^\\)/',
  65.                 '/^=/',
  66.                 '/^,/',
  67.                 '/^;/',
  68.                 '/^void/',
  69.                 '/^bool(ean)?/',
  70.                 '/^[0-9]+|0x[0-9a-fA-F]+/',
  71.                 '/^[_a-zA-Z][_a-zA-Z0-9]+/',
  72.             );
  73.             $match = false;
  74.             foreach ($rules as $index => $rule{
  75.                 if (preg_match($rulesubstr($this->data$this->count)$yymatches)) {
  76.                     if ($match{
  77.                         if (strlen($yymatches[0]strlen($match[0][0])) {
  78.                             $match = array($yymatches$index)// matches, token
  79.                         }
  80.                     else {
  81.                         $match = array($yymatches$index);
  82.                     }
  83.                 }
  84.             }
  85.             if (!$match{
  86.                 throw new Exception('Unexpected input at line' $this->line .
  87.                     ': ' $this->data[$this->count]);
  88.             }
  89.             $this->token $match[1];
  90.             $this->value $match[0][0];
  91.             $yysubmatches $match[0];
  92.             array_shift($yysubmatches);
  93.             if (!$yysubmatches{
  94.                 $yysubmatches = array();
  95.             }
  96.             $r $this->{'yy_r1_' $this->token}($yysubmatches);
  97.             if ($r === null{
  98.                 $this->count += strlen($this->value);
  99.                 $this->line += substr_count("\n"$this->value);
  100.                 // accept this token
  101.                 return true;
  102.             elseif ($r === true{
  103.                 // we have changed state
  104.                 // process this token in the new state
  105.                 return $this->yylex();
  106.             elseif ($r === false{
  107.                 $this->count += strlen($this->value);
  108.                 $this->line += substr_count("\n"$this->value);
  109.                 if ($this->count >= strlen($this->data)) {
  110.                     return false; // end of input
  111.                 }
  112.                 // skip this token
  113.                 continue;
  114.             else {
  115.                 $yy_yymore_patterns array_slice($rules$this->tokentrue);
  116.                 // yymore is needed
  117.                 do {
  118.                     if (!isset($yy_yymore_patterns[$this->token])) {
  119.                         throw new Exception('cannot do yymore for the last token');
  120.                     }
  121.                     $match = false;
  122.                     foreach ($yy_yymore_patterns[$this->tokenas $index => $rule{
  123.                         if (preg_match($rule,
  124.                                 substr($this->data$this->count)$yymatches)) {
  125.                             $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  126.                             if ($match{
  127.                                 if (strlen($yymatches[0]strlen($match[0][0])) {
  128.                                     $match = array($yymatches$index)// matches, token
  129.                                 }
  130.                             else {
  131.                                 $match = array($yymatches$index);
  132.                             }
  133.                         }
  134.                     }
  135.                     if (!$match{
  136.                         throw new Exception('Unexpected input at line' $this->line .
  137.                             ': ' $this->data[$this->count]);
  138.                     }
  139.                     $this->token $match[1];
  140.                     $this->value $match[0][0];
  141.                     $yysubmatches $match[0];
  142.                     array_shift($yysubmatches);
  143.                     if (!$yysubmatches{
  144.                         $yysubmatches = array();
  145.                     }
  146.                     $this->line substr_count("\n"$this->value);
  147.                     $r $this->{'yy_r1_' $this->token}();
  148.                 while ($r !== null || !$r);
  149.                 if ($r === true{
  150.                     // we have changed state
  151.                     // process this token in the new state
  152.                     return $this->yylex();
  153.                 else {
  154.                     // accept
  155.                     $this->count += strlen($this->value);
  156.                     $this->line += substr_count("\n"$this->value);
  157.                     return true;
  158.                 }
  159.             }
  160.         while (true);
  161.  
  162.     // end function
  163.  
  164.     function yy_r1_0($yy_subpatterns)
  165.     {
  166.  
  167.     return false;
  168.     }
  169.     function yy_r1_1($yy_subpatterns)
  170.     {
  171.  
  172.     $this->token = CodeGen_PECL_Tools_ProtoParser::PAR_OPEN;
  173.     }
  174.     function yy_r1_2($yy_subpatterns)
  175.     {
  176.  
  177.     $this->token = CodeGen_PECL_Tools_ProtoParser::PAR_CLOSE;
  178.     }
  179.     function yy_r1_3($yy_subpatterns)
  180.     {
  181.  
  182.     $this->token = CodeGen_PECL_Tools_ProtoParser::EQ;
  183.     }
  184.     function yy_r1_4($yy_subpatterns)
  185.     {
  186.  
  187.     $this->token = CodeGen_PECL_Tools_ProtoParser::COMMA;
  188.     }
  189.     function yy_r1_5($yy_subpatterns)
  190.     {
  191.  
  192.     $this->token = CodeGen_PECL_Tools_ProtoParser::SEMICOLON;
  193.     }
  194.     function yy_r1_6($yy_subpatterns)
  195.     {
  196.  
  197.     $this->token = CodeGen_PECL_Tools_ProtoParser::VOID;
  198.     }
  199.     function yy_r1_7($yy_subpatterns)
  200.     {
  201.  
  202.     $this->token = CodeGen_PECL_Tools_ProtoParser::BOOL;
  203.     }
  204.     function yy_r1_8($yy_subpatterns)
  205.     {
  206.  
  207.     $this->token = CodeGen_PECL_Tools_ProtoParser::NUMVAL;
  208.     }
  209.     function yy_r1_9($yy_subpatterns)
  210.     {
  211.  
  212.     $this->token = CodeGen_PECL_Tools_ProtoParser::STRVAL;
  213.     }
  214.  
  215.  
  216. }
  217. $a = new CodeGen_PECL_Tools_ProtoLexer('booler boolboolean boolean bool');
  218. $a->yylex();
  219. var_dump('advance: ' $a->value);
  220. $a->yylex();
  221. var_dump('advance: ' $a->value);
  222. $a->yylex();
  223. var_dump('advance: ' $a->value);
  224. $a->yylex();
  225. var_dump('advance: ' $a->value);

Documentation generated on Mon, 11 Mar 2019 15:40:41 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.