diff --git a/engine/editor.inc b/engine/editor.inc index 3cc3504..c1afe77 100644 --- a/engine/editor.inc +++ b/engine/editor.inc @@ -317,6 +317,7 @@ class PGPEditor extends PGPParser { switch ($type) { case T_INTERFACE: case T_CLASS: + case T_TRAIT: continue; case T_FUNCTION: @@ -364,6 +365,7 @@ class PGPEditor extends PGPParser { case T_INTERFACE: case T_CLASS: + case T_TRAIT: break; } } @@ -414,8 +416,8 @@ class PGPEditor extends PGPParser { * A string of the function signature. */ public function functionGetSignature($statement) { - if (!in_array($statement->type, array(T_INTERFACE, T_CLASS, T_FUNCTION))) { - return 'Expected statement type of: interface, class, or function'; + if (!in_array($statement->type, array(T_INTERFACE, T_CLASS, T_TRAIT, T_FUNCTION))) { + return 'Expected statement type of: interface, class, trait, or function'; } return $statement->signature(); @@ -558,6 +560,7 @@ class PGPEditor extends PGPParser { } switch ($statement->type) { case T_INTERFACE: + case T_TRAIT: case T_CLASS: case T_FUNCTION: return $statement->printName(); diff --git a/engine/list.inc b/engine/list.inc index 2d8659e..f081589 100644 --- a/engine/list.inc +++ b/engine/list.inc @@ -1382,7 +1382,7 @@ class PGPList { // Get the node containing this list. $parent = $this->parent; $statement = $parent->data; - if (is_object($statement) && isset($statement->type) && in_array($statement->type, array(T_INTERFACE, T_CLASS, T_FUNCTION))) { + if (is_object($statement) && isset($statement->type) && in_array($statement->type, array(T_INTERFACE, T_CLASS, T_TRAIT, T_FUNCTION))) { // Stop outward recursion if one of these objects is found. return $items; } @@ -2167,6 +2167,7 @@ class PGPBody extends PGPList { case T_INTERFACE: case T_CLASS: + case T_TRAIT: case T_FUNCTION: $string = $statement->toString($indent, $prepend); break; diff --git a/engine/parser.inc b/engine/parser.inc index 07e3f69..df24b3a 100644 --- a/engine/parser.inc +++ b/engine/parser.inc @@ -21,7 +21,7 @@ const GRAMMAR_PARSER_VERSION = '2.1'; * This parent class holds references to arrays of: * - tokens, * - statements, - * - interfaces, classes, and functions, + * - interfaces, classes, traits, and functions, * - function calls, * - constants, globals, and comments. * @@ -48,9 +48,6 @@ class PGPParser { /** * Array of interfaces included in grammar statements. * - * This should point to a PGPInterfaces class that also holds lists of - * functions. - * * @var array */ protected $interfaces = array(); @@ -58,14 +55,18 @@ class PGPParser { /** * Array of classes included in grammar statements. * - * This should point to a PGPClasses class that also holds lists of functions - * and calls. - * * @var array */ protected $classes = array(); /** + * Array of traits included in grammar statements. + * + * @var array + */ + protected $traits = array(); + + /** * Array of functions included in grammar statements. * * @var array @@ -135,6 +136,8 @@ class PGPParser { $this->defineConstant('T_DEFINE', 606); $this->defineConstant('T_LABEL', 607); $this->defineConstant('T_BODY', 608); + // This should be defined in PHP 5.4.0 and later. + $this->defineConstant('T_TRAIT', 357); // Mask T_ML_COMMENT (PHP4) as T_COMMENT (PHP5). if (!defined('T_ML_COMMENT')) { @@ -211,6 +214,16 @@ class PGPParser { } /** + * Returns array of traits included in grammar statements. + * + * @return array + * An array of statements. + */ + public function &getTraits() { + return $this->traits; + } + + /** * Returns array of functions included in grammar statements. * * @return array diff --git a/engine/reader.inc b/engine/reader.inc index 6d87e32..0142e4c 100644 --- a/engine/reader.inc +++ b/engine/reader.inc @@ -350,7 +350,7 @@ class PGPReader extends PGPParser { * * @param integer $type * Token type of statements to build grammar for. Expected to be one of: - * T_CLASS, T_FUNCTION, or T_INTERFACE. + * T_CLASS, T_FUNCTION, T_TRAIT, or T_INTERFACE. * @param array $values * A list of values to match as the name of the statement. * @param string $snippet @@ -782,7 +782,7 @@ class PGPReader extends PGPParser { ); $cache->insertLast($data, 'operand'); // Include T_VARIABLE for a static variable declaration. - $return_tokens2 = array(T_PUBLIC, T_PRIVATE, T_PROTECTED, T_STATIC, T_ABSTRACT, T_FINAL, T_INTERFACE, T_CLASS, T_FUNCTION, T_VARIABLE); + $return_tokens2 = array(T_PUBLIC, T_PRIVATE, T_PROTECTED, T_STATIC, T_ABSTRACT, T_FINAL, T_INTERFACE, T_CLASS, T_TRAIT, T_FUNCTION, T_VARIABLE); $cache = $this->buildExpression($return_tokens2, $cache); $modifiers->insertLast($cache, 'modifier'); $cache = new PGPExpression(); @@ -798,9 +798,20 @@ class PGPReader extends PGPParser { case T_INTERFACE: $this->isInterface = TRUE; case T_CLASS: + case T_TRAIT: $node->data = $this->buildClass($modifiers); + // Store the node so we can insert statements around the class. - $this->isInterface ? $this->interfaces[] = &$node : $this->classes[] = &$node; + if ($this->tokenType() == T_INTERFACE) { + $this->interfaces[] = &$node; + } + elseif ($this->tokenType() == T_CLASS) { + $this->classes[] = &$node; + } + else { + $this->traits[] = &$node; + } + $modifiers = new PGPList('modifiers', $this->getSettings()); $this->isInterface = FALSE; break; @@ -2383,7 +2394,7 @@ class PGPReader extends PGPParser { } /** - * Builds a class or interface statement block. + * Builds a class, trait, or interface statement block. * * @param PGPList $modifiers * A list of modifiers (e.g. public, private) for the statement block. @@ -3647,6 +3658,7 @@ class PGPReader extends PGPParser { // Obvious ones. T_FUNCTION, T_INTERFACE, + T_TRAIT, T_CLASS, ); // @todo This will not identify a document comment if the next statement diff --git a/engine/writer.inc b/engine/writer.inc index 1254400..7f21b96 100644 --- a/engine/writer.inc +++ b/engine/writer.inc @@ -301,6 +301,9 @@ class PGPWriter extends PGPParser { case T_INTERFACE: return 'interface'; + case T_TRAIT: + return 'trait'; + case T_CONTINUE: return 'continue';