OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4AulParse.h
Go to the documentation of this file.
1 /*
2 * OpenClonk, http://www.openclonk.org
3 *
4 * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5 * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16 
17 #ifndef INC_C4AulParse
18 #define INC_C4AulParse
19 
20 #include <stack>
21 
22 #include "script/C4Aul.h"
23 #include "script/C4AulAST.h"
24 #include "script/C4AulCompiler.h"
25 #include "script/C4AulScriptFunc.h"
26 
27 enum C4AulBCCType : int;
28 enum C4AulTokenType : int;
29 
31 {
32  unsigned short Priority;
33  const char* Identifier;
35  bool Postfix;
36  bool Changer; // changes first operand to result, rewrite to "a = a (op) b"
37  bool NoSecondStatement; // no second statement expected (++/-- postfix)
38  C4V_Type RetType; // type returned. ignored by C4V
41 };
42 
43 extern const C4ScriptOpDef C4ScriptOpMap[];
44 
46 {
47 public:
48  C4AulParse(class C4ScriptHost *host);
50  ~C4AulParse();
51  std::unique_ptr<::aul::ast::FunctionDecl> Parse_DirectExec(const char *code, bool whole_function);
52  std::unique_ptr<::aul::ast::Script> Parse_Script(C4ScriptHost *);
53 
54 private:
55  C4AulScriptFunc *Fn; C4ScriptHost * Host; C4ScriptHost * pOrgScript;
56  C4AulScriptEngine *Engine;
57  const char *SPos; // current position in the script
58  const char *TokenSPos; // start of the current token in the script
59  char Idtf[C4AUL_MAX_Identifier]; // current identifier
60  C4AulTokenType TokenType; // current token type
61  int32_t cInt; // current int constant
62  C4String * cStr; // current string constant
63  C4AulScriptContext* ContextToExecIn;
64  void Parse_Function(bool parse_for_direct_exec);
65  void Parse_WarningPragma();
66 
67 protected:
68  // All of the Parse_* functions need to be protected (not private!) so
69  // we can make them public in a derived class for unit testing purposes
70  std::unique_ptr<::aul::ast::FunctionDecl> Parse_ToplevelFunctionDecl();
71  std::unique_ptr<::aul::ast::Stmt> Parse_Statement();
72  std::unique_ptr<::aul::ast::Block> Parse_Block();
73  std::unique_ptr<::aul::ast::ArrayLit> Parse_Array();
74  std::unique_ptr<::aul::ast::ProplistLit> Parse_PropList();
75  std::unique_ptr<::aul::ast::DoLoop> Parse_DoWhile();
76  std::unique_ptr<::aul::ast::WhileLoop> Parse_While();
77  std::unique_ptr<::aul::ast::If> Parse_If();
78  std::unique_ptr<::aul::ast::ForLoop> Parse_For();
79  std::unique_ptr<::aul::ast::RangeLoop> Parse_ForEach();
80  std::unique_ptr<::aul::ast::Expr> Parse_Expression(int iParentPrio = -1);
81  std::unique_ptr<::aul::ast::VarDecl> Parse_Var();
82  void Shift();
83 
84 private:
85  void Parse_Function(::aul::ast::Function *func);
86  void Parse_CallParams(::aul::ast::CallExpr *call);
87 
88  bool AdvanceSpaces(); // skip whitespaces; return whether script ended
89  int GetOperator(const char* pScript);
90  void ClearToken(); // clear any data held with the current token
91  C4AulTokenType GetNextToken(); // get next token of SPos
92 
93  void Match(C4AulTokenType TokenType, const char * Expected = nullptr);
94  void Check(C4AulTokenType TokenType, const char * Expected = nullptr);
95  NORETURN void UnexpectedToken(const char * Expected);
96 
97  void Warn(C4AulWarningId warning, ...);
98  void Error(const char *pMsg, ...) GNUC_FORMAT_ATTRIBUTE_O;
99  void AppendPosition(StdStrBuf & Buf);
100 
101  friend class C4AulParseError;
102  std::stack<const char *> parse_pos_stack;
103  void PushParsePos();
104  void PopParsePos();
105  void DiscardParsePos();
106 };
107 
108 #endif
C4V_Type
Definition: C4Value.h:23
C4AulWarningId
Definition: C4Aul.h:31
bool NoSecondStatement
Definition: C4AulParse.h:37
C4AulBCCType
std::unique_ptr<::aul::ast::FunctionDecl > Parse_DirectExec(const char *code, bool whole_function)
Definition: C4AulParse.cpp:852
std::unique_ptr<::aul::ast::FunctionDecl > Parse_ToplevelFunctionDecl()
Definition: C4AulParse.cpp:959
std::unique_ptr<::aul::ast::ArrayLit > Parse_Array()
#define NORETURN
#define GNUC_FORMAT_ATTRIBUTE_O
std::unique_ptr<::aul::ast::ForLoop > Parse_For()
std::unique_ptr<::aul::ast::VarDecl > Parse_Var()
std::unique_ptr<::aul::ast::WhileLoop > Parse_While()
C4V_Type Type1
Definition: C4AulParse.h:39
std::unique_ptr<::aul::ast::Block > Parse_Block()
std::unique_ptr<::aul::ast::If > Parse_If()
C4AulParse(class C4ScriptHost *host)
Definition: C4AulParse.cpp:113
#define C4AUL_MAX_Identifier
Definition: C4Aul.h:28
C4V_Type RetType
Definition: C4AulParse.h:38
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:270
unsigned short Priority
Definition: C4AulParse.h:32
C4V_Type Type2
Definition: C4AulParse.h:40
std::unique_ptr<::aul::ast::DoLoop > Parse_DoWhile()
std::unique_ptr<::aul::ast::RangeLoop > Parse_ForEach()
std::unique_ptr<::aul::ast::Expr > Parse_Expression(int iParentPrio=-1)
C4AulBCCType Code
Definition: C4AulParse.h:34
std::unique_ptr<::aul::ast::Stmt > Parse_Statement()
const char * Identifier
Definition: C4AulParse.h:33
void Shift()
Definition: C4AulParse.cpp:755
std::unique_ptr<::aul::ast::ProplistLit > Parse_PropList()
C4AulTokenType
Definition: C4AulParse.cpp:88
std::unique_ptr<::aul::ast::Script > Parse_Script(C4ScriptHost *)
Definition: C4AulParse.cpp:873