Пример #1
0
 /**
  * Initializer
  *
  * Sets up the Graph class with an image width and height defaults to
  * 640x480
  *
  * @param Integer $width Image width
  * @param Integer $height Image height
  */
 public static function init($width = 640, $height = 480)
 {
     // default width and height equal to that of a poor monitor (in early 2000s)
     self::$width = $width;
     self::$height = $height;
     //initialize main class
     Parser::init();
 }
Пример #2
0
 /**
  * init the class
  *
  * @since 3.0.0
  *
  * @param string $mode name of the mode
  *
  * @return mixed
  */
 public function init($mode = null)
 {
     $parser = new Parser($this->_request);
     $parser->init($mode);
     /* run command */
     $commandKey = $parser->getArgument(0);
     if (array_key_exists($commandKey, $this->_namespaceArray)) {
         $commandClass = $this->_namespaceArray[$commandKey];
         if (class_exists($commandClass)) {
             $command = new $commandClass($this->_registry, $this->_request, $this->_config);
             return $command->run($mode);
         }
     }
     return false;
 }
Пример #3
0
 function testDumpLoad()
 {
     $tokens = ['d' => '[0-9]+', 'a', 'b', 'c', 'sp' => '\\s+'];
     $rules = ['A' => [[['d', ['+', ['|', 'a', 'b']], 'B'], true]], 'B' => [[['d'], true]]];
     $lexer = new Lexer($tokens);
     $parser = new Parser();
     $parser->setLexer($lexer);
     $parser->init($rules);
     $parser->setSkipTokens(['sp']);
     $data = $parser->dump();
     $s = serialize($data);
     $e = var_export($data, true);
     $parser = new Parser();
     $parser->setLexer($lexer);
     $v = eval("return {$e};");
     $parser->load($v);
     $parser->tree('123 a a b 1244');
     $parser = new Parser();
     $parser->setLexer($lexer);
     $data = unserialize($s);
     $parser->load($data);
     $parser->tree('123 a a b 1244');
     ob_start();
     $parser->printTree('123 a a b 1244');
     $text = ob_get_clean();
     $this->assertTrue(is_string($text));
 }
Пример #4
0
 /**
  * main entry function
  * @param array input tokens
  * @return LRParseNode parse tree
  */
 function parse(array $input)
 {
     // Parser class initialized input in standard fashion
     parent::init($input);
     // we always start at state 1
     $state = 1;
     // repeat until success or failure
     // if table is correct, this cannot get into infinite loop
     while (true) {
         //echo "\nState #$state, Symbol \"",$this->Lex->name($this->t),"\" \n";
         //echo "Permitted: [", $this->Lex->implode( ', ', $this->Table->permitted( $state, $this->Grammar )) ,"]\n";
         //echo "Stack: [",$this->Lex->implode(', ',$this->stack),"] \n";
         // get next action based up on current terminal in input stream
         $n = $this->Table->lookup($state, $this->t);
         if (is_null($n)) {
             $this->badtoken = $this->tok;
             // display which terminal symbols would be permitted
             $expected = $this->Table->permitted($state, $this->Grammar);
             if (!empty($expected)) {
                 $expected = 'expecting "' . $this->Lex->implode('", or "', $expected) . '"';
             } else {
                 $expected = 'no terminals permitted';
             }
             if (isset($this->stack[0])) {
                 $Node = end($this->stack);
                 if ($this->fail('after "%s" %s in state #%u', $this->Lex->name($Node->scalar_symbol()), $expected, $state)) {
                     continue;
                 } else {
                     break;
                 }
             } else {
                 if ($this->fail('%s in state #%u', $expected, $state)) {
                     continue;
                 } else {
                     break;
                 }
             }
         }
         // Odd numbers are states, even numbers are rules
         if ($n & 1) {
             // SHIFT
             if ($this->t === P_EOF) {
                 $len = count($this->stack);
                 if ($len !== 1) {
                     if ($this->fail('premature EOF stack has %u elements', $len)) {
                         continue;
                     } else {
                         break;
                     }
                 }
                 // Full parse
                 return $this->stack[0];
             }
             // shift, and move to next state
             $state = $this->shift($state, $n);
         } else {
             // REDUCE, and move to previous state
             $state = $this->reduce($state, $n);
         }
     }
 }