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

Source for file TestLexer.php

Documentation is available at TestLexer.php

  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.               1 => 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.         1 => "^(#(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.     const START = 1;
  139.     function yy_r1_1($yy_subpatterns)
  140.     {
  141.  
  142.     echo "complex\n";
  143.     var_dump($this->value);
  144.     echo "    complex subpatterns: \n";
  145.     var_dump($yy_subpatterns);
  146.     }
  147.     function yy_r1_10($yy_subpatterns)
  148.     {
  149.  
  150.     echo "weirdo\n";
  151.     var_dump($this->value);
  152.     echo "    weirdo subpatterns: \n";
  153.     var_dump($yy_subpatterns);
  154.     }
  155.     function yy_r1_13($yy_subpatterns)
  156.     {
  157.  
  158.     echo "rule 1\n";
  159.     var_dump($this->value);
  160.     echo "    rule 1 subpatterns: \n";
  161.     var_dump($yy_subpatterns);
  162.     $this->yypushstate(self::TWO);
  163.     }
  164.     function yy_r1_15($yy_subpatterns)
  165.     {
  166.  
  167.     echo "rule 2\n";
  168.     var_dump($this->value);
  169.     echo "    rule 2 subpatterns: \n";
  170.     var_dump($yy_subpatterns);
  171.     $this->yybegin(self::THREE);
  172.     }
  173.     function yy_r1_16($yy_subpatterns)
  174.     {
  175.  
  176.     echo "rule 3\n";
  177.     var_dump($this->value);
  178.     echo "    rule 3 subpatterns: \n";
  179.     var_dump($yy_subpatterns);
  180.     }
  181.     function yy_r1_18($yy_subpatterns)
  182.     {
  183.  
  184.     echo "whitespace\n";
  185.     echo "    whitespace subpatterns: \n";
  186.     var_dump($yy_subpatterns);
  187.     return false; // skip this token (do not return it)
  188.     }
  189.     function yy_r1_19($yy_subpatterns)
  190.     {
  191.  
  192.     echo "blah\n";
  193.     echo "    blah subpatterns: \n";
  194.     var_dump($yy_subpatterns);
  195.     var_dump($this->value);
  196.     }
  197.     function yy_r1_20($yy_subpatterns)
  198.     {
  199.  
  200.     echo "blahblah\n";
  201.     echo "    blahblah subpatterns: \n";
  202.     var_dump($yy_subpatterns);
  203.     var_dump($this->value);
  204.     }
  205.  
  206.  
  207.     function yylex2()
  208.     {
  209.         $tokenMap = array (
  210.               1 => 0,
  211.             );
  212.         if ($this->>= strlen($this->data)) {
  213.             return false; // end of input
  214.         }
  215.         $yy_global_pattern "/^([a-zA-Z][a-zA-Z])/";
  216.  
  217.         do {
  218.             if (preg_match($yy_global_patternsubstr($this->data$this->N)$yymatches)) {
  219.                 $yysubmatches $yymatches;
  220.                 $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  221.                 if (!count($yymatches)) {
  222.                     throw new Exception('Error: lexing failed because a rule matched' .
  223.                         'an empty string.  Input "' substr($this->data,
  224.                         $this->N5'... state TWO');
  225.                 }
  226.                 next($yymatches)// skip global match
  227.                 $this->token key($yymatches)// token number
  228.                 if ($tokenMap[$this->token]{
  229.                     // extract sub-patterns for passing to lex function
  230.                     $yysubmatches array_slice($yysubmatches$this->token + 1,
  231.                         $tokenMap[$this->token]);
  232.                 else {
  233.                     $yysubmatches = array();
  234.                 }
  235.                 $this->value current($yymatches)// token value
  236.                 $r $this->{'yy_r2_' $this->token}($yysubmatches);
  237.                 if ($r === null{
  238.                     $this->+= strlen($this->value);
  239.                     $this->line += substr_count("\n"$this->value);
  240.                     // accept this token
  241.                     return true;
  242.                 elseif ($r === true{
  243.                     // we have changed state
  244.                     // process this token in the new state
  245.                     return $this->yylex();
  246.                 elseif ($r === false{
  247.                     $this->+= strlen($this->value);
  248.                     $this->line += substr_count("\n"$this->value);
  249.                     if ($this->>= strlen($this->data)) {
  250.                         return false; // end of input
  251.                     }
  252.                     // skip this token
  253.                     continue;
  254.                 else {                    $yy_yymore_patterns = array(
  255.         1 => "",
  256.     );
  257.  
  258.                     // yymore is needed
  259.                     do {
  260.                         if (!strlen($yy_yymore_patterns[$this->token])) {
  261.                             throw new Exception('cannot do yymore for the last token');
  262.                         }
  263.                         if (preg_match($yy_yymore_patterns[$this->token],
  264.                               substr($this->data$this->N)$yymatches)) {
  265.                             $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  266.                             next($yymatches)// skip global match
  267.                             $this->token key($yymatches)// token number
  268.                             $this->value current($yymatches)// token value
  269.                             $this->line substr_count("\n"$this->value);
  270.                         }
  271.                     while ($this->{'yy_r2_' $this->token}(!== null);
  272.                     // accept
  273.                     $this->+= strlen($this->value);
  274.                     $this->line += substr_count("\n"$this->value);
  275.                     return true;
  276.                 }
  277.             else {
  278.                 throw new Exception('Unexpected input at line' $this->line .
  279.                     ': ' $this->data[$this->N]);
  280.             }
  281.             break;
  282.         while (true);
  283.     // end function
  284.  
  285.  
  286.     const TWO = 2;
  287.     function yy_r2_1($yy_subpatterns)
  288.     {
  289.  
  290.     echo "alpha alpha (state TWO)\n";
  291.     var_dump($this->value);
  292.     $this->yypopstate();
  293.     }
  294.  
  295.  
  296.     function yylex3()
  297.     {
  298.         $tokenMap = array (
  299.               1 => 0,
  300.             );
  301.         if ($this->>= strlen($this->data)) {
  302.             return false; // end of input
  303.         }
  304.         $yy_global_pattern "/^(\\$[0-9])/";
  305.  
  306.         do {
  307.             if (preg_match($yy_global_patternsubstr($this->data$this->N)$yymatches)) {
  308.                 $yysubmatches $yymatches;
  309.                 $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  310.                 if (!count($yymatches)) {
  311.                     throw new Exception('Error: lexing failed because a rule matched' .
  312.                         'an empty string.  Input "' substr($this->data,
  313.                         $this->N5'... state THREE');
  314.                 }
  315.                 next($yymatches)// skip global match
  316.                 $this->token key($yymatches)// token number
  317.                 if ($tokenMap[$this->token]{
  318.                     // extract sub-patterns for passing to lex function
  319.                     $yysubmatches array_slice($yysubmatches$this->token + 1,
  320.                         $tokenMap[$this->token]);
  321.                 else {
  322.                     $yysubmatches = array();
  323.                 }
  324.                 $this->value current($yymatches)// token value
  325.                 $r $this->{'yy_r3_' $this->token}($yysubmatches);
  326.                 if ($r === null{
  327.                     $this->+= strlen($this->value);
  328.                     $this->line += substr_count("\n"$this->value);
  329.                     // accept this token
  330.                     return true;
  331.                 elseif ($r === true{
  332.                     // we have changed state
  333.                     // process this token in the new state
  334.                     return $this->yylex();
  335.                 elseif ($r === false{
  336.                     $this->+= strlen($this->value);
  337.                     $this->line += substr_count("\n"$this->value);
  338.                     if ($this->>= strlen($this->data)) {
  339.                         return false; // end of input
  340.                     }
  341.                     // skip this token
  342.                     continue;
  343.                 else {                    $yy_yymore_patterns = array(
  344.         1 => "",
  345.     );
  346.  
  347.                     // yymore is needed
  348.                     do {
  349.                         if (!strlen($yy_yymore_patterns[$this->token])) {
  350.                             throw new Exception('cannot do yymore for the last token');
  351.                         }
  352.                         if (preg_match($yy_yymore_patterns[$this->token],
  353.                               substr($this->data$this->N)$yymatches)) {
  354.                             $yymatches array_filter($yymatches'strlen')// remove empty sub-patterns
  355.                             next($yymatches)// skip global match
  356.                             $this->token key($yymatches)// token number
  357.                             $this->value current($yymatches)// token value
  358.                             $this->line substr_count("\n"$this->value);
  359.                         }
  360.                     while ($this->{'yy_r3_' $this->token}(!== null);
  361.                     // accept
  362.                     $this->+= strlen($this->value);
  363.                     $this->line += substr_count("\n"$this->value);
  364.                     return true;
  365.                 }
  366.             else {
  367.                 throw new Exception('Unexpected input at line' $this->line .
  368.                     ': ' $this->data[$this->N]);
  369.             }
  370.             break;
  371.         while (true);
  372.     // end function
  373.  
  374.  
  375.     const THREE = 3;
  376.     function yy_r3_1($yy_subpatterns)
  377.     {
  378.  
  379.     echo "number (state THREE)\n";
  380.     $this->yybegin(self::START);
  381.     }
  382.  
  383. }
  384.  
  385. $a = new TestLexer('a_AB1yk $09.1 a$B_b$1 #testtesthihi <a href="http://www.example.com/s/style.css">');
  386. $a->yylex();
  387. var_dump('advance: ' $a->value);
  388. $a->yylex();
  389. var_dump('advance: ' $a->value);
  390. $a->yylex();
  391. var_dump('advance: ' $a->value);
  392. $a->yylex();
  393. var_dump('advance: ' $a->value);
  394. $a->yylex();
  395. var_dump('advance: ' $a->value);
  396. $a->yylex();
  397. var_dump('advance: ' $a->value);
  398. $a->yylex();
  399. var_dump('advance: ' $a->value);
  400. $a->yylex();
  401. var_dump('advance: ' $a->value);
  402. $a->yylex();
  403. var_dump('advance: ' $a->value);

Documentation generated on Mon, 11 Mar 2019 14:46:17 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.