From: epriestley Date: Mon, 10 Jan 2011 06:11:31 +0000 (-0800) Subject: Remove XHPAST from arcanist. X-Git-Tag: conduit-4~449 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=12f1ba1d77cc31ff8417fe84504084a7e9365dda;p=arcanist.git Remove XHPAST from arcanist. Summary: see corresponding commit in libphutil Test Plan: linted and analyzed both libraries, got more sensible build behavior Reviewers: CC: --- diff --git a/.gitignore b/.gitignore index babd834d..eda77f49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,2 @@ .DS_Store ._* -*.a -*.o -/support/xhpast/xhpast -/src/staticanalysis/parsers/xhpast/bin/xhpast -parser.yacc.cpp -parser.yacc.hpp -scanner.lex.cpp -scanner.lex.hpp -parser.yacc.output diff --git a/scripts/phutil_analyzer.php b/scripts/phutil_analyzer.php index 0e69a274..67b679d7 100755 --- a/scripts/phutil_analyzer.php +++ b/scripts/phutil_analyzer.php @@ -48,9 +48,13 @@ if ($argc != 2) { phutil_require_module('phutil', 'filesystem'); $dir = Filesystem::resolvePath($argv[1]); -phutil_require_module('arcanist', 'staticanalysis/parsers/xhpast/bin'); +phutil_require_module('phutil', 'parser/xhpast/bin'); +phutil_require_module('phutil', 'parser/xhpast/api/tree'); + phutil_require_module('arcanist', 'lint/linter/phutilmodule'); phutil_require_module('arcanist', 'lint/message'); +phutil_require_module('arcanist', 'staticanalysis/parsers/phutilmodule'); + $data = array(); $futures = array(); @@ -62,8 +66,6 @@ foreach (Filesystem::listDirectory($dir, $hidden_files = false) as $file) { $futures[$file] = xhpast_get_parser_future($data[$file]); } -phutil_require_module('arcanist', 'staticanalysis/parsers/xhpast/api/tree'); -phutil_require_module('arcanist', 'staticanalysis/parsers/phutilmodule'); $requirements = new PhutilModuleRequirements(); $requirements->addBuiltins($builtin); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 4f28a81e..e8184561 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -64,17 +64,9 @@ phutil_register_library_map(array( 'PhutilModuleRequirements' => 'staticanalysis/parsers/phutilmodule', 'PhutilUnitTestEngine' => 'unit/engine/phutil', 'UnitTestableArcanistLintEngine' => 'lint/engine/test', - 'XHPASTNode' => 'staticanalysis/parsers/xhpast/api/node', - 'XHPASTNodeList' => 'staticanalysis/parsers/xhpast/api/list', - 'XHPASTSyntaxErrorException' => 'staticanalysis/parsers/xhpast/api/exception', - 'XHPASTToken' => 'staticanalysis/parsers/xhpast/api/token', - 'XHPASTTree' => 'staticanalysis/parsers/xhpast/api/tree', ), 'function' => array( - 'xhp_parser_node_constants' => 'staticanalysis/parsers/xhpast/constants', - 'xhpast_get_parser_future' => 'staticanalysis/parsers/xhpast/bin', - 'xhpast_parser_token_constants' => 'staticanalysis/parsers/xhpast/constants', ), 'requires_class' => array( diff --git a/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php b/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php index 709b0fb3..5182eb1b 100644 --- a/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php +++ b/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php @@ -90,6 +90,12 @@ class ArcanistPhutilModuleLinter extends ArcanistLinter { public function willLintPaths(array $paths) { + if ($paths) { + if (!xhpast_is_available()) { + throw new Exception(xhpast_get_build_instructions()); + } + } + $modules = array(); $moduleinfo = array(); diff --git a/src/lint/linter/phutilmodule/__init__.php b/src/lint/linter/phutilmodule/__init__.php index c4dddeca..97c77273 100644 --- a/src/lint/linter/phutilmodule/__init__.php +++ b/src/lint/linter/phutilmodule/__init__.php @@ -14,6 +14,7 @@ phutil_require_module('phutil', 'filesystem'); phutil_require_module('phutil', 'future'); phutil_require_module('phutil', 'future/exec'); phutil_require_module('phutil', 'moduleutils'); +phutil_require_module('phutil', 'parser/xhpast/bin'); phutil_require_source('ArcanistPhutilModuleLinter.php'); diff --git a/src/lint/linter/xhpast/__init__.php b/src/lint/linter/xhpast/__init__.php index 364f4d66..934c9387 100644 --- a/src/lint/linter/xhpast/__init__.php +++ b/src/lint/linter/xhpast/__init__.php @@ -8,8 +8,8 @@ phutil_require_module('arcanist', 'lint/linter/base'); phutil_require_module('arcanist', 'lint/severity'); -phutil_require_module('arcanist', 'staticanalysis/parsers/xhpast/api/tree'); -phutil_require_module('arcanist', 'staticanalysis/parsers/xhpast/bin'); +phutil_require_module('phutil', 'parser/xhpast/api/tree'); +phutil_require_module('phutil', 'parser/xhpast/bin'); phutil_require_module('phutil', 'utils'); diff --git a/src/staticanalysis/parsers/xhpast/api/exception/XHPASTSyntaxErrorException.php b/src/staticanalysis/parsers/xhpast/api/exception/XHPASTSyntaxErrorException.php deleted file mode 100644 index c89f6808..00000000 --- a/src/staticanalysis/parsers/xhpast/api/exception/XHPASTSyntaxErrorException.php +++ /dev/null @@ -1,32 +0,0 @@ -errorLine = $line; - parent::__construct($message); - } - - public function getErrorLine() { - return $this->errorLine; - } - -} diff --git a/src/staticanalysis/parsers/xhpast/api/exception/__init__.php b/src/staticanalysis/parsers/xhpast/api/exception/__init__.php deleted file mode 100644 index 8f31d953..00000000 --- a/src/staticanalysis/parsers/xhpast/api/exception/__init__.php +++ /dev/null @@ -1,10 +0,0 @@ -ids); - } - - public function current() { - return $this->list[$this->key()]; - } - - public function rewind() { - $this->pos = 0; - } - - public function valid() { - return $this->pos < count($this->ids); - } - - public function next() { - $this->pos++; - } - - public function key() { - return $this->ids[$this->pos]; - } - - public static function newFromTreeAndNodes(XHPASTTree $tree, array $nodes) { - $obj = new XHPASTNodeList(); - $obj->tree = $tree; - $obj->list = $nodes; - $obj->ids = array_keys($nodes); - return $obj; - } - - public static function newFromTree(XHPASTTree $tree) { - $obj = new XHPASTNodeList(); - $obj->tree = $tree; - $obj->list = array(0 => $tree->getRootNode()); - $obj->ids = array(0 => 0); - return $obj; - } - - protected function __construct() { - - } - - public function getDescription() { - if (empty($this->list)) { - return 'an empty node list'; - } - - $desc = array(); - $desc[] = "a list of ".count($this->list)." nodes:"; - foreach ($this->list as $node) { - $desc[] = ' '.$node->getDescription().";"; - } - - return implode("\n", $desc); - } - - - - protected function newList(array $nodes) { - return XHPASTNodeList::newFromTreeAndNodes( - $this->tree, - $nodes); - } - - public function selectDescendantsOfType($type_name) { - $results = array(); - foreach ($this->list as $id => $node) { - $results += $node->selectDescendantsOfType($type_name)->getRawNodes(); - } - return $this->newList($results); - } - - public function selectDescendantsOfTypes(array $type_names) { - $results = array(); - foreach ($type_names as $type_name) { - foreach ($this->list as $id => $node) { - $results += $node->selectDescendantsOfType($type_name)->getRawNodes(); - } - } - return $this->newList($results); - } - - public function getChildrenByIndex($index) { - $results = array(); - foreach ($this->list as $id => $node) { - $child = $node->getChildByIndex($index); - $results[$child->getID()] = $child; - } - return $this->newList($results); - } - - public function add(XHPASTNodeList $list) { - foreach ($list->list as $id => $node) { - $this->list[$id] = $node; - } - $this->ids = array_keys($this->list); - return $this; - } - - - protected function executeSelectDescendantsOfType($node, $type) { - $results = array(); - foreach ($node->getChildren() as $id => $child) { - if ($child->getTypeID() == $type) { - $results[$id] = $child; - } else { - $results += $this->executeSelectDescendantsOfType($child, $type); - } - } - return $results; - } - - public function getTokens() { - $tokens = array(); - foreach ($this->list as $node) { - $tokens += $node->getTokens(); - } - return $tokens; - } - - public function getRawNodes() { - return $this->list; - } - -} diff --git a/src/staticanalysis/parsers/xhpast/api/list/__init__.php b/src/staticanalysis/parsers/xhpast/api/list/__init__.php deleted file mode 100644 index 6c192345..00000000 --- a/src/staticanalysis/parsers/xhpast/api/list/__init__.php +++ /dev/null @@ -1,19 +0,0 @@ -id = $id; - $this->typeID = $data[0]; - $this->l = idx($data, 1, -1); - $this->r = idx($data, 2, -1); - $this->tree = $tree; - } - - public function setParentNode($parent_node) { - $this->parentNode = $parent_node; - return $this; - } - - public function getParentNode() { - return $this->parentNode; - } - - public function setChildren(array $children) { - $this->children = $children; - return $this; - } - - public function getID() { - return $this->id; - } - - public function getTypeID() { - return $this->typeID; - } - - public function getTypeName() { - static $map; - if (empty($map)) { - $map = xhp_parser_node_constants(); - } - - $type_id = $this->getTypeID(); - if (empty($map[$type_id])) { - throw new Exception("No type name for node type ID '{$type_id}'."); - } - - return $map[$type_id]; - } - - public function getChildren() { - return $this->children; - } - - public function getChildOfType($index, $type) { - $child = $this->getChildByIndex($index); - if ($child->getTypeName() != $type) { - throw new Exception( - "Child in position '{$index}' is not of type '{$type}': ". - $this->getDescription()); - } - - return $child; - } - - public function getChildByIndex($index) { - $child = idx(array_values($this->children), $index); - if (!$child) { - throw new Exception( - "No child with index '{$index}'."); - } - return $child; - } - - public function selectDescendantsOfType($type_name) { - $type = $this->getTypeIDFromTypeName($type_name); - return XHPASTNodeList::newFromTreeAndNodes( - $this->tree, - $this->executeSelectDescendantsOfType($this, $type)); - } - - protected function executeSelectDescendantsOfType($node, $type) { - $results = array(); - foreach ($node->getChildren() as $id => $child) { - if ($child->getTypeID() == $type) { - $results[$id] = $child; - } - $results += $this->executeSelectDescendantsOfType($child, $type); - } - return $results; - } - - public function getTokens() { - if ($this->l == -1 || $this->r == -1) { - return array(); - } - $tokens = $this->tree->getRawTokenStream(); - $result = array(); - foreach (range($this->l, $this->r) as $token_id) { - $result[$token_id] = $tokens[$token_id]; - } - return $result; - } - - public function getConcreteString() { - $values = array(); - foreach ($this->getTokens() as $token) { - $values[] = $token->getValue(); - } - return implode('', $values); - } - - public function getStringLiteralValue() { - // TODO: This function should accommodate concatenation of literals and - // return 'null' if the literal contains variables. - - if ($this->getTypeName() != 'n_STRING_SCALAR') { - return null; - } - $value = $this->getConcreteString(); - $value = substr($value, 1, -1); - $value = stripcslashes($value); - return $value; - } - - public function getSemanticString() { - $tokens = $this->getTokens(); - foreach ($tokens as $id => $token) { - if ($token->isComment()) { - unset($tokens[$id]); - } - } - return implode('', mpull($tokens, 'getValue')); - } - - public function getDescription() { - $concrete = $this->getConcreteString(); - if (strlen($concrete) > 75) { - $concrete = substr($concrete, 0, 36).'...'.substr($concrete, -36); - } - - $concrete = addcslashes($concrete, "\\\n\""); - - return 'a node of type '.$this->getTypeName().': "'.$concrete.'"'; - } - - protected function getTypeIDFromTypeName($type_name) { - static $node_types; - if (empty($node_types)) { - $node_types = xhp_parser_node_constants(); - $node_types = array_flip($node_types); - } - - if (empty($node_types[$type_name])) { - throw new Exception("Unknown XHPAST Node type name '{$type_name}'!"); - } - - return $node_types[$type_name]; - } - - public function getOffset() { - $first_token = idx($this->tree->getRawTokenStream(), $this->l); - if (!$first_token) { - return null; - } - return $first_token->getOffset(); - } - - public function isStaticScalar() { - return ($this->getTypeName() == 'n_STRING_SCALAR' || - $this->getTypeName() == 'n_NUMERIC_SCALAR'); - } - - public function getSurroundingNonsemanticTokens() { - $before = array(); - $after = array(); - - $tokens = $this->tree->getRawTokenStream(); - - if ($this->l != -1) { - $before = $tokens[$this->l]->getNonsemanticTokensBefore(); - } - - if ($this->r != -1) { - $after = $tokens[$this->r]->getNonsemanticTokensAfter(); - } - - return array($before, $after); - } - - public function getDocblockToken() { - if ($this->l == -1) { - return null; - } - $tokens = $this->tree->getRawTokenStream(); - - for ($ii = $this->l - 1; $ii >= 0; $ii--) { - if ($tokens[$ii]->getTypeName() == 'T_DOC_COMMENT') { - return $tokens[$ii]; - } - if (!$tokens[$ii]->isAnyWhitespace()) { - return null; - } - } - - return null; - } - -} diff --git a/src/staticanalysis/parsers/xhpast/api/node/__init__.php b/src/staticanalysis/parsers/xhpast/api/node/__init__.php deleted file mode 100644 index 6e5f237e..00000000 --- a/src/staticanalysis/parsers/xhpast/api/node/__init__.php +++ /dev/null @@ -1,23 +0,0 @@ -id = $id; - $this->typeID = $type; - $this->offset = $offset; - $this->value = $value; - $this->tree = $tree; - } - - public function getTypeID() { - return $this->typeID; - } - - public function getTypeName() { - static $map; - if (empty($map)) { - $map = xhpast_parser_token_constants(); - } - - $type_id = $this->getTypeID(); - - if ($type_id <= 255) { - return chr($type_id); - } - - if (empty($map[$type_id])) { - throw new Exception("No type name for token type ID '{$type_id}'."); - } - - return $map[$type_id]; - } - - public function getValue() { - return $this->value; - } - - public function getOffset() { - return $this->offset; - } - - public function isComment() { - return ($this->getTypeName() == 'T_COMMENT' || - $this->getTypeName() == 'T_DOC_COMMENT'); - } - - public function isAnyWhitespace() { - return ($this->getTypeName() == 'T_WHITESPACE' || - $this->getTypeName() == 'T_XHP_WHITESPACE'); - } - - public function isSemantic() { - return !($this->isComment() || $this->isAnyWhitespace()); - } - - public function getNonsemanticTokensBefore() { - $tokens = $this->tree->getRawTokenStream(); - $result = array(); - $ii = $this->id - 1; - while ($ii >= 0 && !$tokens[$ii]->isSemantic()) { - $result[$ii] = $tokens[$ii]; - --$ii; - } - return array_reverse($result); - } - - public function getNonsemanticTokensAfter() { - $tokens = $this->tree->getRawTokenStream(); - $result = array(); - $ii = $this->id + 1; - while ($ii < count($tokens) && !$tokens[$ii]->isSemantic()) { - $result[$ii] = $tokens[$ii]; - ++$ii; - } - return $result; - } - - -} diff --git a/src/staticanalysis/parsers/xhpast/api/token/__init__.php b/src/staticanalysis/parsers/xhpast/api/token/__init__.php deleted file mode 100644 index b07c3932..00000000 --- a/src/staticanalysis/parsers/xhpast/api/token/__init__.php +++ /dev/null @@ -1,12 +0,0 @@ -resolve()); - } - - public static function newFromDataAndResolvedExecFuture( - $php_source, - array $resolved) { - - list($err, $stdout, $stderr) = $resolved; - if ($err) { - if ($err == 1) { - $matches = null; - $is_syntax = preg_match( - '/^XHPAST Parse Error: (.*) on line (\d+)/', - $stderr, - $matches); - if ($is_syntax) { - throw new XHPASTSyntaxErrorException($matches[2], $stderr); - } - } - throw new Exception("XHPAST failed to parse file data {$err}: {$stderr}"); - } - - $data = json_decode($stdout, true); - if (!is_array($data)) { - throw new Exception("XHPAST: failed to decode tree."); - } - - return new XHPASTTree($data['tree'], $data['stream'], $php_source); - } - - public function __construct(array $tree, array $stream, $source) { - $ii = 0; - $offset = 0; - foreach ($stream as $token) { - $this->stream[$ii] = new XHPASTToken( - $ii, - $token[0], - substr($source, $offset, $token[1]), - $offset, - $this); - $offset += $token[1]; - ++$ii; - } - - $this->buildTree(array($tree)); - } - - public function getRootNode() { - return $this->tree[0]; - } - - protected function buildTree(array $tree) { - $ii = count($this->tree); - $nodes = array(); - foreach ($tree as $node) { - $this->tree[$ii] = new XHPASTNode($ii, $node, $this); - $nodes[$ii] = $node; - ++$ii; - } - foreach ($nodes as $node_id => $node) { - if (isset($node[3])) { - $children = $this->buildTree($node[3]); - foreach ($children as $child) { - $child->setParentNode($this->tree[$node_id]); - } - $this->tree[$node_id]->setChildren($children); - } - } - return array_select_keys($this->tree, array_keys($nodes)); - } - - public function getRawTokenStream() { - return $this->stream; - } - - public function renderAsText() { - return $this->executeRenderAsText(array($this->getRootNode()), 0); - } - - protected function executeRenderAsText($list, $depth) { - $return = ''; - foreach ($list as $node) { - if ($depth) { - $return .= str_repeat(' ', $depth); - } - $return .= $node->getDescription()."\n"; - $return .= $this->executeRenderAsText($node->getChildren(), $depth + 1); - } - return $return; - } - -} diff --git a/src/staticanalysis/parsers/xhpast/api/tree/__init__.php b/src/staticanalysis/parsers/xhpast/api/tree/__init__.php deleted file mode 100644 index affc4112..00000000 --- a/src/staticanalysis/parsers/xhpast/api/tree/__init__.php +++ /dev/null @@ -1,17 +0,0 @@ -write($data); - - return $future; -} diff --git a/src/staticanalysis/parsers/xhpast/constants/__init__.php b/src/staticanalysis/parsers/xhpast/constants/__init__.php deleted file mode 100644 index 5f45fb4e..00000000 --- a/src/staticanalysis/parsers/xhpast/constants/__init__.php +++ /dev/null @@ -1,20 +0,0 @@ - 'n_PROGRAM', - 9001 => 'n_SYMBOL_NAME', - 9002 => 'n_HALT_COMPILER', - 9003 => 'n_NAMESPACE', - 9004 => 'n_STATEMENT', - 9005 => 'n_EMPTY', - 9006 => 'n_STATEMENT_LIST', - 9007 => 'n_OPEN_TAG', - 9008 => 'n_CLOSE_TAG', - 9009 => 'n_USE_LIST', - 9010 => 'n_USE', - 9011 => 'n_CONSTANT_DECLARATION_LIST', - 9012 => 'n_CONSTANT_DECLARATION', - 9013 => 'n_STRING', - 9014 => 'n_LABEL', - 9015 => 'n_CONDITION_LIST', - 9016 => 'n_CONTROL_CONDITION', - 9017 => 'n_IF', - 9018 => 'n_ELSEIF', - 9019 => 'n_ELSE', - 9020 => 'n_WHILE', - 9021 => 'n_DO_WHILE', - 9022 => 'n_FOR', - 9023 => 'n_FOR_EXPRESSION', - 9024 => 'n_SWITCH', - 9025 => 'n_BREAK', - 9026 => 'n_CONTINUE', - 9027 => 'n_RETURN', - 9028 => 'n_GLOBAL_DECLARATION_LIST', - 9029 => 'n_GLOBAL_DECLARATION', - 9030 => 'n_STATIC_DECLARATION_LIST', - 9031 => 'n_STATIC_DECLARATION', - 9032 => 'n_ECHO_LIST', - 9033 => 'n_ECHO', - 9034 => 'n_INLINE_HTML', - 9035 => 'n_UNSET_LIST', - 9036 => 'n_UNSET', - 9037 => 'n_FOREACH', - 9038 => 'n_FOREACH_EXPRESSION', - 9039 => 'n_THROW', - 9040 => 'n_GOTO', - 9041 => 'n_TRY', - 9042 => 'n_CATCH_LIST', - 9043 => 'n_CATCH', - 9044 => 'n_DECLARE', - 9045 => 'n_DECLARE_DECLARATION_LIST', - 9046 => 'n_DECLARE_DECLARATION', - 9047 => 'n_VARIABLE', - 9048 => 'n_REFERENCE', - 9049 => 'n_VARIABLE_REFERENCE', - 9050 => 'n_FUNCTION_DECLARATION', - 9051 => 'n_CLASS_DECLARATION', - 9052 => 'n_CLASS_ATTRIBUTES', - 9053 => 'n_EXTENDS', - 9054 => 'n_EXTENDS_LIST', - 9055 => 'n_IMPLEMENTS_LIST', - 9056 => 'n_INTERFACE_DECLARATION', - 9057 => 'n_CASE', - 9058 => 'n_DEFAULT', - 9059 => 'n_DECLARATION_PARAMETER_LIST', - 9060 => 'n_DECLARATION_PARAMETER', - 9061 => 'n_TYPE_NAME', - 9062 => 'n_VARIABLE_VARIABLE', - 9063 => 'n_CLASS_MEMBER_DECLARATION_LIST', - 9064 => 'n_CLASS_MEMBER_DECLARATION', - 9065 => 'n_CLASS_CONSTANT_DECLARATION_LIST', - 9066 => 'n_CLASS_CONSTANT_DECLARATION', - 9067 => 'n_METHOD_DECLARATION', - 9068 => 'n_METHOD_MODIFIER_LIST', - 9069 => 'n_FUNCTION_MODIFIER_LIST', - 9070 => 'n_CLASS_MEMBER_MODIFIER_LIST', - 9071 => 'n_EXPRESSION_LIST', - 9072 => 'n_LIST', - 9073 => 'n_ASSIGNMENT', - 9074 => 'n_NEW', - 9075 => 'n_UNARY_PREFIX_EXPRESSION', - 9076 => 'n_UNARY_POSTFIX_EXPRESSION', - 9077 => 'n_BINARY_EXPRESSION', - 9078 => 'n_TERNARY_EXPRESSION', - 9079 => 'n_CAST_EXPRESSION', - 9080 => 'n_CAST', - 9081 => 'n_OPERATOR', - 9082 => 'n_ARRAY_LITERAL', - 9083 => 'n_EXIT_EXPRESSION', - 9084 => 'n_BACKTICKS_EXPRESSION', - 9085 => 'n_LEXICAL_VARIABLE_LIST', - 9086 => 'n_NUMERIC_SCALAR', - 9087 => 'n_STRING_SCALAR', - 9088 => 'n_MAGIC_SCALAR', - 9089 => 'n_CLASS_STATIC_ACCESS', - 9090 => 'n_CLASS_NAME', - 9091 => 'n_MAGIC_CLASS_KEYWORD', - 9092 => 'n_OBJECT_PROPERTY_ACCESS', - 9093 => 'n_ARRAY_VALUE_LIST', - 9094 => 'n_ARRAY_VALUE', - 9095 => 'n_CALL_PARAMETER_LIST', - 9096 => 'n_VARIABLE_EXPRESSION', - 9097 => 'n_INCLUDE_FILE', - 9098 => 'n_HEREDOC', - 9099 => 'n_FUNCTION_CALL', - 9100 => 'n_INDEX_ACCESS', - 9101 => 'n_ASSIGNMENT_LIST', - 9102 => 'n_METHOD_CALL', - 9103 => 'n_XHP_TAG', - 9104 => 'n_XHP_TAG_OPEN', - 9105 => 'n_XHP_TAG_CLOSE', - 9106 => 'n_XHP_TEXT', - 9107 => 'n_XHP_EXPRESSION', - 9108 => 'n_XHP_ATTRIBUTE_LIST', - 9109 => 'n_XHP_ATTRIBUTE', - 9110 => 'n_XHP_LITERAL', - 9111 => 'n_XHP_ATTRIBUTE_LITERAL', - 9112 => 'n_XHP_ATTRIBUTE_EXPRESSION', - 9113 => 'n_XHP_NODE_LIST', - 9114 => 'n_XHP_ENTITY', - 9115 => 'n_CONCATENATION_LIST', - 9116 => 'n_PARENTHETICAL_EXPRESSION', - ); -} diff --git a/src/staticanalysis/parsers/xhpast/constants/parser_tokens.php b/src/staticanalysis/parsers/xhpast/constants/parser_tokens.php deleted file mode 100644 index a2a44f12..00000000 --- a/src/staticanalysis/parsers/xhpast/constants/parser_tokens.php +++ /dev/null @@ -1,172 +0,0 @@ - 'T_REQUIRE_ONCE', - 259 => 'T_REQUIRE', - 260 => 'T_EVAL', - 261 => 'T_INCLUDE_ONCE', - 262 => 'T_INCLUDE', - 263 => 'T_LOGICAL_OR', - 264 => 'T_LOGICAL_XOR', - 265 => 'T_LOGICAL_AND', - 266 => 'T_PRINT', - 267 => 'T_SR_EQUAL', - 268 => 'T_SL_EQUAL', - 269 => 'T_XOR_EQUAL', - 270 => 'T_OR_EQUAL', - 271 => 'T_AND_EQUAL', - 272 => 'T_MOD_EQUAL', - 273 => 'T_CONCAT_EQUAL', - 274 => 'T_DIV_EQUAL', - 275 => 'T_MUL_EQUAL', - 276 => 'T_MINUS_EQUAL', - 277 => 'T_PLUS_EQUAL', - 278 => 'T_BOOLEAN_OR', - 279 => 'T_BOOLEAN_AND', - 280 => 'T_IS_NOT_IDENTICAL', - 281 => 'T_IS_IDENTICAL', - 282 => 'T_IS_NOT_EQUAL', - 283 => 'T_IS_EQUAL', - 284 => 'T_IS_GREATER_OR_EQUAL', - 285 => 'T_IS_SMALLER_OR_EQUAL', - 286 => 'T_SR', - 287 => 'T_SL', - 288 => 'T_INSTANCEOF', - 289 => 'T_UNSET_CAST', - 290 => 'T_BOOL_CAST', - 291 => 'T_OBJECT_CAST', - 292 => 'T_ARRAY_CAST', - 293 => 'T_BINARY_CAST', - 294 => 'T_UNICODE_CAST', - 295 => 'T_STRING_CAST', - 296 => 'T_DOUBLE_CAST', - 297 => 'T_INT_CAST', - 298 => 'T_DEC', - 299 => 'T_INC', - 300 => 'T_CLONE', - 301 => 'T_NEW', - 302 => 'T_EXIT', - 303 => 'T_IF', - 304 => 'T_ELSEIF', - 305 => 'T_ELSE', - 306 => 'T_ENDIF', - 307 => 'T_LNUMBER', - 308 => 'T_DNUMBER', - 309 => 'T_STRING', - 310 => 'T_STRING_VARNAME', - 311 => 'T_VARIABLE', - 312 => 'T_NUM_STRING', - 313 => 'T_INLINE_HTML', - 314 => 'T_CHARACTER', - 315 => 'T_BAD_CHARACTER', - 316 => 'T_ENCAPSED_AND_WHITESPACE', - 317 => 'T_CONSTANT_ENCAPSED_STRING', - 318 => 'T_BACKTICKS_EXPR', - 319 => 'T_ECHO', - 320 => 'T_DO', - 321 => 'T_WHILE', - 322 => 'T_ENDWHILE', - 323 => 'T_FOR', - 324 => 'T_ENDFOR', - 325 => 'T_FOREACH', - 326 => 'T_ENDFOREACH', - 327 => 'T_DECLARE', - 328 => 'T_ENDDECLARE', - 329 => 'T_AS', - 330 => 'T_SWITCH', - 331 => 'T_ENDSWITCH', - 332 => 'T_CASE', - 333 => 'T_DEFAULT', - 334 => 'T_BREAK', - 335 => 'T_CONTINUE', - 336 => 'T_GOTO', - 337 => 'T_FUNCTION', - 338 => 'T_CONST', - 339 => 'T_RETURN', - 340 => 'T_TRY', - 341 => 'T_CATCH', - 342 => 'T_THROW', - 343 => 'T_USE', - 344 => 'T_GLOBAL', - 345 => 'T_PUBLIC', - 346 => 'T_PROTECTED', - 347 => 'T_PRIVATE', - 348 => 'T_FINAL', - 349 => 'T_ABSTRACT', - 350 => 'T_STATIC', - 351 => 'T_VAR', - 352 => 'T_UNSET', - 353 => 'T_ISSET', - 354 => 'T_EMPTY', - 355 => 'T_HALT_COMPILER', - 356 => 'T_CLASS', - 357 => 'T_INTERFACE', - 358 => 'T_EXTENDS', - 359 => 'T_IMPLEMENTS', - 360 => 'T_OBJECT_OPERATOR', - 361 => 'T_DOUBLE_ARROW', - 362 => 'T_LIST', - 363 => 'T_ARRAY', - 364 => 'T_CLASS_C', - 365 => 'T_METHOD_C', - 366 => 'T_FUNC_C', - 367 => 'T_LINE', - 368 => 'T_FILE', - 369 => 'T_COMMENT', - 370 => 'T_DOC_COMMENT', - 371 => 'T_OPEN_TAG', - 372 => 'T_OPEN_TAG_WITH_ECHO', - 373 => 'T_OPEN_TAG_FAKE', - 374 => 'T_CLOSE_TAG', - 375 => 'T_WHITESPACE', - 376 => 'T_START_HEREDOC', - 377 => 'T_END_HEREDOC', - 378 => 'T_HEREDOC', - 379 => 'T_DOLLAR_OPEN_CURLY_BRACES', - 380 => 'T_CURLY_OPEN', - 381 => 'T_PAAMAYIM_NEKUDOTAYIM', - 382 => 'T_BINARY_DOUBLE', - 383 => 'T_BINARY_HEREDOC', - 384 => 'T_NAMESPACE', - 385 => 'T_NS_C', - 386 => 'T_DIR', - 387 => 'T_NS_SEPARATOR', - 388 => 'T_XHP_WHITESPACE', - 389 => 'T_XHP_TEXT', - 390 => 'T_XHP_LT_DIV', - 391 => 'T_XHP_LT_DIV_GT', - 392 => 'T_XHP_ATTRIBUTE', - 393 => 'T_XHP_CATEGORY', - 394 => 'T_XHP_CHILDREN', - 395 => 'T_XHP_ANY', - 396 => 'T_XHP_EMPTY', - 397 => 'T_XHP_PCDATA', - 398 => 'T_XHP_COLON', - 399 => 'T_XHP_HYPHEN', - 400 => 'T_XHP_BOOLEAN', - 401 => 'T_XHP_NUMBER', - 402 => 'T_XHP_ARRAY', - 403 => 'T_XHP_STRING', - 404 => 'T_XHP_ENUM', - 405 => 'T_XHP_FLOAT', - 406 => 'T_XHP_REQUIRED', - 407 => 'T_XHP_ENTITY', - ); -} diff --git a/support/xhpast/Makefile b/support/xhpast/Makefile deleted file mode 100755 index c7ff8b3e..00000000 --- a/support/xhpast/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -ifdef DEBUG -CPPFLAGS = -fPIC -ggdb -Wall -DDEBUG -else -CPPFLAGS = -fPIC -g -Wall -O3 -minline-all-stringops -endif - -ifdef PROFILE -CPPFLAGS += -pg -endif - -all: xhpast - -clean: - -rm xhpast parser.yacc.cpp scanner.lex.cpp scanner.lex.hpp parser.yacc.output parser.yacc.hpp libxhpast.a *.o 2>/dev/null - -install: xhpast - cp xhpast ../../src/staticanalysis/parsers/xhpast/bin/ - -parser.yacc.cpp: parser.y - bison --debug --verbose -d -o $@ $< - -parser.yacc.hpp: parser.yacc.cpp - -scanner.lex.cpp: scanner.l - `which flex35 2>/dev/null || which flex 2>/dev/null` \ - -C --header-file=scanner.lex.hpp -o $@ -d $< - -scanner.lex.hpp: scanner.lex.cpp - -node_names.hpp: generate_nodes.php - php -f generate_nodes.php - -%.o: %.cpp - $(CXX) -c $(CPPFLAGS) -o $@ $< - -parser.yacc.o: scanner.lex.hpp - -scanner.lex.o: parser.yacc.hpp node_names.hpp - -libxhpast.a: astnode.o scanner.lex.o parser.yacc.o - $(AR) -crs $@ $^ - -xhpast: xhpast.cpp libxhpast.a - $(CXX) $(CPPFLAGS) -o $@ $^ - -.PHONY: all clean tags diff --git a/support/xhpast/ast.hpp b/support/xhpast/ast.hpp deleted file mode 100755 index 7548b9ab..00000000 --- a/support/xhpast/ast.hpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2011 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include -#include -#include - -#include "astnode.hpp" - -class yy_extra_type { - public: - yy_extra_type() { - lineno = 1; - terminated = false; - used = false; - short_tags = true; - asp_tags = false; - idx_expr = false; - include_debug = false; - expecting_xhp_class_statements = false; - list_size = 0; - colon_hack = false; - pushStack(); - } - - bool short_tags; // `short_open_tag` in php.ini - bool asp_tags; // `asp_tags` in php.ini - bool idx_expr; // allow code like `foo()['bar']` - bool include_debug; // include line numbers and file names in XHP object creation - size_t first_lineno; // line number before scanning the current token - size_t lineno; // current line number being scanned. - std::string error; // description of error (if terminated true) - bool terminated; // becomes true when the parser terminates with an error - bool used; // were any XHP-specific extensions found in this code? - int last_token; // the last token to be returned by the scanner - int insert_token; // insert this token without reading from buffer - size_t heredoc_yyleng; // last length of yytext while scannling - const char* heredoc_data; // where our heredoc data starts - std::string heredoc_label; // heredoc sentinel label - std::stack curly_stack; // tokens appearing before a { - bool expecting_xhp_class_statements; // when we're one level deep in a class - bool old_expecting_xhp_class_statements; // store old value while inside class method - bool used_attributes; // did this class use the `attribute` keyword - unsigned int list_size; - bool colon_hack; - - xhpast::token_list_t token_list; - - /* Utility functions for checking proper tag closing */ - bool haveTag() { - return !tag_stack.front().empty(); - } - const std::string &peekTag() { - return tag_stack.front().front(); - } - void pushTag(const std::string &tag) { - tag_stack.front().push_front(tag); - } - void popTag() { - tag_stack.front().pop_front(); - } - void pushStack() { - tag_stack.push_front(std::deque()); - } - void popStack() { - tag_stack.pop_front(); - } - - protected: - std::deque > tag_stack; -}; - -#define YYSTYPE xhpast::Node * -#define YY_HEADER_EXPORT_START_CONDITIONS -#define YY_EXTRA_TYPE yy_extra_type* - -#include "parser.yacc.hpp" -#ifndef FLEX_SCANNER - #include "scanner.lex.hpp" -#endif - -int xhpparse(void*, YYSTYPE *); -void xhp_new_push_state(int s, struct yyguts_t* yyg); -void xhp_new_pop_state(struct yyguts_t* yyg); -void xhp_set_state(int s, struct yyguts_t* yyg); diff --git a/support/xhpast/astnode.cpp b/support/xhpast/astnode.cpp deleted file mode 100755 index ac3ece2d..00000000 --- a/support/xhpast/astnode.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright 2011 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "astnode.hpp" diff --git a/support/xhpast/astnode.hpp b/support/xhpast/astnode.hpp deleted file mode 100755 index a76dbda1..00000000 --- a/support/xhpast/astnode.hpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2011 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once -#include -#include -#include -#include - -namespace xhpast { - - class Token; - typedef std::list token_list_t; - - class Token { - - public: - unsigned int type; - std::string value; - unsigned int lineno; - unsigned int n; - - Token(unsigned int type, char *value, unsigned int n) : - type(type), - value(value), - n(n) { - } - }; - - class Node; - typedef std::list node_list_t; - - class Node { - public: - unsigned int type; - - int l_tok; - int r_tok; - - node_list_t children; - - - Node() : type(0), l_tok(-1), r_tok(-1) {}; - - Node(unsigned int type) : type(type), l_tok(-1), r_tok(-1) {}; - - Node(unsigned int type, int end_tok) : - type(type) { - this->l_tok = end_tok; - this->r_tok = end_tok; - } - - Node(unsigned int type, int l_tok, int r_tok) : - type(type), - l_tok(l_tok), - r_tok(r_tok) { - - } - - Node *appendChild(Node *node) { - this->children.push_back(node); - return this->setEnd(node); - } - - Node *appendChildren(Node *node) { - for (node_list_t::iterator ii = node->children.begin(); ii != node->children.end(); ++ii) { - this->children.push_back(*ii); - this->setEnd(*ii); - } - return this; - } - - Node *firstChild() { - return *(this->children.begin()); - } - - Node *setType(unsigned int t) { - this->type = t; - return this; - } - - Node *setEnd(Node *n) { - if (!n) { - fprintf(stderr, "Trying to setEnd() a null node to one of type %d\n", this->type); - exit(1); - } - - if (n->r_tok != -1 && (n->r_tok > this->r_tok || (this->r_tok == -1))) { - this->r_tok = n->r_tok; - } - if (this->l_tok == -1) { - this->l_tok = n->l_tok; - } - return this; - } - - Node *setBegin(Node *n) { - if (!n) { - fprintf(stderr, "Trying to setBegin() a null node to one of type %d\n", this->type); - exit(1); - } - - if (n->l_tok != -1 && (n->l_tok < this->l_tok || (this->l_tok == -1))) { - this->l_tok = n->l_tok; - } - if (this->r_tok == -1) { - this->r_tok = n->r_tok; - } - return this; - } - - }; -} diff --git a/support/xhpast/generate_nodes.php b/support/xhpast/generate_nodes.php deleted file mode 100755 index 0a3b57af..00000000 --- a/support/xhpast/generate_nodes.php +++ /dev/null @@ -1,165 +0,0 @@ -#!/usr/local/bin/php - $value) { - $hpp .= "#define {$node} {$value}\n"; -} -file_put_contents('node_names.hpp', $hpp); -echo "Wrote C++ definition.\n"; - -$php = - " $value) { - $php .= " {$value} => '{$node}',\n"; -} -$php .= " );\n"; -$php .= "}\n"; -file_put_contents('parser_nodes.php', $php); -echo "Wrote PHP definition.\n"; - - - diff --git a/support/xhpast/node_names.hpp b/support/xhpast/node_names.hpp deleted file mode 100755 index 8b19f343..00000000 --- a/support/xhpast/node_names.hpp +++ /dev/null @@ -1,117 +0,0 @@ -#define n_PROGRAM 9000 -#define n_SYMBOL_NAME 9001 -#define n_HALT_COMPILER 9002 -#define n_NAMESPACE 9003 -#define n_STATEMENT 9004 -#define n_EMPTY 9005 -#define n_STATEMENT_LIST 9006 -#define n_OPEN_TAG 9007 -#define n_CLOSE_TAG 9008 -#define n_USE_LIST 9009 -#define n_USE 9010 -#define n_CONSTANT_DECLARATION_LIST 9011 -#define n_CONSTANT_DECLARATION 9012 -#define n_STRING 9013 -#define n_LABEL 9014 -#define n_CONDITION_LIST 9015 -#define n_CONTROL_CONDITION 9016 -#define n_IF 9017 -#define n_ELSEIF 9018 -#define n_ELSE 9019 -#define n_WHILE 9020 -#define n_DO_WHILE 9021 -#define n_FOR 9022 -#define n_FOR_EXPRESSION 9023 -#define n_SWITCH 9024 -#define n_BREAK 9025 -#define n_CONTINUE 9026 -#define n_RETURN 9027 -#define n_GLOBAL_DECLARATION_LIST 9028 -#define n_GLOBAL_DECLARATION 9029 -#define n_STATIC_DECLARATION_LIST 9030 -#define n_STATIC_DECLARATION 9031 -#define n_ECHO_LIST 9032 -#define n_ECHO 9033 -#define n_INLINE_HTML 9034 -#define n_UNSET_LIST 9035 -#define n_UNSET 9036 -#define n_FOREACH 9037 -#define n_FOREACH_EXPRESSION 9038 -#define n_THROW 9039 -#define n_GOTO 9040 -#define n_TRY 9041 -#define n_CATCH_LIST 9042 -#define n_CATCH 9043 -#define n_DECLARE 9044 -#define n_DECLARE_DECLARATION_LIST 9045 -#define n_DECLARE_DECLARATION 9046 -#define n_VARIABLE 9047 -#define n_REFERENCE 9048 -#define n_VARIABLE_REFERENCE 9049 -#define n_FUNCTION_DECLARATION 9050 -#define n_CLASS_DECLARATION 9051 -#define n_CLASS_ATTRIBUTES 9052 -#define n_EXTENDS 9053 -#define n_EXTENDS_LIST 9054 -#define n_IMPLEMENTS_LIST 9055 -#define n_INTERFACE_DECLARATION 9056 -#define n_CASE 9057 -#define n_DEFAULT 9058 -#define n_DECLARATION_PARAMETER_LIST 9059 -#define n_DECLARATION_PARAMETER 9060 -#define n_TYPE_NAME 9061 -#define n_VARIABLE_VARIABLE 9062 -#define n_CLASS_MEMBER_DECLARATION_LIST 9063 -#define n_CLASS_MEMBER_DECLARATION 9064 -#define n_CLASS_CONSTANT_DECLARATION_LIST 9065 -#define n_CLASS_CONSTANT_DECLARATION 9066 -#define n_METHOD_DECLARATION 9067 -#define n_METHOD_MODIFIER_LIST 9068 -#define n_FUNCTION_MODIFIER_LIST 9069 -#define n_CLASS_MEMBER_MODIFIER_LIST 9070 -#define n_EXPRESSION_LIST 9071 -#define n_LIST 9072 -#define n_ASSIGNMENT 9073 -#define n_NEW 9074 -#define n_UNARY_PREFIX_EXPRESSION 9075 -#define n_UNARY_POSTFIX_EXPRESSION 9076 -#define n_BINARY_EXPRESSION 9077 -#define n_TERNARY_EXPRESSION 9078 -#define n_CAST_EXPRESSION 9079 -#define n_CAST 9080 -#define n_OPERATOR 9081 -#define n_ARRAY_LITERAL 9082 -#define n_EXIT_EXPRESSION 9083 -#define n_BACKTICKS_EXPRESSION 9084 -#define n_LEXICAL_VARIABLE_LIST 9085 -#define n_NUMERIC_SCALAR 9086 -#define n_STRING_SCALAR 9087 -#define n_MAGIC_SCALAR 9088 -#define n_CLASS_STATIC_ACCESS 9089 -#define n_CLASS_NAME 9090 -#define n_MAGIC_CLASS_KEYWORD 9091 -#define n_OBJECT_PROPERTY_ACCESS 9092 -#define n_ARRAY_VALUE_LIST 9093 -#define n_ARRAY_VALUE 9094 -#define n_CALL_PARAMETER_LIST 9095 -#define n_VARIABLE_EXPRESSION 9096 -#define n_INCLUDE_FILE 9097 -#define n_HEREDOC 9098 -#define n_FUNCTION_CALL 9099 -#define n_INDEX_ACCESS 9100 -#define n_ASSIGNMENT_LIST 9101 -#define n_METHOD_CALL 9102 -#define n_XHP_TAG 9103 -#define n_XHP_TAG_OPEN 9104 -#define n_XHP_TAG_CLOSE 9105 -#define n_XHP_TEXT 9106 -#define n_XHP_EXPRESSION 9107 -#define n_XHP_ATTRIBUTE_LIST 9108 -#define n_XHP_ATTRIBUTE 9109 -#define n_XHP_LITERAL 9110 -#define n_XHP_ATTRIBUTE_LITERAL 9111 -#define n_XHP_ATTRIBUTE_EXPRESSION 9112 -#define n_XHP_NODE_LIST 9113 -#define n_XHP_ENTITY 9114 -#define n_CONCATENATION_LIST 9115 -#define n_PARENTHETICAL_EXPRESSION 9116 diff --git a/support/xhpast/parser.y b/support/xhpast/parser.y deleted file mode 100755 index 2a5e2679..00000000 --- a/support/xhpast/parser.y +++ /dev/null @@ -1,2915 +0,0 @@ -/* - * Copyright 2011 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -%{ -#include "ast.hpp" -#include "node_names.hpp" -// PHP's if/else rules use right reduction rather than left reduction which -// means while parsing nested if/else's the stack grows until it the last -// statement is read. This is annoying, particularly because of a quirk in -// bison. -// http://www.gnu.org/software/bison/manual/html_node/Memory-Management.html -// Apparently if you compile a bison parser with g++ it can no longer grow -// the stack. The work around is to just make your initial stack ridiculously -// large. Unfortunately that increases memory usage while parsing which is -// dumb. Anyway, putting a TODO here to fix PHP's if/else grammar. -#define YYINITDEPTH 500 -%} - -%{ -#undef yyextra -#define yyextra static_cast(xhpastget_extra(yyscanner)) -#undef yylineno -#define yylineno yyextra->first_lineno -#define push_state(s) xhp_new_push_state(s, (struct yyguts_t*) yyscanner) -#define pop_state() xhp_new_pop_state((struct yyguts_t*) yyscanner) -#define set_state(s) xhp_set_state(s, (struct yyguts_t*) yyscanner) - -#define NNEW(t) \ - (new xhpast::Node(t)) - -#define NTYPE(n, type) \ - ((n)->setType(type)) - -#define NMORE(n, end) \ - ((n)->setEnd(end)) - -#define NSPAN(n, type, end) \ - (NMORE(NTYPE((n), type), end)) - -#define NLMORE(n, begin) \ - ((n)->setBegin(begin)) - -#define NEXPAND(l, n, r) \ - ((n)->setBegin(l)->setEnd(r)) - -using namespace std; - -static void yyerror(void* yyscanner, void* _, const char* error) { - if (yyextra->terminated) { - return; - } - yyextra->terminated = true; - yyextra->error = error; -} - -static void replacestr(string &source, const string &find, const string &rep) { - size_t j; - while ((j = source.find(find)) != std::string::npos) { - source.replace(j, find.length(), rep); - } -} - -%} - -%expect 9 -// 2: PHP's if/else grammar -// 7: expr '[' dim_offset ']' -- shift will default to first grammar -%name-prefix = "xhpast" -%pure-parser -%parse-param { void* yyscanner } -%parse-param { xhpast::Node** root } -%lex-param { void* yyscanner } -%error-verbose - -%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE -%left ',' -%left T_LOGICAL_OR -%left T_LOGICAL_XOR -%left T_LOGICAL_AND -%right T_PRINT -%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL -%left '?' ':' -%left T_BOOLEAN_OR -%left T_BOOLEAN_AND -%left '|' -%left '^' -%left '&' -%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL -%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL -%left T_SL T_SR -%left '+' '-' '.' -%left '*' '/' '%' -%right '!' -%nonassoc T_INSTANCEOF -%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_UNICODE_CAST T_BINARY_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@' -%right '[' -%nonassoc T_NEW T_CLONE -%token T_EXIT -%token T_IF -%left T_ELSEIF -%left T_ELSE -%left T_ENDIF - -%token T_LNUMBER -%token T_DNUMBER -%token T_STRING -%token T_STRING_VARNAME /* unused in XHP: `foo` in `"$foo"` */ -%token T_VARIABLE -%token T_NUM_STRING /* unused in XHP: `0` in `"$foo[0]"` */ -%token T_INLINE_HTML -%token T_CHARACTER /* unused in vanilla PHP */ -%token T_BAD_CHARACTER /* unused in vanilla PHP */ -%token T_ENCAPSED_AND_WHITESPACE /* unused in XHP: ` ` in `" "` */ -%token T_CONSTANT_ENCAPSED_STRING /* overloaded in XHP; replaces '"' encaps_list '"' */ -%token T_BACKTICKS_EXPR /* new in XHP; replaces '`' backticks_expr '`' */ -%token T_ECHO -%token T_DO -%token T_WHILE -%token T_ENDWHILE -%token T_FOR -%token T_ENDFOR -%token T_FOREACH -%token T_ENDFOREACH -%token T_DECLARE -%token T_ENDDECLARE -%token T_AS -%token T_SWITCH -%token T_ENDSWITCH -%token T_CASE -%token T_DEFAULT -%token T_BREAK -%token T_CONTINUE -%token T_GOTO -%token T_FUNCTION -%token T_CONST -%token T_RETURN -%token T_TRY -%token T_CATCH -%token T_THROW -%token T_USE -%token T_GLOBAL -%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC -%token T_VAR -%token T_UNSET -%token T_ISSET -%token T_EMPTY -%token T_HALT_COMPILER -%token T_CLASS -%token T_INTERFACE -%token T_EXTENDS -%token T_IMPLEMENTS -%token T_OBJECT_OPERATOR -%token T_DOUBLE_ARROW -%token T_LIST -%token T_ARRAY -%token T_CLASS_C -%token T_METHOD_C -%token T_FUNC_C -%token T_LINE -%token T_FILE -%token T_COMMENT -%token T_DOC_COMMENT -%token T_OPEN_TAG -%token T_OPEN_TAG_WITH_ECHO -%token T_OPEN_TAG_FAKE -%token T_CLOSE_TAG -%token T_WHITESPACE -%token T_START_HEREDOC /* unused in XHP; replaced with T_HEREDOC */ -%token T_END_HEREDOC /* unused in XHP; replaced with T_HEREDOC */ -%token T_HEREDOC /* new in XHP; replaces start_heredoc encaps_list T_END_HEREDOC */ -%token T_DOLLAR_OPEN_CURLY_BRACES /* unused in XHP: `${` in `"${foo}"` */ -%token T_CURLY_OPEN /* unused in XHP: `{$` in `"{$foo}"` */ -%token T_PAAMAYIM_NEKUDOTAYIM -%token T_BINARY_DOUBLE /* unsused in XHP: `b"` in `b"foo"` */ -%token T_BINARY_HEREDOC /* unsused in XHP: `b<<<` in `b<<appendChild($1); - } -; - -top_statement_list: - top_statement_list top_statement { - $$ = $1->appendChild($2); - } -| /* empty */ { - $$ = NNEW(n_STATEMENT_LIST); - } -; - -namespace_name: - T_STRING { - $$ = NTYPE($1, n_SYMBOL_NAME); - } -| namespace_name T_NS_SEPARATOR T_STRING { - $$ = NMORE($1, $3); - } -; - -top_statement: - statement -| function_declaration_statement -| class_declaration_statement -| T_HALT_COMPILER '(' ')' ';' { - $1 = NSPAN($1, n_HALT_COMPILER, $3); - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $4); - } -| T_NAMESPACE namespace_name ';' { - NSPAN($1, n_NAMESPACE, $2); - $1->appendChild($2); - $1->appendChild(NNEW(n_EMPTY)); - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - } -| T_NAMESPACE namespace_name '{' top_statement_list '}' { - NSPAN($1, n_NAMESPACE, $5); - $1->appendChild($2); - NMORE($4, $5); - NLMORE($4, $3); - $1->appendChild($4); - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_NAMESPACE '{' top_statement_list '}' { - NSPAN($1, n_NAMESPACE, $4); - $1->appendChild(NNEW(n_EMPTY)); - NMORE($3, $4); - NLMORE($3, $2); - $1->appendChild($3); - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_USE use_declarations ';' { - NSPAN($1, n_USE, $2); - $1->appendChild($2); - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - } -| constant_declaration ';' { - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $2); - } -; - -use_declarations: - use_declarations ',' use_declaration { - $$ = $1->appendChild($3); - } -| use_declaration { - $$ = NNEW(n_USE_LIST); - $$->appendChild($1); - } -; - -use_declaration: - namespace_name { - $$ = NNEW(n_USE); - $$->appendChild($1); - $$->appendChild(NNEW(n_EMPTY)); - } -| namespace_name T_AS T_STRING { - $$ = NNEW(n_USE); - $$->appendChild($1); - NTYPE($3, n_STRING); - $$->appendChild($3); - } -| T_NS_SEPARATOR namespace_name { - $$ = NNEW(n_USE); - NLMORE($2, $1); - $$->appendChild($2); - $$->appendChild(NNEW(n_EMPTY)); - } -| T_NS_SEPARATOR namespace_name T_AS T_STRING { - $$ = NNEW(n_USE); - NLMORE($2, $1); - $$->appendChild($2); - NTYPE($4, n_STRING); - $$->appendChild($4); - } -; - -constant_declaration: - constant_declaration ',' T_STRING '=' static_scalar { - NMORE($$, $5); - $$->appendChild( - NNEW(n_CONSTANT_DECLARATION) - ->appendChild(NTYPE($3, n_STRING)) - ->appendChild($5)); - } -| T_CONST T_STRING '=' static_scalar { - NSPAN($$, n_CONSTANT_DECLARATION_LIST, $4); - $$->appendChild( - NNEW(n_CONSTANT_DECLARATION) - ->appendChild(NTYPE($2, n_STRING)) - ->appendChild($4)); - } -; - -inner_statement_list: - inner_statement_list inner_statement { - $$ = $1->appendChild($2); - } -| /* empty */ { - $$ = NNEW(n_STATEMENT_LIST); - } -; - -inner_statement: - statement -| function_declaration_statement -| class_declaration_statement -| T_HALT_COMPILER '(' ')' ';' { - $1 = NSPAN($1, n_HALT_COMPILER, $3); - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $4); - } -; - -statement: - unticked_statement -| T_STRING ':' { - NTYPE($1, n_STRING); - $$ = NNEW(n_LABEL); - $$->appendChild($1); - NMORE($$, $2); - } -| T_OPEN_TAG { - $$ = NTYPE($1, n_OPEN_TAG); - } -| T_OPEN_TAG_WITH_ECHO { - $$ = NTYPE($1, n_OPEN_TAG); - } -| T_CLOSE_TAG { - $$ = NTYPE($1, n_CLOSE_TAG); - } -; - -unticked_statement: - '{' inner_statement_list '}' { - NMORE($2, $3); - NLMORE($2, $1); - $$ = $2; - } -| T_IF '(' expr ')' statement elseif_list else_single { - $$ = NNEW(n_CONDITION_LIST); - - $1 = NTYPE($1, n_IF); - $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); - $1->appendChild($5); - - $$->appendChild($1); - $$->appendChildren($6); - - // Hacks: merge a list of if (x) { } else if (y) { } into a single condition - // list instead of a condition tree. - - if ($7->type == n_EMPTY) { - // Ignore. - } else if ($7->type == n_ELSE) { - xhpast::Node *stype = $7->firstChild()->firstChild(); - if (stype && stype->type == n_CONDITION_LIST) { - NTYPE(stype->firstChild(), n_ELSEIF); - stype->firstChild()->l_tok = $7->l_tok; - $$->appendChildren(stype); - } else { - $$->appendChild($7); - } - } else { - $$->appendChild($7); - } - - $$ = NNEW(n_STATEMENT)->appendChild($$); - } -| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';' { - - $$ = NNEW(n_CONDITION_LIST); - NTYPE($1, n_IF); - $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); - $1->appendChild($6); - - $$->appendChild($1); - $$->appendChildren($7); - $$->appendChild($8); - NMORE($$, $9); - - $$ = NNEW(n_STATEMENT)->appendChild($$); - NMORE($$, $10); - } -| T_WHILE '(' expr ')' while_statement { - NTYPE($1, n_WHILE); - $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); - $1->appendChild($5); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_DO statement T_WHILE '(' expr ')' ';' { - NTYPE($1, n_DO_WHILE); - $1->appendChild($2); - $1->appendChild(NSPAN($4, n_CONTROL_CONDITION, $6)->appendChild($5)); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $7); - } -| T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement { - NTYPE($1, n_FOR); - - NSPAN($2, n_FOR_EXPRESSION, $8) - ->appendChild($3) - ->appendChild($5) - ->appendChild($7); - - $1->appendChild($2); - $1->appendChild($9); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_SWITCH '(' expr ')' switch_case_list { - NTYPE($1, n_SWITCH); - $1->appendChild(NSPAN($2, n_CONTROL_CONDITION, $4)->appendChild($3)); - $1->appendChild($5); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_BREAK ';' { - NTYPE($1, n_BREAK); - $1->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $2); - } -| T_BREAK expr ';' { - NTYPE($1, n_BREAK); - $1->appendChild($2); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - } -| T_CONTINUE ';' { - NTYPE($1, n_CONTINUE); - $1->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $2); - } -| T_CONTINUE expr ';' { - NTYPE($1, n_CONTINUE); - $1->appendChild($2); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - } -| T_RETURN ';' { - NTYPE($1, n_RETURN); - $1->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $2); - } -| T_RETURN expr_without_variable ';' { - NTYPE($1, n_RETURN); - $1->appendChild($2); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - } -| T_RETURN variable ';' { - NTYPE($1, n_RETURN); - $1->appendChild($2); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - } -| T_GLOBAL global_var_list ';' { - NLMORE($2, $1); - $$ = NNEW(n_STATEMENT)->appendChild($2); - NMORE($$, $3); - } -| T_STATIC static_var_list ';' { - NLMORE($2, $1); - $$ = NNEW(n_STATEMENT)->appendChild($2); - NMORE($$, $3); - } -| T_ECHO echo_expr_list ';' { - NLMORE($2, $1); - $$ = NNEW(n_STATEMENT)->appendChild($2); - NMORE($$, $3); - } -| T_INLINE_HTML { - NTYPE($1, n_INLINE_HTML); - $$ = $1; - } -| expr ';' { - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $2); - } -| T_UNSET '(' unset_variables ')' ';' { - NMORE($3, $4); - NLMORE($3, $1); - $$ = NNEW(n_STATEMENT)->appendChild($3); - NMORE($$, $5); - } -| T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement { - NTYPE($1, n_FOREACH); - NSPAN($2, n_FOREACH_EXPRESSION, $7); - $2->appendChild($3); - if ($6->type == n_EMPTY) { - $2->appendChild($6); - $2->appendChild($5); - } else { - $2->appendChild($5); - $2->appendChild($6); - } - $1->appendChild($2); - - $1->appendChild($8); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_FOREACH '(' expr_without_variable T_AS variable foreach_optional_arg ')' foreach_statement { - NTYPE($1, n_FOREACH); - NSPAN($2, n_FOREACH_EXPRESSION, $7); - $2->appendChild($3); - if ($6->type == n_EMPTY) { - $2->appendChild($6); - $2->appendChild($5); - } else { - $2->appendChild($5); - $2->appendChild($6); - } - $1->appendChild($2); - $1->appendChild($8); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_DECLARE '(' declare_list ')' declare_statement { - NTYPE($1, n_DECLARE); - $1->appendChild($3); - $1->appendChild($5); - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| ';' /* empty statement */ { - $$ = NNEW(n_STATEMENT)->appendChild(NNEW(n_EMPTY)); - NMORE($$, $1); - } -| T_TRY '{' inner_statement_list '}' T_CATCH '(' fully_qualified_class_name T_VARIABLE ')' '{' inner_statement_list '}' additional_catches { - NTYPE($1, n_TRY); - $1->appendChild($3); - - NTYPE($5, n_CATCH); - $5->appendChild($7); - $5->appendChild(NTYPE($8, n_VARIABLE)); - $5->appendChild($11); - - $1->appendChild(NNEW(n_CATCH_LIST)->appendChild($5)->appendChildren($13)); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -| T_THROW expr ';' { - NTYPE($1, n_THROW); - $1->appendChild($2); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - - } -| T_GOTO T_STRING ';' { - NTYPE($1, n_GOTO); - NTYPE($2, n_STRING); - $1->appendChild($2); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $3); - } -; - -additional_catches: - non_empty_additional_catches -| /* empty */ { - $$ = NNEW(n_EMPTY); - } -; - -non_empty_additional_catches: - additional_catch { - $$ = NNEW(n_CATCH_LIST); - $$->appendChild($1); - } -| non_empty_additional_catches additional_catch { - $1->appendChild($2); - $$ = $1; - } -; - -additional_catch: - T_CATCH '(' fully_qualified_class_name T_VARIABLE ')' '{' inner_statement_list '}' { - NTYPE($1, n_CATCH); - $1->appendChild($3); - $1->appendChild(NTYPE($4, n_VARIABLE)); - $1->appendChild($7); - NMORE($1, $8); - $$ = $1; - } -; - -unset_variables: - unset_variable { - $$ = NNEW(n_UNSET_LIST); - $$->appendChild($1); - } -| unset_variables ',' unset_variable { - $1->appendChild($3); - $$ = $1; - } -; - -unset_variable: - variable -; - -function_declaration_statement: - unticked_function_declaration_statement -; - -class_declaration_statement: - unticked_class_declaration_statement -; - -is_reference: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| '&' { - $$ = NTYPE($1, n_REFERENCE); - } -; - -unticked_function_declaration_statement: - function is_reference T_STRING '(' parameter_list ')' '{' inner_statement_list '}' { - NSPAN($1, n_FUNCTION_DECLARATION, $9); - $1->appendChild(NNEW(n_EMPTY)); - $1->appendChild($2); - $1->appendChild(NTYPE($3, n_STRING)); - $1->appendChild(NEXPAND($4, $5, $6)); - $$->appendChild(NNEW(n_EMPTY)); - $1->appendChild($8); - - $$ = NNEW(n_STATEMENT)->appendChild($1); - } -; - -unticked_class_declaration_statement: - class_entry_type T_STRING extends_from implements_list '{' class_statement_list '}' { - $$ = NNEW(n_CLASS_DECLARATION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_CLASS_NAME)); - $$->appendChild($3); - $$->appendChild($4); - $$->appendChild($6); - NMORE($$, $7); - - $$ = NNEW(n_STATEMENT)->appendChild($$); - } -| interface_entry T_STRING interface_extends_list '{' class_statement_list '}' { - $$ = NNEW(n_INTERFACE_DECLARATION); - $$->appendChild(NNEW(n_EMPTY)); - NLMORE($$, $1); - $$->appendChild(NTYPE($2, n_CLASS_NAME)); - $$->appendChild($3); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild($5); - NMORE($$, $6); - - $$ = NNEW(n_STATEMENT)->appendChild($$); - } -; - -class_entry_type: - T_CLASS { - NTYPE($1, n_CLASS_ATTRIBUTES); - $1->appendChild(NNEW(n_EMPTY)); - $$ = $1; - } -| T_ABSTRACT T_CLASS { - NTYPE($2, n_CLASS_ATTRIBUTES); - NLMORE($2, $1); - $2->appendChild(NTYPE($1, n_STRING)); - - $$ = $1; - } -| T_FINAL T_CLASS { - NTYPE($2, n_CLASS_ATTRIBUTES); - NLMORE($2, $1); - $2->appendChild(NTYPE($1, n_STRING)); - - $$ = $1; - } -; - -extends_from: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| T_EXTENDS fully_qualified_class_name { - $$ = NTYPE($1, n_EXTENDS_LIST)->appendChild($2); - } -; - -interface_entry: - T_INTERFACE -; - -interface_extends_list: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| T_EXTENDS interface_list { - NTYPE($1, n_EXTENDS_LIST); - $1->appendChildren($2); - $$ = $1; - } -; - -implements_list: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| T_IMPLEMENTS interface_list { - NTYPE($1, n_IMPLEMENTS_LIST); - $1->appendChildren($2); - $$ = $1; - } -; - -interface_list: - fully_qualified_class_name { - $$ = NNEW(n_IMPLEMENTS_LIST)->appendChild($1); - } -| interface_list ',' fully_qualified_class_name { - $$ = $1->appendChild($3); - } -; - -foreach_optional_arg: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| T_DOUBLE_ARROW foreach_variable { - $$ = $2; - } -; - -foreach_variable: - variable -| '&' variable { - NTYPE($1, n_VARIABLE_REFERENCE); - $1->appendChild($2); - $$ = $1; - } -; - -for_statement: - statement -| ':' inner_statement_list T_ENDFOR ';' { - NLMORE($2, $1); - NMORE($2, $4); - $$ = $2; - } -; - -foreach_statement: - statement -| ':' inner_statement_list T_ENDFOREACH ';' { - NLMORE($2, $1); - NMORE($2, $4); - $$ = $2; - } -; - -declare_statement: - statement -| ':' inner_statement_list T_ENDDECLARE ';' { - NLMORE($2, $1); - NMORE($2, $4); - $$ = $2; - } -; - -declare_list: - T_STRING '=' static_scalar { - $$ = NNEW(n_DECLARE_DECLARATION); - $$->appendChild(NTYPE($1, n_STRING)); - $$->appendChild($3); - $$ = NNEW(n_DECLARE_DECLARATION_LIST)->appendChild($$); - } -| declare_list ',' T_STRING '=' static_scalar { - $$ = NNEW(n_DECLARE_DECLARATION); - $$->appendChild(NTYPE($3, n_STRING)); - $$->appendChild($5); - - $1->appendChild($$); - $$ = $1; - } -; - -switch_case_list: - '{' case_list '}' { - NMORE($2, $3); - NLMORE($2, $1); - $$ = $2; - } -| '{' ';' case_list '}' { - // ...why does this rule exist? - - NTYPE($2, n_STATEMENT); - $1->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_STATEMENT_LIST)->appendChild($2); - $$->appendChildren($3); - NMORE($$, $4); - NLMORE($$, $1); - } -| ':' case_list T_ENDSWITCH ';' { - NMORE($2, $4); - NLMORE($2, $1); - $$ = $2; - } -| ':' ';' case_list T_ENDSWITCH ';' { - NTYPE($2, n_STATEMENT); - $1->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_STATEMENT_LIST)->appendChild($2); - $$->appendChildren($3); - NMORE($$, $5); - NLMORE($$, $1); - } -; - -case_list: - /* empty */ { - $$ = NNEW(n_STATEMENT_LIST); - } -| case_list T_CASE expr case_separator inner_statement_list { - NTYPE($2, n_CASE); - $2->appendChild($3); - $2->appendChild($5); - - $1->appendChild($2); - $$ = $1; - } -| case_list T_DEFAULT case_separator inner_statement_list { - NTYPE($2, n_DEFAULT); - $2->appendChild($4); - - $1->appendChild($2); - $$ = $1; - } -; - -case_separator: - ':' -| ';' -; - -while_statement: - statement -| ':' inner_statement_list T_ENDWHILE ';' { - NMORE($2, $4); - NLMORE($2, $1); - $$ = $2; - } -; - -elseif_list: - /* empty */ { - $$ = NNEW(n_CONDITION_LIST); - } -| elseif_list T_ELSEIF '(' expr ')' statement { - NTYPE($2, n_ELSEIF); - $2->appendChild(NSPAN($3, n_CONTROL_CONDITION, $5)->appendChild($4)); - $2->appendChild($6); - - $$ = $1->appendChild($2); - } -; - -new_elseif_list: - /* empty */ { - $$ = NNEW(n_CONDITION_LIST); - } -| new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list { - NTYPE($2, n_ELSEIF); - $2->appendChild($4); - $2->appendChild($7); - - $$ = $1->appendChild($2); - } -; - -else_single: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| T_ELSE statement { - NTYPE($1, n_ELSE); - $1->appendChild($2); - $$ = $1; - } -; - -new_else_single: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| T_ELSE ':' inner_statement_list { - NTYPE($1, n_ELSE); - $1->appendChild($3); - $$ = $1; - } -; - -parameter_list: - non_empty_parameter_list -| /* empty */ { - $$ = NNEW(n_DECLARATION_PARAMETER_LIST); - } -; - -non_empty_parameter_list: - optional_class_type T_VARIABLE { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_VARIABLE)); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); - } -| optional_class_type '&' T_VARIABLE { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_VARIABLE_REFERENCE)); - $2->appendChild(NTYPE($3, n_VARIABLE)); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); - } -| optional_class_type '&' T_VARIABLE '=' static_scalar { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_VARIABLE_REFERENCE)); - $2->appendChild(NTYPE($3, n_VARIABLE)); - $$->appendChild($5); - - $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); - } -| optional_class_type T_VARIABLE '=' static_scalar { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_VARIABLE)); - $$->appendChild($4); - - $$ = NNEW(n_DECLARATION_PARAMETER_LIST)->appendChild($$); - } -| non_empty_parameter_list ',' optional_class_type T_VARIABLE { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($3); - $$->appendChild(NTYPE($4, n_VARIABLE)); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = $1->appendChild($$); - } -| non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($3); - $$->appendChild(NTYPE($4, n_VARIABLE_REFERENCE)); - $4->appendChild(NTYPE($5, n_VARIABLE)); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = $1->appendChild($$); - } -| non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '=' static_scalar { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($3); - $$->appendChild(NTYPE($4, n_VARIABLE_REFERENCE)); - $4->appendChild(NTYPE($5, n_VARIABLE)); - $$->appendChild($7); - - $$ = $1->appendChild($$); - } -| non_empty_parameter_list ',' optional_class_type T_VARIABLE '=' static_scalar { - $$ = NNEW(n_DECLARATION_PARAMETER); - $$->appendChild($3); - $$->appendChild(NTYPE($4, n_VARIABLE)); - $$->appendChild($6); - - $$ = $1->appendChild($$); - } -; - -optional_class_type: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| fully_qualified_class_name { - $$ = $1; - } -| T_ARRAY { - $$ = NTYPE($1, n_TYPE_NAME); - } -; - -function_call_parameter_list: - non_empty_function_call_parameter_list -| /* empty */ { - $$ = NNEW(n_CALL_PARAMETER_LIST); - } -; - -non_empty_function_call_parameter_list: - expr_without_variable { - $$ = NNEW(n_CALL_PARAMETER_LIST)->appendChild($1); - } -| variable { - $$ = NNEW(n_CALL_PARAMETER_LIST)->appendChild($1); - } -| '&' w_variable { - NTYPE($1, n_VARIABLE_REFERENCE); - $1->appendChild($2); - $$ = NNEW(n_CALL_PARAMETER_LIST)->appendChild($1); - } -| non_empty_function_call_parameter_list ',' expr_without_variable { - $$ = $1->appendChild($3); - } -| non_empty_function_call_parameter_list ',' variable { - $$ = $1->appendChild($3); - } -| non_empty_function_call_parameter_list ',' '&' w_variable { - $$ = $1->appendChild($3); - } -; - -global_var_list: - global_var_list ',' global_var { - $1->appendChild($3); - $$ = $1; - } -| global_var { - $$ = NNEW(n_GLOBAL_DECLARATION_LIST); - $$->appendChild($1); - } -; - -global_var: - T_VARIABLE { - $$ = NTYPE($1, n_VARIABLE); - } -| '$' r_variable { - $$ = NTYPE($1, n_VARIABLE_VARIABLE); - } -| '$' '{' expr '}' { - $$ = NTYPE($1, n_VARIABLE_VARIABLE); - } -; - -static_var_list: - static_var_list ',' T_VARIABLE { - NTYPE($3, n_VARIABLE); - $$ = NNEW(n_STATIC_DECLARATION); - $$->appendChild($3); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = $1->appendChild($$); - } -| static_var_list ',' T_VARIABLE '=' static_scalar { - NTYPE($3, n_VARIABLE); - $$ = NNEW(n_STATIC_DECLARATION); - $$->appendChild($3); - $$->appendChild($5); - - $$ = $1->appendChild($$); - } -| T_VARIABLE { - NTYPE($1, n_VARIABLE); - $$ = NNEW(n_STATIC_DECLARATION); - $$->appendChild($1); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_STATIC_DECLARATION_LIST)->appendChild($$); - } -| T_VARIABLE '=' static_scalar { - NTYPE($1, n_VARIABLE); - $$ = NNEW(n_STATIC_DECLARATION); - $$->appendChild($1); - $$->appendChild($3); - - $$ = NNEW(n_STATIC_DECLARATION_LIST)->appendChild($$); - } -; - -class_statement_list: - class_statement_list class_statement { - $$ = $1->appendChild($2); - } -| /* empty */ { - $$ = NNEW(n_STATEMENT_LIST); - } -; - -class_statement: - variable_modifiers class_variable_declaration ';' { - $$ = NNEW(n_CLASS_MEMBER_DECLARATION_LIST); - $$->appendChild($1); - $$->appendChildren($2); - - $$ = NNEW(n_STATEMENT)->appendChild($$); - NMORE($$, $3); - } -| class_constant_declaration ';' { - $$ = NNEW(n_STATEMENT)->appendChild($1); - NMORE($$, $2); - } -| method_modifiers function { - yyextra->old_expecting_xhp_class_statements = yyextra->expecting_xhp_class_statements; - yyextra->expecting_xhp_class_statements = false; - } is_reference T_STRING '(' parameter_list ')' method_body { - yyextra->expecting_xhp_class_statements = yyextra->old_expecting_xhp_class_statements; - - $$ = NNEW(n_METHOD_DECLARATION); - $$->appendChild($1); - $$->appendChild($4); - $$->appendChild(NTYPE($5, n_STRING)); - $$->appendChild(NEXPAND($6, $7, $8)); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild($9); - - $$ = NNEW(n_STATEMENT)->appendChild($$); - } -; - -method_body: - ';' /* abstract method */ { - $$ = NNEW(n_EMPTY); - } -| '{' inner_statement_list '}' { - NMORE($2, $3); - NLMORE($2, $1); - $$ = $2; - } -; - -variable_modifiers: - non_empty_member_modifiers -| T_VAR { - $$ = NNEW(n_CLASS_MEMBER_MODIFIER_LIST); - $$->appendChild(NTYPE($1, n_STRING)); - } -; - -method_modifiers: - /* empty */ { - $$ = NNEW(n_METHOD_MODIFIER_LIST); - } -| non_empty_member_modifiers { - NTYPE($1, n_METHOD_MODIFIER_LIST); - $$ = $1; - } -; - -non_empty_member_modifiers: - member_modifier { - $$ = NNEW(n_CLASS_MEMBER_MODIFIER_LIST); - $$->appendChild(NTYPE($1, n_STRING)); - } -| non_empty_member_modifiers member_modifier { - $$ = $1->appendChild(NTYPE($2, n_STRING)); - } -; - -member_modifier: - T_PUBLIC -| T_PROTECTED -| T_PRIVATE -| T_STATIC -| T_ABSTRACT -| T_FINAL -; - -class_variable_declaration: - class_variable_declaration ',' T_VARIABLE { - $$ = NNEW(n_CLASS_MEMBER_DECLARATION); - $$->appendChild(NTYPE($3, n_VARIABLE)); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = $1->appendChild($$); - } -| class_variable_declaration ',' T_VARIABLE '=' static_scalar { - $$ = NNEW(n_CLASS_MEMBER_DECLARATION); - $$->appendChild(NTYPE($3, n_VARIABLE)); - $$->appendChild($5); - - $$ = $1->appendChild($$); - } -| T_VARIABLE { - $$ = NNEW(n_CLASS_MEMBER_DECLARATION); - $$->appendChild(NTYPE($1, n_VARIABLE)); - $$->appendChild(NNEW(n_EMPTY)); - - $$ = NNEW(n_CLASS_MEMBER_DECLARATION_LIST)->appendChild($$); - } -| T_VARIABLE '=' static_scalar { - $$ = NNEW(n_CLASS_MEMBER_DECLARATION); - $$->appendChild(NTYPE($1, n_VARIABLE)); - $$->appendChild($3); - - $$ = NNEW(n_CLASS_MEMBER_DECLARATION_LIST)->appendChild($$); - } -; - -class_constant_declaration: - class_constant_declaration ',' T_STRING '=' static_scalar { - $$ = NNEW(n_CLASS_CONSTANT_DECLARATION); - $$->appendChild(NTYPE($3, n_STRING)); - $$->appendChild($5); - - $1->appendChild($$); - - $$ = $1; - } -| T_CONST T_STRING '=' static_scalar { - NTYPE($1, n_CLASS_CONSTANT_DECLARATION_LIST); - $$ = NNEW(n_CLASS_CONSTANT_DECLARATION); - $$->appendChild(NTYPE($2, n_STRING)); - $$->appendChild($4); - $1->appendChild($$); - - $$ = $1; - } -; - -echo_expr_list: - echo_expr_list ',' expr { - $1->appendChild($3); - } -| expr { - $$ = NNEW(n_ECHO_LIST); - $$->appendChild($1); - } -; - -for_expr: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| non_empty_for_expr -; - - -non_empty_for_expr: - non_empty_for_expr ',' expr { - $1->appendChild($3); - } -| expr { - $$ = NNEW(n_EXPRESSION_LIST); - $$->appendChild($1); - } -; - -expr_without_variable: - T_LIST '(' assignment_list ')' '=' expr { - NTYPE($1, n_LIST); - $1->appendChild(NEXPAND($2, $3, $4)); - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($5, n_OPERATOR)); - $$->appendChild($6); - } -| variable '=' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable '=' '&' variable { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - - NTYPE($3, n_VARIABLE_REFERENCE); - $3->appendChild($4); - - $$->appendChild($3); - } -| variable '=' '&' T_NEW class_name_reference ctor_arguments { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - - NTYPE($4, n_NEW); - $4->appendChild($5); - $4->appendChild($6); - - NTYPE($3, n_VARIABLE_REFERENCE); - $3->appendChild($4); - - $$->appendChild($3); - } -| T_NEW class_name_reference ctor_arguments { - NTYPE($1, n_NEW); - $1->appendChild($2); - $1->appendChild($3); - $$ = $1; - } -| T_CLONE expr { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| variable T_PLUS_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_MINUS_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_MUL_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_DIV_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_CONCAT_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_MOD_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_AND_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_OR_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_XOR_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_SL_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| variable T_SR_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| rw_variable T_INC { - $$ = NNEW(n_UNARY_POSTFIX_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - } -| T_INC rw_variable { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| rw_variable T_DEC { - $$ = NNEW(n_UNARY_POSTFIX_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - } -| T_DEC rw_variable { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| expr T_BOOLEAN_OR expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_BOOLEAN_AND expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_LOGICAL_OR expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_LOGICAL_AND expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_LOGICAL_XOR expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '|' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '&' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '^' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '.' expr { - - /* The concatenation operator generates n_CONCATENATION_LIST instead of - n_BINARY_EXPRESSION because we tend to run into stack depth issues in a - lot of real-world cases otherwise (e.g., in PHP and JSON decoders). */ - - if ($1->type == n_CONCATENATION_LIST && $3->type == n_CONCATENATION_LIST) { - $1->appendChild(NTYPE($2, n_OPERATOR)); - $1->appendChildren($3); - $$ = $1; - } else if ($1->type == n_CONCATENATION_LIST) { - $1->appendChild(NTYPE($2, n_OPERATOR)); - $1->appendChild($3); - $$ = $1; - } else if ($3->type == n_CONCATENATION_LIST) { - $$ = NNEW(n_CONCATENATION_LIST); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChildren($3); - } else { - $$ = NNEW(n_CONCATENATION_LIST); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } - } -| expr '+' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '-' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '*' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '/' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '%' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_SL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_SR expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| '+' expr %prec T_INC { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| '-' expr %prec T_INC { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| '!' expr { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| '~' expr { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| expr T_IS_IDENTICAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_IS_NOT_IDENTICAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_IS_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_IS_NOT_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '<' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_IS_SMALLER_OR_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr '>' expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_IS_GREATER_OR_EQUAL expr { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| expr T_INSTANCEOF class_name_reference { - $$ = NNEW(n_BINARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NTYPE($2, n_OPERATOR)); - $$->appendChild($3); - } -| '(' expr ')' { - NSPAN($1, n_PARENTHETICAL_EXPRESSION, $3); - $1->appendChild($2); - $$ = $1; - } -| expr '?' expr ':' expr { - $$ = NNEW(n_TERNARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild($3); - $$->appendChild($5); - } -| expr '?' ':' expr { - $$ = NNEW(n_TERNARY_EXPRESSION); - $$->appendChild($1); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild($4); - } -| internal_functions_in_yacc -| T_INT_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_DOUBLE_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_STRING_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_UNICODE_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_BINARY_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_ARRAY_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_OBJECT_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_BOOL_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_UNSET_CAST expr { - $$ = NNEW(n_CAST_EXPRESSION); - $$->appendChild(NTYPE($1, n_CAST)); - $$->appendChild($2); - } -| T_EXIT exit_expr { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| '@' expr { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| T_ARRAY '(' array_pair_list ')' { - NTYPE($1, n_ARRAY_LITERAL); - $1->appendChild($3); - NMORE($1, $4); - $$ = $1; - } -| T_BACKTICKS_EXPR { - NTYPE($1, n_BACKTICKS_EXPRESSION); - $$ = $1; - } -| scalar -| T_PRINT expr { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}' { - NSPAN($1, n_FUNCTION_DECLARATION, $9); - $1->appendChild(NNEW(n_EMPTY)); - $1->appendChild($2); - $1->appendChild(NNEW(n_EMPTY)); - $1->appendChild(NEXPAND($3, $4, $5)); - $$->appendChild($6); - $1->appendChild($8); - - $$ = $1; - } -| T_STATIC function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}' { - NSPAN($2, n_FUNCTION_DECLARATION, $10); - NLMORE($2, $1); - - $$ = NNEW(n_FUNCTION_MODIFIER_LIST); - $$->appendChild(NTYPE($1, n_STRING)); - $2->appendChild($1); - - $2->appendChild(NNEW(n_EMPTY)); - $2->appendChild($3); - $2->appendChild(NNEW(n_EMPTY)); - $2->appendChild(NEXPAND($4, $5, $6)); - $2->appendChild($7); - $2->appendChild($9); - - $$ = $2; - } -; - -function: - T_FUNCTION -; - -lexical_vars: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| T_USE '(' lexical_var_list ')' { - NTYPE($1, n_LEXICAL_VARIABLE_LIST); - $1->appendChildren($3); - $$ = $1; - } -; - -lexical_var_list: - lexical_var_list ',' T_VARIABLE { - $$ = $1->appendChild(NTYPE($3, n_VARIABLE)); - } -| lexical_var_list ',' '&' T_VARIABLE { - NTYPE($3, n_VARIABLE_REFERENCE); - $3->appendChild(NTYPE($4, n_VARIABLE)); - $$ = $1->appendChild($3); - } -| T_VARIABLE { - $$ = NNEW(n_LEXICAL_VARIABLE_LIST); - $$->appendChild(NTYPE($1, n_VARIABLE)); - } -| '&' T_VARIABLE { - NTYPE($1, n_VARIABLE_REFERENCE); - $1->appendChild(NTYPE($2, n_VARIABLE)); - $$ = NNEW(n_LEXICAL_VARIABLE_LIST); - $$->appendChild($1); - } -; - -function_call: - namespace_name '(' function_call_parameter_list ')' { - $$ = NNEW(n_FUNCTION_CALL); - $$->appendChild($1); - $$->appendChild(NEXPAND($2, $3, $4)); - } -| T_NAMESPACE T_NS_SEPARATOR namespace_name '(' function_call_parameter_list ')' { - NLMORE($3, $1); - $$ = NNEW(n_FUNCTION_CALL); - $$->appendChild($3); - $$->appendChild(NEXPAND($4, $5, $6)); - } -| T_NS_SEPARATOR namespace_name '(' function_call_parameter_list ')' { - NLMORE($2, $1); - $$ = NNEW(n_FUNCTION_CALL); - $$->appendChild($2); - $$->appendChild(NEXPAND($3, $4, $5)); - } -| class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')' { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_STRING)); - - $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); - $$->appendChild(NEXPAND($4, $5, $6)); - } -| variable_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' function_call_parameter_list ')' { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_STRING)); - - $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); - $$->appendChild(NEXPAND($4, $5, $6)); - } -| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')' { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_STRING)); - - $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); - $$->appendChild(NEXPAND($4, $5, $6)); - } -| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects '(' function_call_parameter_list ')' { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_STRING)); - - $$ = NNEW(n_FUNCTION_CALL)->appendChild($$); - $$->appendChild(NEXPAND($4, $5, $6)); - } -| variable_without_objects '(' function_call_parameter_list ')' { - $$ = NNEW(n_FUNCTION_CALL); - $$->appendChild($1); - $$->appendChild(NEXPAND($2, $3, $4)); - } -; - -class_name: - T_STATIC { - $$ = NTYPE($1, n_CLASS_NAME); - } -| namespace_name { - $$ = NTYPE($1, n_CLASS_NAME); - } -| T_NAMESPACE T_NS_SEPARATOR namespace_name { - NLMORE($3, $1); - $$ = NTYPE($3, n_CLASS_NAME); - } -| T_NS_SEPARATOR namespace_name { - NLMORE($2, $1); - $$ = NTYPE($2, n_CLASS_NAME); - } -; - -fully_qualified_class_name: - namespace_name { - $$ = NTYPE($1, n_CLASS_NAME); - } -| T_NAMESPACE T_NS_SEPARATOR namespace_name { - NLMORE($3, $1); - $$ = NTYPE($3, n_CLASS_NAME); - } -| T_NS_SEPARATOR namespace_name { - NLMORE($2, $1); - $$ = NTYPE($2, n_CLASS_NAME); - } -; - -class_name_reference: - class_name -| dynamic_class_name_reference -; - -dynamic_class_name_reference: - base_variable T_OBJECT_OPERATOR object_property dynamic_class_name_variable_properties { - $$ = NNEW(n_OBJECT_PROPERTY_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - for (xhpast::node_list_t::iterator ii = $4->children.begin(); ii != $4->children.end(); ++ii) { - $$ = NNEW(n_OBJECT_PROPERTY_ACCESS)->appendChild($$); - $$->appendChild(*ii); - } - } -| base_variable -; - -dynamic_class_name_variable_properties: - dynamic_class_name_variable_properties dynamic_class_name_variable_property { - $$ = $1->appendChild($2); - } -| /* empty */ { - $$ = NNEW(n_EMPTY); - } -; - -dynamic_class_name_variable_property: - T_OBJECT_OPERATOR object_property { - $$ = $2; - } -; - -exit_expr: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| '(' ')' { - NSPAN($1, n_EMPTY, $2); - $$ = $1; - } -| '(' expr ')' { - NSPAN($1, n_PARENTHETICAL_EXPRESSION, $3); - $1->appendChild($2); - $$ = $1; - } -; - -ctor_arguments: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| '(' function_call_parameter_list ')' { - $$ = NEXPAND($1, $2, $3); - } -; - -common_scalar: - T_LNUMBER { - $$ = NTYPE($1, n_NUMERIC_SCALAR); - } -| T_DNUMBER { - $$ = NTYPE($1, n_NUMERIC_SCALAR); - } -| T_CONSTANT_ENCAPSED_STRING { - $$ = NTYPE($1, n_STRING_SCALAR); - } -| T_LINE { - $$ = NTYPE($1, n_MAGIC_SCALAR); - } -| T_FILE { - $$ = NTYPE($1, n_MAGIC_SCALAR); - } -| T_DIR { - $$ = NTYPE($1, n_MAGIC_SCALAR); - } -| T_CLASS_C { - $$ = NTYPE($1, n_MAGIC_SCALAR); - } -| T_METHOD_C { - $$ = NTYPE($1, n_MAGIC_SCALAR); - } -| T_FUNC_C { - $$ = NTYPE($1, n_MAGIC_SCALAR); - } -| T_NS_C { - $$ = NTYPE($1, n_MAGIC_SCALAR); - } -| T_HEREDOC { - $$ = NTYPE($1, n_HEREDOC); - } -; - -static_scalar: /* compile-time evaluated scalars */ - common_scalar -| namespace_name -| T_NAMESPACE T_NS_SEPARATOR namespace_name { - NLMORE($3, $1); - $$ = $3; - } -| T_NS_SEPARATOR namespace_name { - NLMORE($2, $1); - $$ = $2; - } -| '+' static_scalar { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| '-' static_scalar { - $$ = NNEW(n_UNARY_PREFIX_EXPRESSION); - $$->appendChild(NTYPE($1, n_OPERATOR)); - $$->appendChild($2); - } -| T_ARRAY '(' static_array_pair_list ')' { - NTYPE($1, n_ARRAY_LITERAL); - $1->appendChild($3); - NMORE($1, $4); - $$ = $1; - } -| static_class_constant -; - -static_class_constant: - class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_STRING)); - } -; - -scalar: - T_STRING_VARNAME -| class_constant -| namespace_name -| T_NAMESPACE T_NS_SEPARATOR namespace_name { - $$ = NLMORE($3, $1); - } -| T_NS_SEPARATOR namespace_name { - $$ = NLMORE($2, $1); - } -| common_scalar -; - -static_array_pair_list: - /* empty */ { - $$ = NNEW(n_ARRAY_VALUE_LIST); - } -| non_empty_static_array_pair_list possible_comma { - $$ = NMORE($1, $2); - } -; - -possible_comma: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| ',' -; - -non_empty_static_array_pair_list: - non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW static_scalar { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild($3); - $$->appendChild($5); - - $$ = $1->appendChild($$); - } -| non_empty_static_array_pair_list ',' static_scalar { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild($3); - - $$ = $1->appendChild($$); - } -| static_scalar T_DOUBLE_ARROW static_scalar { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild($1); - $$->appendChild($3); - } -| static_scalar { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild($1); - } -; - -expr: - r_variable -| expr_without_variable -; - -r_variable: - variable -; - -w_variable: - variable -; - -rw_variable: - variable -; - -variable: - base_variable_with_function_calls T_OBJECT_OPERATOR object_property method_or_not variable_properties { - $$ = NNEW(n_OBJECT_PROPERTY_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - - if ($4->type != n_EMPTY) { - $$ = NNEW(n_METHOD_CALL)->appendChild($$); - $$->appendChild($4); - } - - for (xhpast::node_list_t::iterator ii = $5->children.begin(); ii != $5->children.end(); ++ii) { - if ((*ii)->type == n_CALL_PARAMETER_LIST) { - $$ = NNEW(n_METHOD_CALL)->appendChild($1); - $$->appendChild((*ii)); - } else { - $$ = NNEW(n_OBJECT_PROPERTY_ACCESS); - $$->appendChild((*ii)); - } - } - } -| base_variable_with_function_calls -; - -variable_properties: - variable_properties variable_property { - $$ = $1->appendChildren($2); - } -| /* empty */ { - $$ = NNEW(n_EMPTY); - } -; - -variable_property: - T_OBJECT_OPERATOR object_property method_or_not { - $$ = NNEW(n_EMPTY); - $$->appendChild($2); - if ($3->type != n_EMPTY) { - $$->appendChild($3); - } - } -; - -method_or_not: - '(' function_call_parameter_list ')' { - $$ = NEXPAND($1, $2, $3); - } -| /* empty */ { - $$ = NNEW(n_EMPTY); - } -; - -variable_without_objects: - reference_variable -| simple_indirect_reference reference_variable { - $$ = $1->appendChild($2); - } -; - -static_member: - class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - } -| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - } -; - -variable_class_name: - reference_variable -; - -base_variable_with_function_calls: - base_variable -| function_call -; - -base_variable: - reference_variable -| simple_indirect_reference reference_variable { - $$ = $1->appendChild($2); - } -| static_member -; - -reference_variable: - reference_variable '[' dim_offset ']' { - $$ = NNEW(n_INDEX_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - NMORE($$, $4); - } -| reference_variable '{' expr '}' { - $$ = NNEW(n_INDEX_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - NMORE($$, $4); - } -| compound_variable -; - -compound_variable: - T_VARIABLE { - NTYPE($1, n_VARIABLE); - } -| '$' '{' expr '}' { - NSPAN($1, n_VARIABLE_EXPRESSION, $4); - $1->appendChild($3); - $$ = $1; - } -; - -dim_offset: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| expr { - $$ = $1; - } -; - -object_property: - object_dim_list -| variable_without_objects -; - -object_dim_list: - object_dim_list '[' dim_offset ']' { - $$ = NNEW(n_INDEX_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - NMORE($$, $4) - } -| object_dim_list '{' expr '}' { - $$ = NNEW(n_INDEX_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - NMORE($$, $4); - } -| variable_name -; - -variable_name: - T_STRING { - NTYPE($1, n_STRING); - $$ = $1; - } -| '{' expr '}' { - $$ = NEXPAND($1, $2, $3); - } -; - -simple_indirect_reference: - '$' { - $$ = NTYPE($1, n_VARIABLE_VARIABLE); - } -| simple_indirect_reference '$' { - $$ = NMORE($1, $2); - } -; - -assignment_list: - assignment_list ',' assignment_list_element { - $$ = $1->appendChild($3); - } -| assignment_list_element { - $$ = NNEW(n_ASSIGNMENT_LIST); - $$->appendChild($1); - } -; - -assignment_list_element: - variable -| T_LIST '(' assignment_list ')' { - $$ = NNEW(n_LIST); - $$->appendChild($3); - NMORE($$, $4); - } -| /* empty */ { - $$ = NNEW(n_EMPTY); - } -; - -array_pair_list: - /* empty */ { - $$ = NNEW(n_ARRAY_VALUE_LIST); - } -| non_empty_array_pair_list possible_comma { - $$ = NMORE($1, $2); - } -; - -non_empty_array_pair_list: - non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild($3); - $$->appendChild($5); - - $$ = $1->appendChild($$); - } -| non_empty_array_pair_list ',' expr { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild($3); - - $$ = $1->appendChild($$); - } -| expr T_DOUBLE_ARROW expr { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild($1); - $$->appendChild($3); - - $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); - } -| expr { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild($1); - - $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); - } -| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild($3); - $$->appendChild(NTYPE($5, n_VARIABLE_REFERENCE)->appendChild($6)); - - $$ = $1->appendChild($$); - } -| non_empty_array_pair_list ',' '&' w_variable { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild(NTYPE($3, n_VARIABLE_REFERENCE)->appendChild($4)); - - $$ = $1->appendChild($$); - } -| expr T_DOUBLE_ARROW '&' w_variable { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_VARIABLE_REFERENCE)->appendChild($4)); - - $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); - } -| '&' w_variable { - $$ = NNEW(n_ARRAY_VALUE); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild(NTYPE($1, n_VARIABLE_REFERENCE)->appendChild($2)); - - $$ = NNEW(n_ARRAY_VALUE_LIST)->appendChild($$); - } -; - -internal_functions_in_yacc: - T_ISSET '(' isset_variables ')' { - NTYPE($1, n_SYMBOL_NAME); - - NSPAN($2, n_CALL_PARAMETER_LIST, $4); - $2->appendChildren($3); - - $$ = NNEW(n_FUNCTION_CALL); - $$->appendChild($1); - $$->appendChild($2); - } -| T_EMPTY '(' variable ')' { - NTYPE($1, n_SYMBOL_NAME); - - NSPAN($2, n_CALL_PARAMETER_LIST, $4); - $2->appendChild($3); - - $$ = NNEW(n_FUNCTION_CALL); - $$->appendChild($1); - $$->appendChild($2); - } -| T_INCLUDE expr { - $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); - } -| T_INCLUDE_ONCE expr { - $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); - } -| T_EVAL '(' expr ')' { - NTYPE($1, n_SYMBOL_NAME); - - NSPAN($2, n_CALL_PARAMETER_LIST, $4); - $2->appendChild($3); - - $$ = NNEW(n_FUNCTION_CALL); - $$->appendChild($1); - $$->appendChild($2); - } -| T_REQUIRE expr { - $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); - } -| T_REQUIRE_ONCE expr { - $$ = NTYPE($1, n_INCLUDE_FILE)->appendChild($2); - } -; - -isset_variables: - variable { - $$ = NNEW(n_EMPTY); - $$->appendChild($1); - } -| isset_variables ',' variable { - $$ = $1->appendChild($3); - } -; - -class_constant: - class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_STRING)); - } -| variable_class_name T_PAAMAYIM_NEKUDOTAYIM T_STRING { - $$ = NNEW(n_CLASS_STATIC_ACCESS); - $$->appendChild($1); - $$->appendChild(NTYPE($3, n_STRING)); - } -; - -// -// XHP Extensions - -// Tags -expr_without_variable: - xhp_tag_expression { - yyextra->used = true; - $$ = $1; - } -; - -xhp_tag_expression: - xhp_singleton -| xhp_tag_open xhp_children xhp_tag_close { - $$ = NNEW(n_XHP_TAG); - $$->appendChild($1); - $$->appendChild($2); - $$->appendChild($3); - } -; - -xhp_singleton: - xhp_tag_start xhp_attributes '/' '>' { - pop_state(); // XHP_ATTRS - - $1->appendChild($2); - NMORE($$, $4); - - $$ = NNEW(n_XHP_TAG)->appendChild($1); - $$->appendChild(NNEW(n_EMPTY)); - $$->appendChild(NNEW(n_EMPTY)); - } -; - -xhp_tag_open: - xhp_tag_start xhp_attributes '>' { - pop_state(); // XHP_ATTRS - push_state(XHP_CHILD_START); -/* TODO: RESTORE THIS - yyextra->pushTag((*($1->l_tok))->value.c_str()); -*/ - yyextra->pushTag("TODO"); - - $$ = $1->appendChild($2); - NMORE($1, $3); - } -; - -xhp_tag_close: - T_XHP_LT_DIV xhp_label_no_space '>' { - pop_state(); // XHP_CHILD_START -/* TOOD: RESTORE THIS - if (yyextra->peekTag() != (*($2->l_tok))->value.c_str()) { - string e1 = (*($2->l_tok))->value.c_str(); - string e2 = yyextra->peekTag(); - replacestr(e1, "__", ":"); - replacestr(e1, "_", "-"); - replacestr(e2, "__", ":"); - replacestr(e2, "_", "-"); - string e = "syntax error, mismatched tag , expecting "; - yyerror(yyscanner, NULL, e.c_str()); - yyextra->terminated = true; - } -*/ - yyextra->popTag(); - if (yyextra->haveTag()) { - set_state(XHP_CHILD_START); - } - - $$ = NSPAN($1, n_XHP_TAG_CLOSE, $3); - } -| T_XHP_LT_DIV_GT { - // empty end tag -- SGML SHORTTAG - pop_state(); // XHP_CHILD_START - yyextra->popTag(); - if (yyextra->haveTag()) { - set_state(XHP_CHILD_START); - } - $$ = NTYPE($1, n_XHP_TAG_CLOSE); - } -; - -xhp_tag_start: - '<' xhp_label_immediate { - $$ = NTYPE($1, n_XHP_TAG_OPEN); - $$->appendChild($2); - } -; - -// Children -xhp_literal_text: - T_XHP_TEXT { - $$ = NTYPE($1, n_XHP_TEXT); - } -| T_XHP_ENTITY { - $$ = NTYPE($1, n_XHP_ENTITY); - } -| xhp_literal_text T_XHP_TEXT { - $$ = NMORE($1, $2); - } -| xhp_literal_text T_XHP_ENTITY { - $$ = NMORE($1, $2); - } -; - -xhp_children: - /* empty */ { - $$ = NNEW(n_XHP_NODE_LIST); - } -| xhp_literal_text { - set_state(XHP_CHILD_START); - $$ = NNEW(n_XHP_NODE_LIST)->appendChild($1); - } -| xhp_children xhp_child { - set_state(XHP_CHILD_START); - $$ = $1->appendChild($2); - } -| xhp_children xhp_child xhp_literal_text { - set_state(XHP_CHILD_START); - $$ = $1->appendChild($2)->appendChild($3); - } -; - -xhp_child: - xhp_tag_expression -| '{' { - push_state(PHP); - yyextra->pushStack(); - } expr '}' { - pop_state(); - yyextra->popStack(); - } { - set_state(XHP_CHILD_START); - $$ = NNEW(n_XHP_EXPRESSION); - $$->appendChild($3); - NEXPAND($1, $$, $4); - } -; - -// Attributes -xhp_attributes: - /* empty */ { - push_state(XHP_ATTRS); - $$ = NNEW(n_XHP_ATTRIBUTE_LIST); - } -| xhp_attributes xhp_attribute { - $$ = $1->appendChild($2); - } -; - -xhp_attribute: - xhp_label_pass '=' xhp_attribute_value { - $$ = NNEW(n_XHP_ATTRIBUTE); - $$->appendChild($1); - $$->appendChild($3); - } -; - -xhp_attribute_value: - '"' { push_state(XHP_ATTR_VAL); } xhp_attribute_quoted_value '"' { - $$ = NSPAN($1, n_XHP_ATTRIBUTE_LITERAL, $4); - $$->appendChild($3); - } -| '{' { push_state(PHP); } expr { pop_state(); } '}' { - $$ = NSPAN($1, n_XHP_ATTRIBUTE_EXPRESSION, $5); - $$->appendChild($3); - } -; - -xhp_attribute_quoted_value: - /* empty */ { - $$ = NNEW(n_EMPTY); - } -| xhp_literal_text { - $$ = NTYPE($1, n_XHP_LITERAL); - } -; - -// Misc -xhp_label_immediate: - { push_state(XHP_LABEL); } xhp_label_ xhp_whitespace_hack { - pop_state(); - $$ = $2; - } -; - -xhp_label_no_space: - { push_state(XHP_LABEL); } xhp_label_ { - pop_state(); - $$ = $2; - } -; - -xhp_label_pass: - { push_state(XHP_LABEL_WHITESPACE); } xhp_label_pass_ xhp_whitespace_hack { - pop_state(); - $$ = $2; - } -; - -xhp_label_pass_immediate: - { push_state(XHP_LABEL); } xhp_label_pass_ xhp_whitespace_hack { - pop_state(); - $$ = $2; - } -; - -xhp_label: - { push_state(XHP_LABEL_WHITESPACE); } xhp_label_ xhp_whitespace_hack { - pop_state(); - $$ = $2; - } -; - -xhp_label_: - T_STRING { - // XHP_LABEL is popped in the scanner on " ", ">", "/", or "=" - push_state(XHP_LABEL); - $$ = NTYPE($1, n_XHP_LITERAL); - } -| xhp_label_ T_XHP_COLON T_STRING { - $$ = NMORE($1, $3); - } -| xhp_label_ T_XHP_HYPHEN T_STRING { - $$ = NMORE($1, $3); - } -; - -xhp_label_pass_: - T_STRING { - // XHP_LABEL is popped in the scanner on " ", ">", "/", or "=" - push_state(XHP_LABEL); - $$ = NTYPE($1, n_XHP_LITERAL); - } -| xhp_label_pass_ T_XHP_COLON T_STRING { - $$ = NMORE($1, $3); - } -| xhp_label_pass_ T_XHP_HYPHEN T_STRING { - $$ = NMORE($1, $3); - } -; - -xhp_whitespace_hack: - T_XHP_WHITESPACE -| /* empty */ -; - -// Elements -class_declaration_statement: - class_entry_type ':' xhp_label_immediate extends_from implements_list '{' { - yyextra->expecting_xhp_class_statements = true; - yyextra->used_attributes = false; - } class_statement_list { - yyextra->expecting_xhp_class_statements = false; - } '}' { - $$ = NNEW(n_CLASS_DECLARATION); - $$->appendChild($1); - $$->appendChild(NSPAN($2, n_STRING, $3)); - $$->appendChild($4); - $$->appendChild($5); - $$->appendChild($8); - NMORE($$, $10); - - $$ = NNEW(n_STATEMENT)->appendChild($$); - - -//! $$ = $1 + " xhp_" + $3 + $4 + $5 + $6 + $8; -//! if (yyextra->used_attributes) { -//! $$ = $$ + -//! "protected static function &__xhpAttributeDeclaration() {" + -//! "static $_ = -1;" + -//! "if ($_ === -1) {" + -//! "$_ = array_merge(parent::__xhpAttributeDeclaration(), " + -//! yyextra->attribute_inherit + -//! "array(" + yyextra->attribute_decls + "));" + -//! "}" + -//! "return $_;" -//! "}"; -//! } -//! $$ = $$ + $10; - yyextra->used = true; - } -; - -// Element attribute declaration -class_statement: - T_XHP_ATTRIBUTE { push_state(XHP_ATTR_TYPE_DECL); } xhp_attribute_decls ';' { - pop_state(); - yyextra->used = true; - yyextra->used_attributes = true; -//! $$ = ""; // this will be injected when the class closes - } -; - -xhp_attribute_decls: - xhp_attribute_decl {} -| xhp_attribute_decls ',' xhp_attribute_decl {} -; - -xhp_attribute_decl: - xhp_attribute_decl_type xhp_label_pass xhp_attribute_default xhp_attribute_is_required { -//! $2.strip_lines(); -//! yyextra->attribute_decls = yyextra->attribute_decls + -//! "'" + $2 + "'=>array(" + $1 + "," + $3 + ", " + $4 + ")," - } -| T_XHP_COLON xhp_label_immediate { -//! $2.strip_lines(); -//! yyextra->attribute_inherit = yyextra->attribute_inherit + -//! "xhp_" + $2 + "::__xhpAttributeDeclaration(),"; - } -; - -xhp_attribute_decl_type: - T_XHP_STRING { -//! $$ = "1, null"; - } -| T_XHP_BOOLEAN { -//! $$ = "2, null"; - } -| T_XHP_NUMBER { -//! $$ = "3, null"; - } -| T_XHP_ARRAY { -//! $$ = "4, null"; - } -| class_name { -//! $$ = "5, '" + $1 + "'"; - } -| T_VAR { -//! $$ = "6, null"; - } -| T_XHP_ENUM '{' { push_state(PHP); } xhp_attribute_enum { pop_state(); } '}' { -//! $$ = "7, array(" + $4 + ")"; - } -| T_XHP_FLOAT { -//! ... -} -; - -xhp_attribute_enum: - common_scalar { -//! $1.strip_lines(); -//! $$ = $1; - } -| xhp_attribute_enum ',' common_scalar { -//! $3.strip_lines(); -//! $$ = $1 + ", " + $3; - } -; - -xhp_attribute_default: - '=' common_scalar { -//! $2.strip_lines(); -//! $$ = $2; - } -| '=' T_STRING { -//! $2.strip_lines(); -//! $$ = $2; - } -| /* empty */ { -//! $$ = "null"; - } -; - -xhp_attribute_is_required: - T_XHP_REQUIRED { -//! $$ = "1"; - } -| /* empty */ { -//! $$ = "0"; - } -; - -// Element category declaration -class_statement: - T_XHP_CATEGORY { push_state(PHP_NO_RESERVED_WORDS_PERSIST); } xhp_category_list ';' { - pop_state(); - yyextra->used = true; -//! $$ = -//! "protected function &__xhpCategoryDeclaration() { \ !!! -//! static $_ = array(" + $3 + ");" + -//! "return $_;" + -//! "}"; - } -; - -xhp_category_list: - '%' xhp_label_pass_immediate { -//! $$ = "'" + $2 + "' => 1"; - } -| xhp_category_list ',' '%' xhp_label_pass_immediate { -//! $$ = $1 + ",'" + $4 + "' => 1"; - } -; - -// Element child list -class_statement: - T_XHP_CHILDREN { push_state(XHP_CHILDREN_DECL); } xhp_children_decl ';' { - // XHP_CHILDREN_DECL is popped in the scanner on ';' - yyextra->used = true; -//! $$ = "protected function &__xhpChildrenDeclaration() {" + $3 + "}"; - } -; - -xhp_children_decl: - xhp_children_paren_expr { -//! $$ = "static $_ = " + $1 + "; return $_;"; - } -| T_XHP_ANY { -//! $$ = "static $_ = 1; return $_;"; - } -| T_XHP_EMPTY { -//! $$ = "static $_ = 0; return $_;"; - } -; - -xhp_children_paren_expr: - '(' xhp_children_decl_expr ')' { -//! $$ = "array(0, 5, " + $2 + ")"; - } -| '(' xhp_children_decl_expr ')' '*' { -//! $$ = "array(1, 5, " + $2 + ")"; - } -| '(' xhp_children_decl_expr ')' '?' { -//! $$ = "array(2, 5, " + $2 + ")"; - } -| '(' xhp_children_decl_expr ')' '+' { -//! $$ = "array(3, 5, " + $2 + ")"; - } -; - -xhp_children_decl_expr: - xhp_children_paren_expr -| xhp_children_decl_tag { -//! $$ = "array(0, " + $1 + ")"; - } -| xhp_children_decl_tag '*' { -//! $$ = "array(1, " + $1 + ")"; - } -| xhp_children_decl_tag '?' { -//! $$ = "array(2, " + $1 + ")"; - } -| xhp_children_decl_tag '+' { -//! $$ = "array(3, " + $1 + ")"; - } -| xhp_children_decl_expr ',' xhp_children_decl_expr { -//! $$ = "array(4, " + $1 + "," + $3 + ")" - } -| xhp_children_decl_expr '|' xhp_children_decl_expr { -//! $$ = "array(5, " + $1 + "," + $3 + ")" - } -; - -xhp_children_decl_tag: - T_XHP_ANY { -//! $$ = "1, null"; - } -| T_XHP_PCDATA { -//! $$ = "2, null"; - } -| T_XHP_COLON xhp_label { -//! $$ = "3, \'xhp_" + $2 + "\'"; - } -| '%' xhp_label { -//! $$ = "4, \'" + $2 + "\'"; - } -; - -// Make XHP classes usable anywhere you see a real class -class_name: - T_XHP_COLON xhp_label_immediate { - pop_state(); - push_state(PHP); - yyextra->used = true; -//! $$ = "xhp_" + $2; - } -; - -fully_qualified_class_name: - T_XHP_COLON xhp_label_immediate { - pop_state(); - push_state(PHP); - yyextra->used = true; -//! $$ = "xhp_" + $2; - } -; - -// Fix the "bug" in PHP's grammar where you can't chain the [] operator on a -// function call. -// This introduces some shift/reduce conflicts. We want the shift here to fall -// back to regular PHP grammar. In the case where it's an extension of the PHP -// grammar our code gets picked up. -expr_without_variable: - expr '[' dim_offset ']' { - if (yyextra->idx_expr) { - yyextra->used = true; - } - $$ = NNEW(n_INDEX_ACCESS); - $$->appendChild($1); - $$->appendChild($3); - NMORE($$, $4); - } -; - - -%% - -const char* yytokname(int tok) { - if (tok < 255) { - return NULL; - } - return yytname[YYTRANSLATE(tok)]; -} diff --git a/support/xhpast/parser_nodes.php b/support/xhpast/parser_nodes.php deleted file mode 100755 index 20864f09..00000000 --- a/support/xhpast/parser_nodes.php +++ /dev/null @@ -1,139 +0,0 @@ - 'n_PROGRAM', - 9001 => 'n_SYMBOL_NAME', - 9002 => 'n_HALT_COMPILER', - 9003 => 'n_NAMESPACE', - 9004 => 'n_STATEMENT', - 9005 => 'n_EMPTY', - 9006 => 'n_STATEMENT_LIST', - 9007 => 'n_OPEN_TAG', - 9008 => 'n_CLOSE_TAG', - 9009 => 'n_USE_LIST', - 9010 => 'n_USE', - 9011 => 'n_CONSTANT_DECLARATION_LIST', - 9012 => 'n_CONSTANT_DECLARATION', - 9013 => 'n_STRING', - 9014 => 'n_LABEL', - 9015 => 'n_CONDITION_LIST', - 9016 => 'n_CONTROL_CONDITION', - 9017 => 'n_IF', - 9018 => 'n_ELSEIF', - 9019 => 'n_ELSE', - 9020 => 'n_WHILE', - 9021 => 'n_DO_WHILE', - 9022 => 'n_FOR', - 9023 => 'n_FOR_EXPRESSION', - 9024 => 'n_SWITCH', - 9025 => 'n_BREAK', - 9026 => 'n_CONTINUE', - 9027 => 'n_RETURN', - 9028 => 'n_GLOBAL_DECLARATION_LIST', - 9029 => 'n_GLOBAL_DECLARATION', - 9030 => 'n_STATIC_DECLARATION_LIST', - 9031 => 'n_STATIC_DECLARATION', - 9032 => 'n_ECHO_LIST', - 9033 => 'n_ECHO', - 9034 => 'n_INLINE_HTML', - 9035 => 'n_UNSET_LIST', - 9036 => 'n_UNSET', - 9037 => 'n_FOREACH', - 9038 => 'n_FOREACH_EXPRESSION', - 9039 => 'n_THROW', - 9040 => 'n_GOTO', - 9041 => 'n_TRY', - 9042 => 'n_CATCH_LIST', - 9043 => 'n_CATCH', - 9044 => 'n_DECLARE', - 9045 => 'n_DECLARE_DECLARATION_LIST', - 9046 => 'n_DECLARE_DECLARATION', - 9047 => 'n_VARIABLE', - 9048 => 'n_REFERENCE', - 9049 => 'n_VARIABLE_REFERENCE', - 9050 => 'n_FUNCTION_DECLARATION', - 9051 => 'n_CLASS_DECLARATION', - 9052 => 'n_CLASS_ATTRIBUTES', - 9053 => 'n_EXTENDS', - 9054 => 'n_EXTENDS_LIST', - 9055 => 'n_IMPLEMENTS_LIST', - 9056 => 'n_INTERFACE_DECLARATION', - 9057 => 'n_CASE', - 9058 => 'n_DEFAULT', - 9059 => 'n_DECLARATION_PARAMETER_LIST', - 9060 => 'n_DECLARATION_PARAMETER', - 9061 => 'n_TYPE_NAME', - 9062 => 'n_VARIABLE_VARIABLE', - 9063 => 'n_CLASS_MEMBER_DECLARATION_LIST', - 9064 => 'n_CLASS_MEMBER_DECLARATION', - 9065 => 'n_CLASS_CONSTANT_DECLARATION_LIST', - 9066 => 'n_CLASS_CONSTANT_DECLARATION', - 9067 => 'n_METHOD_DECLARATION', - 9068 => 'n_METHOD_MODIFIER_LIST', - 9069 => 'n_FUNCTION_MODIFIER_LIST', - 9070 => 'n_CLASS_MEMBER_MODIFIER_LIST', - 9071 => 'n_EXPRESSION_LIST', - 9072 => 'n_LIST', - 9073 => 'n_ASSIGNMENT', - 9074 => 'n_NEW', - 9075 => 'n_UNARY_PREFIX_EXPRESSION', - 9076 => 'n_UNARY_POSTFIX_EXPRESSION', - 9077 => 'n_BINARY_EXPRESSION', - 9078 => 'n_TERNARY_EXPRESSION', - 9079 => 'n_CAST_EXPRESSION', - 9080 => 'n_CAST', - 9081 => 'n_OPERATOR', - 9082 => 'n_ARRAY_LITERAL', - 9083 => 'n_EXIT_EXPRESSION', - 9084 => 'n_BACKTICKS_EXPRESSION', - 9085 => 'n_LEXICAL_VARIABLE_LIST', - 9086 => 'n_NUMERIC_SCALAR', - 9087 => 'n_STRING_SCALAR', - 9088 => 'n_MAGIC_SCALAR', - 9089 => 'n_CLASS_STATIC_ACCESS', - 9090 => 'n_CLASS_NAME', - 9091 => 'n_MAGIC_CLASS_KEYWORD', - 9092 => 'n_OBJECT_PROPERTY_ACCESS', - 9093 => 'n_ARRAY_VALUE_LIST', - 9094 => 'n_ARRAY_VALUE', - 9095 => 'n_CALL_PARAMETER_LIST', - 9096 => 'n_VARIABLE_EXPRESSION', - 9097 => 'n_INCLUDE_FILE', - 9098 => 'n_HEREDOC', - 9099 => 'n_FUNCTION_CALL', - 9100 => 'n_INDEX_ACCESS', - 9101 => 'n_ASSIGNMENT_LIST', - 9102 => 'n_METHOD_CALL', - 9103 => 'n_XHP_TAG', - 9104 => 'n_XHP_TAG_OPEN', - 9105 => 'n_XHP_TAG_CLOSE', - 9106 => 'n_XHP_TEXT', - 9107 => 'n_XHP_EXPRESSION', - 9108 => 'n_XHP_ATTRIBUTE_LIST', - 9109 => 'n_XHP_ATTRIBUTE', - 9110 => 'n_XHP_LITERAL', - 9111 => 'n_XHP_ATTRIBUTE_LITERAL', - 9112 => 'n_XHP_ATTRIBUTE_EXPRESSION', - 9113 => 'n_XHP_NODE_LIST', - 9114 => 'n_XHP_ENTITY', - 9115 => 'n_CONCATENATION_LIST', - 9116 => 'n_PARENTHETICAL_EXPRESSION', - ); -} diff --git a/support/xhpast/scanner.l b/support/xhpast/scanner.l deleted file mode 100755 index 222f3c33..00000000 --- a/support/xhpast/scanner.l +++ /dev/null @@ -1,1085 +0,0 @@ -/* - * Copyright 2011 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -%{ -#include "ast.hpp" -#define push_state(s) xhp_new_push_state(s, yyg) -#define pop_state() xhp_new_pop_state(yyg) -#define set_state(s) xhp_set_state(s, yyg) -#define last_token() yyextra->last_token - -#define YY_USER_ACTION \ - if (!yyg->yy_more_len) \ - yyextra->first_lineno = yyextra->lineno; -#define pttok(t, txt) \ - yyextra->token_list.push_back(new xhpast::Token(t, txt, yyextra->list_size++)); \ - *yylval = new xhpast::Node(0, yyextra->list_size - 1); -#define ptok(t) \ - pttok(t, yytext); -#define tok(t) \ - ptok(t); \ - return yy_token(t, yyg) -#define tokt(t) pttok(T_XHP_ENTITY, yytext); push_state(XHP_AFTER_ENT); return yyextra->last_token = T_XHP_ENTITY; -#define YY_USER_INIT \ - if (yyextra->insert_token) { \ - yyg->yy_init = 0; \ - int ft = yyextra->insert_token; \ - yyextra->insert_token = 0; \ - return yy_token(ft, yyg); \ - } - -using namespace std; - -const char* yytokname(int tok); -static int yy_token(int tok, struct yyguts_t* yyg); -static void yy_scan_newlines(const char* text, struct yyguts_t* yyg); - -static bool utf8ize(uint32_t v, char* buf /* [5] */) { - if (v <= 0x7f) { // 0xxxxxxx - buf[0] = v; - buf[1] = 0; - } else if (v <= 0x7ff) { // 110yyyxx 10xxxxxx - buf[0] = 0xc0 | (v >> 6); - buf[1] = 0x80 | (v & 0x3f); - buf[2] = 0; - } else if (v <= 0xffff) { // 1110yyyy 10yyyyxx 10xxxxxx - buf[0] = 0xe0 | (v >> 12); - buf[1] = 0x80 | ((v >> 6) & 0x3f); - buf[2] = 0x80 | (v & 0x3f); - buf[3] = 0; - } else if (v <= 0x1fffff) { // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx - buf[0] = 0xf0 | (v >> 18); - buf[1] = 0x80 | ((v >> 12) & 0x3f); - buf[2] = 0x80 | ((v >> 6) & 0x3f); - buf[3] = 0x80 | (v & 0x3f); - buf[4] = 0; - } else { - return false; - } - return true; -} - -%} - -%option prefix="xhpast" -%option reentrant - /* PHP allows IF or if */ -%option case-insensitive -%option noyywrap nodefault -%option stack -%option bison-bridge -%option 8bit - - /* I think an interactive scanner is required because of the bison state - * pushing we do. I'm putting an explicit interactive declaration here in case - * someone tries adding -CF or whatever to the make flags. */ -%option interactive - - /* The different lexing states. Note that the transitions are done either - * in the lex actions, or in a generic manner in yy_token(). */ -%s PHP -%s PHP_COMMENT -%s PHP_EOL_COMMENT -%s PHP_DOC_COMMENT -%s PHP_HEREDOC_START -%s PHP_HEREDOC_NSTART -%s PHP_HEREDOC_NEWLINE -%s PHP_HEREDOC_DATA -%s PHP_NO_RESERVED_WORDS -%s PHP_NO_RESERVED_WORDS_PERSIST -%s PHP_ -%s XHP_LABEL -%s XHP_LABEL_WHITESPACE -%s XHP_ATTRS -%s XHP_ATTR_VAL -%s XHP_AFTER_ENT -%s XHP_CHILD -%s XHP_CHILD_START -%s XHP_INVALID_ENTITY -%s XHP_ATTR_TYPE_DECL -%s XHP_CHILDREN_DECL - -LNUM [0-9]+ -DNUM ([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*) -EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) -HNUM "0x"[0-9a-fA-F]+ - -LABEL [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* -BYTE (.|\n) - -WHITESPACE [ \n\r\t]+ -TABS_AND_SPACES [ \t]* -NEWLINE ("\r\n"|"\n"|"\r") - -%% - -{ - "bool" tok(T_XHP_BOOLEAN); - "int" tok(T_XHP_NUMBER); - "float" tok(T_XHP_FLOAT); - "var" tok(T_VAR); - "array" tok(T_XHP_ARRAY); - "string" tok(T_XHP_STRING); - "enum" tok(T_XHP_ENUM); - @required tok(T_XHP_REQUIRED); - "(" tok('('); - ":" tok(T_XHP_COLON); -} - - /* Open / close PHP + inline HTML */ -{ - "short_tags) { - tok(T_OPEN_TAG); - } else { - tok(T_INLINE_HTML); - } - } - "short_tags) { - tok(T_OPEN_TAG_WITH_ECHO); - } else { - tok(T_INLINE_HTML); - } - } - "<%" { - if (yyextra->asp_tags) { - tok(T_OPEN_TAG); - } else { - tok(T_INLINE_HTML); - } - } - "<%=" { - if (yyextra->asp_tags) { - tok(T_OPEN_TAG_WITH_ECHO); - } else { - tok(T_INLINE_HTML); - } - } - "<"|[^<]* { - yy_scan_newlines(yytext, yyg); - tok(T_INLINE_HTML); - } -} -{ - ("?>"|""){NEWLINE}? { - yy_scan_newlines(yytext + 2, yyg); - tok(T_CLOSE_TAG); - } - "%>" { - if (yyextra->asp_tags) { - tok(T_CLOSE_TAG); - } else { - yyless(1); - tok(yytext[0]); - } - } -} - - /* Comments and whitespace */ -{ - "#"|"//" { - push_state(PHP_EOL_COMMENT); - yymore(); - } - "/**"{WHITESPACE} { - yy_scan_newlines(yytext + 3, yyg); - push_state(PHP_DOC_COMMENT); - yymore(); - } - "/*" { - push_state(PHP_COMMENT); - yymore(); - } - {WHITESPACE}+ { - yy_scan_newlines(yytext, yyg); - ptok(T_WHITESPACE); - } -} -{ - {NEWLINE} { - ++yyextra->lineno; - ptok(T_COMMENT); - pop_state(); - } - [^\r\n?]+ yymore(); - "?>" { - yyless(yyleng - 2); - ptok(T_COMMENT); - pop_state(); - } - . yymore(); -} -{ - {NEWLINE} { - ++yyextra->lineno; - yymore(); - } - [^*\r\n]+|"*" yymore(); -} -"*/" { - ptok(T_DOC_COMMENT); - pop_state(); -} -"*/" { - ptok(T_COMMENT); - pop_state(); -} - - /* Reserved words */ -{ - include tok(T_INCLUDE); - include_once tok(T_INCLUDE_ONCE); - eval tok(T_EVAL); - require tok(T_REQUIRE); - require_once tok(T_REQUIRE_ONCE); - or tok(T_LOGICAL_OR); - xor tok(T_LOGICAL_XOR); - and tok(T_LOGICAL_AND); - print tok(T_PRINT); - instanceof tok(T_INSTANCEOF); - new tok(T_NEW); - clone tok(T_CLONE); - exit tok(T_EXIT); - if tok(T_IF); - elseif tok(T_ELSEIF); - else tok(T_ELSE); - endif tok(T_ENDIF); - echo tok(T_ECHO); - do tok(T_DO); - while tok(T_WHILE); - endwhile tok(T_ENDWHILE); - for tok(T_FOR); - endfor tok(T_ENDFOR); - foreach tok(T_FOREACH); - endforeach tok(T_ENDFOREACH); - declare tok(T_DECLARE); - enddeclare tok(T_ENDDECLARE); - as tok(T_AS); - switch tok(T_SWITCH); - endswitch tok(T_ENDSWITCH); - case tok(T_CASE); - default tok(T_DEFAULT); - break tok(T_BREAK); - continue tok(T_CONTINUE); - goto tok(T_GOTO); - function tok(T_FUNCTION); - const tok(T_CONST); - return tok(T_RETURN); - try tok(T_TRY); - catch tok(T_CATCH); - throw tok(T_THROW); - use tok(T_USE); - global tok(T_GLOBAL); - static tok(T_STATIC); - abstract tok(T_ABSTRACT); - final tok(T_FINAL); - private tok(T_PRIVATE); - protected tok(T_PROTECTED); - public tok(T_PUBLIC); - var tok(T_VAR); - unset tok(T_UNSET); - isset tok(T_ISSET); - empty tok(T_EMPTY); - __halt_compiler tok(T_HALT_COMPILER); - class tok(T_CLASS); - interface tok(T_INTERFACE); - extends tok(T_EXTENDS); - implements tok(T_IMPLEMENTS); - list tok(T_LIST); - array tok(T_ARRAY); - __class__ tok(T_CLASS_C); - __method__ tok(T_METHOD_C); - __function__ tok(T_FUNC_C); - __line__ tok(T_LINE); - __file__ tok(T_FILE); - namespace tok(T_NAMESPACE); - __namespace__ tok(T_NS_C); - __dir__ tok(T_DIR); - attribute { - // expecting_xhp_class_statements is set in some actions in the grammar. - // This means the lexer and parser are interdependent. - if ((last_token() == '{' || last_token() == '}' || last_token() == ';') && - (yyextra->expecting_xhp_class_statements)) { - tok(T_XHP_ATTRIBUTE); - } else { - tok(T_STRING); - } - } - category { - if ((last_token() == '{' || last_token() == '}' || last_token() == ';') && - (yyextra->expecting_xhp_class_statements)) { - tok(T_XHP_CATEGORY); - } else { - tok(T_STRING); - } - } - children { - if ((last_token() == '{' || last_token() == '}' || last_token() == ';') && - (yyextra->expecting_xhp_class_statements)) { - tok(T_XHP_CHILDREN); - } else { - tok(T_STRING); - } - } -} - - /* Operators */ -{ - "+=" tok(T_PLUS_EQUAL); - "-=" tok(T_MINUS_EQUAL); - "*=" tok(T_MUL_EQUAL); - "/=" tok(T_DIV_EQUAL); - ".=" tok(T_CONCAT_EQUAL); - "%=" tok(T_MOD_EQUAL); - "&=" tok(T_AND_EQUAL); - "|=" tok(T_OR_EQUAL); - "^=" tok(T_XOR_EQUAL); - "<<=" tok(T_SL_EQUAL); - ">>=" tok(T_SR_EQUAL); - "||" tok(T_BOOLEAN_OR); - "&&" tok(T_BOOLEAN_AND); - "==" tok(T_IS_EQUAL); - "!="|"<>" tok(T_IS_NOT_EQUAL); - "===" tok(T_IS_IDENTICAL); - "!==" tok(T_IS_NOT_IDENTICAL); - "<=" tok(T_IS_SMALLER_OR_EQUAL); - ">=" tok(T_IS_GREATER_OR_EQUAL); - "<<" tok(T_SL); - ">>" tok(T_SR); - "++" tok(T_INC); - "--" tok(T_DEC); - "->" tok(T_OBJECT_OPERATOR); - "=>" tok(T_DOUBLE_ARROW); - "::" tok(T_PAAMAYIM_NEKUDOTAYIM); - "\\" tok(T_NS_SEPARATOR); - ":" { - // A colon can either mean the start (or component) of an XHP class, - // a ternary expression (as in 1?false:null), the colon of a 'case', - // or finally the start of a block in the old PHP syntax. The following - // disambiguate between the XHP case, which requires a special token, - // and the other cases. - switch (yyextra->last_token) { - // In a ternary expression, the colon must follow a full-fledged - // expression so seeing for instance a binary operator means - // it must be an XHP class. - case ',': case '=': case '|': case '^': case '&': case '<': case '>': - case '+': case '-': case '%': case '!': case '~': case '[': case '(': - case '{': case '.': - case T_LOGICAL_OR: case T_LOGICAL_XOR: case T_LOGICAL_AND: - case T_PLUS_EQUAL: case T_MINUS_EQUAL: case T_MUL_EQUAL: - case T_DIV_EQUAL: case T_CONCAT_EQUAL: case T_MOD_EQUAL: - case T_AND_EQUAL: case T_OR_EQUAL: case T_XOR_EQUAL: - case T_SL_EQUAL: case T_SR_EQUAL: case T_BOOLEAN_OR: - case T_BOOLEAN_AND: case T_IS_EQUAL: case T_IS_NOT_EQUAL: - case T_IS_IDENTICAL: case T_IS_NOT_IDENTICAL: case T_IS_SMALLER_OR_EQUAL: - case T_IS_GREATER_OR_EQUAL: - // An XHP class can also occur after certain keywords. Not sure - // we got them all covered though. - case T_ECHO: case T_RETURN: - case T_EXTENDS: case T_INSTANCEOF: case T_DOUBLE_ARROW: - case T_XHP_ATTRIBUTE: - tok(T_XHP_COLON); - break; - default: - tok(':'); - break; - } - } -} - - /* Casts */ -{ - "("{TABS_AND_SPACES}(int|integer){TABS_AND_SPACES}")" tok(T_INT_CAST); - "("{TABS_AND_SPACES}(real|double|float){TABS_AND_SPACES}")" tok(T_DOUBLE_CAST); - "("{TABS_AND_SPACES}string{TABS_AND_SPACES}")" tok(T_STRING_CAST); - "("{TABS_AND_SPACES}unicode{TABS_AND_SPACES}")" tok(T_UNICODE_CAST); - "("{TABS_AND_SPACES}binary{TABS_AND_SPACES}")" tok(T_BINARY_CAST); - "("{TABS_AND_SPACES}array{TABS_AND_SPACES}")" tok(T_ARRAY_CAST); - "("{TABS_AND_SPACES}object{TABS_AND_SPACES}")" tok(T_OBJECT_CAST); - "("{TABS_AND_SPACES}(bool|boolean){TABS_AND_SPACES}")" tok(T_BOOL_CAST); - "("{TABS_AND_SPACES}unset{TABS_AND_SPACES}")" tok(T_UNSET_CAST); -} - - /* Scalars (parsing these doesn't really matter since we just pass them through literally) */ -{ - {LNUM}|{HNUM} tok(T_LNUMBER); - {DNUM}|{EXPONENT_DNUM} tok(T_DNUMBER); - {LABEL} tok(T_STRING); - "$"{LABEL} tok(T_VARIABLE); - b?'(\\.|\\\n|[^\\']+)*'|b?\"(\\.|\\\n|[^\\\"]+)*\" { - yy_scan_newlines(yytext, yyg); - tok(T_CONSTANT_ENCAPSED_STRING); - } - `[^`]*` { - yy_scan_newlines(yytext, yyg); - tok(T_BACKTICKS_EXPR); - } -} - - /* (HERE|NOW)DOC's */ -b?"<<<"{TABS_AND_SPACES} { - push_state(PHP_HEREDOC_START); - yyextra->heredoc_yyleng = yyleng; - yymore(); -} -{ - "'"{LABEL}"'"|\"{LABEL}\" { - // Create a new string for the heredoc label. Since we're using yymore above - // yytext will actually start at the "<<<" and not the label. Use of - // heredoc_yyleng jumps past that. Then we add 1 to get past the " or '. The - // match is similar to calculate length. - yyextra->heredoc_label = string(yytext + yyextra->heredoc_yyleng + 1, yyleng - yyextra->heredoc_yyleng - 2); - set_state(PHP_HEREDOC_NSTART); - yyextra->heredoc_yyleng = yyleng; - yymore(); - } - {LABEL} { - yyextra->heredoc_label = string(yytext + yyextra->heredoc_yyleng); - set_state(PHP_HEREDOC_NSTART); - yyextra->heredoc_yyleng = yyleng; - yymore(); - } -} -{NEWLINE} { - ++yyextra->lineno; - yyextra->heredoc_data = yytext + yyleng; - set_state(PHP_HEREDOC_DATA); - yymore(); -} -{ - [^\r\n]*{NEWLINE} { - ++yyextra->lineno; - set_state(PHP_HEREDOC_NEWLINE); - yyextra->heredoc_yyleng = yyleng; - yymore(); - } -} -{ - {LABEL};?{NEWLINE} { - if (strncmp(yyextra->heredoc_label.c_str(), yytext + yyextra->heredoc_yyleng, yyextra->heredoc_label.size()) == 0) { - switch (yytext[yyextra->heredoc_yyleng + yyextra->heredoc_label.size()]) { - case ';': case '\n': case '\r': - yyless(yyleng - (yyleng - yyextra->heredoc_yyleng - yyextra->heredoc_label.size())); - pop_state(); - tok(T_HEREDOC); - } - } - ++yyextra->lineno; - yyextra->heredoc_yyleng = yyleng; - yymore(); - } - [^\r\n]+ { - set_state(PHP_HEREDOC_DATA); - yyextra->heredoc_yyleng = yyleng; - yymore(); - } - {NEWLINE} { - ++yyextra->lineno; - yyextra->heredoc_yyleng = yyleng; - yymore(); - } -} - - /* XHP */ -{ - {WHITESPACE}+ { - yy_scan_newlines(yytext, yyg); - ptok(T_WHITESPACE); - } -} -{ - ":" tok(T_XHP_COLON); - "-" tok(T_XHP_HYPHEN); - "::" { - pop_state(); - - yyextra->colon_hack = true; - tok(T_PAAMAYIM_NEKUDOTAYIM); - } - "--" { - pop_state(); - tok(T_DEC); - } - {WHITESPACE} { - yy_scan_newlines(yytext, yyg); - pop_state(); - tok(T_XHP_WHITESPACE); - } - {LABEL} tok(T_STRING); - . { - pop_state(); - tok(yytext[0]); - } -} - -{ - "="|"/"|">" tok(yytext[0]); - {WHITESPACE}+ { - yy_scan_newlines(yytext, yyg); - ptok(T_WHITESPACE); - } - {LABEL} tok(T_STRING); -} - -{ - [^&'\\"]+ tok(T_XHP_TEXT); - \" { - pop_state(); - tok('"'); - } -} - -{ - {WHITESPACE}+ { - /* ignore whitespace at the start */ - yy_scan_newlines(yytext, yyg); - ptok(T_WHITESPACE); - set_state(XHP_CHILD); - } - . { - yyless(0); - set_state(XHP_CHILD); - } -} - - /* Below we use tokt() (and not tok) which internally transits to - * the XHP_AFTER_ENT state. */ -{ - /* xml entities */ - (?-i:") tokt("\""); - (?-i:&) tokt("&"); - (?-i:') tokt("\\'"); - (?-i:<) tokt("<") - (?-i:>) tokt(">"); - - /* html entities */ - (?-i: ) tokt("\u00A0"); - (?-i:¡) tokt("\u00A1"); - (?-i:¢) tokt("\u00A2"); - (?-i:£) tokt("\u00A3"); - (?-i:¤) tokt("\u00A4"); - (?-i:¥) tokt("\u00A5"); - (?-i:¦) tokt("\u00A6"); - (?-i:§) tokt("\u00A7"); - (?-i:¨) tokt("\u00A8"); - (?-i:©) tokt("\u00A9"); - (?-i:ª) tokt("\u00AA"); - (?-i:«) tokt("\u00AB"); - (?-i:¬) tokt("\u00AC"); - (?-i:­) tokt("\u00AD"); - (?-i:®) tokt("\u00AE"); - (?-i:¯) tokt("\u00AF"); - (?-i:°) tokt("\u00B0"); - (?-i:±) tokt("\u00B1"); - (?-i:²) tokt("\u00B2"); - (?-i:³) tokt("\u00B3"); - (?-i:´) tokt("\u00B4"); - (?-i:µ) tokt("\u00B5"); - (?-i:¶) tokt("\u00B6"); - (?-i:·) tokt("\u00B7"); - (?-i:¸) tokt("\u00B8"); - (?-i:¹) tokt("\u00B9"); - (?-i:º) tokt("\u00BA"); - (?-i:») tokt("\u00BB"); - (?-i:¼) tokt("\u00BC"); - (?-i:½) tokt("\u00BD"); - (?-i:¾) tokt("\u00BE"); - (?-i:¿) tokt("\u00BF"); - (?-i:À) tokt("\u00C0"); - (?-i:Á) tokt("\u00C1"); - (?-i:Â) tokt("\u00C2"); - (?-i:Ã) tokt("\u00C3"); - (?-i:Ä) tokt("\u00C4"); - (?-i:Å) tokt("\u00C5"); - (?-i:Æ) tokt("\u00C6"); - (?-i:Ç) tokt("\u00C7"); - (?-i:È) tokt("\u00C8"); - (?-i:É) tokt("\u00C9"); - (?-i:Ê) tokt("\u00CA"); - (?-i:Ë) tokt("\u00CB"); - (?-i:Ì) tokt("\u00CC"); - (?-i:Í) tokt("\u00CD"); - (?-i:Î) tokt("\u00CE"); - (?-i:Ï) tokt("\u00CF"); - (?-i:Ð) tokt("\u00D0"); - (?-i:Ñ) tokt("\u00D1"); - (?-i:Ò) tokt("\u00D2"); - (?-i:Ó) tokt("\u00D3"); - (?-i:Ô) tokt("\u00D4"); - (?-i:Õ) tokt("\u00D5"); - (?-i:Ö) tokt("\u00D6"); - (?-i:×) tokt("\u00D7"); - (?-i:Ø) tokt("\u00D8"); - (?-i:Ù) tokt("\u00D9"); - (?-i:Ú) tokt("\u00DA"); - (?-i:Û) tokt("\u00DB"); - (?-i:Ü) tokt("\u00DC"); - (?-i:Ý) tokt("\u00DD"); - (?-i:Þ) tokt("\u00DE"); - (?-i:ß) tokt("\u00DF"); - (?-i:à) tokt("\u00E0"); - (?-i:á) tokt("\u00E1"); - (?-i:â) tokt("\u00E2"); - (?-i:ã) tokt("\u00E3"); - (?-i:ä) tokt("\u00E4"); - (?-i:å) tokt("\u00E5"); - (?-i:æ) tokt("\u00E6"); - (?-i:ç) tokt("\u00E7"); - (?-i:è) tokt("\u00E8"); - (?-i:é) tokt("\u00E9"); - (?-i:ê) tokt("\u00EA"); - (?-i:ë) tokt("\u00EB"); - (?-i:ì) tokt("\u00EC"); - (?-i:í) tokt("\u00ED"); - (?-i:î) tokt("\u00EE"); - (?-i:ï) tokt("\u00EF"); - (?-i:ð) tokt("\u00F0"); - (?-i:ñ) tokt("\u00F1"); - (?-i:ò) tokt("\u00F2"); - (?-i:ó) tokt("\u00F3"); - (?-i:ô) tokt("\u00F4"); - (?-i:õ) tokt("\u00F5"); - (?-i:ö) tokt("\u00F6"); - (?-i:÷) tokt("\u00F7"); - (?-i:ø) tokt("\u00F8"); - (?-i:ù) tokt("\u00F9"); - (?-i:ú) tokt("\u00FA"); - (?-i:û) tokt("\u00FB"); - (?-i:ü) tokt("\u00FC"); - (?-i:ý) tokt("\u00FD"); - (?-i:þ) tokt("\u00FE"); - (?-i:ÿ) tokt("\u00FF"); - (?-i:Œ) tokt("\u0152"); - (?-i:œ) tokt("\u0153"); - (?-i:Š) tokt("\u0160"); - (?-i:š) tokt("\u0161"); - (?-i:Ÿ) tokt("\u0178"); - (?-i:ƒ) tokt("\u0192"); - (?-i:ˆ) tokt("\u02C6"); - (?-i:˜) tokt("\u02DC"); - (?-i:Α) tokt("\u0391"); - (?-i:Β) tokt("\u0392"); - (?-i:Γ) tokt("\u0393"); - (?-i:Δ) tokt("\u0394"); - (?-i:Ε) tokt("\u0395"); - (?-i:Ζ) tokt("\u0396"); - (?-i:Η) tokt("\u0397"); - (?-i:Θ) tokt("\u0398"); - (?-i:Ι) tokt("\u0399"); - (?-i:Κ) tokt("\u039A"); - (?-i:Λ) tokt("\u039B"); - (?-i:Μ) tokt("\u039C"); - (?-i:Ν) tokt("\u039D"); - (?-i:Ξ) tokt("\u039E"); - (?-i:Ο) tokt("\u039F"); - (?-i:Π) tokt("\u03A0"); - (?-i:Ρ) tokt("\u03A1"); - (?-i:Σ) tokt("\u03A3"); - (?-i:Τ) tokt("\u03A4"); - (?-i:Υ) tokt("\u03A5"); - (?-i:Φ) tokt("\u03A6"); - (?-i:Χ) tokt("\u03A7"); - (?-i:Ψ) tokt("\u03A8"); - (?-i:Ω) tokt("\u03A9"); - (?-i:α) tokt("\u03B1"); - (?-i:β) tokt("\u03B2"); - (?-i:γ) tokt("\u03B3"); - (?-i:δ) tokt("\u03B4"); - (?-i:ε) tokt("\u03B5"); - (?-i:ζ) tokt("\u03B6"); - (?-i:η) tokt("\u03B7"); - (?-i:θ) tokt("\u03B8"); - (?-i:ι) tokt("\u03B9"); - (?-i:κ) tokt("\u03BA"); - (?-i:λ) tokt("\u03BB"); - (?-i:μ) tokt("\u03BC"); - (?-i:ν) tokt("\u03BD"); - (?-i:ξ) tokt("\u03BE"); - (?-i:ο) tokt("\u03BF"); - (?-i:π) tokt("\u03C0"); - (?-i:ρ) tokt("\u03C1"); - (?-i:ς) tokt("\u03C2"); - (?-i:σ) tokt("\u03C3"); - (?-i:τ) tokt("\u03C4"); - (?-i:υ) tokt("\u03C5"); - (?-i:φ) tokt("\u03C6"); - (?-i:χ) tokt("\u03C7"); - (?-i:ψ) tokt("\u03C8"); - (?-i:ω) tokt("\u03C9"); - (?-i:ϑ) tokt("\u03D1"); - (?-i:ϒ) tokt("\u03D2"); - (?-i:ϖ) tokt("\u03D6"); - (?-i: ) tokt("\u2002"); - (?-i: ) tokt("\u2003"); - (?-i: ) tokt("\u2009"); - (?-i:‌) tokt("\u200C"); - (?-i:‍) tokt("\u200D"); - (?-i:‎) tokt("\u200E"); - (?-i:‏) tokt("\u200F"); - (?-i:–) tokt("\u2013"); - (?-i:—) tokt("\u2014"); - (?-i:‘) tokt("\u2018"); - (?-i:’) tokt("\u2019"); - (?-i:‚) tokt("\u201A"); - (?-i:“) tokt("\u201C"); - (?-i:”) tokt("\u201D"); - (?-i:„) tokt("\u201E"); - (?-i:†) tokt("\u2020"); - (?-i:‡) tokt("\u2021"); - (?-i:•) tokt("\u2022"); - (?-i:…) tokt("\u2026"); - (?-i:‰) tokt("\u2030"); - (?-i:′) tokt("\u2032"); - (?-i:″) tokt("\u2033"); - (?-i:‹) tokt("\u2039"); - (?-i:›) tokt("\u203A"); - (?-i:‾) tokt("\u203E"); - (?-i:⁄) tokt("\u2044"); - (?-i:€) tokt("\u20AC"); - (?-i:ℑ) tokt("\u2111"); - (?-i:℘) tokt("\u2118"); - (?-i:ℜ) tokt("\u211C"); - (?-i:™) tokt("\u2122"); - (?-i:ℵ) tokt("\u2135"); - (?-i:←) tokt("\u2190"); - (?-i:↑) tokt("\u2191"); - (?-i:→) tokt("\u2192"); - (?-i:↓) tokt("\u2193"); - (?-i:↔) tokt("\u2194"); - (?-i:↵) tokt("\u21B5"); - (?-i:⇐) tokt("\u21D0"); - (?-i:⇑) tokt("\u21D1"); - (?-i:⇒) tokt("\u21D2"); - (?-i:⇓) tokt("\u21D3"); - (?-i:⇔) tokt("\u21D4"); - (?-i:∀) tokt("\u2200"); - (?-i:∂) tokt("\u2202"); - (?-i:∃) tokt("\u2203"); - (?-i:∅) tokt("\u2205"); - (?-i:∇) tokt("\u2207"); - (?-i:∈) tokt("\u2208"); - (?-i:∉) tokt("\u2209"); - (?-i:∋) tokt("\u220B"); - (?-i:∏) tokt("\u220F"); - (?-i:∑) tokt("\u2211"); - (?-i:−) tokt("\u2212"); - (?-i:∗) tokt("\u2217"); - (?-i:√) tokt("\u221A"); - (?-i:∝) tokt("\u221D"); - (?-i:∞) tokt("\u221E"); - (?-i:∠) tokt("\u2220"); - (?-i:∧) tokt("\u2227"); - (?-i:∨) tokt("\u2228"); - (?-i:∩) tokt("\u2229"); - (?-i:∪) tokt("\u222A"); - (?-i:∫) tokt("\u222B"); - (?-i:∴) tokt("\u2234"); - (?-i:∼) tokt("\u223C"); - (?-i:≅) tokt("\u2245"); - (?-i:≈) tokt("\u2248"); - (?-i:≠) tokt("\u2260"); - (?-i:≡) tokt("\u2261"); - (?-i:≤) tokt("\u2264"); - (?-i:≥) tokt("\u2265"); - (?-i:⊂) tokt("\u2282"); - (?-i:⊃) tokt("\u2283"); - (?-i:⊄) tokt("\u2284"); - (?-i:⊆) tokt("\u2286"); - (?-i:⊇) tokt("\u2287"); - (?-i:⊕) tokt("\u2295"); - (?-i:⊗) tokt("\u2297"); - (?-i:⊥) tokt("\u22A5"); - (?-i:⋅) tokt("\u22C5"); - (?-i:⌈) tokt("\u2308"); - (?-i:⌉) tokt("\u2309"); - (?-i:⌊) tokt("\u230A"); - (?-i:⌋) tokt("\u230B"); - (?-i:⟨) tokt("\u2329"); - (?-i:⟩) tokt("\u232A"); - (?-i:◊) tokt("\u25CA"); - (?-i:♠) tokt("\u2660"); - (?-i:♣) tokt("\u2663"); - (?-i:♥) tokt("\u2665"); - (?-i:♦) tokt("\u2666"); - - /* awesome entities */ - (?-i:&cloud;) tokt("\u2601"); - (?-i:&umbrella;) tokt("\u2602"); - (?-i:&snowman;) tokt("\u2603"); - (?-i:&snowflake;) tokt("\u2745"); - (?-i:&comet;) tokt("\u2604"); - (?-i:&thunderstorm;) tokt("\u2608"); - - /* pseudo entities */ - ' tokt("\\'"); - "\\" tokt("\\\\"); - - /* meta entities */ - (?-i:&#[0-9]+;) { - char buf[5]; - utf8ize(atoi(yytext + 2), buf); - tokt(buf); - } - (?-i:&#x)[A-F0-9]+; { - char buf[5]; - char *_; - utf8ize(strtol(yytext + 3, &_, 16), buf); - tokt(buf); - } - - /* not entities */ - & { - yymore(); - BEGIN(XHP_INVALID_ENTITY); - } -} - -{ - {BYTE}{1,10} { - for (char* ii = yytext; *ii; ++ii) { - if (*ii == ';') { - ii[1] = 0; - break; - } - } - if (!yyextra->terminated) { - yyextra->error = string("Invalid entity: (") + yytext + ")"; - yyextra->terminated = true; - } - } -} - -{ - [ \t\x0b\x0c\xa0\r\n]|\r\n { - if (*yytext == '\r' || *yytext == '\n') { - // Since we rewrite newlines into space we need to increment both line - // counters. The first_lineno increment is quite a hack, and makes it so - // that this ent is on the wrong line but it doesn't mess up the rest of - // the file. - ++yyextra->lineno; - ++yyextra->first_lineno; - } - pop_state(); - tok(T_XHP_TEXT); - } - . { - pop_state(); - yyless(0); - } -} - -{ - [^&'<>\\{ \t\x0b\x0c\xa0\r\n]+{WHITESPACE}? { - yy_scan_newlines(yytext, yyg); - tok(T_XHP_TEXT); - } - {WHITESPACE}* { - yy_scan_newlines(yytext, yyg); - tok(T_XHP_TEXT); - } - /* TODO: I removed {WHITESPACE}* from all of these but it needs to be restored - if the tree is unflattened. */ - "{" { - yy_scan_newlines(yytext, yyg); - tok('{'); - } - "<" { - yy_scan_newlines(yytext, yyg); - tok('<'); - } - "" { - yy_scan_newlines(yytext, yyg); - tok(T_XHP_LT_DIV_GT); - } -} - -{ - any tok(T_XHP_ANY); - pcdata tok(T_XHP_PCDATA); - empty tok(T_XHP_EMPTY); - {LABEL} tok(T_STRING); - ";" { - pop_state(); - tok(';'); - } - ":" { - tok(T_XHP_COLON); - } -} - - /* Other */ -<*>{BYTE} { - tok(yytext[0]); - // fix unused function warnings - yy_top_state(NULL); - yyunput(0, 0, NULL); -} - -%% - -#ifdef DEBUG -static const char* yy_state_name(int state) { - switch (state) { - case INITIAL: - return "INITIAL"; - case PHP: - return "PHP"; - case PHP_COMMENT: - return "PHP_COMMENT"; - case PHP_EOL_COMMENT: - return "PHP_EOL_COMMENT"; - case PHP_DOC_COMMENT: - return "PHP_DOC_COMMENT"; - case PHP_HEREDOC_START: - return "PHP_HEREDOC_START"; - case PHP_HEREDOC_NSTART: - return "PHP_HEREDOC_NSTART"; - case PHP_HEREDOC_NEWLINE: - return "PHP_HEREDOC_NEWLINE"; - case PHP_HEREDOC_DATA: - return "PHP_HEREDOC_DATA"; - case PHP_NO_RESERVED_WORDS: - return "PHP_NO_RESERVED_WORDS"; - case PHP_NO_RESERVED_WORDS_PERSIST: - return "PHP_NO_RESERVED_WORDS_PERSIST"; - case XHP_LABEL: - return "XHP_LABEL"; - case XHP_LABEL_WHITESPACE: - return "XHP_LABEL_WHITESPACE"; - case XHP_ATTRS: - return "XHP_ATTRS"; - case XHP_ATTR_VAL: - return "XHP_ATTR_VAL"; - case XHP_AFTER_ENT: - return "XHP_AFTER_ENT"; - case XHP_CHILD: - return "XHP_CHILD"; - case XHP_CHILD_START: - return "XHP_CHILD_START"; - case XHP_INVALID_ENTITY: - return "XHP_INVALID_ENTITY"; - case XHP_ATTR_TYPE_DECL: - return "XHP_ATTR_TYPE_DECL"; - case XHP_CHILDREN_DECL: - return "XHP_CHILDREN_DECL"; - default: - return "???"; - } -} - -static void yy_log_token(int tok) { - const char* tokname = yytokname(tok); - if (tokname) { - fprintf(stderr, "--> %s\n", tokname); - } else { - fprintf(stderr, "--> '%c'\n", tok); - } -} -#endif - -static int yy_token(int tok, yyguts_t* yyg) { - if (YY_START == PHP_NO_RESERVED_WORDS) { - pop_state(); - } - - switch (tok) { - case T_OPEN_TAG: - case T_OPEN_TAG_WITH_ECHO: - case T_OPEN_TAG_FAKE: - push_state(PHP); - break; - - case T_CLOSE_TAG: - pop_state(); - // We need to return a ';', not a T_CLOSE_TAG, because a construct like - // "" is valid and there are about a billion parser rules - // which terminate with ';' so making a new rule like - // "semicolon_or_close_tag" would be hard. The token in yylval has the - // correct type and value, we just don't generate a node. - return ';'; - - // In PHP it's ok to use keywords such as 'if' as field names - // or function names. - case T_OBJECT_OPERATOR: - case T_FUNCTION: - push_state(PHP_NO_RESERVED_WORDS); - break; - - case T_PAAMAYIM_NEKUDOTAYIM: - if (yyextra->colon_hack) { - yyextra->colon_hack = false; - } else { - push_state(PHP_NO_RESERVED_WORDS); - } - break; - - case '{': - // not used anymore - yyextra->curly_stack.push(tok); - break; - } -#ifdef DEBUG - yy_log_token(tok); -#endif - return yyextra->last_token = tok; -} - -static inline void yy_scan_newlines(const char* text, struct yyguts_t* yyg) { - for (; *text; ++text) { - if (*text == '\r') { - if (text[1] == '\n') { - ++text; - } - ++yyextra->lineno; - } else if (*text == '\n') { - ++yyextra->lineno; - } - } -} - -void xhp_new_push_state(int s, struct yyguts_t* yyg) { -#ifdef DEBUG - fprintf(stderr, "--> PUSH(%s -> %s)\n", yy_state_name(YY_START), yy_state_name(s)); -#endif - yy_push_state(s, yyg); -} - -void xhp_new_pop_state(struct yyguts_t* yyg) { -#ifdef DEBUG - int s = YY_START; -#endif - yy_pop_state(yyg); -#ifdef DEBUG - fprintf(stderr, "--> POP(%s -> %s)\n", yy_state_name(s), yy_state_name(YY_START)); -#endif -} - -void xhp_set_state(int s, struct yyguts_t* yyg) { -#ifdef DEBUG - fprintf(stderr, "--> SET(%s)\n", yy_state_name(s)); -#endif - BEGIN(s); -} diff --git a/support/xhpast/xhpast.cpp b/support/xhpast/xhpast.cpp deleted file mode 100755 index a1619856..00000000 --- a/support/xhpast/xhpast.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2011 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ast.hpp" -#include -#include -#include -#include -#include -using namespace std; - -int xhpastparse(void*, xhpast::Node **); - -int xhpast_process(std::string &in); -void print_node(xhpast::Node *node); - -int main(int argc, char* argv[]) { - vector files; - -/* if (argc != 2) { - cerr << "usage: xhpast \n"; - return 1; - } - */ - - ifstream inputFile; - istream *inputStream; -// inputFile.open(argv[1]); -// inputStream = &inputFile; - inputStream = &cin; - - std::stringbuf sb; - *inputStream >> noskipws >> &sb; - std::string buffer = sb.str(); - inputFile.close(); - - return xhpast_process(buffer); -} - -int xhpast_process(std::string &in) { - - char *buffer; - in.reserve(in.size() + 1); - buffer = const_cast(in.c_str()); - buffer[in.size() + 1] = 0; // need double NULL for scan_buffer - - void* scanner; - yy_extra_type extra; - extra.idx_expr = true;//flags.idx_expr; - extra.include_debug = true;//flags.include_debug; - extra.insert_token = 0;//flags.eval ? T_OPEN_TAG_FAKE : 0; - extra.short_tags = true;//flags.short_tags; - extra.asp_tags = false;//flags.asp_tags; - - xhpast::Node *root = NULL; - - xhpastlex_init(&scanner); - xhpastset_extra(&extra, scanner); - xhpast_scan_buffer(buffer, in.size() + 2, scanner); -#ifdef DEBUG - xhpdebug = 1; -#endif - xhpastparse(scanner, &root); - xhpastlex_destroy(scanner); - - if (extra.terminated) { - fprintf( - stderr, - "XHPAST Parse Error: %s on line %d\n", - extra.error.c_str(), - (int)extra.lineno); - return 1; - } - - printf("{"); - printf("\"tree\":"); - if (root) { - // Extend the right token for the root node to the end of the concrete - // token stream. This ensure all tokens appear in the tree. If we don't - // do this and the file ends in tokens which don't go to the parser (like - // comments and whitespace) they won't be represented in the tree. - root->r_tok = (extra.token_list.size() - 1); - print_node(root); - } else { - printf("null"); - } - printf(","); - printf("\"stream\":"); - printf("["); - - if (!extra.token_list.empty()) { - for (xhpast::token_list_t::iterator ii = extra.token_list.begin();;) { - printf("[%d, %d]", (*ii)->type, (int)(*ii)->value.length()); - if (++ii != extra.token_list.end()) { - printf(","); - } else { - break; - } - } - } - printf("]"); - printf("}\n"); - - return 0; -} - -void print_node(xhpast::Node *node) { - int l = -1; - int r = -1; - if (node->l_tok != -1) { - l = node->l_tok; - } - - if (l == -1) { - printf("[%d]", node->type); - } else { - if (node->r_tok != -1) { - r = node->r_tok; - } - - printf("[%d, %d, %d", node->type, l, r); - if (!node->children.empty()) { - printf(", ["); - for (xhpast::node_list_t::iterator ii = node->children.begin();;) { - print_node(*ii); - if (++ii != node->children.end()) { - printf(","); - } else { - break; - } - } - printf("]"); - } - printf("]"); - } -} -