Pol  Revision:f37d500
parser.h
Go to the documentation of this file.
1 
8 #ifndef __PARSER_H
9 #define __PARSER_H
10 
11 #include <iosfwd>
12 #include <stddef.h>
13 #include <string>
14 
15 #include "../clib/compilerspecifics.h"
16 #include "tokens.h"
17 
18 namespace Pol
19 {
20 namespace Bscript
21 {
22 class ModuleFunction;
23 class Token;
24 class UserFunction;
25 } // namespace Bscript
26 } // namespace Pol
27 
28 #ifndef __TOKEN_H
29 #include "token.h"
30 #endif
31 #ifndef __OPERATOR_H
32 #include "operator.h"
33 #endif
34 
35 #include <queue>
36 #include <stack>
37 #include <vector>
38 
39 namespace Pol
40 {
41 namespace Bscript
42 {
43 class CompilerContext;
44 
45 typedef enum {
47  PERR_UNEXRPAREN, // unexpected RIGHT Paren
49  PERR_MISSRPAREN, // missing RPAREN
50  PERR_BADTOKEN, // bad token ??
51  PERR_BADOPER, // unknown operator..
52  PERR_WAAH, // god knows what happened
53  PERR_UNTERMSTRING, // "abcd (not terminated with '"')
54  PERR_INVESCAPE, // an invalid escape sequence (eg. \xFG)
58  PERR_ILLEGALCONS, // illegal construction
67 } ParseError;
68 
69 extern const char* ParseErrorStr[];
70 
71 
73 {
74 public:
75  ~Expression();
76 
77  void eat( Expression& expr );
78  void eat2( Expression& expr );
79  void optimize();
80  void optimize_binary_operations();
81  void optimize_unary_operations();
82  void optimize_assignments();
83  int get_num_tokens( int idx ) const;
84  bool optimize_token( int i );
85 
86  typedef std::vector<Token*> Tokens;
87  Tokens tokens;
88 
89 public:
90  std::stack<Token*> TX;
91  std::queue<Token*> CA;
92 };
93 
94 
95 const unsigned EXPR_FLAG_SEMICOLON_TERM_ALLOWED = 0x0001;
96 const unsigned EXPR_FLAG_COMMA_TERM_ALLOWED = 0x0002;
97 const unsigned EXPR_FLAG_RIGHTPAREN_TERM_ALLOWED = 0x0004;
98 const unsigned EXPR_FLAG_SINGLE_ELEMENT = 0x0008;
99 const unsigned EXPR_FLAG_ENDENUM_TERM_ALLOWED = 0x0010;
100 const unsigned EXPR_FLAG_RIGHTBRACE_TERM_ALLOWED = 0x0020;
101 const unsigned EXPR_FLAG_TO_TERM_ALLOWED = 0x0040;
102 const unsigned EXPR_FLAG_AUTO_TERM_ALLOWED = 0x0080;
103 const unsigned EXPR_FLAG_CONSUME_RESULT = 0x0100;
104 const unsigned EXPR_FLAG_DICTKEY_TERM_ALLOWED = 0x0200;
105 
106 
107 class Parser
108 {
109 public:
110  Parser();
111  virtual ~Parser() {}
112  Parser& operator=( const Parser& ) { return *this; }
113  int quiet;
114  ParseError err;
115 
116  char ext_err[50];
117  char buffer[51];
118 
120 
121 public:
122 public:
123  void reinit( Expression& ex );
124 
125  static void write_words( std::ostream& os );
126  virtual int recognize_binary( Token& tok, const char* buf, const char** s );
127  virtual int recognize_unary( Token& tok, const char* buf );
128  virtual int recognize( Token& tok, const char* buf, const char** s );
129  virtual bool recognize_reserved_word( Token& tok, const char* buf );
130 
131  virtual int tryOperator( Token& tok, const char* buf, const char** s, Operator* opList, int n_ops,
132  char* opbuf );
133  virtual int tryBinaryOperator( Token& tok, CompilerContext& ctx );
134  virtual int tryUnaryOperator( Token& tok, CompilerContext& ctx );
135 
136  virtual int tryNumeric( Token& tok, CompilerContext& ctx );
137  virtual int tryLiteral( Token& tok, CompilerContext& ctx );
138 
139  virtual int peekToken( const CompilerContext& ctx, Token& token, Expression* expr = NULL );
140  virtual int getToken( CompilerContext& ctx, Token& token, Expression* expr = NULL );
141 
142 
143  virtual int parseToken( CompilerContext& ctx, Expression& expr, Token* token ) = 0;
144  int IP( Expression& expr, char* s );
145 
146  void setQuiet( int x ) { quiet = x; }
147 };
148 
149 
150 class SmartParser : public Parser
151 {
152 public:
153  virtual ~SmartParser() {}
154 
155 protected:
156  virtual int tryLiteral( Token& tok, CompilerContext& ctx ) POL_OVERRIDE;
157 
160 
161 public:
162  SmartParser() : Parser(), modfunc_( nullptr ), userfunc_( nullptr ) {}
163  SmartParser& operator=( const SmartParser& ) { return *this; }
164  virtual int isLegal( Token& tok );
165  virtual int isOkay( const Token& token, BTokenType last_type );
166 
167  virtual int isFunc( Token& tok, ModuleFunction** v ) = 0;
168 
169  virtual int isUserFunc( Token& tok, UserFunction** userfunc );
170 
171  virtual int parseToken( CompilerContext& ctx, Expression& expr, Token* ) POL_OVERRIDE;
172  virtual int getToken( CompilerContext& ctx, Token& token, Expression* expr = NULL ) POL_OVERRIDE;
173 
174  bool callingMethod( CompilerContext& ctx );
175 
176  int getArgs( Expression& expr, CompilerContext& ctx );
177  virtual int getUserArgs( Expression& expr, CompilerContext& ctx, bool inject_jsr = true ) = 0;
178  virtual int getArrayElements( Expression& expr, CompilerContext& ctx ) = 0;
179  virtual int getNewArrayElements( Expression& expr, CompilerContext& ctx ) = 0;
180  virtual int getStructMembers( Expression& expr, CompilerContext& ctx ) = 0;
181  virtual int getDictionaryMembers( Expression& expr, CompilerContext& ctx ) = 0;
182  virtual int getMethodArguments( Expression& expr, CompilerContext& ctx, int& nargs ) = 0;
183  virtual int getFunctionPArgument( Expression& expr, CompilerContext& ctx, Token* tok ) = 0;
184 
185  int IIP( Expression& expr, CompilerContext& ctx, unsigned expr_flags );
186  int IP( Expression& expr, char* s );
187  int IP( Expression& expr, CompilerContext& ctx );
188 };
189 
191 {
192  return 1;
193 }
195 {
196  return 0;
197 }
198 }
199 }
200 #endif
const unsigned EXPR_FLAG_DICTKEY_TERM_ALLOWED
Definition: parser.h:104
const unsigned EXPR_FLAG_AUTO_TERM_ALLOWED
Definition: parser.h:102
#define POL_OVERRIDE
std::queue< Token * > CA
Definition: parser.h:91
UserFunction * userfunc_
Definition: parser.h:159
std::stack< Token * > TX
Definition: parser.h:90
std::vector< Token * > Tokens
Definition: parser.h:86
ParseError err
Definition: parser.h:114
const unsigned EXPR_FLAG_SINGLE_ELEMENT
Definition: parser.h:98
const unsigned EXPR_FLAG_RIGHTPAREN_TERM_ALLOWED
Definition: parser.h:97
virtual ~SmartParser()
Definition: parser.h:153
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
const unsigned EXPR_FLAG_CONSUME_RESULT
Definition: parser.h:103
const unsigned EXPR_FLAG_TO_TERM_ALLOWED
Definition: parser.h:101
void setQuiet(int x)
Definition: parser.h:146
Parser & operator=(const Parser &)
Definition: parser.h:112
class ModuleFunction * modfunc_
Definition: parser.h:158
const char * ParseErrorStr[PERR_NUM_ERRORS]
Definition: parser.cpp:82
virtual int isUserFunc(Token &tok, UserFunction **userfunc)
Definition: parser.h:194
const unsigned EXPR_FLAG_RIGHTBRACE_TERM_ALLOWED
Definition: parser.h:100
virtual ~Parser()
Definition: parser.h:111
virtual int isLegal(Token &tok)
Definition: parser.h:190
const unsigned EXPR_FLAG_COMMA_TERM_ALLOWED
Definition: parser.h:96
Definition: berror.cpp:12
SmartParser & operator=(const SmartParser &)
Definition: parser.h:163
const unsigned EXPR_FLAG_ENDENUM_TERM_ALLOWED
Definition: parser.h:99
const unsigned EXPR_FLAG_SEMICOLON_TERM_ALLOWED
Definition: parser.h:95