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

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