cvs: pear /PHP_Parser package.xml /PHP_Parser/Parser Core.php Core.y Tokenizer.php /PHP_Parser/Parser/DocBlock Parser.php /PHP_Parser/tests
global_var.phpt

From: Date: Sun, 04 Mar 2007 17:15:46 +0000
Subject: cvs: pear /PHP_Parser package.xml /PHP_Parser/Parser Core.php Core.y Tokenizer.php /PHP_Parser/Parser/DocBlock Parser.php /PHP_Parser/tests
global_var.phpt
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-44942@lists.php.net to get a copy of this message
cellog Sun Mar 4 17:15:46 2007 UTC Added files: /pear/PHP_Parser/tests global_var.phpt Modified files: /pear/PHP_Parser/Parser Core.php Core.y Tokenizer.php /pear/PHP_Parser/Parser/DocBlock Parser.php /pear/PHP_Parser package.xml Log: implement global variable recognition through the docblock parser (yay!)

http://cvs.php.net/viewvc.cgi/pear/PHP_Parser/Parser/Core.php?r1=1.6&r2=1.7&diff_format=u Index: pear/PHP_Parser/Parser/Core.php diff -u pear/PHP_Parser/Parser/Core.php:1.6 pear/PHP_Parser/Parser/Core.php:1.7 --- pear/PHP_Parser/Parser/Core.php:1.6 Wed Feb 28 05:18:11 2007 +++ pear/PHP_Parser/Parser/Core.php Sun Mar 4 17:15:46 2007 @@ -6470,8 +6470,8 @@ #line 6475 "Core.php" #line 1132 "Core.y" function yy_r245(){ - $this->_retvalue = new PHP_Parser_CoreyyToken((string) $this->yystack[$this->yyidx + -4]->minor . '->' . (string) $this->yystack[$this->yyidx + -2]->minor . - (string) $this->yystack[$this->yyidx + -1]->minor . (string) $this->yystack[$this->yyidx + 0]->minor, array()); + $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -4]->minor->string . '->' . $this->yystack[$this->yyidx + -2]->minor->string . + $this->yystack[$this->yyidx + -1]->minor->string . $this->yystack[$this->yyidx + 0]->minor->string, array()); $this->_retvalue[] = $this->yystack[$this->yyidx + -4]->minor; if (is_array($this->yystack[$this->yyidx + -2]->minor)) { $this->_retvalue[] = $this->yystack[$this->yyidx + -2]->minor; @@ -6509,7 +6509,7 @@ #line 6514 "Core.php" #line 1173 "Core.y" function yy_r254(){ - $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -2]->minor . '::' . (string) $this->yystack[$this->yyidx + 0]->minor, array( + $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -2]->minor . '::' . $this->yystack[$this->yyidx + 0]->minor->string, array( array( 'usedclass' => $this->yystack[$this->yyidx + -2]->minor, ) @@ -6519,20 +6519,20 @@ #line 6524 "Core.php" #line 1191 "Core.y" function yy_r260(){ - $this->_retvalue = new PHP_Parser_CoreyyToken((string) $this->yystack[$this->yyidx + -3]->minor . '[' . (string) $this->yystack[$this->yyidx + -1]->minor . ']', array()); + $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -3]->minor->string . '[' . $this->yystack[$this->yyidx + -1]->minor->string . ']', array()); $this->_retvalue[] = $this->yystack[$this->yyidx + -3]->minor; $this->_retvalue[] = $this->yystack[$this->yyidx + -1]->minor; } #line 6531 "Core.php" #line 1196 "Core.y" function yy_r261(){ - $this->_retvalue = new PHP_Parser_CoreyyToken((string) $this->yystack[$this->yyidx + -3]->minor . '{' . (string) $this->yystack[$this->yyidx + -1]->minor . '}', array()); + $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -3]->minor->string . '{' . $this->yystack[$this->yyidx + -1]->minor->string . '}', array()); $this->_retvalue[] = $this->yystack[$this->yyidx + -3]->minor; $this->_retvalue[] = $this->yystack[$this->yyidx + -1]->minor; } #line 6538 "Core.php" #line 1204 "Core.y" - function yy_r264(){$this->_retvalue = new PHP_Parser_CoreyyToken('${' . (string) $this->yystack[$this->yyidx + -1]->minor . '}', $this->yystack[$this->yyidx + -1]->minor); } + function yy_r264(){$this->_retvalue = new PHP_Parser_CoreyyToken('${' . $this->yystack[$this->yyidx + -1]->minor->string . '}', $this->yystack[$this->yyidx + -1]->minor); } #line 6541 "Core.php" #line 1212 "Core.y" function yy_r269(){ @@ -6739,7 +6739,7 @@ } #line 6745 "Core.php" #line 1409 "Core.y" - function yy_r324(){$this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -3]->minor . '(' . (string) $this->yystack[$this->yyidx + -1]->minor . ')', $this->yystack[$this->yyidx + -1]->minor); } + function yy_r324(){$this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -3]->minor . '(' . $this->yystack[$this->yyidx + -1]->minor->string . ')', $this->yystack[$this->yyidx + -1]->minor); } #line 6748 "Core.php" #line 1410 "Core.y" function yy_r325(){ @@ -6758,7 +6758,7 @@ #line 6763 "Core.php" #line 1423 "Core.y" function yy_r326(){ - $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -5]->minor . '::' . (string) $this->yystack[$this->yyidx + -3]->minor . '(' . $this->yystack[$this->yyidx + -1]->minor->string . ')', $this->yystack[$this->yyidx + -3]->minor); + $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -5]->minor . '::' . $this->yystack[$this->yyidx + -3]->minor->string . '(' . $this->yystack[$this->yyidx + -1]->minor->string . ')', $this->yystack[$this->yyidx + -3]->minor); $this->_retvalue[] = $this->yystack[$this->yyidx + -1]->minor; $this->_retvalue[] = array( 'uses' => 'class', @@ -6768,7 +6768,7 @@ #line 6773 "Core.php" #line 1431 "Core.y" function yy_r327(){ - $this->_retvalue = new PHP_Parser_CoreyyToken((string) $this->yystack[$this->yyidx + -3]->minor . '(' . $this->yystack[$this->yyidx + -1]->minor->string . ')', $this->yystack[$this->yyidx + -3]->minor); + $this->_retvalue = new PHP_Parser_CoreyyToken($this->yystack[$this->yyidx + -3]->minor->string . '(' . $this->yystack[$this->yyidx + -1]->minor->string . ')', $this->yystack[$this->yyidx + -3]->minor); $this->_retvalue[] = $this->yystack[$this->yyidx + -1]->minor; } #line 6779 "Core.php" http://cvs.php.net/viewvc.cgi/pear/PHP_Parser/Parser/Core.y?r1=1.3&r2=1.4&diff_format=u Index: pear/PHP_Parser/Parser/Core.y diff -u pear/PHP_Parser/Parser/Core.y:1.3 pear/PHP_Parser/Parser/Core.y:1.4 --- pear/PHP_Parser/Parser/Core.y:1.3 Wed Feb 28 05:18:11 2007 +++ pear/PHP_Parser/Parser/Core.y Sun Mar 4 17:15:46 2007 @@ -1130,8 +1130,8 @@ rw_variable(A) ::= variable(B). {A = B;} variable(A) ::= base_variable_with_function_calls(BASE) T_OBJECT_OPERATOR object_property(PROP) method_or_not(IS_METHOD) variable_properties(VARP). { - A = new PHP_Parser_CoreyyToken((string) BASE . '->' . (string) PROP . - (string) IS_METHOD . (string) VARP, array()); + A = new PHP_Parser_CoreyyToken(BASE->string . '->' . PROP->string . + IS_METHOD->string . VARP->string, array()); A[] = BASE; if (is_array(PROP)) { A[] = PROP; @@ -1171,7 +1171,7 @@ } static_member(A) ::= fully_qualified_class_name(CLASS) T_PAAMAYIM_NEKUDOTAYIM variable_without_objects(VAR). { - A = new PHP_Parser_CoreyyToken(CLASS . '::' . (string) VAR, array( + A = new PHP_Parser_CoreyyToken(CLASS . '::' . VAR->string, array( array( 'usedclass' => CLASS, ) @@ -1189,19 +1189,19 @@ base_variable(A) ::= static_member(B). {A = B;} reference_variable(A) ::= reference_variable(REF) LBRACKET dim_offset(DIM) RBRACKET. { - A = new PHP_Parser_CoreyyToken((string) REF . '[' . (string) DIM . ']', array()); + A = new PHP_Parser_CoreyyToken(REF->string . '[' . DIM->string . ']', array()); A[] = REF; A[] = DIM; } reference_variable(A) ::= reference_variable(REF) LCURLY expr(DIM) RCURLY. { - A = new PHP_Parser_CoreyyToken((string) REF . '{' . (string) DIM . '}', array()); + A = new PHP_Parser_CoreyyToken(REF->string . '{' . DIM->string . '}', array()); A[] = REF; A[] = DIM; } reference_variable(A) ::= compound_variable(B). {A = new PHP_Parser_CoreyyToken(B);} compound_variable(A) ::= T_VARIABLE(B). {A = B;} -compound_variable(A) ::= DOLLAR LCURLY expr(B) RCURLY. {A = new PHP_Parser_CoreyyToken('${' . (string) B . '}', B);} +compound_variable(A) ::= DOLLAR LCURLY expr(B) RCURLY. {A = new PHP_Parser_CoreyyToken('${' . B->string . '}', B);} dim_offset(A) ::= expr(B). {A = new PHP_Parser_CoreyyToken(B);} dim_offset(A) ::= . {A = new PHP_Parser_CoreyyToken('');} @@ -1406,7 +1406,7 @@ fully_qualified_class_name(A) ::= T_STRING(B). {A = B;} -function_call(A) ::= T_STRING(B) LPAREN function_call_parameter_list(C) RPAREN. {A = new PHP_Parser_CoreyyToken(B . '(' . (string) C . ')', C);} +function_call(A) ::= T_STRING(B) LPAREN function_call_parameter_list(C) RPAREN. {A = new PHP_Parser_CoreyyToken(B . '(' . C->string . ')', C);} function_call(A) ::= fully_qualified_class_name(CLAS) T_PAAMAYIM_NEKUDOTAYIM T_STRING(FUNC) LPAREN function_call_parameter_list(PL) RPAREN. { A = new PHP_Parser_CoreyyToken(CLAS . '::' . FUNC . '(' . PL->string . ')', PL); @@ -1421,7 +1421,7 @@ ); } function_call(A) ::= fully_qualified_class_name(CLAS) T_PAAMAYIM_NEKUDOTAYIM variable_without_objects(V) LPAREN function_call_parameter_list(PL) RPAREN. { - A = new PHP_Parser_CoreyyToken(CLAS . '::' . (string) V . '(' . PL->string . ')', V); + A = new PHP_Parser_CoreyyToken(CLAS . '::' . V->string . '(' . PL->string . ')', V); A[] = PL; A[] = array( 'uses' => 'class', @@ -1429,7 +1429,7 @@ ); } function_call(A) ::= variable_without_objects(B) LPAREN function_call_parameter_list(PL) RPAREN. { - A = new PHP_Parser_CoreyyToken((string) B . '(' . PL->string . ')', B); + A = new PHP_Parser_CoreyyToken(B->string . '(' . PL->string . ')', B); A[] = PL; } http://cvs.php.net/viewvc.cgi/pear/PHP_Parser/Parser/Tokenizer.php?r1=1.11&r2=1.12&diff_format=u Index: pear/PHP_Parser/Parser/Tokenizer.php diff -u pear/PHP_Parser/Parser/Tokenizer.php:1.11 pear/PHP_Parser/Parser/Tokenizer.php:1.12 --- pear/PHP_Parser/Parser/Tokenizer.php:1.11 Wed Feb 21 05:57:33 2007 +++ pear/PHP_Parser/Parser/Tokenizer.php Sun Mar 4 17:15:46 2007 @@ -16,7 +16,7 @@ // | Authors: nobody <nobody@localhost> | // +----------------------------------------------------------------------+ // -// $Id: Tokenizer.php,v 1.11 2007/02/21 05:57:33 cellog Exp $ +// $Id: Tokenizer.php,v 1.12 2007/03/04 17:15:46 cellog Exp $ // require_once 'PHP/Parser/Core.php'; @@ -40,7 +40,7 @@ * - {@link $line} - current line number * - {@link $pos} - current token id * - {@link $N} - total no. of tokens -* @version $Id: Tokenizer.php,v 1.11 2007/02/21 05:57:33 cellog Exp $ +* @version $Id: Tokenizer.php,v 1.12 2007/03/04 17:15:46 cellog Exp $ */ class PHP_Parser_Tokenizer { @@ -153,6 +153,7 @@ var $_options; private $_whitespace; private $_trackingWhitespace = 0; + private $_docParser; /** * Constructor @@ -166,48 +167,9 @@ * @return none * @access public */ - function __construct($data, $options = array()) + function __construct($data, $docparser = false) { - $this->_options['documentationParser'] = - $this->_options['documentationLexer'] = - $this->_options['publishAllDocumentation'] = - $this->_options['documentationContainer'] = - $this->_options['publisher'] = - $this->_options['publishMethod'] = - $this->_options['publishMessageClass'] = - $this->_options['publishDocumentation'] = - $this->_options['publishDocumentationMessage'] = - false; - $this->_options = array_merge($this->_options, $options); - if (!class_exists($this->_options['documentationContainer'])) { - $this->_options['documentationContainer'] = false; - } - if (!is_object($this->_options['documentationParser'])) { - $this->_options['documentationParser'] = false; - $this->_options['documentationLexer'] = false; - } else { - $this->_options['documentationParser'] = &$options['documentationParser']; - $this->_options['documentationLexer'] = &$options['documentationLexer']; - // make sure it's an exact match - } - if (!is_object($this->_options['publisher'])) { - $this->_options['publisher'] = false; - $this->_options['publishAllDocumentation'] = false; - } else { - if (!method_exists($this->_options['publisher'], $this->_options['publishMethod'])) { - $this->_options['publishMethod'] = false; - if (!method_exists($this->_options['publisher'], 'publish')) { - $this->_options['publisher'] = false; - $this->_options['publishAllDocumentation'] = false; - } else { - $this->_options['publishMethod'] = 'publish'; - } - } else { - if (!class_exists($this->_options['publishMessageClass'])) { - $this->_options['publishMessageClass'] = false; - } - } - } + $this->_docParser = $docparser; $this->tokens = token_get_all($data); $this->N = count($this->tokens); for ($i=0;$i<$this->N;$i++) { @@ -310,37 +272,14 @@ function _handleDocumentation() { $this->lastComment = $this->tokens[$this->pos][1]; + $this->lastCommentToken = $this->tokens[$this->pos][0]; $this->lastCommentLine = $this->line; - $this->lastCommentToken = $this->pos; - if ($this->_options['documentationParser']) { - $parser = &$this->_options['documentationParser']; - $err = $parser->parse(array('comment' => $this->lastComment, - 'commentline' => $this->lastCommentLine, - 'commenttoken' => $this->lastCommentToken, - 'lexer' => $this->_options['documentationLexer'], - )); - if (PEAR::isError($err)) { - $this->lastComment = false; - $this->lastCommentLine = -1; - $this->lastCommentToken = -1; - } else { - $this->lastComment = $err; - } - } - if ($this->_options['publishAllDocumentation']) { - $publish = $this->_options['publishMethod']; - $message = 'documentation'; - if ($this->_options['publishDocumentationMessage']) { - $message = $this->_options['publishDocumentationMessage']; - } - if ($this->_options['publishMessageClass']) { - $pc = $this->_options['publishMessageClass']; - $publisher = $this->_options['publisher']; - $message = new $pc($message, $this->lastComment); - $publisher->$publish($pc); - } else { - $publisher = $this->_options['publisher']; - $publisher->$publish($message, $this->lastComment); + if ($this->_docParser) { + try { + $this->lastParsedComment = + $this->_docParser->parse($this->lastComment, $this); + } catch (Exception $e) { + } } } @@ -369,11 +308,6 @@ (PHP_Parser_Core::tokenName(PHP_Parser_Core::$transTable[$this->tokens[$this->pos[0]]])) . ')' ." : {$this->tokens[$this->pos][1]}\n"; } - static $T_DOC_COMMENT = false; - - if (!$T_DOC_COMMENT) { - $T_DOC_COMMENT = defined('T_DOC_COMMENT') ? constant('T_DOC_COMMENT') : 10000; - } switch ($this->tokens[$this->pos][0]) { @@ -386,14 +320,14 @@ continue; // comments - store for phpdoc - case $T_DOC_COMMENT; + case T_DOC_COMMENT; case T_COMMENT: $this->lastComment = ''; $this->lastCommentToken = -1; $this->lastCommentLine = -1; - if (substr($this->tokens[$this->pos][1],0,2) == '/*') { + if (substr($this->tokens[$this->pos][1], 0, 3) == '/**') { $this->_handleDocumentation(); } $this->line += substr_count ($this->tokens[$this->pos][1], "\n"); http://cvs.php.net/viewvc.cgi/pear/PHP_Parser/Parser/DocBlock/Parser.php?r1=1.1&r2=1.2&diff_format=u Index: pear/PHP_Parser/Parser/DocBlock/Parser.php diff -u pear/PHP_Parser/Parser/DocBlock/Parser.php:1.1 pear/PHP_Parser/Parser/DocBlock/Parser.php:1.2 --- pear/PHP_Parser/Parser/DocBlock/Parser.php:1.1 Sun Mar 4 06:15:00 2007 +++ pear/PHP_Parser/Parser/DocBlock/Parser.php Sun Mar 4 17:15:46 2007 @@ -14,16 +14,48 @@ private $_parser; function __construct($data, $processInternal = false) { - $this->_lex = new PHP_DocblockParser_Tokenizer($data); $this->_parser = new PHP_DocblockParser($this->_lex, $processInternal); } - function parse() + function parse($data, PHP_Parser_Tokenizer $tokenizer) { + $this->_lex = new PHP_DocblockParser_Tokenizer($data); while ($this->_lex->advance()) { $this->_parser->doParse($this->_lex->token, $this->_lex->getValue()); } $this->_parser->doParse(0, 0); + if (!count($this->_parser->data['tags'])) { + return $this->_parser->data; + } + if (!isset($this->_parser->data['tags']['global'])) { + return $this->_parser->data; + } + if (count($this->_parser->data['tags']['global']) > 1) { + // too many @global tags, this isn't valid + return $this->_parser->data; + } + if (count($this->_parser->data['tags'])) { + foreach ($this->_parser->data['tags']['global'] as $tag) { + if ($tag['text']) { + if (!is_array($tag['text'])) { + $info = preg_split('/[\t ]+/', trim($tag['text']), 2); + if (count($info) != 2) { + break; + } + if ($info[0][0] == '$') { + // invalid + break; + } + if ($info[1][0] != '$') { + // function-level @global + break; + } + $tokenizer->_setGlobalSearch(trim($info[1])); + return $this->_parser->data; + } + } + } + } return $this->_parser->data; } } http://cvs.php.net/viewvc.cgi/pear/PHP_Parser/package.xml?r1=1.15&r2=1.16&diff_format=u Index: pear/PHP_Parser/package.xml diff -u pear/PHP_Parser/package.xml:1.15 pear/PHP_Parser/package.xml:1.16 --- pear/PHP_Parser/package.xml:1.15 Mon Feb 19 05:52:27 2007 +++ pear/PHP_Parser/package.xml Sun Mar 4 17:15:46 2007 @@ -82,6 +82,9 @@ <file baseinstalldir="PHP" name="test.php" role="doc" /> </dir> <!-- /examples --> <dir baseinstalldir="PHP" name="Parser"> + <dir baseinstalldir="PHP" name="Docblock"> + <file name="Parser.php" role="php" /> + </dir> <!-- /Parser/Docblock --> <file name="Core.y" role="data" /> <file name="Core.php" role="php" /> <file name="Tokenizer.php" role="php" /> http://cvs.php.net/viewvc.cgi/pear/PHP_Parser/tests/global_var.phpt?view=markup&rev=1.1 Index: pear/PHP_Parser/tests/global_var.phpt +++ pear/PHP_Parser/tests/global_var.phpt --TEST-- PHP_Parser: test global variables --SKIPIF-- <?php $fp = @fopen('PHP/DocblockParser.php', 'r', true); if ($fp) { fclose($fp); } else { echo 'skip no PHP_DocblockParser'; } ?> --FILE-- <?php require_once 'PHP/Parser/Core.php'; require_once 'PHP/Parser/Tokenizer.php'; require_once 'PHP/Parser/Docblock/Parser.php'; $a = new PHP_Parser_Tokenizer('<?php $a = "hi"; /** * @global string $GLOBALS[\'hi there\'] */ $GLOBALS[\'hi there\'] = "holy crap"; ?>', new PHP_Parser_Docblock_Parser('')); $b = new PHP_Parser_Core($a); while ($a->advance()) { $b->doParse($a->token, $a->getValue(), $a); } $b->doParse(0, 0); var_dump($b->data); var_dump($b->classes); var_dump($b->interfaces); var_dump($b->functions); var_dump($b->includes); var_dump($b->globals); ?> ===DONE=== --EXPECT-- array(1) { [0]=> array(4) { ["type"]=> string(6) "global" ["name"]=> string(20) "$GLOBALS['hi there']" ["line"]=> int(6) ["default"]=> string(11) ""holy crap"" } } array(0) { } array(0) { } array(0) { } array(0) { } array(1) { ["$GLOBALS['hi there']"]=> array(1) { [0]=> array(4) { ["type"]=> string(6) "global" ["name"]=> string(20) "$GLOBALS['hi there']" ["line"]=> int(6) ["default"]=> string(11) ""holy crap"" } } } ===DONE===
« previous php.pear.cvs (#44942) next »