1+ // This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2+ #pragma once
3+
4+ #include " Luau/Location.h"
5+
6+ #include < string>
7+
8+ namespace Luau
9+ {
10+
11+ extern int gCstRttiIndex ;
12+
13+ template <typename T>
14+ struct CstRtti
15+ {
16+ static const int value;
17+ };
18+
19+ template <typename T>
20+ const int CstRtti<T>::value = ++gCstRttiIndex ;
21+
22+ #define LUAU_CST_RTTI (Class ) \
23+ static int CstClassIndex () \
24+ { \
25+ return CstRtti<Class>::value; \
26+ }
27+
28+ class CstNode
29+ {
30+ public:
31+ explicit CstNode (int classIndex)
32+ : classIndex(classIndex)
33+ {
34+ }
35+
36+ template <typename T>
37+ bool is () const
38+ {
39+ return classIndex == T::CstClassIndex ();
40+ }
41+ template <typename T>
42+ T* as ()
43+ {
44+ return classIndex == T::CstClassIndex () ? static_cast <T*>(this ) : nullptr ;
45+ }
46+ template <typename T>
47+ const T* as () const
48+ {
49+ return classIndex == T::CstClassIndex () ? static_cast <const T*>(this ) : nullptr ;
50+ }
51+
52+ const int classIndex;
53+ };
54+
55+ class CstExprConstantNumber : public CstNode
56+ {
57+ public:
58+ LUAU_CST_RTTI (CstExprConstantNumber)
59+
60+ explicit CstExprConstantNumber (const AstArray<char >& value);
61+
62+ AstArray<char > value;
63+ };
64+
65+ class CstExprConstantString : public CstNode
66+ {
67+ public:
68+ LUAU_CST_RTTI (CstExprConstantNumber)
69+
70+ enum QuoteStyle
71+ {
72+ QuotedSingle,
73+ QuotedDouble,
74+ QuotedRaw,
75+ QuotedInterp,
76+ };
77+
78+ CstExprConstantString (AstArray<char > sourceString, QuoteStyle quoteStyle, unsigned int blockDepth);
79+
80+ AstArray<char > sourceString;
81+ QuoteStyle quoteStyle;
82+ unsigned int blockDepth;
83+ };
84+
85+ class CstExprCall : public CstNode
86+ {
87+ public:
88+ LUAU_CST_RTTI (CstExprCall)
89+
90+ CstExprCall (std::optional<Position> openParens, std::optional<Position> closeParens, AstArray<Position> commaPositions);
91+
92+ std::optional<Position> openParens;
93+ std::optional<Position> closeParens;
94+ AstArray<Position> commaPositions;
95+ };
96+
97+ class CstExprIndexExpr : public CstNode
98+ {
99+ public:
100+ LUAU_CST_RTTI (CstExprIndexExpr)
101+
102+ CstExprIndexExpr (Position openBracketPosition, Position closeBracketPosition);
103+
104+ Position openBracketPosition;
105+ Position closeBracketPosition;
106+ };
107+
108+ class CstExprTable : public CstNode
109+ {
110+ public:
111+ LUAU_CST_RTTI (CstExprTable)
112+
113+ enum Separator
114+ {
115+ Comma,
116+ Semicolon,
117+ };
118+
119+ struct Item
120+ {
121+ std::optional<Position> indexerOpenPosition; // '[', only if Kind == General
122+ std::optional<Position> indexerClosePosition; // ']', only if Kind == General
123+ std::optional<Position> equalsPosition; // only if Kind != List
124+ std::optional<Separator> separator; // may be missing for last Item
125+ std::optional<Position> separatorPosition;
126+ };
127+
128+ explicit CstExprTable (const AstArray<Item>& items);
129+
130+ AstArray<Item> items;
131+ };
132+
133+ // TODO: Shared between unary and binary, should we split?
134+ class CstExprOp : public CstNode
135+ {
136+ public:
137+ LUAU_CST_RTTI (CstExprOp)
138+
139+ explicit CstExprOp (Position opPosition);
140+
141+ Position opPosition;
142+ };
143+
144+ class CstExprIfElse : public CstNode
145+ {
146+ public:
147+ LUAU_CST_RTTI (CstExprIfElse)
148+
149+ CstExprIfElse (Position thenPosition, Position elsePosition, bool isElseIf);
150+
151+ Position thenPosition;
152+ Position elsePosition;
153+ bool isElseIf;
154+ };
155+
156+ class CstExprInterpString : public CstNode
157+ {
158+ public:
159+ LUAU_CST_RTTI (CstExprInterpString)
160+
161+ explicit CstExprInterpString (AstArray<AstArray<char >> sourceStrings, AstArray<Position> stringPositions);
162+
163+ AstArray<AstArray<char >> sourceStrings;
164+ AstArray<Position> stringPositions;
165+ };
166+
167+ class CstStatDo : public CstNode
168+ {
169+ public:
170+ LUAU_CST_RTTI (CstStatDo)
171+
172+ explicit CstStatDo (Position endPosition);
173+
174+ Position endPosition;
175+ };
176+
177+ class CstStatRepeat : public CstNode
178+ {
179+ public:
180+ LUAU_CST_RTTI (CstStatRepeat)
181+
182+ explicit CstStatRepeat (Position untilPosition);
183+
184+ Position untilPosition;
185+ };
186+
187+ class CstStatReturn : public CstNode
188+ {
189+ public:
190+ LUAU_CST_RTTI (CstStatReturn)
191+
192+ explicit CstStatReturn (AstArray<Position> commaPositions);
193+
194+ AstArray<Position> commaPositions;
195+ };
196+
197+ class CstStatLocal : public CstNode
198+ {
199+ public:
200+ LUAU_CST_RTTI (CstStatLocal)
201+
202+ CstStatLocal (AstArray<Position> varsCommaPositions, AstArray<Position> valuesCommaPositions);
203+
204+ AstArray<Position> varsCommaPositions;
205+ AstArray<Position> valuesCommaPositions;
206+ };
207+
208+ class CstStatFor : public CstNode
209+ {
210+ public:
211+ LUAU_CST_RTTI (CstStatFor)
212+
213+ CstStatFor (Position equalsPosition, Position endCommaPosition, std::optional<Position> stepCommaPosition);
214+
215+ Position equalsPosition;
216+ Position endCommaPosition;
217+ std::optional<Position> stepCommaPosition;
218+ };
219+
220+ class CstStatForIn : public CstNode
221+ {
222+ public:
223+ LUAU_CST_RTTI (CstStatForIn)
224+
225+ CstStatForIn (AstArray<Position> varsCommaPositions, AstArray<Position> valuesCommaPositions);
226+
227+ AstArray<Position> varsCommaPositions;
228+ AstArray<Position> valuesCommaPositions;
229+ };
230+
231+ class CstStatAssign : public CstNode
232+ {
233+ public:
234+ LUAU_CST_RTTI (CstStatAssign)
235+
236+ CstStatAssign (AstArray<Position> varsCommaPositions, Position equalsPosition, AstArray<Position> valuesCommaPositions);
237+
238+ AstArray<Position> varsCommaPositions;
239+ Position equalsPosition;
240+ AstArray<Position> valuesCommaPositions;
241+ };
242+
243+ class CstStatCompoundAssign : public CstNode
244+ {
245+ public:
246+ LUAU_CST_RTTI (CstStatCompoundAssign)
247+
248+ explicit CstStatCompoundAssign (Position opPosition);
249+
250+ Position opPosition;
251+ };
252+
253+ class CstStatLocalFunction : public CstNode
254+ {
255+ public:
256+ LUAU_CST_RTTI (CstStatLocalFunction)
257+
258+ explicit CstStatLocalFunction (Position functionKeywordPosition);
259+
260+ Position functionKeywordPosition;
261+ };
262+
263+ class CstTypeReference : public CstNode
264+ {
265+ public:
266+ LUAU_CST_RTTI (CstTypeReference)
267+
268+ CstTypeReference (
269+ std::optional<Position> prefixPointPosition,
270+ Position openParametersPosition,
271+ AstArray<Position> parametersCommaPositions,
272+ Position closeParametersPosition
273+ );
274+
275+ std::optional<Position> prefixPointPosition;
276+ Position openParametersPosition;
277+ AstArray<Position> parametersCommaPositions;
278+ Position closeParametersPosition;
279+ };
280+
281+ class CstTypeTable : public CstNode
282+ {
283+ public:
284+ LUAU_CST_RTTI (CstTypeTable)
285+
286+ struct Item
287+ {
288+ enum struct Kind
289+ {
290+ Indexer,
291+ Property,
292+ StringProperty,
293+ };
294+
295+ Kind kind;
296+ Position indexerOpenPosition; // '[', only if Kind != Property
297+ Position indexerClosePosition; // ']' only if Kind != Property
298+ Position colonPosition;
299+ std::optional<CstExprTable::Separator> separator; // may be missing for last Item
300+ std::optional<Position> separatorPosition;
301+
302+ CstExprConstantString* stringInfo = nullptr ; // only if Kind == StringProperty
303+ };
304+
305+ CstTypeTable (AstArray<Item> items, bool isArray);
306+
307+ AstArray<Item> items;
308+ bool isArray = false ;
309+ };
310+
311+ class CstTypeTypeof : public CstNode
312+ {
313+ public:
314+ LUAU_CST_RTTI (CstTypeTypeof)
315+
316+ CstTypeTypeof (Position openPosition, Position closePosition);
317+
318+ Position openPosition;
319+ Position closePosition;
320+ };
321+
322+ class CstTypeSingletonString : public CstNode
323+ {
324+ public:
325+ LUAU_CST_RTTI (CstTypeSingletonString)
326+
327+ CstTypeSingletonString (AstArray<char > sourceString, CstExprConstantString::QuoteStyle quoteStyle, unsigned int blockDepth);
328+
329+ AstArray<char > sourceString;
330+ CstExprConstantString::QuoteStyle quoteStyle;
331+ unsigned int blockDepth;
332+ };
333+
334+ } // namespace Luau
0 commit comments