/** * @dataProvider getErroneousTestDocuments */ public function testParseErroneousRstFile($file, $message) { $tokenizer = new ezcDocumentRstTokenizer(); $parser = new ezcDocumentRstParser(); try { $document = $parser->parse($tokenizer->tokenizeFile($file)); $this->fail('Expected ezcDocumentRstParserException.'); } catch (ezcDocumentParserException $e) { $this->assertSame($message, $e->getMessage(), 'Different parse error expected.'); } }
/** * Parse directive token list with RST parser * * This method is intended to parse the token list, provided for the RST * contents using the standard RST parser. It will be visited afterwards by * the provided RST-visitor implementation. * * The method returns the created document as a DOMDocument. You normally * need to use DOMDocument::importNode to embed the conatined nodes in your * target document. * * @param array $tokens * @param ezcDocumentRstVisitor $visitor * @return DOMDocument */ protected function parseTokens(array $tokens, ezcDocumentRstVisitor $visitor) { $parser = new ezcDocumentRstParser(); $ast = $parser->parse($tokens); $doc = $visitor->visit($ast, $this->path); return $doc; }
/** * Validate the input string * * Validate the input string against the specification of the current * document format. * * Returns true, if the validation succeded, and an array with * ezcDocumentValidationError objects otherwise. * * @param string $string * @return mixed */ public function validateString($string) { $tokenizer = new ezcDocumentRstTokenizer(); $parser = new ezcDocumentRstParser(); // Only halt on parse errors, and collect all other errors. $parser->options->errorReporting = E_PARSE; $errors = array(); $ast = null; try { // Try to parse the document and keep the parse tree for evetual // checking for decoration errors $ast = $parser->parse($tokenizer->tokenizeString($string)); } catch (ezcDocumentParserException $e) { $errors[] = $e; } // Get errors and notices from parsed document $errors = array_merge($errors, $parser->errors); // If we had no parse error until now, we also try to decorate the // document, which may leed to another class of errors. if ($ast !== null) { $oldErrorReporting = $this->options->errorReporting; $this->options->errorReporting = E_PARSE; try { $visitor = new ezcDocumentRstDocbookVisitor($this, $this->path); $visitor->visit($ast, $this->path); // Get errors and notices from parsed document $errors = array_merge($errors, $visitor->getErrors()); } catch (ezcDocumentVisitException $e) { $errors[] = $e; } // Reset error reporting $this->options->errorReporting = $oldErrorReporting; } if (count($errors) === 0) { // If no problem could be found, jsut return true return true; } else { // Transform aggregated errors into validation errors foreach ($errors as $nr => $error) { $errors[$nr] = ezcDocumentValidationError::createFromException($error); } return $errors; } }
/** * Reenter parser with a list of tokens * * Returns a parsed document created from the given tokens. With optional, * but default, reindetation of the tokens relative to the first token. * * @param array $tokens * @param bool $reindent * @return ezcDocumentRstDocumentNode */ protected function reenterParser(array $tokens, $reindent = true) { if (count($tokens) < 1) { return array(); } // Fix indentation for all cell tokens, as they were a single document. $fixedTokens = $reindent ? $this->realignTokens($tokens) : $tokens; $parser = new ezcDocumentRstParser(); return $parser->parse($fixedTokens); }
/** * Reenter parser with a list of tokens * * Returns a parsed document created from the given tokens. With optional, * but default, reindetation of the tokens relative to the first token. * * @param array $tokens * @param bool $reindent * @return ezcDocumentRstDocumentNode */ protected function reenterParser(array $tokens, $reindent = true) { if (count($tokens) < 1) { return array(); } /* DEBUG static $c = 0; file_put_contents( "tokens-reentered-$c.php", "<?php\n\n return " . var_export( $tokens, true ) . ";\n\n" ); // /DEBUG */ // Fix indentation for all cell tokens, as they were a single document. if ($reindent) { $firstToken = reset($tokens); $offset = $firstToken->position + ($firstToken->type === ezcDocumentRstToken::WHITESPACE ? 0 : -1); $fixedTokens = array(); foreach ($tokens as $nr => $token) { if ($token->type === ezcDocumentRstToken::WHITESPACE && isset($tokens[$nr + 1]) && $tokens[$nr + 1]->type === ezcDocumentRstToken::WHITESPACE) { // Skip multiple whitespace tokens in a row. continue; } if ($token->type === ezcDocumentRstToken::WHITESPACE && $token->position <= $offset) { if (strlen($token->content) <= 1) { // Just skip token, completely out of tokens bounds continue; } else { // Shorten starting whitespace token $token = clone $token; $token->position = 0; $token->content = substr($token->content, 1); $fixedTokens[] = $token; } } else { $token = clone $token; $token->position -= $offset; $fixedTokens[] = $token; } } // If required add a second newline, if the provided token array does // not contain any newlines at the end. if ($token->type !== ezcDocumentRstToken::NEWLINE) { $fixedTokens[] = new ezcDocumentRstToken(ezcDocumentRstToken::NEWLINE, "\n", null, null); } } else { $fixedTokens = $tokens; } $fixedTokens[] = new ezcDocumentRstToken(ezcDocumentRstToken::NEWLINE, "\n", null, null); $fixedTokens[] = new ezcDocumentRstToken(ezcDocumentRstToken::EOF, null, null, null); /* DEBUG file_put_contents( "tokens-reentered-$c-fixed.php", "<?php\n\n return " . var_export( $fixedTokens, true ) . ";\n\n" ); ++$c; // /DEBUG */ $parser = new ezcDocumentRstParser(); return $parser->parse($fixedTokens); }