Joey Mazzarelli avatar Joey Mazzarelli committed 0108ebd

reacquainting myself

Comments (0)

Files changed (19)

src/php/Trekel/Evaluator.php

     $node = $this->tree;
     do {
       $staging = new Trekel_List();
-      foreach ($matches as $match) {
-        $staging->appendAll($node->reduce($match));
+      foreach ($matches as $idx => $match) {
+        $staging->appendAll($node->reduce($match, $idx));
       }
       $matches = $staging;
     } while (null !== ($node = $node->getChild()));

src/php/Trekel/Node.php

 abstract class Trekel_Node {
 
 
+  protected $child = null;
+  protected $identifier = null;
   protected $selector = null;
-  protected $child = null;
 
-  public function __construct ($selector) {
+  public function setSelector ($selector) {
     $this->selector = $selector;
   }
 
     return $this->selector;
   }
 
+  public function setIdentifier ($id) {
+    $this->setSelector($id);
+    $this->identifier = $id;
+  }
+
+  public function getIdentifier () {
+    return $this->identifier;
+  }
+
   public function setChild ($node) {
     $this->child = $node;
   }
     return $this->getChild();
   }
 
-  abstract public function reduce ($node);
+  abstract public function reduce ($node, $idx);
 
   private $matches = null;
 
   }
 
   protected function matchType ($node) {
-    $type = $this->getSelector();
+    $type = $this->getIdentifier();
     if ($node instanceof $type) {
       $this->matches->append($node);
     }

src/php/Trekel/Node/Attribute.php

 abstract class Trekel_Node_Attribute extends Trekel_Node {
 
   protected $attributeValue = null;
+  protected $attributeId = null;
 
-  public function __construct ($attr, $value = null) {
-    parent::__construct($attr);
+  public function getValue () {
+    return $this->attributeValue;
+  }
+
+  public function setValue ($value) {
     $this->attributeValue = $value;
   }
 
-  public function getAttributeValue () {
-    return $this->attributeValue;
+  public function setIdentifier ($id) {
+    $this->attributeId = $id;
+  }
+
+  public function getIdentifier () {
+    return $this->attributeId;
   }
 
 }

src/php/Trekel/Node/Attribute/Contains.php

 <?php
 
 class Trekel_Node_Attribute_Contains extends Trekel_Node_Attribute {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }// end Trekel_Node_Attribute_Contains

src/php/Trekel/Node/Attribute/EndsWith.php

 <?php
 
 class Trekel_Node_Attribute_EndsWith extends Trekel_Node_Attribute {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }// end Trekel_Node_Attribute_EndsWith

src/php/Trekel/Node/Attribute/Equals.php

 <?php
 
 class Trekel_Node_Attribute_Equals extends Trekel_Node_Attribute {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }// end Trekel_Node_Attribute_Equals

src/php/Trekel/Node/Attribute/Exists.php

 <?php
 
 class Trekel_Node_Attribute_Exists extends Trekel_Node_Attribute {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }// end Trekel_Node_Attribute_Exists

src/php/Trekel/Node/Attribute/NotEquals.php

 <?php
 
 class Trekel_Node_Attribute_NotEquals extends Trekel_Node_Attribute {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }// end Trekel_Node_Attribute_NotEquals

src/php/Trekel/Node/Attribute/StartsWith.php

 <?php
 
 class Trekel_Node_Attribute_StartsWith extends Trekel_Node_Attribute {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }// end Trekel_Node_Attribute_StartsWith

src/php/Trekel/Node/Pseudo/First.php

 <?php
 
 class Trekel_Node_Pseudo_First extends Trekel_Node_Pseudo {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }

src/php/Trekel/Node/Pseudo/Last.php

+<?php
+
+class Trekel_Node_Pseudo_Last extends Trekel_Node_Pseudo {
+  public function reduce ($node, $idx) { return array(); }
+}

src/php/Trekel/Node/Selector.php

     if (get_class($this) == $type) {
       return $this;
     } else {
-      $obj = new $type($this->getSelector());
+      $obj = new $type();
+      $obj->setIdentifier($this->getIdentifier());
       $obj->setChild($this->getChild());
       return $obj;
     }

src/php/Trekel/Node/Selector/Adjacent.php

 <?php
 
 class Trekel_Node_Selector_Adjacent extends Trekel_Node_Selector {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }

src/php/Trekel/Node/Selector/Child.php

 
 class Trekel_Node_Selector_Child extends Trekel_Node_Selector {
 
-  public function reduce ($node) {
+  public function reduce ($node, $idx) {
+    $type = $this->getIdentifier();
+    print_r($type);
     $this->initMatches();
     $this->matchType($node);
     return $this->getMatches();

src/php/Trekel/Node/Selector/Descendant.php

 
 class Trekel_Node_Selector_Descendant extends Trekel_Node_Selector {
 
-  public function reduce ($node) {
+  public function reduce ($node, $idx) {
     $this->initMatches();
     $this->initQueue($node);
     while ($node = $this->dequeue()) {

src/php/Trekel/Node/Selector/Sibling.php

 <?php
 
 class Trekel_Node_Selector_Sibling extends Trekel_Node_Selector {
-  public function reduce ($node) { return array(); }
+  public function reduce ($node, $idx) { return array(); }
 }

src/phplemon/Trekel/Parser.y

   #echo "found type selector:";
   #print_r(A);
   #echo "\n";
-  OUT = new Trekel_Node_Selector_Descendant(A);
+  $desc = new Trekel_Node_Selector_Descendant();
+  $desc->setIdentifier(A);
+  OUT = $desc;
 }
 
 // Same, but with attributes: div[id='header']
 attribute_selector(OUT) ::= IDENTIFIER(A) attribute(B). {
-  $ident= new Trekel_Node_Selector_Descendant(A);
-  $ident->setChild(B);
-  OUT = $ident;
+  $desc = new Trekel_Node_Selector_Descendant();
+  $desc->setIdentifier(A);
+  $desc->setChild(B);
+  OUT = $desc;
 }
 
 // With pseudo classes, div:first
 pseudo_selector(OUT) ::= IDENTIFIER(A) pseudo(B). {
-  $ident = new Trekel_Node_Selector_Descendant(A);
-  $ident->setChild(B);
-  OUT = $ident;
+  $desc = new Trekel_Node_Selector_Descendant();
+  $desc->setIdentifier(A);
+  $desc->setChild(B);
+  OUT = $desc;
 }
 
 pseudo_selector(OUT) ::= IDENTIFIER(A) pseudo(B) attribute(C). {
   $desc->setIdentifier(A);
   $desc->setChild(B);
   B->setChild(C);
-  OUT = $ident;
+  OUT = $desc;
 }
 
 pseudo(OUT) ::= PSEUDO(IN). {

src/test/php/Trekel/ChildTest.php

+<?php
+/**
+ * Copyright 2010 Joey Mazzarelli. All rights reserved.
+ *
+ * Redistribution and use in source, with or without modification, is
+ * permitted provided that the following condition is met:
+ *
+ *    1. Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JOEY MAZZARELLI ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JOEY MAZZARELLI OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of Joey Mazzarelli.
+ */
+
+require_once dirname(__FILE__) . '/TestCase.php';
+
+class Trekel_ChildTest extends Trekel_TestCase {
+
+  public function xtestSimpleSingle () {
+    $parser = new Trekel_Parser();
+    $parser->setSelector('> Trekel_ChildTest_A');
+    $parseTree = $parser->parse();
+    $rootNode = new Trekel_ChildTest_A();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setParseTree($parseTree);
+    $evaluator->setRootNode($rootNode);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(1, count($nodes));
+  }
+
+  public function xtestSimpleEmpty () {
+    $parser = new Trekel_Parser();
+    $parser->setSelector('> Trekel_ChildTest_A');
+    $parseTree = $parser->parse();
+    $rootNode = new Trekel_ChildTest_B();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setParseTree($parseTree);
+    $evaluator->setRootNode($rootNode);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(0, count($nodes));
+  }
+
+  public function xtestSimpleCycle () {
+    $parser = new Trekel_Parser();
+    $parser->setSelector('> Trekel_ChildTest_A');
+    $parseTree = $parser->parse();
+    $rootNode = new Trekel_ChildTest_A();
+    // throw in a cycle, test for infinite loops
+    $rootNode->addChild($rootNode);
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setParseTree($parseTree);
+    $evaluator->setRootNode($rootNode);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(1, count($nodes));
+  }
+
+  public function testAnother () {
+    $nodeA = new Trekel_ChildTest_A();
+    $nodeB = new Trekel_ChildTest_B();
+    $nodeC = new Trekel_ChildTest_C();
+    $nodeD = new Trekel_ChildTest_D();
+    $nodeA->addChild($nodeB);
+    $nodeB->addChild($nodeC);
+    $nodeC->addChild($nodeD);
+
+    /**
+    A -> B -> C -> D -> 0
+    BASE -> BASE -> BASE -> D -> 0
+    */
+
+    //$parser = new Trekel_Parser();
+    //$parser->setSelector('> Trekel_ChildTest_Base');
+    //$parseTree = $parser->parse();
+    //$evaluator = new Trekel_Evaluator();
+    //$evaluator->setRootNode($nodeA);
+    //$evaluator->setParseTree($parseTree);
+    //$nodes = $evaluator->evaluate();
+    //$this->assertEquals(1, count($nodes));
+
+    //$parser = new Trekel_Parser();
+    //$parser->setSelector('> Trekel_ChildTest_A');
+    //$parseTree = $parser->parse();
+    //$evaluator = new Trekel_Evaluator();
+    //$evaluator->setRootNode($nodeA);
+    //$evaluator->setParseTree($parseTree);
+    //$nodes = $evaluator->evaluate();
+    //$this->assertEquals(1, count($nodes));
+
+    $parser = new Trekel_Parser();
+    $parser->setSelector('> Trekel_ChildTest_A > Trekel_ChildTest_B');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(1, count($nodes));
+
+  }
+
+
+} // end Trekel_ChildTest
+
+class Trekel_ChildTest_Base {
+  private $child;
+  private $parent;
+  public function getChild () {
+    return $this->child;
+  }
+  public function getParent () {
+    return $this->parent;
+  }
+  public function addChild ($child) {
+    $child->setParent($this);
+    $this->child = $child;
+  }
+  public function setParent ($parent) {
+    $this->parent = $parent;
+  }
+}
+class Trekel_ChildTest_A extends Trekel_ChildTest_Base { }
+class Trekel_ChildTest_B extends Trekel_ChildTest_Base { }
+class Trekel_ChildTest_C extends Trekel_ChildTest_Base { }
+class Trekel_ChildTest_D extends Trekel_ChildTest_C { }
+class Trekel_ChildTest_E extends Trekel_ChildTest_D { }
+

src/test/php/Trekel/ParserTest.php

         'Trekel_Node_Selector_Descendant',
         'Trekel_Node_Pseudo_First',
       ));
-    $this->assertParseTree("> A + object[this='THAT']:first", array(
+    $this->assertParseTree("> A:last + object[this='THAT']:first", array(
         'Trekel_Node_Selector_Child',
+        'Trekel_Node_Pseudo_Last',
         'Trekel_Node_Selector_Adjacent',
         'Trekel_Node_Attribute_Equals',
         'Trekel_Node_Pseudo_First',
       } while (empty($sel));
 
       if ($node instanceof Trekel_Node_Attribute) {
+        //print_r($node);
         preg_match('/(?<attr>[^=*$^!]+)([=*$^!]+(?<val>.+))?/', $sel, $matches);
         $sel = $matches['attr'];
         $value = @$matches['val'];
         if (!empty($value)) {
           $value = substr($value, 1, -1);
         }
-        $this->assertEquals($node->getAttributeValue(), $value);
-        $this->assertEquals($node->getSelector(), $sel);
+        $this->assertEquals($node->getValue(), $value);
+        $this->assertEquals($node->getIdentifier(), $sel);
       } else if ($node instanceof Trekel_Node_Pseudo) {
         $type = "Trekel_Node_Pseudo_" . ucfirst($sel);
         $this->assertEquals(get_class($node), $type);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.