2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_PARSER_PARSER_H_
18 #define incl_HPHP_PARSER_PARSER_H_
26 #include "hphp/parser/scanner.h"
27 #include "hphp/util/lock.h"
28 #include "hphp/util/functional.h"
29 #include "hphp/util/hash-map-typedefs.h"
31 #include <folly/String.h>
33 #define IMPLEMENT_XHP_ATTRIBUTES \
34 Token m_xhpAttributes; \
35 Token *xhpGetAttributes() { \
36 if (m_xhpAttributes.num()) { \
37 return &m_xhpAttributes; \
41 void xhpSetAttributes(Token &t) { \
42 m_xhpAttributes = t; \
43 m_xhpAttributes = 1; \
45 void xhpResetAttributes() { \
46 m_xhpAttributes.reset(); \
49 #define NAMESPACE_SEP '\\'
53 //////////////////////////////////////////////////////////////////////
56 * HHVM supports multiple types of lambda expressions.
58 enum class ClosureType
{
60 * Short = Lambda syntax. Automatically captures variables mentioned in the
66 * Long = Traditional closure syntax. Only captures variables that are
67 * explicitly specified in the "use" list.
72 enum class PropAccessType
{
82 //////////////////////////////////////////////////////////////////////
84 typedef void* TStatementPtr
;
95 static bool IsClosureName(const std::string
&name
);
97 * Is this the name of an anonymous class (either a closure,
98 * or a ClassExpression).
100 static bool IsAnonymousClassName(folly::StringPiece name
);
102 std::string
newClosureName(
103 const std::string
&namespaceName
,
104 const std::string
&className
,
105 const std::string
&funcName
);
106 std::string
newAnonClassName(
107 const std::string
&prefix
,
108 const std::string
&namespaceName
,
109 const std::string
&className
,
110 const std::string
&funcName
);
113 ParserBase(Scanner
&scanner
, const char *fileName
);
114 virtual ~ParserBase();
116 Scanner
&scanner() { return m_scanner
;}
119 * Main function to call to start parsing the file. This function is already
120 * implemented in hphp.y. Therefore, a subclass only has to declare it.
122 virtual bool parseImpl() = 0;
125 * Raise a parser error.
127 virtual void error(ATTRIBUTE_PRINTF_STRING
const char* fmt
, ...)
128 ATTRIBUTE_PRINTF(2,3) = 0;
133 const char *file() const { return m_fileName
;}
134 std::string
getMessage(bool filename
= false,
135 bool rawPosWhenNoError
= false) const;
136 std::string
getMessage(const Location::Range
& loc
,
137 bool filename
= false) const;
138 const Location::Range
& getRange() const;
140 int line0() const { return m_loc
.r
.line0
;}
141 int char0() const { return m_loc
.r
.char0
;}
142 int line1() const { return m_loc
.r
.line1
;}
143 int char1() const { return m_loc
.r
.char1
;}
144 int cursor() const { return m_loc
.cursor
;}
146 // called by generated code
147 int scan(ScannerToken
*token
, Location
*loc
) {
148 return m_scanner
.getNextToken(*token
, *loc
);
150 void setRuleLocation(Location
*loc
) {
153 virtual void fatal(const Location
* loc
, const char* msg
) {}
154 virtual void parseFatal(const Location
* loc
, const char* msg
) {}
156 void pushFuncLocation();
157 Location::Range
popFuncLocation();
158 void pushClass(bool isXhpClass
);
162 // for typevar checking
163 void pushTypeScope();
165 void addTypeVar(const std::string
&name
);
166 bool isTypeVar(const std::string
&name
);
167 bool isTypeVarInImmediateScope(const std::string
&name
);
169 // for goto syntax checking
170 void pushLabelInfo();
171 void pushLabelScope();
172 void popLabelScope();
173 void addLabel(const std::string
&label
, const Location::Range
& loc
,
175 void addGoto(const std::string
&label
, const Location::Range
& loc
,
184 virtual void invalidateGoto(TStatementPtr expr
, GotoError error
) = 0;
185 virtual void invalidateLabel(TStatementPtr expr
) = 0;
187 virtual TStatementPtr
extractStatement(ScannerToken
*stmt
) = 0;
191 const char *m_fileName
;
194 std::vector
<Location::Range
> m_funcLocs
;
195 std::vector
<bool> m_classes
; // used to determine if we are currently
196 // inside a regular class or an XHP class
198 struct LabelStmtInfo
{
202 bool inTryCatchBlock
;
204 typedef std::map
<std::string
, LabelStmtInfo
> LabelMap
;
205 // name => LabelStmtInfo
207 typedef std::set
<std::string
> TypevarScope
;
208 typedef std::vector
<TypevarScope
> TypevarScopeStack
;
209 TypevarScope m_typeVars
;
210 TypevarScopeStack m_typeScopes
;
212 // for goto syntax checking
213 typedef std::vector
<int> LabelScopes
;
222 LabelInfo() : scopeId(0) {}
226 std::vector
<GotoInfo
> gotos
;
228 std::vector
<LabelInfo
> m_labelInfos
; // stack by function
230 // for namespace support
231 enum NamespaceState
{
233 SeenNonNamespaceStatement
,
234 SeenNamespaceStatement
,
237 NamespaceState m_nsState
;
239 std::string m_namespace
; // current namespace
240 hphp_string_imap
<std::string
> m_aliases
;
241 hphp_string_imap
<int> m_seenAnonClasses
;
244 ///////////////////////////////////////////////////////////////////////////////
247 #endif // incl_HPHP_PARSER_PARSER_H_