Joey Mazzarelli avatar Joey Mazzarelli committed ad7a324

all tests written now pass

Comments (0)

Files changed (10)

 ${PARSER} : src/phplemon/Trekel/Parser.y
 	${LEMON} $< && mv src/phplemon/Trekel/Parser.php ${PARSER}
 
-test : ${PARSER} ${LEXER}
+test : ${PARSER} ${LEXER} lint
 	phpunit src/test/php
 
+lint :
+	find src/php -name *.php -exec php -l {} >/dev/null \;
+	find src/test/php -name *.php -exec php -l {} >/dev/null \;
+
 release : ${PARSER} ${LEXER} ${TARBALL}
 
 ${TARBALL} : ${COMPILED}
 clean :
 	rm -f ${PARSER} ${LEXER} ${COMPILED} ${TARBALL}
 
-.PHONY : clean lexer parser test release
+.PHONY : clean lexer parser test release lint
 

src/php/Trekel/Evaluator.php

   }
 
   public function setRootNode ($root) {
-    $this->root = $root;
+    $this->root = new Trekel_Root($root);
   }
 
   public function evaluate () {
       }
       $matches = $staging;
     } while (null !== ($node = $node->getChild()));
+    $matches->remove($this->root);
     return $matches->getItems();
   }
 

src/php/Trekel/Node.php

 
   protected function matchType ($node) {
     $type = $this->getIdentifier();
-    if ($node instanceof $type) {
+    if ('*' == $type || $node instanceof $type) {
       $this->matches->append($node);
     }
   }
     return $this->matches;
   }
 
