Author: kn
Date: Thu Feb 14 15:45:25 2008
New Revision: 7375
Log:
- Redefined parser structure
- Successfully parse empty documents
Added:
experimental/Document/tests/document_rst_parser_tests.php (with props)
experimental/Document/tests/files/rst/parser/
experimental/Document/tests/files/rst/parser/SOURCE
experimental/Document/tests/files/rst/parser/empty.rst
experimental/Document/tests/files/rst/parser/empty.txt (with props)
experimental/Document/tests/files/rst/parser/titles.txt (with props)
Modified:
experimental/Document/design/class_diagram.png
experimental/Document/src/document/rst/node.php
experimental/Document/src/document/rst/nodes/document.php
experimental/Document/src/document/rst/parser.php
experimental/Document/src/document_autoload.php
experimental/Document/tests/suite.php
Modified: experimental/Document/design/class_diagram.png
==============================================================================
Binary files - no diff available.
Modified: experimental/Document/src/document/rst/node.php
==============================================================================
--- experimental/Document/src/document/rst/node.php [iso-8859-1] (original)
+++ experimental/Document/src/document/rst/node.php [iso-8859-1] Thu Feb 14
15:45:25 2008
@@ -16,7 +16,7 @@
* @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
*/
-class ezcDocumentRstNode extends ezcBaseStruct implements RecursiveIterator
+class ezcDocumentRstNode extends ezcBaseStruct // implements RecursiveIterator
{
// Node types
const DOCUMENT = 0;
@@ -42,11 +42,8 @@
/**
* Construct RST node
*
- * @ignore
* @param int $type
- * @param mixed $content
- * @param int $line
- * @param int $position
+ * @param array $nodes
* @return void
*/
public function __construct( $type, array $nodes = array() )
@@ -64,7 +61,7 @@
*/
public static function __set_state( $properties )
{
- return new ezcDocumentRstNode(
+ return new static(
$properties['type'],
$properties['nodes']
);
Modified: experimental/Document/src/document/rst/nodes/document.php
==============================================================================
--- experimental/Document/src/document/rst/nodes/document.php [iso-8859-1]
(original)
+++ experimental/Document/src/document/rst/nodes/document.php [iso-8859-1] Thu
Feb 14 15:45:25 2008
@@ -21,11 +21,7 @@
/**
* Construct RST document node
*
- * @ignore
- * @param int $type
- * @param mixed $content
- * @param int $line
- * @param int $position
+ * @param array $nodes
* @return void
*/
public function __construct( array $nodes = array() )
@@ -34,6 +30,20 @@
// added.
parent::__construct( self::DOCUMENT, $nodes );
}
+
+ /**
+ * Set state after var_export
+ *
+ * @param array $properties
+ * @return void
+ * @ignore
+ */
+ public static function __set_state( $properties )
+ {
+ return new ezcDocumentRstDocumentNode(
+ $properties['nodes']
+ );
+ }
}
?>
Modified: experimental/Document/src/document/rst/parser.php
==============================================================================
--- experimental/Document/src/document/rst/parser.php [iso-8859-1] (original)
+++ experimental/Document/src/document/rst/parser.php [iso-8859-1] Thu Feb 14
15:45:25 2008
@@ -51,11 +51,14 @@
ezcDocumentRstToken::NEWLINE => array(
),
ezcDocumentRstToken::BACKSLASH => array(
- 'reduceBackslash'
+ 'shiftBackslash'
),
ezcDocumentRstToken::SPECIAL_CHARS => array(
),
ezcDocumentRstToken::TEXT_LINE => array(
+ ),
+ ezcDocumentRstToken::EOF => array(
+ 'shiftDocument'
),
);
@@ -69,9 +72,7 @@
*
* <code>
* array(
- * end_of_file => array(
- * 'reduceDocument'
- * ),
+ * ezcDocumentRstNode::DOCUMENT => 'reduceDocument'
* ...
* )
* </code>
@@ -79,6 +80,7 @@
* @var array
*/
protected $reductions = array(
+ ezcDocumentRstNode::DOCUMENT => 'reduceDocument',
);
/**
@@ -97,14 +99,11 @@
protected $documentStack = array();
/**
- * List with parsed tokens.
- *
- * Each token which has been parsed will be removed from the list, until
- * the list is empty.
- *
- * @var array
- */
- protected $tokens = array();
+ * The resulting sucessfully parsed document.
+ *
+ * @var ezcDocumentRstDocumentNode
+ */
+ protected $parsedDocument = null;
/**
* Construct parser
@@ -112,11 +111,92 @@
* Construct the parser from an array of tokens (ezcDocumentRstToken), and
* create a cocument out of this token array.
*
+ * @ignore
* @return void
- */
- public function __construct( array $tokens )
- {
- // @TODO: Implement
+ * /
+ public function __construct()
+ {
+ } // */
+
+ /**
+ * Shift- / reduce-parser for RST token stack
+ *
+ * @param array $tokens
+ * @return void
+ */
+ public function parse( array $tokens )
+ {
+ while ( ( $token = array_shift( $tokens ) ) !== null )
+ {
+ // First shift given token by the defined reduction methods
+ foreach ( $this->shifts[$token->type] as $method )
+ {
+ if ( ( $node = $this->$method( $token, $tokens ) ) === false )
+ {
+ // The shift method processed the token, go to next one.
+ continue;
+ }
+
+ // Call reduce methods for node
+ $reduction = $this->reductions[$node->type];
+ $this->$reduction( $node );
+
+ // We found a matching rule, so that we can leave the loop
+ break;
+ }
+ }
+
+ // This should always be set at this point, each other case should have
+ // caused an exception.
+ return $this->parsedDocument;
+ }
+
+ /**
+ * Create new document node
+ *
+ * @param ezcDocumentRstToken $token
+ * @param array $tokens
+ * @return ezcDocumentRstDocumentNode
+ */
+ protected function shiftDocument( ezcDocumentRstToken $token, array
$tokens )
+ {
+ // If there are any tokens left after the end of the file, something
+ // went seriously wrong in the tokenizer.
+ if ( count( $tokens ) )
+ {
+ throw new ezcDocumentRstParserException(
+ $token, ezcDocumentRstParserException::FATAL,
+ 'Unexpected end of file.'
+ );
+ }
+
+ return new ezcDocumentRstDocumentNode();
+ }
+
+ /**
+ * Reduce all elements to one document node.
+ *
+ * @param ezcDocumentRstDocumentNode $node
+ * @return void
+ */
+ protected function reduceDocument( ezcDocumentRstDocumentNode $node )
+ {
+ foreach ( $this->documentStack as $child )
+ {
+ if ( ( $child->type !== ezcDocumentRstNode::SECTION ) &&
+ ( $child->type !== ezcDocumentRstNode::METADATA ) )
+ {
+ throw new ezcDocumentRstParserException(
+ $child, ezcDocumentRstParserException::FATAL,
+ 'Unexpected node.'
+ );
+ }
+
+ $node->nodes[] = $child;
+ }
+
+ $this->documentStack = array();
+ $this->parsedDocument = $node;
}
}
Modified: experimental/Document/src/document_autoload.php
==============================================================================
--- experimental/Document/src/document_autoload.php [iso-8859-1] (original)
+++ experimental/Document/src/document_autoload.php [iso-8859-1] Thu Feb 14
15:45:25 2008
@@ -18,6 +18,7 @@
'ezcDocumentConverter' =>
'Document/interfaces/converter.php',
'ezcDocumentValidation' =>
'Document/interfaces/validation.php',
'ezcDocumentConverterBaseOptions' =>
'Document/options/converter_base.php',
+ 'ezcDocumentRstNode' =>
'Document/document/rst/node.php',
'ezcDocumentXmlBase' =>
'Document/document/xml_base.php',
'ezcDocumentXmlBaseOptions' =>
'Document/options/document_xml_base.php',
'ezcDocumentXsltConverter' => 'Document/converters/xslt.php',
@@ -33,7 +34,9 @@
'ezcDocumentManager' => 'Document/document_manager.php',
'ezcDocumentRelaxNgValidator' =>
'Document/validator/realxng.php',
'ezcDocumentRst' => 'Document/document/rst.php',
+ 'ezcDocumentRstDocumentNode' =>
'Document/document/rst/nodes/document.php',
'ezcDocumentRstOptions' =>
'Document/options/document_rst.php',
+ 'ezcDocumentRstParser' =>
'Document/document/rst/parser.php',
'ezcDocumentRstToken' =>
'Document/document/rst/token.php',
'ezcDocumentRstTokenizer' =>
'Document/document/rst/tokenizer.php',
'ezcDocumentXhtml' =>
'Document/document/xml/xhtml.php',
Added: experimental/Document/tests/document_rst_parser_tests.php
==============================================================================
--- experimental/Document/tests/document_rst_parser_tests.php (added)
+++ experimental/Document/tests/document_rst_parser_tests.php [iso-8859-1] Thu
Feb 14 15:45:25 2008
@@ -1,0 +1,83 @@
+<?php
+/**
+ * ezcDocumentRstParserTests
+ *
+ * @package Document
+ * @version //autogen//
+ * @subpackage Tests
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Test suite for class.
+ *
+ * @package Document
+ * @subpackage Tests
+ */
+class ezcDocumentRstParserTests extends ezcTestCase
+{
+ protected static $testDocuments = null;
+
+ public static function suite()
+ {
+ return new PHPUnit_Framework_TestSuite( __CLASS__ );
+ }
+
+ public static function getTestDocuments()
+ {
+ if ( self::$testDocuments === null )
+ {
+ // Get a list of all test files from the respektive folder
+ $testFiles = glob( dirname( __FILE__ ) . '/files/rst/parser/*.txt'
);
+
+ // Create array with the test file and the expected result file
+ foreach ( $testFiles as $file )
+ {
+ self::$testDocuments[] = array(
+ $file,
+ substr( $file, 0, -3 ) . 'rst'
+ );
+ }
+ }
+
+ return self::$testDocuments;
+ }
+
+ /**
+ * @dataProvider getTestDocuments
+ */
+ public function testParseRstFile( $from, $to )
+ {
+ if ( !is_file( $to ) )
+ {
+ $this->markTestSkipped( "Comparision file '$to' not yet defined."
);
+ }
+
+ $tokenizer = new ezcDocumentRstTokenizer();
+ $parser = new ezcDocumentRstParser();
+
+ $document = $parser->parse( $tokenizer->tokenizeFile( $from ) );
+
+ $this->assertTrue(
+ $document instanceof ezcDocumentRstDocumentNode
+ );
+
+ $expected = include $to;
+
+ // Store test file, to have something to compare on failure
+ $tempDir = $this->createTempDir( 'rst_parser_' ) . '/';
+ file_put_contents( $tempDir . basename( $to ), "<?php\n\nreturn " .
var_export( $document, true ) . ";\n\n" );
+
+ $this->assertEquals(
+ $expected,
+ $document,
+ 'Parsed document does not match expected document.'
+ );
+
+ // Remove tempdir, when nothing failed.
+ $this->removeTempDir();
+ }
+}
+
+?>
Propchange: experimental/Document/tests/document_rst_parser_tests.php
------------------------------------------------------------------------------
svn:eol-style = native
Added: experimental/Document/tests/files/rst/parser/SOURCE
==============================================================================
--- experimental/Document/tests/files/rst/parser/SOURCE (added)
+++ experimental/Document/tests/files/rst/parser/SOURCE [iso-8859-1] Thu Feb 14
15:45:25 2008
@@ -1,0 +1,5 @@
+Test data
+=========
+
+Extracted from
+http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html
Added: experimental/Document/tests/files/rst/parser/empty.rst
==============================================================================
--- experimental/Document/tests/files/rst/parser/empty.rst (added)
+++ experimental/Document/tests/files/rst/parser/empty.rst [iso-8859-1] Thu Feb
14 15:45:25 2008
@@ -1,0 +1,9 @@
+<?php
+
+return ezcDocumentRstDocumentNode::__set_state(array(
+ 'type' => 0,
+ 'nodes' =>
+ array (
+ ),
+));
+
Added: experimental/Document/tests/files/rst/parser/empty.txt
==============================================================================
(empty)
Propchange: experimental/Document/tests/files/rst/parser/empty.txt
------------------------------------------------------------------------------
svn:eol-style = native
Added: experimental/Document/tests/files/rst/parser/titles.txt
==============================================================================
--- experimental/Document/tests/files/rst/parser/titles.txt (added)
+++ experimental/Document/tests/files/rst/parser/titles.txt [iso-8859-1] Thu
Feb 14 15:45:25 2008
@@ -1,0 +1,34 @@
+===============
+ Section Title
+===============
+
+---------------
+ Section Title
+---------------
+
+Section Title
+=============
+
+Section Title
+-------------
+
+Section Title
+`````````````
+
+Section Title
+'''''''''''''
+
+Section Title
+.............
+
+Section Title
+~~~~~~~~~~~~~
+
+Section Title
+*************
+
+Section Title
++++++++++++++
+
+Section Title
+^^^^^^^^^^^^^
Propchange: experimental/Document/tests/files/rst/parser/titles.txt
------------------------------------------------------------------------------
svn:eol-style = native
Modified: experimental/Document/tests/suite.php
==============================================================================
--- experimental/Document/tests/suite.php [iso-8859-1] (original)
+++ experimental/Document/tests/suite.php [iso-8859-1] Thu Feb 14 15:45:25 2008
@@ -20,6 +20,7 @@
require_once 'document_options_xml_base_test.php';
require_once 'document_xml_base_test.php';
require_once 'document_rst_tokenizer_tests.php';
+require_once 'document_rst_parser_tests.php';
require_once 'converter_options_ezp3_ezp4_test.php';
require_once 'converter_ezp3_ezp4_test.php';
@@ -41,6 +42,7 @@
$this->addTest( ezcDocumentOptionsXmlBaseTests::suite() );
$this->addTest( ezcDocumentXmlBaseTests::suite() );
$this->addTest( ezcDocumentRstTokenizerTests::suite() );
+ $this->addTest( ezcDocumentRstParserTests::suite() );
$this->addTest( ezcDocumentConverterOptionsEzp3ToEzp4Tests::suite() );
$this->addTest( ezcDocumentConverterEzp3ToEzp4Tests::suite() );
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components