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

Example lexer generated php code

  1. <?php
  2. class TestLexer
  3. {
  4.     private $data;
  5.     private $N;
  6.     public $token;
  7.     public $value;
  8.     private $line;
  9.     private $state 1;
  10.  
  11.     function __construct($data)
  12.     {
  13.         $this->data $data;
  14.         $this->0;
  15.         $this->line 1;
  16.     }
  17.  
  18.  
  19.     private $_yy_state 1;
  20.     private $_yy_stack array();
  21.  
  22.     function yylex()
  23.     {
  24.         return $this->{'yylex' $this->_yy_state}();
  25.     }
  26.  
  27.     function yypushstate($state)
  28.     {
  29.         array_push($this->_yy_stack$this->_yy_state);
  30.         $this->_yy_state $state;
  31.     }
  32.  
  33.     function yypopstate()
  34.     {
  35.         $this->_yy_state array_pop($this->_yy_stack);
  36.     }
  37.  
  38.     function yybegin($state)
  39.     {
  40.         $this->_yy_state $state;
  41.     }
  42.  
  43.  
  44.  
  45.     function yylex1()
  46.     {
  47.         $tokenMap array (
  48.               => 8,
  49.               10 => 2,
  50.               13 => 1,
  51.               15 => 0,
  52.               16 => 1,
  53.               18 => 0,
  54.               19 => 0,
  55.               20 => 0,
  56.             );
  57.         if ($this->>= strlen($this->data)) {
  58.             return false// end of input
  59.         }
  60.         $yy_global_pattern "/^(((@import\\s+[\"'`]([\\w:?=@&\#._;-]+)[\"'`];)|(:\\s*url\\s*\\([\\s\"'`]*([\\w:?=@&\#._;-]+)([\\s\"'`]*\\))|<[^>]*\\s+(src|href|url)=[\\s\"'`]*([\\w:?=@&\#._;-]+)[\\s\"'`]*[^>]*>)))|^(#(test)\\11{1,2}(hi)\\12)|^([a-zA-Z]_[a-zA-Z]+([0-9])+)|^([a-zA-Z]_[a-zA-Z]+)|^([0-9][0-9]\\.([0-9])+)|^([ \t\n]+)|^(\\$)|^(a\\$)/";
  61.  
  62.         do {
  63.             if (preg_match($yy_global_patternsubstr($this->data$this->N)$yymatches)) {
  64.                 $yysubmatches $yymatches;
  65.                 $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  66.                 if (!count($yymatches)) {
  67.                     throw new Exception('Error: lexing failed because a rule matched' .
  68.                         'an empty string.  Input "' substr($this->data,
  69.                         $this->N5'... state START');
  70.                 }
  71.                 next($yymatches)// skip global match
  72.                 $this->token key($yymatches)// token number
  73.                 if ($tokenMap[$this->token]{
  74.                     // extract sub-patterns for passing to lex function
  75.                     $yysubmatches array_slice($yysubmatches$this->token 1,
  76.                         $tokenMap[$this->token]);
  77.                 else {
  78.                     $yysubmatches array();
  79.                 }
  80.                 $this->value current($yymatches)// token value
  81.                 $r $this->{'yy_r1_' $this->token}($yysubmatches);
  82.                 if ($r === null{
  83.                     $this->+= strlen($this->value);
  84.                     $this->line += substr_count("\n"$this->value);
  85.                     // accept this token
  86.                     return true;
  87.                 elseif ($r === true{
  88.                     // we have changed state
  89.                     // process this token in the new state
  90.                     return $this->yylex();
  91.                 elseif ($r === false{
  92.                     $this->+= strlen($this->value);
  93.                     $this->line += substr_count("\n"$this->value);
  94.                     if ($this->>= strlen($this->data)) {
  95.                         return false// end of input
  96.                     }
  97.                     // skip this token
  98.                     continue;
  99.                 else {                    $yy_yymore_patterns array(
  100.         => "^(#(test)\\11{1,2}(hi)\\12)|^([a-zA-Z]_[a-zA-Z]+([0-9])+)|^([a-zA-Z]_[a-zA-Z]+)|^([0-9][0-9]\\.([0-9])+)|^([ \t\n]+)|^(\\$)|^(a\\$)",
  101.         10 => "^([a-zA-Z]_[a-zA-Z]+([0-9])+)|^([a-zA-Z]_[a-zA-Z]+)|^([0-9][0-9]\\.([0-9])+)|^([ \t\n]+)|^(\\$)|^(a\\$)",
  102.         13 => "^([a-zA-Z]_[a-zA-Z]+)|^([0-9][0-9]\\.([0-9])+)|^([ \t\n]+)|^(\\$)|^(a\\$)",
  103.         15 => "^([0-9][0-9]\\.([0-9])+)|^([ \t\n]+)|^(\\$)|^(a\\$)",
  104.         16 => "^([ \t\n]+)|^(\\$)|^(a\\$)",
  105.         18 => "^(\\$)|^(a\\$)",
  106.         19 => "^(a\\$)",
  107.         20 => "",
  108.     );
  109.  
  110.                     // yymore is needed
  111.                     do {
  112.                         if (!strlen($yy_yymore_patterns[$this->token])) {
  113.                             throw new Exception('cannot do yymore for the last token');
  114.                         }
  115.                         if (preg_match($yy_yymore_patterns[$this->token],
  116.                               substr($this->data$this->N)$yymatches)) {
  117.                             $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  118.                             next($yymatches)// skip global match
  119.                             $this->token key($yymatches)// token number
  120.                             $this->value current($yymatches)// token value
  121.                             $this->line substr_count("\n"$this->value);
  122.                         }
  123.                     while ($this->{'yy_r1_' $this->token}(!== null);
  124.                     // accept
  125.                     $this->+= strlen($this->value);
  126.                     $this->line += substr_count("\n"$this->value);
  127.                     return true;
  128.                 }
  129.             else {
  130.                 throw new Exception('Unexpected input at line' $this->line .
  131.                     ': ' $this->data[$this->N]);
  132.             }
  133.             break;
  134.         while (true);
  135.     // end function
  136.  
  137.  
  138.     
  139.  
  140.  
  141.     const START 1;
  142.     function yy_r1_1($yy_subpatterns)
  143.     {
  144.  
  145.     echo "complex\n";
  146.     var_dump($this->value);
  147.     echo "    complex subpatterns: \n";
  148.     var_dump($yy_subpatterns);
  149.     }
  150.     function yy_r1_10($yy_subpatterns)
  151.     {
  152.  
  153.     echo "weirdo\n";
  154.     var_dump($this->value);
  155.     echo "    weirdo subpatterns: \n";
  156.     var_dump($yy_subpatterns);
  157.     }
  158.     function yy_r1_13($yy_subpatterns)
  159.     {
  160.  
  161.     echo "rule 1\n";
  162.     var_dump($this->value);
  163.     echo "    rule 1 subpatterns: \n";
  164.     var_dump($yy_subpatterns);
  165.     $this->yypushstate(self::TWO);
  166.     }
  167.     function yy_r1_15($yy_subpatterns)
  168.     {
  169.  
  170.     echo "rule 2\n";
  171.     var_dump($this->value);
  172.     echo "    rule 2 subpatterns: \n";
  173.     var_dump($yy_subpatterns);
  174.     $this->yybegin(self::THREE);
  175.     }
  176.     function yy_r1_16($yy_subpatterns)
  177.     {
  178.  
  179.     echo "rule 3\n";
  180.     var_dump($this->value);
  181.     echo "    rule 3 subpatterns: \n";
  182.     var_dump($yy_subpatterns);
  183.     }
  184.     function yy_r1_18($yy_subpatterns)
  185.     {
  186.  
  187.     echo "whitespace\n";
  188.     echo "    whitespace subpatterns: \n";
  189.     var_dump($yy_subpatterns);
  190.     return false// skip this token (do not return it)
  191.     }
  192.     function yy_r1_19($yy_subpatterns)
  193.     {
  194.  
  195.     echo "blah\n";
  196.     echo "    blah subpatterns: \n";
  197.     var_dump($yy_subpatterns);
  198.     var_dump($this->value);
  199.     }
  200.     function yy_r1_20($yy_subpatterns)
  201.     {
  202.  
  203.     echo "blahblah\n";
  204.     echo "    blahblah subpatterns: \n";
  205.     var_dump($yy_subpatterns);
  206.     var_dump($this->value);
  207.     }
  208.  
  209.  
  210.     function yylex2()
  211.     {
  212.         $tokenMap array (
  213.               => 0,
  214.             );
  215.         if ($this->>= strlen($this->data)) {
  216.             return false// end of input
  217.         }
  218.         $yy_global_pattern "/^([a-zA-Z][a-zA-Z])/";
  219.  
  220.         do {
  221.             if (preg_match($yy_global_patternsubstr($this->data$this->N)$yymatches)) {
  222.                 $yysubmatches $yymatches;
  223.                 $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  224.                 if (!count($yymatches)) {
  225.                     throw new Exception('Error: lexing failed because a rule matched' .
  226.                         'an empty string.  Input "' substr($this->data,
  227.                         $this->N5'... state TWO');
  228.                 }
  229.                 next($yymatches)// skip global match
  230.                 $this->token key($yymatches)// token number
  231.                 if ($tokenMap[$this->token]{
  232.                     // extract sub-patterns for passing to lex function
  233.                     $yysubmatches array_slice($yysubmatches$this->token 1,
  234.                         $tokenMap[$this->token]);
  235.                 else {
  236.                     $yysubmatches array();
  237.                 }
  238.                 $this->value current($yymatches)// token value
  239.                 $r $this->{'yy_r2_' $this->token}($yysubmatches);
  240.                 if ($r === null{
  241.                     $this->+= strlen($this->value);
  242.                     $this->line += substr_count("\n"$this->value);
  243.                     // accept this token
  244.                     return true;
  245.                 elseif ($r === true{
  246.                     // we have changed state
  247.                     // process this token in the new state
  248.                     return $this->yylex();
  249.                 elseif ($r === false{
  250.                     $this->+= strlen($this->value);
  251.                     $this->line += substr_count("\n"$this->value);
  252.                     if ($this->>= strlen($this->data)) {
  253.                         return false// end of input
  254.                     }
  255.                     // skip this token
  256.                     continue;
  257.                 else {                    $yy_yymore_patterns array(
  258.         => "",
  259.     );
  260.  
  261.                     // yymore is needed
  262.                     do {
  263.                         if (!strlen($yy_yymore_patterns[$this->token])) {
  264.                             throw new Exception('cannot do yymore for the last token');
  265.                         }
  266.                         if (preg_match($yy_yymore_patterns[$this->token],
  267.                               substr($this->data$this->N)$yymatches)) {
  268.                             $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  269.                             next($yymatches)// skip global match
  270.                             $this->token key($yymatches)// token number
  271.                             $this->value current($yymatches)// token value
  272.                             $this->line substr_count("\n"$this->value);
  273.                         }
  274.                     while ($this->{'yy_r2_' $this->token}(!== null);
  275.                     // accept
  276.                     $this->+= strlen($this->value);
  277.                     $this->line += substr_count("\n"$this->value);
  278.                     return true;
  279.                 }
  280.             else {
  281.                 throw new Exception('Unexpected input at line' $this->line .
  282.                     ': ' $this->data[$this->N]);
  283.             }
  284.             break;
  285.         while (true);
  286.     // end function
  287.  
  288.  
  289.     
  290.  
  291.  
  292.     const TWO 2;
  293.     function yy_r2_1($yy_subpatterns)
  294.     {
  295.  
  296.     echo "alpha alpha (state TWO)\n";
  297.     var_dump($this->value);
  298.     $this->yypopstate();
  299.     }
  300.  
  301.  
  302.     function yylex3()
  303.     {
  304.         $tokenMap array (
  305.               => 0,
  306.             );
  307.         if ($this->>= strlen($this->data)) {
  308.             return false// end of input
  309.         }
  310.         $yy_global_pattern "/^(\\$[0-9])/";
  311.  
  312.         do {
  313.             if (preg_match($yy_global_patternsubstr($this->data$this->N)$yymatches)) {
  314.                 $yysubmatches $yymatches;
  315.                 $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  316.                 if (!count($yymatches)) {
  317.                     throw new Exception('Error: lexing failed because a rule matched' .
  318.                         'an empty string.  Input "' substr($this->data,
  319.                         $this->N5'... state THREE');
  320.                 }
  321.                 next($yymatches)// skip global match
  322.                 $this->token key($yymatches)// token number
  323.                 if ($tokenMap[$this->token]{
  324.                     // extract sub-patterns for passing to lex function
  325.                     $yysubmatches array_slice($yysubmatches$this->token 1,
  326.                         $tokenMap[$this->token]);
  327.                 else {
  328.                     $yysubmatches array();
  329.                 }
  330.                 $this->value current($yymatches)// token value
  331.                 $r $this->{'yy_r3_' $this->token}($yysubmatches);
  332.                 if ($r === null{
  333.                     $this->+= strlen($this->value);
  334.                     $this->line += substr_count("\n"$this->value);
  335.                     // accept this token
  336.                     return true;
  337.                 elseif ($r === true{
  338.                     // we have changed state
  339.                     // process this token in the new state
  340.                     return $this->yylex();
  341.                 elseif ($r === false{
  342.                     $this->+= strlen($this->value);
  343.                     $this->line += substr_count("\n"$this->value);
  344.                     if ($this->>= strlen($this->data)) {
  345.                         return false// end of input
  346.                     }
  347.                     // skip this token
  348.                     continue;
  349.                 else {                    $yy_yymore_patterns array(
  350.         => "",
  351.     );
  352.  
  353.                     // yymore is needed
  354.                     do {
  355.                         if (!strlen($yy_yymore_patterns[$this->token])) {
  356.                             throw new Exception('cannot do yymore for the last token');
  357.                         }
  358.                         if (preg_match($yy_yymore_patterns[$this->token],
  359.                               substr($this->data$this->N)$yymatches)) {
  360.                             $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  361.                             next($yymatches)// skip global match
  362.                             $this->token key($yymatches)// token number
  363.                             $this->value current($yymatches)// token value
  364.                             $this->line substr_count("\n"$this->value);
  365.                         }
  366.                     while ($this->{'yy_r3_' $this->token}(!== null);
  367.                     // accept
  368.                     $this->+= strlen($this->value);
  369.                     $this->line += substr_count("\n"$this->value);
  370.                     return true;
  371.                 }
  372.             else {
  373.                 throw new Exception('Unexpected input at line' $this->line .
  374.                     ': ' $this->data[$this->N]);
  375.             }
  376.             break;
  377.         while (true);
  378.     // end function
  379.  
  380.  
  381.     
  382.  
  383.  
  384.     const THREE 3;
  385.     function yy_r3_1($yy_subpatterns)
  386.     {
  387.  
  388.     echo "number (state THREE)\n";
  389.     $this->yybegin(self::START);
  390.     }
  391.  
  392. }
  393.  
  394. $a new TestLexer('a_AB1yk $09.1 a$B_b$1 #testtesthihi <a href="http://www.example.com/s/style.css">');
  395. $a->yylex();
  396. var_dump('advance: ' $a->value);
  397. $a->yylex();
  398. var_dump('advance: ' $a->value);
  399. $a->yylex();
  400. var_dump('advance: ' $a->value);
  401. $a->yylex();
  402. var_dump('advance: ' $a->value);
  403. $a->yylex();
  404. var_dump('advance: ' $a->value);
  405. $a->yylex();
  406. var_dump('advance: ' $a->value);
  407. $a->yylex();
  408. var_dump('advance: ' $a->value);
  409. $a->yylex();
  410. var_dump('advance: ' $a->value);
  411. $a->yylex();
  412. var_dump('advance: ' $a->value);

Documentation generated on Sun, 02 Jul 2006 08:51:26 -0400 by phpDocumentor 1.3.0