2016-01-28 14:52:40 +01:00
|
|
|
/** @file
|
|
|
|
|
*
|
|
|
|
|
* @par History
|
|
|
|
|
* - 2009/08/25 Shinigami: STLport-5.2.1 fix: ParseErrorStr changed little bit
|
|
|
|
|
*/
|
2014-08-30 12:25:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __PARSER_H
|
|
|
|
|
#define __PARSER_H
|
|
|
|
|
|
|
|
|
|
#include <iosfwd>
|
2019-10-05 13:19:38 +02:00
|
|
|
#include <queue>
|
|
|
|
|
#include <stack>
|
2018-01-29 21:55:48 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <string>
|
2019-10-05 13:19:38 +02:00
|
|
|
#include <vector>
|
2014-08-30 12:25:08 +02:00
|
|
|
|
2018-01-29 21:55:48 +01:00
|
|
|
#include "tokens.h"
|
|
|
|
|
|
2014-08-30 12:25:08 +02:00
|
|
|
#ifndef __TOKEN_H
|
|
|
|
|
#include "token.h"
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef __OPERATOR_H
|
|
|
|
|
#include "operator.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
namespace Pol
|
|
|
|
|
{
|
|
|
|
|
namespace Bscript
|
|
|
|
|
{
|
2018-01-01 21:49:30 +01:00
|
|
|
class CompilerContext;
|
2020-04-24 12:45:14 -07:00
|
|
|
class Expression;
|
2019-10-05 13:19:38 +02:00
|
|
|
class ModuleFunction;
|
|
|
|
|
class Token;
|
|
|
|
|
class UserFunction;
|
2018-01-29 21:55:48 +01:00
|
|
|
|
2019-09-22 00:34:14 +02:00
|
|
|
typedef enum
|
|
|
|
|
{
|
2016-02-19 19:26:35 +01:00
|
|
|
PERR_NONE,
|
|
|
|
|
PERR_UNEXRPAREN, // unexpected RIGHT Paren
|
|
|
|
|
PERR_MISSLPAREN,
|
|
|
|
|
PERR_MISSRPAREN, // missing RPAREN
|
|
|
|
|
PERR_BADTOKEN, // bad token ??
|
|
|
|
|
PERR_BADOPER, // unknown operator..
|
|
|
|
|
PERR_WAAH, // god knows what happened
|
|
|
|
|
PERR_UNTERMSTRING, // "abcd (not terminated with '"')
|
|
|
|
|
PERR_INVESCAPE, // an invalid escape sequence (eg. \xFG)
|
|
|
|
|
PERR_TOOFEWARGS,
|
|
|
|
|
PERR_TOOMANYARGS,
|
|
|
|
|
PERR_UNEXPCOMMA,
|
|
|
|
|
PERR_ILLEGALCONS, // illegal construction
|
|
|
|
|
PERR_MISSINGDELIM,
|
|
|
|
|
PERR_NOTLEGALHERE,
|
|
|
|
|
PERR_PROCNOTALLOWED,
|
|
|
|
|
PERR_UNEXPSEMI,
|
|
|
|
|
PERR_EXPWHILE,
|
|
|
|
|
PERR_UNEXRBRACKET,
|
|
|
|
|
PERR_MISSRBRACKET,
|
2020-06-10 22:14:31 -07:00
|
|
|
PERR_EOFEXPECTINGTERM,
|
2016-02-19 19:26:35 +01:00
|
|
|
PERR_NUM_ERRORS
|
|
|
|
|
} ParseError;
|
|
|
|
|
|
|
|
|
|
extern const char* ParseErrorStr[];
|
|
|
|
|
|
|
|
|
|
const unsigned EXPR_FLAG_SEMICOLON_TERM_ALLOWED = 0x0001;
|
|
|
|
|
const unsigned EXPR_FLAG_COMMA_TERM_ALLOWED = 0x0002;
|
|
|
|
|
const unsigned EXPR_FLAG_RIGHTPAREN_TERM_ALLOWED = 0x0004;
|
|
|
|
|
const unsigned EXPR_FLAG_SINGLE_ELEMENT = 0x0008;
|
|
|
|
|
const unsigned EXPR_FLAG_ENDENUM_TERM_ALLOWED = 0x0010;
|
|
|
|
|
const unsigned EXPR_FLAG_RIGHTBRACE_TERM_ALLOWED = 0x0020;
|
|
|
|
|
const unsigned EXPR_FLAG_TO_TERM_ALLOWED = 0x0040;
|
|
|
|
|
const unsigned EXPR_FLAG_AUTO_TERM_ALLOWED = 0x0080;
|
|
|
|
|
const unsigned EXPR_FLAG_CONSUME_RESULT = 0x0100;
|
|
|
|
|
const unsigned EXPR_FLAG_DICTKEY_TERM_ALLOWED = 0x0200;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Parser
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Parser();
|
2019-10-05 13:19:38 +02:00
|
|
|
virtual ~Parser() = default;
|
2016-02-19 19:26:35 +01:00
|
|
|
Parser& operator=( const Parser& ) { return *this; }
|
|
|
|
|
int quiet;
|
|
|
|
|
ParseError err;
|
|
|
|
|
|
|
|
|
|
char ext_err[50];
|
|
|
|
|
char buffer[51];
|
|
|
|
|
|
|
|
|
|
bool contains_tabs;
|
|
|
|
|
|
|
|
|
|
void reinit( Expression& ex );
|
|
|
|
|
|
|
|
|
|
static void write_words( std::ostream& os );
|
|
|
|
|
virtual int recognize_binary( Token& tok, const char* buf, const char** s );
|
|
|
|
|
virtual int recognize_unary( Token& tok, const char* buf );
|
|
|
|
|
virtual int recognize( Token& tok, const char* buf, const char** s );
|
|
|
|
|
virtual bool recognize_reserved_word( Token& tok, const char* buf );
|
|
|
|
|
|
|
|
|
|
virtual int tryOperator( Token& tok, const char* buf, const char** s, Operator* opList, int n_ops,
|
|
|
|
|
char* opbuf );
|
|
|
|
|
virtual int tryBinaryOperator( Token& tok, CompilerContext& ctx );
|
|
|
|
|
virtual int tryUnaryOperator( Token& tok, CompilerContext& ctx );
|
|
|
|
|
|
|
|
|
|
virtual int tryNumeric( Token& tok, CompilerContext& ctx );
|
|
|
|
|
virtual int tryLiteral( Token& tok, CompilerContext& ctx );
|
|
|
|
|
|
2018-10-09 16:36:35 +02:00
|
|
|
virtual int peekToken( const CompilerContext& ctx, Token& token, Expression* expr = nullptr );
|
|
|
|
|
virtual int getToken( CompilerContext& ctx, Token& token, Expression* expr = nullptr );
|
2016-02-19 19:26:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual int parseToken( CompilerContext& ctx, Expression& expr, Token* token ) = 0;
|
|
|
|
|
int IP( Expression& expr, char* s );
|
|
|
|
|
|
|
|
|
|
void setQuiet( int x ) { quiet = x; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SmartParser : public Parser
|
|
|
|
|
{
|
|
|
|
|
public:
|
2019-10-05 13:19:38 +02:00
|
|
|
virtual ~SmartParser() = default;
|
2018-01-29 21:55:48 +01:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
protected:
|
2018-10-09 09:47:27 +02:00
|
|
|
virtual int tryLiteral( Token& tok, CompilerContext& ctx ) override;
|
2016-02-19 19:26:35 +01:00
|
|
|
|
|
|
|
|
class ModuleFunction* modfunc_;
|
|
|
|
|
UserFunction* userfunc_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SmartParser() : Parser(), modfunc_( nullptr ), userfunc_( nullptr ) {}
|
|
|
|
|
SmartParser& operator=( const SmartParser& ) { return *this; }
|
|
|
|
|
virtual int isLegal( Token& tok );
|
2019-09-22 00:34:14 +02:00
|
|
|
virtual int isOkay( const Token& token, const Token& last_token );
|
2014-08-30 12:25:08 +02:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
virtual int isFunc( Token& tok, ModuleFunction** v ) = 0;
|
2014-08-30 12:25:08 +02:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
virtual int isUserFunc( Token& tok, UserFunction** userfunc );
|
2014-08-30 12:25:08 +02:00
|
|
|
|
2018-10-09 09:47:27 +02:00
|
|
|
virtual int parseToken( CompilerContext& ctx, Expression& expr, Token* ) override;
|
2018-10-09 16:36:35 +02:00
|
|
|
virtual int getToken( CompilerContext& ctx, Token& token, Expression* expr = nullptr ) override;
|
2020-06-15 22:26:33 -07:00
|
|
|
int getTokenWithoutConversions( CompilerContext& ctx, Token& token );
|
2016-02-19 19:26:35 +01:00
|
|
|
|
|
|
|
|
bool callingMethod( CompilerContext& ctx );
|
|
|
|
|
|
|
|
|
|
virtual int getUserArgs( Expression& expr, CompilerContext& ctx, bool inject_jsr = true ) = 0;
|
|
|
|
|
virtual int getArrayElements( Expression& expr, CompilerContext& ctx ) = 0;
|
|
|
|
|
virtual int getNewArrayElements( Expression& expr, CompilerContext& ctx ) = 0;
|
|
|
|
|
virtual int getStructMembers( Expression& expr, CompilerContext& ctx ) = 0;
|
|
|
|
|
virtual int getDictionaryMembers( Expression& expr, CompilerContext& ctx ) = 0;
|
|
|
|
|
virtual int getMethodArguments( Expression& expr, CompilerContext& ctx, int& nargs ) = 0;
|
2018-02-03 19:05:22 +01:00
|
|
|
virtual int getFunctionPArgument( Expression& expr, CompilerContext& ctx, Token* tok ) = 0;
|
2014-08-30 12:25:08 +02:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
int IIP( Expression& expr, CompilerContext& ctx, unsigned expr_flags );
|
|
|
|
|
};
|
2014-08-30 12:25:08 +02:00
|
|
|
|
2016-02-19 19:26:35 +01:00
|
|
|
inline int SmartParser::isLegal( Token& )
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
inline int SmartParser::isUserFunc( Token&, UserFunction** )
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-09-22 00:34:14 +02:00
|
|
|
} // namespace Bscript
|
|
|
|
|
} // namespace Pol
|
2014-08-30 12:25:08 +02:00
|
|
|
#endif
|