Author: rb Date: Thu Aug 16 11:08:50 2007 New Revision: 5923 Log: - Added {capture $var} ... {/capture} to assign everything inside that block to the given variable ($var in this case).
Added: trunk/Template/src/parsers/source_to_tst/implementations/capture.php (with props) trunk/Template/src/syntax_trees/tst/nodes/capture.php (with props) trunk/Template/tests/regression_tests/capture/ trunk/Template/tests/regression_tests/capture/correct/ trunk/Template/tests/regression_tests/capture/correct/capture.in (with props) trunk/Template/tests/regression_tests/capture/correct/capture.out (with props) trunk/Template/tests/regression_tests/capture/incorrect/ trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.in (with props) trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.out (with props) trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.in (with props) trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.out (with props) Modified: trunk/Template/src/parsers/source_to_tst/implementations/control_structure.php trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php trunk/Template/src/template_autoload.php Added: trunk/Template/src/parsers/source_to_tst/implementations/capture.php ============================================================================== --- trunk/Template/src/parsers/source_to_tst/implementations/capture.php (added) +++ trunk/Template/src/parsers/source_to_tst/implementations/capture.php [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,125 @@ +<?php +/** + * File containing the ezcTemplateCaptureSourceToTstParser class + * + * @package Template + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @access private + */ +/** + * + * @package Template + * @version //autogen// + * @access private + */ +class ezcTemplateCaptureSourceToTstParser extends ezcTemplateSourceToTstParser +{ + /** + * Passes control to parent. + * + * @param ezcTemplateParser $parser + * @param ezcTemplateSourceToTstParser $parentParser + * @param ezcTemplateCursor $startCursor + */ + function __construct( ezcTemplateParser $parser, /*ezcTemplateSourceToTstParser*/ $parentParser, /*ezcTemplateCursor*/ $startCursor ) + { + parent::__construct( $parser, $parentParser, $startCursor ); + $this->block = null; + } + + /** + * Parses the expression by using the ezcTemplateExpressionSourceToTstParser class. + * + * @param ezcTemplateCursor $cursor + * @return bool + */ + protected function parseCurrent( ezcTemplateCursor $cursor ) + { + if ( $this->block->name == "capture" ) + { + // handle closing block + if ( $this->block->isClosingBlock ) + { + $this->findNextElement(); + if ( !$this->parentParser->atEnd( $cursor, null, false ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_CURLY_BRACKET_CLOSE ); + } + + $cursor->advance(); + + $el = new ezcTemplateCaptureTstNode( $this->parser->source, $this->startCursor, $cursor ); + $el->isClosingBlock = true; + $this->appendElement( $el ); + return true; + } + + + + $capture = new ezcTemplateCaptureTstNode( $this->parser->source, $this->startCursor, $cursor ); + $this->findNextElement(); + + if ( !$this->parseOptionalType( 'Variable', null, false ) ) + { + throw new ezcTemplateSourceToTstParserException( $this, $this->currentCursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_VARIABLE ); + } + + $capture->variable = $this->lastParser->element; + + $type = $this->parser->symbolTable->retrieve( $capture->variable->name ); + if ( $type === false ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->endCursor, $this->endCursor, $this->parser->symbolTable->getErrorMessage() ); + } + + + + + $this->findNextElement(); + if ( !$cursor->match( "}" ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $cursor, $cursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_CURLY_BRACKET_CLOSE ); + } + + $this->appendElement( $capture); + return true; + } + + return false; + } + + /* + if ( !$this->parseOptionalType( "Variable", null, false) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_VARIABLE ); + } + + $cycle->variables[] = $this->lastParser->elements[0]; + + $this->findNextElement(); + + } + while ( $this->currentCursor->match( "," ) ); + + $this->appendElement( $cycle ); + // $this->elements[0] = $cycle; // Replace the variable, with the cycle. + + + if ( !$this->parentParser->atEnd( $cursor, null, false ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_CURLY_BRACKET_CLOSE ); + } + $cursor->advance(); + + return true; + } + + + + return false; + */ +} + +?> Propchange: trunk/Template/src/parsers/source_to_tst/implementations/capture.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: trunk/Template/src/parsers/source_to_tst/implementations/control_structure.php ============================================================================== --- trunk/Template/src/parsers/source_to_tst/implementations/control_structure.php [iso-8859-1] (original) +++ trunk/Template/src/parsers/source_to_tst/implementations/control_structure.php [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -69,7 +69,7 @@ // Check if any control structure names are used. // Note: The code inside the (?:) brace ensures that the next character // is not an alphabetical character ie. a word boundary - $matches = $cursor->pregMatchComplete( "#^(foreach|while|if|elseif|else|switch|case|default|include|return|break|continue|skip|delimiter|increment|decrement|reset|charset)(?:[^a-zA-Z0-9_])#" ); + $matches = $cursor->pregMatchComplete( "#^(foreach|while|if|elseif|else|switch|case|default|include|return|break|continue|skip|delimiter|increment|decrement|reset|charset|capture)(?:[^a-zA-Z0-9_])#" ); if ( $matches === false ) { @@ -100,6 +100,7 @@ $csMap['decrement'] = 'Cycle'; $csMap['reset'] = 'Cycle'; $csMap['charset'] = 'Charset'; + $csMap['capture'] = 'Capture'; // tmp if ( !isset( $csMap[$name] ) ) Modified: trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php ============================================================================== --- trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php [iso-8859-1] (original) +++ trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -821,6 +821,40 @@ return new ezcTemplateNopAstNode(); } + public function visitCaptureTstNode( ezcTemplateCaptureTstNode $node ) + { + // TODO GET the variable AST node. + $var = $node->variable->accept($this); + $this->outputVariable->push( $var->name, $var ); + + $result = array(); // Will contain an array with AST nodes. + + // Set the output to "". + $result[] = $this->outputVariable->getInitializationAst(); + + // execute all the 'children' in the custom block. + foreach ( $node->elements as $element ) + { + $r = $element->accept( $this ); + if ( is_array( $r ) ) + { + foreach ($r as $a ) + { + $result[] = $a; + } + } + else + { + $result[] = $r; + } + } + + + $this->outputVariable->pop(); + + return $result; + } + /** * visitLiteralBlockTstNode Added: trunk/Template/src/syntax_trees/tst/nodes/capture.php ============================================================================== --- trunk/Template/src/syntax_trees/tst/nodes/capture.php (added) +++ trunk/Template/src/syntax_trees/tst/nodes/capture.php [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,38 @@ +<?php +/** + * File containing the ezcTemplateCaptureTstNode class + * + * @package Template + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @access private + */ +/** + * @package Template + * @version //autogen// + * @access private + */ +class ezcTemplateCaptureTstNode extends ezcTemplateBlockTstNode +{ + public $variable; + + /** + * + * @param ezcTemplateSource $source + * @param ezcTemplateCursor $start + * @param ezcTemplateCursor $end + */ + public function __construct( ezcTemplateSourceCode $source, /*ezcTemplateCursor*/ $start, /*ezcTemplateCursor*/ $end) + { + parent::__construct( $source, $start, $end ); + $this->isNestingBlock = true; + } + + public function getTreeProperties() + { + return array( 'variable' => $this->variable); + } + +} +?> Propchange: trunk/Template/src/syntax_trees/tst/nodes/capture.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: trunk/Template/src/template_autoload.php ============================================================================== --- trunk/Template/src/template_autoload.php [iso-8859-1] (original) +++ trunk/Template/src/template_autoload.php [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -97,6 +97,8 @@ 'ezcTemplateCatchAstNode' => 'Template/syntax_trees/ast/nodes/control/catch.php', 'ezcTemplateCharsetSourceToTstParser' => 'Template/parsers/source_to_tst/implementations/charset.php', 'ezcTemplateCharsetTstNode' => 'Template/syntax_trees/tst/nodes/charset.php', + 'ezcTemplateCaptureSourceToTstParser' => 'Template/parsers/source_to_tst/implementations/capture.php', + 'ezcTemplateCaptureTstNode' => 'Template/syntax_trees/tst/nodes/capture.php', 'ezcTemplateCloneAstNode' => 'Template/syntax_trees/ast/nodes/constructs/clone.php', 'ezcTemplateCompiledCode' => 'Template/compiled_code.php', 'ezcTemplateConcatAssignmentOperatorAstNode' => 'Template/syntax_trees/ast/nodes/operators/concat_assignment_operator.php', Added: trunk/Template/tests/regression_tests/capture/correct/capture.in ============================================================================== --- trunk/Template/tests/regression_tests/capture/correct/capture.in (added) +++ trunk/Template/tests/regression_tests/capture/correct/capture.in [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,7 @@ +{var $myVar} +{capture $myVar} +Hello world +{/capture} +----- +{$myVar} +----- Propchange: trunk/Template/tests/regression_tests/capture/correct/capture.in ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Template/tests/regression_tests/capture/correct/capture.out ============================================================================== --- trunk/Template/tests/regression_tests/capture/correct/capture.out (added) +++ trunk/Template/tests/regression_tests/capture/correct/capture.out [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,4 @@ +----- +Hello world + +----- Propchange: trunk/Template/tests/regression_tests/capture/correct/capture.out ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.in ============================================================================== --- trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.in (added) +++ trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.in [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,3 @@ +{capture} +Hello +{/capture} Propchange: trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.in ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.out ============================================================================== --- trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.out (added) +++ trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.out [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,10 @@ +mock:1:8: Expecting a variable + +{capture} + ^ + +failed in: +ezcTemplateProgramSourceToTstParser + -> ezcTemplateBlockSourceToTstParser + -> ezcTemplateControlStructureSourceToTstParser + -> ezcTemplateCaptureSourceToTstParser Propchange: trunk/Template/tests/regression_tests/capture/incorrect/forgot_var.out ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.in ============================================================================== --- trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.in (added) +++ trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.in [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,3 @@ +{capture $a} +Hello +{/capture} Propchange: trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.in ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.out ============================================================================== --- trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.out (added) +++ trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.out [iso-8859-1] Thu Aug 16 11:08:50 2007 @@ -1,0 +1,4 @@ +mock:1:9: The symbol <$a> is not declared + +{capture $a} + ^ Propchange: trunk/Template/tests/regression_tests/capture/incorrect/no_variable_declared.out ------------------------------------------------------------------------------ svn:eol-style = native -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components