-  protected function getChildren ($node) {
-    if (!$node) {
-      return array();
-    }
-    return array($node->getChild());
-  }
+  //protected function getChildren ($node) {
+    //if (!$node) {
+      //return array();
+    //}
+    //return array($node->getChild());
+  //}
 
   protected function getParent ($node) {
     return $node->getParent();
 
   protected function initQueue ($node) {
     $this->queue = new Trekel_List();
-    $this->queue->append($node);
+    $this->enqueue($node);
   }
 
   protected function enqueue ($node) {

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

 class Trekel_Node_Selector_Child extends Trekel_Node_Selector {
 
   public function reduce ($node, $idx) {
+    //echo "reducing: " . get_class($node) . "\n";
+    //echo "ident: " . $this->getIdentifier() . "\n";
     $type = $this->getIdentifier();
-    print_r($type);
     $this->initMatches();
-    $this->matchType($node);
+    foreach ($node->getChildren() as $child) {
+      $this->matchType($child);
+    }
     return $this->getMatches();
   }
 

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

 
   public function reduce ($node, $idx) {
     $this->initMatches();
-    $this->initQueue($node);
+    $this->initQueue($node->getChildren());
     while ($node = $this->dequeue()) {
       $this->matchType($node);
-      $this->enqueue($this->getChildren($node));
+      $this->enqueue($node->getChildren());
     }
     return $this->getMatches();
   }

src/php/Trekel/Root.php

+<?php
+class Trekel_Root {
+  public function __construct ($root) {
+    $this->root = $root;
+  }
+  public function getChildren () {
+    return array($this->root);
+  }
+}

src/phplemon/Trekel/Parser.y

 }
 
 // [<id>="value"]
-attribute(OUT) ::= LBRACKET IDENTIFIER(ID) EQ string(VAL) RBRACKET. {
+attribute(OUT) ::= LBRACKET property(ID) EQ string(VAL) RBRACKET. {
   $attr = new Trekel_Node_Attribute_Equals();
   $attr->setIdentifier(ID);
   $attr->setValue(VAL);
 }
 
 // [<id>!="value"]
-attribute(OUT) ::= LBRACKET IDENTIFIER(ID) NEQ string(VAL) RBRACKET. {
+attribute(OUT) ::= LBRACKET property(ID) NEQ string(VAL) RBRACKET. {
   $attr = new Trekel_Node_Attribute_NotEquals();
   $attr->setIdentifier(ID);
   $attr->setValue(VAL);
 }
 
 // [<id>^="value"]
-attribute(OUT) ::= LBRACKET IDENTIFIER(ID) STARTSWITH string(VAL) RBRACKET. {
+attribute(OUT) ::= LBRACKET property(ID) STARTSWITH string(VAL) RBRACKET. {
   $attr = new Trekel_Node_Attribute_StartsWith();
   $attr->setIdentifier(ID);
   $attr->setValue(VAL);
 }
 
 // [<id>$="value"]
-attribute(OUT) ::= LBRACKET IDENTIFIER(ID) ENDSWITH string(VAL) RBRACKET. {
+attribute(OUT) ::= LBRACKET property(ID) ENDSWITH string(VAL) RBRACKET. {
   $attr = new Trekel_Node_Attribute_EndsWith();
   $attr->setIdentifier(ID);
   $attr->setValue(VAL);
 }
 
 // [<id>*="value"]
-attribute(OUT) ::= LBRACKET IDENTIFIER(ID) CONTAINS string(VAL) RBRACKET. {
+attribute(OUT) ::= LBRACKET property(ID) CONTAINS string(VAL) RBRACKET. {
   $attr = new Trekel_Node_Attribute_Contains();
   $attr->setIdentifier(ID);
   $attr->setValue(VAL);
   OUT = $attr;
 }
 
+// When the attribute is an identifier, use as an instance variable
+property(OUT) ::= IDENTIFIER(ID). {
+  OUT = ID;
+}
+
+// When the attribute is a method, use as a getter
+property(OUT) ::= GETTER(ID). {
+  OUT = ID;
+}
+
 // Strip of the quotes and just use the value
 string(OUT) ::= STRING(VAL). {
   OUT = substr(VAL, 1, -1);

src/plex/Trekel/Lexer.plex

 
 // Regular expression tokens
 
+GETTER = /[a-zA-Z][a-zA-Z0-9_]*\(\)/
 IDENTIFIER = /[a-zA-Z][a-zA-Z0-9_]*/
 DQUOTESTRING = /"([^"]|(\\"))*"/
 PSEUDO = /:[a-zA-Z][a-zA-Z0-9_-]*/
 
 // Regular expression tokens
 
+GETTER {
+  $this->updateColumn();
+  $this->token = Trekel_Parser::T_GETTER;
+}
+
 IDENTIFIER {
   $this->updateColumn();
   $this->token = Trekel_Parser::T_IDENTIFIER;

src/test/php/Trekel/ChildTest.php

 
 class Trekel_ChildTest extends Trekel_TestCase {
 
-  public function xtestSimpleSingle () {
+  public function testSimpleSingle () {
     $parser = new Trekel_Parser();
     $parser->setSelector('> Trekel_ChildTest_A');
     $parseTree = $parser->parse();
     $this->assertEquals(1, count($nodes));
   }
 
-  public function xtestSimpleEmpty () {
+  public function testSimpleEmpty () {
     $parser = new Trekel_Parser();
     $parser->setSelector('> Trekel_ChildTest_A');
     $parseTree = $parser->parse();
     $this->assertEquals(0, count($nodes));
   }
 
-  public function xtestSimpleCycle () {
+  public function testSimpleCycle () {
     $parser = new Trekel_Parser();
     $parser->setSelector('> Trekel_ChildTest_A');
     $parseTree = $parser->parse();
     $nodeA->addChild($nodeB);
     $nodeB->addChild($nodeC);
     $nodeC->addChild($nodeD);
+    $nodeC->addChild($nodeA);
 
     /**
     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_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');
+    $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');
     $nodes = $evaluator->evaluate();
     $this->assertEquals(1, count($nodes));
 
+    $parser = new Trekel_Parser();
+    $parser->setSelector('> Trekel_ChildTest_A  Trekel_ChildTest_C');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(2, count($nodes)); // since D is a C
+
+    $parser = new Trekel_Parser();
+    $parser->setSelector('> Trekel_ChildTest_A  Trekel_ChildTest_D');
+    $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_C > * ');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(2, count($nodes));
+
   }
 
 
 } // end Trekel_ChildTest
 
 class Trekel_ChildTest_Base {
-  private $child;
+  private $children = array();
   private $parent;
-  public function getChild () {
-    return $this->child;
+  public function getChildren () {
+    return $this->children;
   }
   public function getParent () {
     return $this->parent;
   }
   public function addChild ($child) {
     $child->setParent($this);
-    $this->child = $child;
+    $this->children[] = $child;
   }
   public function setParent ($parent) {
     $this->parent = $parent;

src/test/php/Trekel/DescendantTest.php

     $this->assertEquals(1, count($nodes));
   }
 
+  public function testStar () {
+    $nodeA = new Trekel_DescendantTest_A();
+    $nodeB = new Trekel_DescendantTest_B();
+    $nodeC = new Trekel_DescendantTest_C();
+    $nodeD = new Trekel_DescendantTest_D();
+    $nodeA->addChild($nodeB);
+    $nodeB->addChild($nodeC);
+    $nodeC->addChild($nodeD);
+
+    $parser = new Trekel_Parser();
+    $parser->setSelector('*');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(4, count($nodes));
+
+    $parser = new Trekel_Parser();
+    $parser->setSelector('* *');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(3, count($nodes));
+
+    $parser = new Trekel_Parser();
+    $parser->setSelector('* * *');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(2, count($nodes));
+
+    $parser = new Trekel_Parser();
+    $parser->setSelector('* * * *');
+    $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('* * * * *');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(0, count($nodes));
+
+    $parser = new Trekel_Parser();
+    $parser->setSelector('* * * * *');
+    $parseTree = $parser->parse();
+    $evaluator = new Trekel_Evaluator();
+    $evaluator->setRootNode($nodeA);
+    $evaluator->setParseTree($parseTree);
+    $nodes = $evaluator->evaluate();
+    $this->assertEquals(0, count($nodes));
+  }
+
 
 } // end Trekel_DescendantTest
 
 class Trekel_DescendantTest_Base {
-  private $child;
+  private $children = array();
   private $parent;
-  public function getChild () {
-    return $this->child;
+  public function getChildren () {
+    return $this->children;
   }
   public function getParent () {
     return $this->parent;
   }
   public function addChild ($child) {
     $child->setParent($this);
-    $this->child = $child;
+    $this->children[] = $child;
   }
   public function setParent ($parent) {
     $this->parent = $parent;
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.