v0.3.4 - Handle commas in argument lists
[hiphop-php.git] / hphp / parser / parser.h
blob0bba0fa262b20d6f740de10266aa369bf7b36d7c
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2016 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_
20 #include <map>
21 #include <set>
22 #include <vector>
23 #include <string>
24 #include <memory>
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; \
38 } \
39 return nullptr; \
40 } \
41 void xhpSetAttributes(Token &t) { \
42 m_xhpAttributes = t; \
43 m_xhpAttributes = 1; \
44 } \
45 void xhpResetAttributes() { \
46 m_xhpAttributes.reset(); \
47 } \
49 #define NAMESPACE_SEP '\\'
51 namespace HPHP {
53 //////////////////////////////////////////////////////////////////////
56 * HHVM supports multiple types of lambda expressions.
58 enum class ClosureType {
60 * Short = Lambda syntax. Automatically captures variables mentioned in the
61 * body.
63 Short,
66 * Long = Traditional closure syntax. Only captures variables that are
67 * explicitly specified in the "use" list.
69 Long,
72 enum class PropAccessType {
73 Normal,
74 NullSafe,
77 enum ObjPropType {
78 ObjPropNormal,
79 ObjPropXhpAttr
82 //////////////////////////////////////////////////////////////////////
84 typedef void* TStatementPtr;
86 struct ParserBase {
87 enum NameKind {
88 StringName,
89 VarName,
90 ExprName,
91 StaticClassExprName,
92 StaticName
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);
112 public:
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;
131 * Public accessors.
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) {
151 m_loc = *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);
159 bool peekClass();
160 void popClass();
162 // for typevar checking
163 void pushTypeScope();
164 void popTypeScope();
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,
174 ScannerToken *stmt);
175 void addGoto(const std::string &label, const Location::Range& loc,
176 ScannerToken *stmt);
177 void popLabelInfo();
179 enum GotoError {
180 UndefLabel = 1,
181 InvalidBlock,
184 virtual void invalidateGoto(TStatementPtr expr, GotoError error) = 0;
185 virtual void invalidateLabel(TStatementPtr expr) = 0;
187 virtual TStatementPtr extractStatement(ScannerToken *stmt) = 0;
189 protected:
190 Scanner &m_scanner;
191 const char *m_fileName;
193 Location m_loc;
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 {
199 TStatementPtr stmt;
200 int scopeId;
201 Location::Range loc;
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;
214 struct GotoInfo {
215 std::string label;
216 LabelScopes scopes;
217 Location::Range loc;
218 TStatementPtr stmt;
221 struct LabelInfo {
222 LabelInfo() : scopeId(0) {}
223 int scopeId;
224 LabelScopes scopes;
225 LabelMap labels;
226 std::vector<GotoInfo> gotos;
228 std::vector<LabelInfo> m_labelInfos; // stack by function
230 // for namespace support
231 enum NamespaceState {
232 SeenNothing,
233 SeenNonNamespaceStatement,
234 SeenNamespaceStatement,
235 InsideNamespace,
237 NamespaceState m_nsState;
238 bool m_nsFileScope;
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_