ScummVM API documentation
as_parser.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2018 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 
32 //
33 // as_parser.h
34 //
35 // This class parses the script code and builds a tree for compilation
36 //
37 
38 
39 
40 #ifndef AS_PARSER_H
41 #define AS_PARSER_H
42 
43 #include "as_scriptnode.h"
44 #include "as_scriptcode.h"
45 #include "as_builder.h"
46 #include "as_tokenizer.h"
47 
48 BEGIN_AS_NAMESPACE
49 
50 class asCParser {
51 public:
52  asCParser(asCBuilder *builder);
53  ~asCParser();
54 
55  int ParseFunctionDefinition(asCScriptCode *script, bool expectListPattern);
56  int ParsePropertyDeclaration(asCScriptCode *script);
57  int ParseDataType(asCScriptCode *script, bool isReturnType);
58  int ParseTemplateDecl(asCScriptCode *script);
59 
60 #ifndef AS_NO_COMPILER
61  int ParseScript(asCScriptCode *script);
62 
63  // Called from compiler
64  int ParseStatementBlock(asCScriptCode *script, asCScriptNode *block);
65  int ParseVarInit(asCScriptCode *script, asCScriptNode *init);
66  int ParseExpression(asCScriptCode *script);
67 #endif
68 
69  asCScriptNode *GetScriptNode();
70 
71 protected:
72  void Reset();
73 
74  void GetToken(sToken *token);
75  void RewindTo(const sToken *token);
76  void SetPos(size_t pos);
77  void Error(const asCString &text, sToken *token);
78  void Warning(const asCString &text, sToken *token);
79  void Info(const asCString &text, sToken *token);
80 
81  asCScriptNode *CreateNode(eScriptNode type);
82 
83  asCScriptNode *ParseFunctionDefinition();
84  asCScriptNode *ParseParameterList();
85  asCScriptNode *SuperficiallyParseExpression();
86  asCScriptNode *ParseType(bool allowConst, bool allowVariableType = false, bool allowAuto = false);
87  asCScriptNode *ParseTypeMod(bool isParam);
88  void ParseOptionalScope(asCScriptNode *node);
89  asCScriptNode *ParseRealType();
90  asCScriptNode *ParseDataType(bool allowVariableType = false, bool allowAuto = false);
91  asCScriptNode *ParseIdentifier();
92  bool ParseTemplTypeList(asCScriptNode *node, bool required = true);
93  void ParseMethodAttributes(asCScriptNode *funcNode);
94 
95  asCScriptNode *ParseListPattern();
96 
97  bool IsRealType(int tokenType);
98  bool IsDataType(const sToken &token);
99  bool IdentifierIs(const sToken &t, const char *str);
100 
101 #ifndef AS_NO_COMPILER
102  // Statements
103  asCScriptNode *SuperficiallyParseStatementBlock();
104  asCScriptNode *SuperficiallyParseVarInit();
105  asCScriptNode *ParseStatementBlock();
106  asCScriptNode *ParseStatement();
107  asCScriptNode *ParseExpressionStatement();
108  asCScriptNode *ParseSwitch();
109  asCScriptNode *ParseCase();
110  asCScriptNode *ParseIf();
111  asCScriptNode *ParseFor();
112  asCScriptNode *ParseWhile();
113  asCScriptNode *ParseDoWhile();
114  asCScriptNode *ParseReturn();
115  asCScriptNode *ParseBreak();
116  asCScriptNode *ParseContinue();
117  asCScriptNode *ParseTryCatch();
118 
119  // Declarations
120  asCScriptNode *ParseDeclaration(bool isClassProp = false, bool isGlobalVar = false);
121  asCScriptNode *ParseImport();
122  asCScriptNode *ParseScript(bool inBlock);
123  asCScriptNode *ParseNamespace();
124  asCScriptNode *ParseFunction(bool isMethod = false);
125  asCScriptNode *ParseFuncDef();
126  asCScriptNode *ParseClass();
127  asCScriptNode *ParseMixin();
128  asCScriptNode *ParseInitList();
129  asCScriptNode *ParseInterface();
130  asCScriptNode *ParseInterfaceMethod();
131  asCScriptNode *ParseVirtualPropertyDecl(bool isMethod, bool isInterface);
132  asCScriptNode *ParseEnumeration();
133  asCScriptNode *ParseTypedef();
134  bool IsVarDecl();
135  bool IsVirtualPropertyDecl();
136  bool IsFuncDecl(bool isMethod);
137  bool IsLambda();
138  bool IsFunctionCall();
139 
140  // Expressions
141  asCScriptNode *ParseAssignment();
142  asCScriptNode *ParseAssignOperator();
143  asCScriptNode *ParseCondition();
144  asCScriptNode *ParseExpression();
145  asCScriptNode *ParseExprTerm();
146  asCScriptNode *ParseExprOperator();
147  asCScriptNode *ParseExprPreOp();
148  asCScriptNode *ParseExprPostOp();
149  asCScriptNode *ParseExprValue();
150  asCScriptNode *ParseArgList(bool withParenthesis = true);
151  asCScriptNode *ParseFunctionCall();
152  asCScriptNode *ParseVariableAccess();
153  asCScriptNode *ParseConstructCall();
154  asCScriptNode *ParseCast();
155  asCScriptNode *ParseConstant();
156  asCScriptNode *ParseStringConstant();
157  asCScriptNode *ParseLambda();
158 
159  bool IsType(sToken &nextToken);
160  bool IsConstant(int tokenType);
161  bool IsOperator(int tokenType);
162  bool IsPreOperator(int tokenType);
163  bool IsPostOperator(int tokenType);
164  bool IsAssignOperator(int tokenType);
165 
166  bool CheckTemplateType(const sToken &t);
167 #endif
168 
169  asCScriptNode *ParseToken(int token);
170  asCScriptNode *ParseOneOf(int *tokens, int num);
171 
172  asCString ExpectedToken(const char *token);
173  asCString ExpectedTokens(const char *token1, const char *token2);
174  asCString ExpectedOneOf(int *tokens, int count);
175  asCString ExpectedOneOf(const char **tokens, int count);
176  asCString InsteadFound(sToken &t);
177 
178  bool errorWhileParsing;
179  bool isSyntaxError;
180  bool checkValidTypes;
181  bool isParsingAppInterface;
182 
183  asCScriptEngine *engine;
184  asCBuilder *builder;
185  asCScriptCode *script;
186  asCScriptNode *scriptNode;
187 
188  asCString tempString; // Used for reduzing amount of dynamic allocations
189 
190  sToken lastToken;
191  size_t sourcePos;
192 };
193 
194 END_AS_NAMESPACE
195 
196 #endif
Definition: as_builder.h:140
Definition: as_scriptcode.h:48
Definition: as_scriptnode.h:96
Definition: as_parser.h:50
Definition: as_scriptengine.h:64
Definition: as_scriptnode.h:104
Definition: as_string.h:41