2011-04-06 23:30:59 +03:00
/*
* ERMParser . h , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
2017-07-13 10:26:03 +02:00
# pragma once
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
2014-11-15 13:10:51 +02:00
# include <boost/spirit/home/support/unused.hpp>
2011-04-06 23:30:59 +03:00
2014-11-15 13:10:51 +02:00
namespace spirit = boost : : spirit ;
2011-04-04 00:38:47 +03:00
class CERMPreprocessor
{
std : : string fname ;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
std : : stringstream sourceStream ;
2011-04-04 00:38:47 +03:00
int lineNo ;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
2011-04-04 00:38:47 +03:00
void getline ( std : : string & ret ) ;
public :
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
enum class Version : ui8
{
INVALID ,
ERM ,
VERM
} ;
Version version ;
CERMPreprocessor ( const std : : string & source ) ;
2018-02-10 20:52:23 +02:00
std : : string retrieveCommandLine ( ) ;
2011-05-11 22:53:55 +03:00
int getCurLineNo ( ) const
{
return lineNo ;
}
2021-09-05 14:23:36 +02:00
const std : : string & getCurFileName ( ) const
{
return fname ;
}
2011-04-04 00:38:47 +03:00
} ;
2011-03-19 19:06:46 +02:00
2011-04-06 23:30:59 +03:00
//various classes that represent ERM/VERM AST
namespace ERM
{
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
using ValType = int ; //todo: set to int64_t
using IType = int ; //todo: set to int32_t
2011-04-06 23:30:59 +03:00
struct TStringConstant
{
std : : string str ;
} ;
struct TMacroUsage
{
std : : string macro ;
} ;
2011-05-14 16:20:19 +03:00
// //macro with '?', for write only
// struct TQMacroUsage
// {
// std::string qmacro;
// };
2011-04-06 23:30:59 +03:00
//definition of a macro
struct TMacroDef
{
std : : string macro ;
} ;
typedef std : : string TCmdName ;
struct TVarExpNotMacro
{
2023-04-16 19:42:56 +02:00
typedef std : : optional < int > Tval ;
std : : optional < char > questionMark ;
2011-04-06 23:30:59 +03:00
std : : string varsym ;
Tval val ;
} ;
2023-04-15 03:33:00 +02:00
typedef std : : variant < TVarExpNotMacro , TMacroUsage > TVarExp ;
2011-04-06 23:30:59 +03:00
//write-only variable expression
struct TVarpExp
{
2011-05-16 15:11:00 +03:00
TVarExp var ;
2011-04-06 23:30:59 +03:00
} ;
//i-expression (identifier expression) - an integral constant, variable symbol or array symbol
2023-04-15 03:33:00 +02:00
typedef std : : variant < TVarExp , int > TIexp ;
2011-04-06 23:30:59 +03:00
struct TArithmeticOp
{
TIexp lhs , rhs ;
char opcode ;
} ;
struct TVRLogic
{
char opcode ;
TIexp var ;
} ;
struct TVRArithmetic
{
char opcode ;
TIexp rhs ;
} ;
struct TSemiCompare
{
std : : string compSign ;
TIexp rhs ;
} ;
struct TCurriedString
{
TIexp iexp ;
TStringConstant string ;
} ;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
struct TVarConcatString
2011-04-06 23:30:59 +03:00
{
TVarExp var ;
TStringConstant string ;
} ;
2023-04-15 03:33:00 +02:00
typedef std : : variant < TVarConcatString , TStringConstant , TCurriedString , TSemiCompare , TMacroDef , TIexp , TVarpExp > TBodyOptionItem ;
2011-04-06 23:30:59 +03:00
typedef std : : vector < TBodyOptionItem > TNormalBodyOptionList ;
struct TNormalBodyOption
{
char optionCode ;
2023-04-16 19:42:56 +02:00
std : : optional < TNormalBodyOptionList > params ;
2011-04-06 23:30:59 +03:00
} ;
2023-04-15 03:33:00 +02:00
typedef std : : variant < TVRLogic , TVRArithmetic , TNormalBodyOption > TBodyOption ;
2011-04-06 23:30:59 +03:00
2023-04-15 03:33:00 +02:00
// typedef std::variant<TIexp, TArithmeticOp > TIdentifierInternal;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
typedef std : : vector < TIexp > Tidentifier ;
2011-04-06 23:30:59 +03:00
struct TComparison
{
std : : string compSign ;
TIexp lhs , rhs ;
} ;
struct Tcondition ;
2023-04-16 19:42:56 +02:00
typedef std : : optional < boost : : recursive_wrapper < Tcondition > > TconditionNode ;
2011-04-06 23:30:59 +03:00
struct Tcondition
{
2023-04-15 03:33:00 +02:00
typedef std : : variant <
2011-04-06 23:30:59 +03:00
TComparison ,
int >
Tcond ; //comparison or condition flag
char ctype ;
Tcond cond ;
TconditionNode rhs ;
} ;
2011-05-14 16:20:19 +03:00
struct TTriggerBase
2011-04-06 23:30:59 +03:00
{
2011-05-14 16:20:19 +03:00
bool pre ; //if false it's !$ post-trigger, elsewise it's !# (pre)trigger
2011-04-06 23:30:59 +03:00
TCmdName name ;
2023-04-16 19:42:56 +02:00
std : : optional < Tidentifier > identifier ;
std : : optional < Tcondition > condition ;
2011-04-06 23:30:59 +03:00
} ;
2011-05-14 16:20:19 +03:00
struct Ttrigger : TTriggerBase
{
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
Ttrigger ( )
2011-05-14 16:20:19 +03:00
{
2011-05-22 21:46:52 +03:00
pre = true ;
2011-05-14 16:20:19 +03:00
}
} ;
struct TPostTrigger : TTriggerBase
{
TPostTrigger ( )
{
2011-05-22 21:46:52 +03:00
pre = false ;
2011-05-14 16:20:19 +03:00
}
} ;
2011-04-06 23:30:59 +03:00
//a dirty workaround for preprocessor magic that prevents the use types with comma in it in BOOST_FUSION_ADAPT_STRUCT
//see http://comments.gmane.org/gmane.comp.lib.boost.user/62501 for some info
//
//moreover, I encountered a quite serious bug in boost: http://boost.2283326.n4.nabble.com/container-hpp-111-error-C2039-value-type-is-not-a-member-of-td3352328.html
//not sure how serious it is...
2023-04-15 03:33:00 +02:00
//typedef std::variant<char, TStringConstant, TMacroUsage, TMacroDef> bodyItem;
2011-04-06 23:30:59 +03:00
typedef std : : vector < TBodyOption > Tbody ;
struct Tinstruction
{
TCmdName name ;
2023-04-16 19:42:56 +02:00
std : : optional < Tidentifier > identifier ;
std : : optional < Tcondition > condition ;
2011-04-06 23:30:59 +03:00
Tbody body ;
} ;
struct Treceiver
{
TCmdName name ;
2023-04-16 19:42:56 +02:00
std : : optional < Tidentifier > identifier ;
std : : optional < Tcondition > condition ;
std : : optional < Tbody > body ;
2011-04-06 23:30:59 +03:00
} ;
struct Tcommand
{
2023-04-15 03:33:00 +02:00
typedef std : : variant <
2011-04-06 23:30:59 +03:00
Ttrigger ,
Tinstruction ,
Treceiver ,
TPostTrigger
>
Tcmd ;
Tcmd cmd ;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
//std::string comment;
2011-04-06 23:30:59 +03:00
} ;
//vector expression
2023-04-15 03:33:00 +02:00
typedef std : : variant < Tcommand , std : : string , boost : : spirit : : unused_type > TERMline ;
2011-04-06 23:30:59 +03:00
typedef std : : string TVModifier ; //'`', ',', ',@', '#''
struct TSymbol
{
std : : vector < TVModifier > symModifier ;
std : : string sym ;
} ;
//for #'symbol expression
2011-04-10 19:39:34 +03:00
enum EVOtions { VEXP , SYMBOL , CHAR , DOUBLE , INT , TCMD , STRINGC } ;
2011-04-06 23:30:59 +03:00
struct TVExp ;
2023-04-15 03:33:00 +02:00
typedef std : : variant < boost : : recursive_wrapper < TVExp > , TSymbol , char , double , int , Tcommand , TStringConstant > TVOption ; //options in v-expression
2011-04-06 23:30:59 +03:00
//v-expression
struct TVExp
{
std : : vector < TVModifier > modifier ;
std : : vector < TVOption > children ;
} ;
//script line
2023-04-15 03:33:00 +02:00
typedef std : : variant < TVExp , TERMline > TLine ;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
2022-06-11 17:45:34 +02:00
template < typename T > struct ERM_grammar ;
2011-04-06 23:30:59 +03:00
}
2011-05-11 22:53:55 +03:00
struct LineInfo
{
ERM : : TLine tl ;
int realLineNum ;
} ;
2011-03-19 19:06:46 +02:00
class ERMParser
{
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
public :
std : : shared_ptr < ERM : : ERM_grammar < std : : string : : const_iterator > > ERMgrammar ;
ERMParser ( ) ;
virtual ~ ERMParser ( ) ;
std : : vector < LineInfo > parseFile ( CERMPreprocessor & preproc ) ;
2011-03-19 19:06:46 +02:00
private :
2011-03-28 22:34:00 +03:00
void repairEncoding ( char * str , int len ) const ; //removes nonstandard ascii characters from string
void repairEncoding ( std : : string & str ) const ; //removes nonstandard ascii characters from string
2011-05-11 22:53:55 +03:00
ERM : : TLine parseLine ( const std : : string & line , int realLineNo ) ;
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
ERM : : TLine parseLine ( const std : : string & line ) ;
2011-03-19 19:06:46 +02:00
} ;