File: test/test-recursion.php

Recommend this page to a friend!
  Classes of Nikos M.  >  Grammar Template  >  test/test-recursion.php  >  Download  
File: test/test-recursion.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Grammar Template
Template engine in PHP and other languages
Author: By
Last change:
Date: 1 year ago
Size: 3,133 bytes
 

Contents

Class file image Download
<?php
include "../src/php/GrammarTemplate.php";
function
echo_($s='')
{
    echo
$s . PHP_EOL;
}

echo_('GrammarTemplate.VERSION = ' . GrammarTemplate::VERSION);
echo_();

/*
    i.e:
    foreach "expression:terms" as "term":
        foreach "term:factors" as "factor":
            ..
   
    here an :EXPR template is defined which itself uses (anonymous) sub-templates
    it is equivalent to (expand sub-templates to distinct):

<:FACTOR>:=[<lhs>[ <?op> <rhs|NULL>]]

<:TERM>:=[(<factor:FACTOR>[ AND <*factor:FACTOR>])]

<:EXPR>:=[<term:TERM>[ OR <*term:TERM>]]

<expression:EXPR>
<expression2:EXPR>

*/
$tpl = "<:EXPR>:=[<term>:=[(<factor>:=[<globalNegation:NEG><lhs>[ <?op:OP> <rhs|NULL>]][ AND <*factor>])][ OR <*term>]]<expression:EXPR>\n<expression2:EXPR>";

function
op_func( $val )
{
    return
'!=' === $val ? '<>' : $val;
}
function
neg_func( $val )
{
    return
$val ? 'NOT ' : '';
}
$expr = new GrammarTemplate($tpl);
GrammarTemplate::$fnGlobal['NEG'] = 'neg_func';
$expr->fn['OP'] = 'op_func';

echo_("input template:");
echo_($tpl);

echo_( );

echo_("output:");
echo_($expr->render((object)array(
   
'globalNegation' => true,
   
'expression' => array(
       
// term
       
array(
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '=', 'rhs'=> 1),
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '=', 'rhs'=> 2),
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '=', 'rhs'=> 3)
        ),
       
// term
       
array(
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '<', 'rhs'=> 1),
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '<', 'rhs'=> 2),
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '<', 'rhs'=> 3)
        ),
       
// term
       
array(
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '>', 'rhs'=> 1),
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '>', 'rhs'=> 2),
           
// factor
           
(object)array('lhs'=> 1, 'op'=> '>', 'rhs'=> 3)
        )
    ),
   
'expression2' => array(
       
// term
       
array(
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '=', 'rhs'=> 1),
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '=', 'rhs'=> 2),
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '=', 'rhs'=> 3)
        ),
       
// term
       
array(
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '<', 'rhs'=> 1),
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '<', 'rhs'=> 2),
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '<', 'rhs'=> 3)
        ),
       
// term
       
array(
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '>', 'rhs'=> 1),
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '>', 'rhs'=> 2),
           
// factor
           
(object)array('lhs'=> 2, 'op'=> '>', 'rhs'=> 3)
        ),
       
// term
       
array(
           
// factor
           
(object)array('lhs'=> 3),
           
// factor
           
(object)array('lhs'=> 3, 'op'=> '!=')
        )
    )
)));

For more information send a message to info at phpclasses dot org.