2
0

gd_parser.cpp 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585
  1. /*************************************************************************/
  2. /* gd_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "gd_parser.h"
  30. #include "print_string.h"
  31. #include "io/resource_loader.h"
  32. #include "os/file_access.h"
  33. #include "script_language.h"
  34. #include "gd_script.h"
  35. template<class T>
  36. T* GDParser::alloc_node() {
  37. T *t = memnew( T);
  38. t->next=list;
  39. list=t;
  40. if (!head)
  41. head=t;
  42. t->line=tokenizer->get_token_line();
  43. t->column=tokenizer->get_token_column();
  44. return t;
  45. }
  46. bool GDParser::_end_statement() {
  47. if (tokenizer->get_token()==GDTokenizer::TK_SEMICOLON) {
  48. tokenizer->advance();
  49. return true; //handle next
  50. } else if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE || tokenizer->get_token()==GDTokenizer::TK_EOF) {
  51. return true; //will be handled properly
  52. }
  53. return false;
  54. }
  55. bool GDParser::_enter_indent_block(BlockNode* p_block) {
  56. if (tokenizer->get_token()!=GDTokenizer::TK_COLON) {
  57. // report location at the previous token (on the previous line)
  58. int error_line = tokenizer->get_token_line(-1);
  59. int error_column = tokenizer->get_token_column(-1);
  60. _set_error("':' expected at end of line.",error_line,error_column);
  61. return false;
  62. }
  63. tokenizer->advance();
  64. if (tokenizer->get_token()!=GDTokenizer::TK_NEWLINE) {
  65. // be more python-like
  66. int current = tab_level.back()->get();
  67. tab_level.push_back(current+1);
  68. return true;
  69. //_set_error("newline expected after ':'.");
  70. //return false;
  71. }
  72. while(true) {
  73. if (tokenizer->get_token()!=GDTokenizer::TK_NEWLINE) {
  74. return false; //wtf
  75. } else if (tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
  76. int indent = tokenizer->get_token_line_indent();
  77. int current = tab_level.back()->get();
  78. if (indent<=current) {
  79. print_line("current: "+itos(current)+" indent: "+itos(indent));
  80. print_line("less than current");
  81. return false;
  82. }
  83. tab_level.push_back(indent);
  84. tokenizer->advance();
  85. return true;
  86. } else if (p_block) {
  87. NewLineNode *nl = alloc_node<NewLineNode>();
  88. nl->line=tokenizer->get_token_line();
  89. p_block->statements.push_back(nl);
  90. }
  91. tokenizer->advance(); // go to next newline
  92. }
  93. }
  94. bool GDParser::_parse_arguments(Node* p_parent,Vector<Node*>& p_args,bool p_static,bool p_can_codecomplete) {
  95. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  96. tokenizer->advance();
  97. } else {
  98. int argidx=0;
  99. while(true) {
  100. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  101. _make_completable_call(argidx);
  102. completion_node=p_parent;
  103. } else if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING && tokenizer->get_token(1)==GDTokenizer::TK_CURSOR) {
  104. //completing a string argument..
  105. completion_cursor=tokenizer->get_token_constant();
  106. _make_completable_call(argidx);
  107. completion_node=p_parent;
  108. tokenizer->advance(1);
  109. return false;
  110. }
  111. Node*arg = _parse_expression(p_parent,p_static);
  112. if (!arg)
  113. return false;
  114. p_args.push_back(arg);
  115. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  116. tokenizer->advance();
  117. break;
  118. } else if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  119. if (tokenizer->get_token(1)==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  120. _set_error("Expression expected");
  121. return false;
  122. }
  123. tokenizer->advance();
  124. argidx++;
  125. } else {
  126. // something is broken
  127. _set_error("Expected ',' or ')'");
  128. return false;
  129. }
  130. }
  131. }
  132. return true;
  133. }
  134. void GDParser::_make_completable_call(int p_arg) {
  135. completion_cursor=StringName();
  136. completion_type=COMPLETION_CALL_ARGUMENTS;
  137. completion_class=current_class;
  138. completion_function=current_function;
  139. completion_line=tokenizer->get_token_line();
  140. completion_argument=p_arg;
  141. completion_block=current_block;
  142. completion_found=true;
  143. tokenizer->advance();
  144. }
  145. bool GDParser::_get_completable_identifier(CompletionType p_type,StringName& identifier) {
  146. identifier=StringName();
  147. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  148. identifier=tokenizer->get_token_identifier();
  149. tokenizer->advance();
  150. }
  151. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  152. completion_cursor=identifier;
  153. completion_type=p_type;
  154. completion_class=current_class;
  155. completion_function=current_function;
  156. completion_line=tokenizer->get_token_line();
  157. completion_block=current_block;
  158. completion_found=true;
  159. completion_ident_is_call=false;
  160. tokenizer->advance();
  161. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  162. identifier=identifier.operator String() + tokenizer->get_token_identifier().operator String();
  163. tokenizer->advance();
  164. }
  165. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  166. completion_ident_is_call=true;
  167. }
  168. return true;
  169. }
  170. return false;
  171. }
  172. GDParser::Node* GDParser::_parse_expression(Node *p_parent,bool p_static,bool p_allow_assign,bool p_parsing_constant) {
  173. // Vector<Node*> expressions;
  174. // Vector<OperatorNode::Operator> operators;
  175. Vector<Expression> expression;
  176. Node *expr=NULL;
  177. int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found)
  178. while(true) {
  179. /*****************/
  180. /* Parse Operand */
  181. /*****************/
  182. if (parenthesis>0) {
  183. //remove empty space (only allowed if inside parenthesis
  184. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  185. tokenizer->advance();
  186. }
  187. }
  188. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  189. //subexpression ()
  190. tokenizer->advance();
  191. parenthesis++;
  192. Node* subexpr = _parse_expression(p_parent,p_static,p_allow_assign,p_parsing_constant);
  193. parenthesis--;
  194. if (!subexpr)
  195. return NULL;
  196. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  197. _set_error("Expected ')' in expression");
  198. return NULL;
  199. }
  200. tokenizer->advance();
  201. expr=subexpr;
  202. } else if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  203. tokenizer->advance();
  204. continue; //no point in cursor in the middle of expression
  205. } else if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT) {
  206. //constant defined by tokenizer
  207. ConstantNode *constant = alloc_node<ConstantNode>();
  208. constant->value=tokenizer->get_token_constant();
  209. tokenizer->advance();
  210. expr=constant;
  211. } else if (tokenizer->get_token()==GDTokenizer::TK_CONST_PI) {
  212. //constant defined by tokenizer
  213. ConstantNode *constant = alloc_node<ConstantNode>();
  214. constant->value=Math_PI;
  215. tokenizer->advance();
  216. expr=constant;
  217. } else if (tokenizer->get_token()==GDTokenizer::TK_PR_PRELOAD) {
  218. //constant defined by tokenizer
  219. tokenizer->advance();
  220. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  221. _set_error("Expected '(' after 'preload'");
  222. return NULL;
  223. }
  224. tokenizer->advance();
  225. String path;
  226. bool valid = false;
  227. Node *subexpr = _parse_and_reduce_expression(p_parent, p_static);
  228. if (subexpr) {
  229. if (subexpr->type == Node::TYPE_CONSTANT) {
  230. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  231. if (cn->value.get_type() == Variant::STRING) {
  232. valid = true;
  233. path = (String) cn->value;
  234. }
  235. }
  236. }
  237. if (!valid) {
  238. _set_error("expected string constant as 'preload' argument.");
  239. return NULL;
  240. }
  241. if (path.begins_with("/")) {
  242. _set_error("Paths cannot start with '/', absolute paths must start with \'res://\', \'user://\', or \'local://\'");
  243. return NULL;
  244. }
  245. if (!path.is_abs_path() && base_path!="")
  246. path=base_path+"/"+path;
  247. path = path.replace("///","//").simplify_path();
  248. if (path==self_path) {
  249. _set_error("Can't preload itself (use 'get_script()').");
  250. return NULL;
  251. }
  252. Ref<Resource> res;
  253. if (!validating) {
  254. //this can be too slow for just validating code
  255. if (for_completion && ScriptCodeCompletionCache::get_sigleton()) {
  256. res = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(path);
  257. } else {
  258. res = ResourceLoader::load(path);
  259. }
  260. if (!res.is_valid()) {
  261. _set_error("Can't preload resource at path: "+path);
  262. return NULL;
  263. }
  264. } else {
  265. if (!FileAccess::exists(path)) {
  266. _set_error("Can't preload resource at path: "+path);
  267. return NULL;
  268. }
  269. }
  270. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  271. _set_error("Expected ')' after 'preload' path");
  272. return NULL;
  273. }
  274. ConstantNode *constant = alloc_node<ConstantNode>();
  275. constant->value=res;
  276. tokenizer->advance();
  277. expr=constant;
  278. } else if (tokenizer->get_token()==GDTokenizer::TK_PR_YIELD) {
  279. //constant defined by tokenizer
  280. tokenizer->advance();
  281. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  282. _set_error("Expected '(' after 'yield'");
  283. return NULL;
  284. }
  285. tokenizer->advance();
  286. OperatorNode *yield = alloc_node<OperatorNode>();
  287. yield->op=OperatorNode::OP_YIELD;
  288. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  289. expr=yield;
  290. tokenizer->advance();
  291. } else {
  292. Node *object = _parse_and_reduce_expression(p_parent,p_static);
  293. if (!object)
  294. return NULL;
  295. yield->arguments.push_back(object);
  296. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  297. _set_error("Expected ',' after first argument of 'yield'");
  298. return NULL;
  299. }
  300. tokenizer->advance();
  301. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  302. completion_cursor=StringName();
  303. completion_node=object;
  304. completion_type=COMPLETION_YIELD;
  305. completion_class=current_class;
  306. completion_function=current_function;
  307. completion_line=tokenizer->get_token_line();
  308. completion_argument=0;
  309. completion_block=current_block;
  310. completion_found=true;
  311. tokenizer->advance();
  312. }
  313. Node *signal = _parse_and_reduce_expression(p_parent,p_static);
  314. if (!signal)
  315. return NULL;
  316. yield->arguments.push_back(signal);
  317. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  318. _set_error("Expected ')' after second argument of 'yield'");
  319. return NULL;
  320. }
  321. tokenizer->advance();
  322. expr=yield;
  323. }
  324. } else if (tokenizer->get_token()==GDTokenizer::TK_SELF) {
  325. if (p_static) {
  326. _set_error("'self'' not allowed in static function or constant expression");
  327. return NULL;
  328. }
  329. //constant defined by tokenizer
  330. SelfNode *self = alloc_node<SelfNode>();
  331. tokenizer->advance();
  332. expr=self;
  333. } else if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1)==GDTokenizer::TK_PERIOD) {
  334. Variant::Type bi_type = tokenizer->get_token_type();
  335. tokenizer->advance(2);
  336. StringName identifier;
  337. if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT,identifier)) {
  338. completion_built_in_constant=bi_type;
  339. }
  340. if (identifier==StringName()) {
  341. _set_error("Built-in type constant expected after '.'");
  342. return NULL;
  343. }
  344. if (!Variant::has_numeric_constant(bi_type,identifier)) {
  345. _set_error("Static constant '"+identifier.operator String()+"' not present in built-in type "+Variant::get_type_name(bi_type)+".");
  346. return NULL;
  347. }
  348. ConstantNode *cn = alloc_node<ConstantNode>();
  349. cn->value=Variant::get_numeric_constant_value(bi_type,identifier);
  350. expr=cn;
  351. } else if (tokenizer->get_token(1)==GDTokenizer::TK_PARENTHESIS_OPEN && (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE || tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER || tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_FUNC)) {
  352. //function or constructor
  353. OperatorNode *op = alloc_node<OperatorNode>();
  354. op->op=OperatorNode::OP_CALL;
  355. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE) {
  356. TypeNode *tn = alloc_node<TypeNode>();
  357. tn->vtype=tokenizer->get_token_type();
  358. op->arguments.push_back(tn);
  359. tokenizer->advance(2);
  360. } else if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_FUNC) {
  361. BuiltInFunctionNode *bn = alloc_node<BuiltInFunctionNode>();
  362. bn->function=tokenizer->get_token_built_in_func();
  363. op->arguments.push_back(bn);
  364. tokenizer->advance(2);
  365. } else {
  366. SelfNode *self = alloc_node<SelfNode>();
  367. op->arguments.push_back(self);
  368. StringName identifier;
  369. if (_get_completable_identifier(COMPLETION_FUNCTION,identifier)) {
  370. }
  371. IdentifierNode* id = alloc_node<IdentifierNode>();
  372. id->name=identifier;
  373. op->arguments.push_back(id);
  374. tokenizer->advance(1);
  375. }
  376. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  377. _make_completable_call(0);
  378. completion_node=op;
  379. }
  380. if (!_parse_arguments(op,op->arguments,p_static,true))
  381. return NULL;
  382. expr=op;
  383. } else if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  384. //identifier (reference)
  385. const ClassNode* cln = current_class;
  386. bool bfn = false;
  387. StringName identifier;
  388. if (_get_completable_identifier(COMPLETION_IDENTIFIER,identifier)) {
  389. }
  390. if (p_parsing_constant) {
  391. for( int i=0; i<cln->constant_expressions.size(); ++i ) {
  392. if( cln->constant_expressions[i].identifier == identifier ) {
  393. expr = cln->constant_expressions[i].expression;
  394. bfn = true;
  395. break;
  396. }
  397. }
  398. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  399. //check from constants
  400. ConstantNode *constant = alloc_node<ConstantNode>();
  401. constant->value = GDScriptLanguage::get_singleton()->get_global_array()[ GDScriptLanguage::get_singleton()->get_global_map()[identifier] ];
  402. expr=constant;
  403. bfn = true;
  404. }
  405. }
  406. if ( !bfn ) {
  407. IdentifierNode *id = alloc_node<IdentifierNode>();
  408. id->name = identifier;
  409. expr = id;
  410. }
  411. } else if (/*tokenizer->get_token()==GDTokenizer::TK_OP_ADD ||*/ tokenizer->get_token()==GDTokenizer::TK_OP_SUB || tokenizer->get_token()==GDTokenizer::TK_OP_NOT || tokenizer->get_token()==GDTokenizer::TK_OP_BIT_INVERT) {
  412. //single prefix operators like !expr -expr ++expr --expr
  413. alloc_node<OperatorNode>();
  414. Expression e;
  415. e.is_op=true;
  416. switch(tokenizer->get_token()) {
  417. case GDTokenizer::TK_OP_SUB: e.op=OperatorNode::OP_NEG; break;
  418. case GDTokenizer::TK_OP_NOT: e.op=OperatorNode::OP_NOT; break;
  419. case GDTokenizer::TK_OP_BIT_INVERT: e.op=OperatorNode::OP_BIT_INVERT;; break;
  420. default: {}
  421. }
  422. tokenizer->advance();
  423. if (e.op!=OperatorNode::OP_NOT && tokenizer->get_token()==GDTokenizer::TK_OP_NOT) {
  424. _set_error("Misplaced 'not'.");
  425. return NULL;
  426. }
  427. expression.push_back(e);
  428. continue; //only exception, must continue...
  429. /*
  430. Node *subexpr=_parse_expression(op,p_static);
  431. if (!subexpr)
  432. return NULL;
  433. op->arguments.push_back(subexpr);
  434. expr=op;*/
  435. } else if (tokenizer->get_token()==GDTokenizer::TK_BRACKET_OPEN) {
  436. // array
  437. tokenizer->advance();
  438. ArrayNode *arr = alloc_node<ArrayNode>();
  439. bool expecting_comma=false;
  440. while(true) {
  441. if (tokenizer->get_token()==GDTokenizer::TK_EOF) {
  442. _set_error("Unterminated array");
  443. return NULL;
  444. } else if (tokenizer->get_token()==GDTokenizer::TK_BRACKET_CLOSE) {
  445. tokenizer->advance();
  446. break;
  447. } else if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  448. tokenizer->advance(); //ignore newline
  449. } else if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  450. if (!expecting_comma) {
  451. _set_error("expression or ']' expected");
  452. return NULL;
  453. }
  454. expecting_comma=false;
  455. tokenizer->advance(); //ignore newline
  456. } else {
  457. //parse expression
  458. if (expecting_comma) {
  459. _set_error("',' or ']' expected");
  460. return NULL;
  461. }
  462. Node *n = _parse_expression(arr,p_static,p_allow_assign,p_parsing_constant);
  463. if (!n)
  464. return NULL;
  465. arr->elements.push_back(n);
  466. expecting_comma=true;
  467. }
  468. }
  469. expr=arr;
  470. } else if (tokenizer->get_token()==GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  471. // array
  472. tokenizer->advance();
  473. DictionaryNode *dict = alloc_node<DictionaryNode>();
  474. enum DictExpect {
  475. DICT_EXPECT_KEY,
  476. DICT_EXPECT_COLON,
  477. DICT_EXPECT_VALUE,
  478. DICT_EXPECT_COMMA
  479. };
  480. Node *key=NULL;
  481. DictExpect expecting=DICT_EXPECT_KEY;
  482. while(true) {
  483. if (tokenizer->get_token()==GDTokenizer::TK_EOF) {
  484. _set_error("Unterminated dictionary");
  485. return NULL;
  486. } else if (tokenizer->get_token()==GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  487. if (expecting==DICT_EXPECT_COLON) {
  488. _set_error("':' expected");
  489. return NULL;
  490. }
  491. if (expecting==DICT_EXPECT_VALUE) {
  492. _set_error("value expected");
  493. return NULL;
  494. }
  495. tokenizer->advance();
  496. break;
  497. } else if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  498. tokenizer->advance(); //ignore newline
  499. } else if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  500. if (expecting==DICT_EXPECT_KEY) {
  501. _set_error("key or '}' expected");
  502. return NULL;
  503. }
  504. if (expecting==DICT_EXPECT_VALUE) {
  505. _set_error("value expected");
  506. return NULL;
  507. }
  508. if (expecting==DICT_EXPECT_COLON) {
  509. _set_error("':' expected");
  510. return NULL;
  511. }
  512. expecting=DICT_EXPECT_KEY;
  513. tokenizer->advance(); //ignore newline
  514. } else if (tokenizer->get_token()==GDTokenizer::TK_COLON) {
  515. if (expecting==DICT_EXPECT_KEY) {
  516. _set_error("key or '}' expected");
  517. return NULL;
  518. }
  519. if (expecting==DICT_EXPECT_VALUE) {
  520. _set_error("value expected");
  521. return NULL;
  522. }
  523. if (expecting==DICT_EXPECT_COMMA) {
  524. _set_error("',' or '}' expected");
  525. return NULL;
  526. }
  527. expecting=DICT_EXPECT_VALUE;
  528. tokenizer->advance(); //ignore newline
  529. } else {
  530. if (expecting==DICT_EXPECT_COMMA) {
  531. _set_error("',' or '}' expected");
  532. return NULL;
  533. }
  534. if (expecting==DICT_EXPECT_COLON) {
  535. _set_error("':' expected");
  536. return NULL;
  537. }
  538. if (expecting==DICT_EXPECT_KEY) {
  539. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token(1)==GDTokenizer::TK_OP_ASSIGN) {
  540. //lua style identifier, easier to write
  541. ConstantNode *cn = alloc_node<ConstantNode>();
  542. cn->value = tokenizer->get_token_identifier();
  543. key = cn;
  544. tokenizer->advance(2);
  545. expecting=DICT_EXPECT_VALUE;
  546. } else {
  547. //python/js style more flexible
  548. key = _parse_expression(dict,p_static,p_allow_assign,p_parsing_constant);
  549. if (!key)
  550. return NULL;
  551. expecting=DICT_EXPECT_COLON;
  552. }
  553. }
  554. if (expecting==DICT_EXPECT_VALUE) {
  555. Node *value = _parse_expression(dict,p_static,p_allow_assign,p_parsing_constant);
  556. if (!value)
  557. return NULL;
  558. expecting=DICT_EXPECT_COMMA;
  559. DictionaryNode::Pair pair;
  560. pair.key=key;
  561. pair.value=value;
  562. dict->elements.push_back(pair);
  563. key=NULL;
  564. }
  565. }
  566. }
  567. expr=dict;
  568. } else if (tokenizer->get_token()==GDTokenizer::TK_PERIOD && (tokenizer->get_token(1)==GDTokenizer::TK_IDENTIFIER || tokenizer->get_token(1)==GDTokenizer::TK_CURSOR) && tokenizer->get_token(2)==GDTokenizer::TK_PARENTHESIS_OPEN) {
  569. // parent call
  570. tokenizer->advance(); //goto identifier
  571. OperatorNode *op = alloc_node<OperatorNode>();
  572. op->op=OperatorNode::OP_PARENT_CALL;
  573. /*SelfNode *self = alloc_node<SelfNode>();
  574. op->arguments.push_back(self);
  575. forbidden for now */
  576. StringName identifier;
  577. if (_get_completable_identifier(COMPLETION_PARENT_FUNCTION,identifier)) {
  578. //indexing stuff
  579. }
  580. IdentifierNode *id = alloc_node<IdentifierNode>();
  581. id->name=identifier;
  582. op->arguments.push_back(id);
  583. tokenizer->advance(1);
  584. if (!_parse_arguments(op,op->arguments,p_static))
  585. return NULL;
  586. expr=op;
  587. } else {
  588. //find list [ or find dictionary {
  589. //print_line("found bug?");
  590. _set_error("Error parsing expression, misplaced: "+String(tokenizer->get_token_name(tokenizer->get_token())));
  591. return NULL; //nothing
  592. }
  593. if (!expr) {
  594. ERR_EXPLAIN("GDParser bug, couldn't figure out what expression is..");
  595. ERR_FAIL_COND_V(!expr,NULL);
  596. }
  597. /******************/
  598. /* Parse Indexing */
  599. /******************/
  600. while (true) {
  601. //expressions can be indexed any number of times
  602. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  603. //indexing using "."
  604. if (tokenizer->get_token(1)!=GDTokenizer::TK_CURSOR && tokenizer->get_token(1)!=GDTokenizer::TK_IDENTIFIER && tokenizer->get_token(1)!=GDTokenizer::TK_BUILT_IN_FUNC ) {
  605. _set_error("Expected identifier as member");
  606. return NULL;
  607. } else if (tokenizer->get_token(2)==GDTokenizer::TK_PARENTHESIS_OPEN) {
  608. //call!!
  609. OperatorNode * op = alloc_node<OperatorNode>();
  610. op->op=OperatorNode::OP_CALL;
  611. tokenizer->advance();
  612. IdentifierNode * id = alloc_node<IdentifierNode>();
  613. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_FUNC ) {
  614. //small hack so built in funcs don't obfuscate methods
  615. id->name=GDFunctions::get_func_name(tokenizer->get_token_built_in_func());
  616. tokenizer->advance();
  617. } else {
  618. StringName identifier;
  619. if (_get_completable_identifier(COMPLETION_METHOD,identifier)) {
  620. completion_node=op;
  621. //indexing stuff
  622. }
  623. id->name=identifier;
  624. }
  625. op->arguments.push_back(expr); // call what
  626. op->arguments.push_back(id); // call func
  627. //get arguments
  628. tokenizer->advance(1);
  629. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  630. _make_completable_call(0);
  631. completion_node=op;
  632. }
  633. if (!_parse_arguments(op,op->arguments,p_static,true))
  634. return NULL;
  635. expr=op;
  636. } else {
  637. //simple indexing!
  638. OperatorNode * op = alloc_node<OperatorNode>();
  639. op->op=OperatorNode::OP_INDEX_NAMED;
  640. tokenizer->advance();
  641. StringName identifier;
  642. if (_get_completable_identifier(COMPLETION_INDEX,identifier)) {
  643. if (identifier==StringName()) {
  644. identifier="@temp"; //so it parses allright
  645. }
  646. completion_node=op;
  647. //indexing stuff
  648. }
  649. IdentifierNode * id = alloc_node<IdentifierNode>();
  650. id->name=identifier;
  651. op->arguments.push_back(expr);
  652. op->arguments.push_back(id);
  653. expr=op;
  654. }
  655. } else if (tokenizer->get_token()==GDTokenizer::TK_BRACKET_OPEN) {
  656. //indexing using "[]"
  657. OperatorNode * op = alloc_node<OperatorNode>();
  658. op->op=OperatorNode::OP_INDEX;
  659. tokenizer->advance(1);
  660. Node *subexpr = _parse_expression(op,p_static,p_allow_assign,p_parsing_constant);
  661. if (!subexpr) {
  662. return NULL;
  663. }
  664. if (tokenizer->get_token()!=GDTokenizer::TK_BRACKET_CLOSE) {
  665. _set_error("Expected ']'");
  666. return NULL;
  667. }
  668. op->arguments.push_back(expr);
  669. op->arguments.push_back(subexpr);
  670. tokenizer->advance(1);
  671. expr=op;
  672. } else
  673. break;
  674. }
  675. /******************/
  676. /* Parse Operator */
  677. /******************/
  678. if (parenthesis>0) {
  679. //remove empty space (only allowed if inside parenthesis
  680. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  681. tokenizer->advance();
  682. }
  683. }
  684. Expression e;
  685. e.is_op=false;
  686. e.node=expr;
  687. expression.push_back(e);
  688. // determine which operator is next
  689. OperatorNode::Operator op;
  690. bool valid=true;
  691. //assign, if allowed is only alowed on the first operator
  692. #define _VALIDATE_ASSIGN if (!p_allow_assign) { _set_error("Unexpected assign."); return NULL; } p_allow_assign=false;
  693. switch(tokenizer->get_token()) { //see operator
  694. case GDTokenizer::TK_OP_IN: op=OperatorNode::OP_IN; break;
  695. case GDTokenizer::TK_OP_EQUAL: op=OperatorNode::OP_EQUAL ; break;
  696. case GDTokenizer::TK_OP_NOT_EQUAL: op=OperatorNode::OP_NOT_EQUAL ; break;
  697. case GDTokenizer::TK_OP_LESS: op=OperatorNode::OP_LESS ; break;
  698. case GDTokenizer::TK_OP_LESS_EQUAL: op=OperatorNode::OP_LESS_EQUAL ; break;
  699. case GDTokenizer::TK_OP_GREATER: op=OperatorNode::OP_GREATER ; break;
  700. case GDTokenizer::TK_OP_GREATER_EQUAL: op=OperatorNode::OP_GREATER_EQUAL ; break;
  701. case GDTokenizer::TK_OP_AND: op=OperatorNode::OP_AND ; break;
  702. case GDTokenizer::TK_OP_OR: op=OperatorNode::OP_OR ; break;
  703. case GDTokenizer::TK_OP_ADD: op=OperatorNode::OP_ADD ; break;
  704. case GDTokenizer::TK_OP_SUB: op=OperatorNode::OP_SUB ; break;
  705. case GDTokenizer::TK_OP_MUL: op=OperatorNode::OP_MUL ; break;
  706. case GDTokenizer::TK_OP_DIV: op=OperatorNode::OP_DIV ; break;
  707. case GDTokenizer::TK_OP_MOD: op=OperatorNode::OP_MOD ; break;
  708. //case GDTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  709. case GDTokenizer::TK_OP_SHIFT_LEFT: op=OperatorNode::OP_SHIFT_LEFT ; break;
  710. case GDTokenizer::TK_OP_SHIFT_RIGHT: op=OperatorNode::OP_SHIFT_RIGHT ; break;
  711. case GDTokenizer::TK_OP_ASSIGN: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN ; break;
  712. case GDTokenizer::TK_OP_ASSIGN_ADD: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_ADD ; break;
  713. case GDTokenizer::TK_OP_ASSIGN_SUB: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_SUB ; break;
  714. case GDTokenizer::TK_OP_ASSIGN_MUL: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_MUL ; break;
  715. case GDTokenizer::TK_OP_ASSIGN_DIV: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_DIV ; break;
  716. case GDTokenizer::TK_OP_ASSIGN_MOD: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_MOD ; break;
  717. case GDTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_SHIFT_LEFT; ; break;
  718. case GDTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_SHIFT_RIGHT; ; break;
  719. case GDTokenizer::TK_OP_ASSIGN_BIT_AND: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_BIT_AND ; break;
  720. case GDTokenizer::TK_OP_ASSIGN_BIT_OR: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_BIT_OR ; break;
  721. case GDTokenizer::TK_OP_ASSIGN_BIT_XOR: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_BIT_XOR ; break;
  722. case GDTokenizer::TK_OP_BIT_AND: op=OperatorNode::OP_BIT_AND ; break;
  723. case GDTokenizer::TK_OP_BIT_OR: op=OperatorNode::OP_BIT_OR ; break;
  724. case GDTokenizer::TK_OP_BIT_XOR: op=OperatorNode::OP_BIT_XOR ; break;
  725. case GDTokenizer::TK_PR_EXTENDS: op=OperatorNode::OP_EXTENDS; break;
  726. case GDTokenizer::TK_CF_IF: op=OperatorNode::OP_TERNARY_IF; break;
  727. case GDTokenizer::TK_CF_ELSE: op=OperatorNode::OP_TERNARY_ELSE; break;
  728. default: valid=false; break;
  729. }
  730. if (valid) {
  731. e.is_op=true;
  732. e.op=op;
  733. expression.push_back(e);
  734. tokenizer->advance();
  735. } else {
  736. break;
  737. }
  738. }
  739. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  740. while(expression.size()>1) {
  741. int next_op=-1;
  742. int min_priority=0xFFFFF;
  743. bool is_unary=false;
  744. bool is_ternary=false;
  745. for(int i=0;i<expression.size();i++) {
  746. if (!expression[i].is_op) {
  747. continue;
  748. }
  749. int priority;
  750. bool unary=false;
  751. bool ternary=false;
  752. bool error=false;
  753. switch(expression[i].op) {
  754. case OperatorNode::OP_EXTENDS: priority=-1; break; //before anything
  755. case OperatorNode::OP_BIT_INVERT: priority=0; unary=true; break;
  756. case OperatorNode::OP_NEG: priority=1; unary=true; break;
  757. case OperatorNode::OP_MUL: priority=2; break;
  758. case OperatorNode::OP_DIV: priority=2; break;
  759. case OperatorNode::OP_MOD: priority=2; break;
  760. case OperatorNode::OP_ADD: priority=3; break;
  761. case OperatorNode::OP_SUB: priority=3; break;
  762. case OperatorNode::OP_SHIFT_LEFT: priority=4; break;
  763. case OperatorNode::OP_SHIFT_RIGHT: priority=4; break;
  764. case OperatorNode::OP_BIT_AND: priority=5; break;
  765. case OperatorNode::OP_BIT_XOR: priority=6; break;
  766. case OperatorNode::OP_BIT_OR: priority=7; break;
  767. case OperatorNode::OP_LESS: priority=8; break;
  768. case OperatorNode::OP_LESS_EQUAL: priority=8; break;
  769. case OperatorNode::OP_GREATER: priority=8; break;
  770. case OperatorNode::OP_GREATER_EQUAL: priority=8; break;
  771. case OperatorNode::OP_EQUAL: priority=8; break;
  772. case OperatorNode::OP_NOT_EQUAL: priority=8; break;
  773. case OperatorNode::OP_IN: priority=10; break;
  774. case OperatorNode::OP_NOT: priority=11; unary=true; break;
  775. case OperatorNode::OP_AND: priority=12; break;
  776. case OperatorNode::OP_OR: priority=13; break;
  777. case OperatorNode::OP_TERNARY_IF: priority=14; ternary=true; break;
  778. case OperatorNode::OP_TERNARY_ELSE: priority=14; error=true; break; // Errors out when found without IF (since IF would consume it)
  779. case OperatorNode::OP_ASSIGN: priority=15; break;
  780. case OperatorNode::OP_ASSIGN_ADD: priority=15; break;
  781. case OperatorNode::OP_ASSIGN_SUB: priority=15; break;
  782. case OperatorNode::OP_ASSIGN_MUL: priority=15; break;
  783. case OperatorNode::OP_ASSIGN_DIV: priority=15; break;
  784. case OperatorNode::OP_ASSIGN_MOD: priority=15; break;
  785. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority=15; break;
  786. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority=15; break;
  787. case OperatorNode::OP_ASSIGN_BIT_AND: priority=15; break;
  788. case OperatorNode::OP_ASSIGN_BIT_OR: priority=15; break;
  789. case OperatorNode::OP_ASSIGN_BIT_XOR: priority=15; break;
  790. default: {
  791. _set_error("GDParser bug, invalid operator in expression: "+itos(expression[i].op));
  792. return NULL;
  793. }
  794. }
  795. if (priority<min_priority) {
  796. if(error) {
  797. _set_error("Unexpected operator");
  798. return NULL;
  799. }
  800. // < is used for left to right (default)
  801. // <= is used for right to left
  802. next_op=i;
  803. min_priority=priority;
  804. is_unary=unary;
  805. is_ternary=ternary;
  806. }
  807. }
  808. if (next_op==-1) {
  809. _set_error("Yet another parser bug....");
  810. ERR_FAIL_COND_V(next_op==-1,NULL);
  811. }
  812. // OK! create operator..
  813. if (is_unary) {
  814. int expr_pos=next_op;
  815. while(expression[expr_pos].is_op) {
  816. expr_pos++;
  817. if (expr_pos==expression.size()) {
  818. //can happen..
  819. _set_error("Unexpected end of expression..");
  820. return NULL;
  821. }
  822. }
  823. //consecutively do unary opeators
  824. for(int i=expr_pos-1;i>=next_op;i--) {
  825. OperatorNode *op = alloc_node<OperatorNode>();
  826. op->op=expression[i].op;
  827. op->arguments.push_back(expression[i+1].node);
  828. op->line=op_line; //line might have been changed from a \n
  829. expression[i].is_op=false;
  830. expression[i].node=op;
  831. expression.remove(i+1);
  832. }
  833. } else if(is_ternary) {
  834. if (next_op <1 || next_op>=(expression.size()-1)) {
  835. _set_error("Parser bug..");
  836. ERR_FAIL_V(NULL);
  837. }
  838. if(next_op>=(expression.size()-2) || expression[next_op+2].op != OperatorNode::OP_TERNARY_ELSE) {
  839. _set_error("Expected else after ternary if.");
  840. ERR_FAIL_V(NULL);
  841. }
  842. if(next_op>=(expression.size()-3)) {
  843. _set_error("Expected value after ternary else.");
  844. ERR_FAIL_V(NULL);
  845. }
  846. OperatorNode *op = alloc_node<OperatorNode>();
  847. op->op=expression[next_op].op;
  848. op->line=op_line; //line might have been changed from a \n
  849. if (expression[next_op-1].is_op) {
  850. _set_error("Parser bug..");
  851. ERR_FAIL_V(NULL);
  852. }
  853. if (expression[next_op+1].is_op) {
  854. // this is not invalid and can really appear
  855. // but it becomes invalid anyway because no binary op
  856. // can be followed by an unary op in a valid combination,
  857. // due to how precedence works, unaries will always dissapear first
  858. _set_error("Unexpected two consecutive operators after ternary if.");
  859. return NULL;
  860. }
  861. if (expression[next_op+3].is_op) {
  862. // this is not invalid and can really appear
  863. // but it becomes invalid anyway because no binary op
  864. // can be followed by an unary op in a valid combination,
  865. // due to how precedence works, unaries will always dissapear first
  866. _set_error("Unexpected two consecutive operators after ternary else.");
  867. return NULL;
  868. }
  869. op->arguments.push_back(expression[next_op+1].node); //next expression goes as first
  870. op->arguments.push_back(expression[next_op-1].node); //left expression goes as when-true
  871. op->arguments.push_back(expression[next_op+3].node); //expression after next goes as when-false
  872. //replace all 3 nodes by this operator and make it an expression
  873. expression[next_op-1].node=op;
  874. expression.remove(next_op);
  875. expression.remove(next_op);
  876. expression.remove(next_op);
  877. expression.remove(next_op);
  878. } else {
  879. if (next_op <1 || next_op>=(expression.size()-1)) {
  880. _set_error("Parser bug..");
  881. ERR_FAIL_V(NULL);
  882. }
  883. OperatorNode *op = alloc_node<OperatorNode>();
  884. op->op=expression[next_op].op;
  885. op->line=op_line; //line might have been changed from a \n
  886. if (expression[next_op-1].is_op) {
  887. _set_error("Parser bug..");
  888. ERR_FAIL_V(NULL);
  889. }
  890. if (expression[next_op+1].is_op) {
  891. // this is not invalid and can really appear
  892. // but it becomes invalid anyway because no binary op
  893. // can be followed by an unary op in a valid combination,
  894. // due to how precedence works, unaries will always dissapear first
  895. _set_error("Unexpected two consecutive operators.");
  896. return NULL;
  897. }
  898. op->arguments.push_back(expression[next_op-1].node); //expression goes as left
  899. op->arguments.push_back(expression[next_op+1].node); //next expression goes as right
  900. //replace all 3 nodes by this operator and make it an expression
  901. expression[next_op-1].node=op;
  902. expression.remove(next_op);
  903. expression.remove(next_op);
  904. }
  905. }
  906. return expression[0].node;
  907. }
  908. GDParser::Node* GDParser::_reduce_expression(Node *p_node,bool p_to_const) {
  909. switch(p_node->type) {
  910. case Node::TYPE_BUILT_IN_FUNCTION: {
  911. //many may probably be optimizable
  912. return p_node;
  913. } break;
  914. case Node::TYPE_ARRAY: {
  915. ArrayNode *an = static_cast<ArrayNode*>(p_node);
  916. bool all_constants=true;
  917. for(int i=0;i<an->elements.size();i++) {
  918. an->elements[i]=_reduce_expression(an->elements[i],p_to_const);
  919. if (an->elements[i]->type!=Node::TYPE_CONSTANT)
  920. all_constants=false;
  921. }
  922. if (all_constants && p_to_const) {
  923. //reduce constant array expression
  924. ConstantNode *cn = alloc_node<ConstantNode>();
  925. Array arr(!p_to_const);
  926. //print_line("mk array "+itos(!p_to_const));
  927. arr.resize(an->elements.size());
  928. for(int i=0;i<an->elements.size();i++) {
  929. ConstantNode *acn = static_cast<ConstantNode*>(an->elements[i]);
  930. arr[i]=acn->value;
  931. }
  932. cn->value=arr;
  933. return cn;
  934. }
  935. return an;
  936. } break;
  937. case Node::TYPE_DICTIONARY: {
  938. DictionaryNode *dn = static_cast<DictionaryNode*>(p_node);
  939. bool all_constants=true;
  940. for(int i=0;i<dn->elements.size();i++) {
  941. dn->elements[i].key=_reduce_expression(dn->elements[i].key,p_to_const);
  942. if (dn->elements[i].key->type!=Node::TYPE_CONSTANT)
  943. all_constants=false;
  944. dn->elements[i].value=_reduce_expression(dn->elements[i].value,p_to_const);
  945. if (dn->elements[i].value->type!=Node::TYPE_CONSTANT)
  946. all_constants=false;
  947. }
  948. if (all_constants && p_to_const) {
  949. //reduce constant array expression
  950. ConstantNode *cn = alloc_node<ConstantNode>();
  951. Dictionary dict(!p_to_const);
  952. for(int i=0;i<dn->elements.size();i++) {
  953. ConstantNode *key_c = static_cast<ConstantNode*>(dn->elements[i].key);
  954. ConstantNode *value_c = static_cast<ConstantNode*>(dn->elements[i].value);
  955. dict[key_c->value]=value_c->value;
  956. }
  957. cn->value=dict;
  958. return cn;
  959. }
  960. return dn;
  961. } break;
  962. case Node::TYPE_OPERATOR: {
  963. OperatorNode *op=static_cast<OperatorNode*>(p_node);
  964. bool all_constants=true;
  965. int last_not_constant=-1;
  966. for(int i=0;i<op->arguments.size();i++) {
  967. op->arguments[i]=_reduce_expression(op->arguments[i],p_to_const);
  968. if (op->arguments[i]->type!=Node::TYPE_CONSTANT) {
  969. all_constants=false;
  970. last_not_constant=i;
  971. }
  972. }
  973. if (op->op==OperatorNode::OP_EXTENDS) {
  974. //nothing much
  975. return op;
  976. } if (op->op==OperatorNode::OP_PARENT_CALL) {
  977. //nothing much
  978. return op;
  979. } else if (op->op==OperatorNode::OP_CALL) {
  980. //can reduce base type constructors
  981. if ((op->arguments[0]->type==Node::TYPE_TYPE || (op->arguments[0]->type==Node::TYPE_BUILT_IN_FUNCTION && GDFunctions::is_deterministic( static_cast<BuiltInFunctionNode*>(op->arguments[0])->function))) && last_not_constant==0) {
  982. //native type constructor or intrinsic function
  983. const Variant **vptr=NULL;
  984. Vector<Variant*> ptrs;
  985. if (op->arguments.size()>1) {
  986. ptrs.resize(op->arguments.size()-1);
  987. for(int i=0;i<ptrs.size();i++) {
  988. ConstantNode *cn = static_cast<ConstantNode*>(op->arguments[i+1]);
  989. ptrs[i]=&cn->value;
  990. }
  991. vptr=(const Variant**)&ptrs[0];
  992. }
  993. Variant::CallError ce;
  994. Variant v;
  995. if (op->arguments[0]->type==Node::TYPE_TYPE) {
  996. TypeNode *tn = static_cast<TypeNode*>(op->arguments[0]);
  997. v = Variant::construct(tn->vtype,vptr,ptrs.size(),ce);
  998. } else {
  999. GDFunctions::Function func = static_cast<BuiltInFunctionNode*>(op->arguments[0])->function;
  1000. GDFunctions::call(func,vptr,ptrs.size(),v,ce);
  1001. }
  1002. if (ce.error!=Variant::CallError::CALL_OK) {
  1003. String errwhere;
  1004. if (op->arguments[0]->type==Node::TYPE_TYPE) {
  1005. TypeNode *tn = static_cast<TypeNode*>(op->arguments[0]);
  1006. errwhere="'"+Variant::get_type_name(tn->vtype)+"'' constructor";
  1007. } else {
  1008. GDFunctions::Function func = static_cast<BuiltInFunctionNode*>(op->arguments[0])->function;
  1009. errwhere=String("'")+GDFunctions::get_func_name(func)+"'' intrinsic function";
  1010. }
  1011. switch(ce.error) {
  1012. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1013. _set_error("Invalid argument (#"+itos(ce.argument+1)+") for "+errwhere+".");
  1014. } break;
  1015. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1016. _set_error("Too many arguments for "+errwhere+".");
  1017. } break;
  1018. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1019. _set_error("Too few arguments for "+errwhere+".");
  1020. } break;
  1021. default: {
  1022. _set_error("Invalid arguments for "+errwhere+".");
  1023. } break;
  1024. }
  1025. error_line=op->line;
  1026. return p_node;
  1027. }
  1028. ConstantNode *cn = alloc_node<ConstantNode>();
  1029. cn->value=v;
  1030. return cn;
  1031. } else if (op->arguments[0]->type==Node::TYPE_BUILT_IN_FUNCTION && last_not_constant==0) {
  1032. }
  1033. return op; //don't reduce yet
  1034. } else if (op->op==OperatorNode::OP_YIELD) {
  1035. return op;
  1036. } else if (op->op==OperatorNode::OP_INDEX) {
  1037. //can reduce indices into constant arrays or dictionaries
  1038. if (all_constants) {
  1039. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  1040. ConstantNode *cb = static_cast<ConstantNode*>(op->arguments[1]);
  1041. bool valid;
  1042. Variant v = ca->value.get(cb->value,&valid);
  1043. if (!valid) {
  1044. _set_error("invalid index in constant expression");
  1045. error_line=op->line;
  1046. return op;
  1047. }
  1048. ConstantNode *cn = alloc_node<ConstantNode>();
  1049. cn->value=v;
  1050. return cn;
  1051. } /*else if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  1052. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  1053. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  1054. bool valid;
  1055. Variant v = ca->value.get_named(ib->name,&valid);
  1056. if (!valid) {
  1057. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  1058. return op;
  1059. }
  1060. ConstantNode *cn = alloc_node<ConstantNode>();
  1061. cn->value=v;
  1062. return cn;
  1063. }*/
  1064. return op;
  1065. } else if (op->op==OperatorNode::OP_INDEX_NAMED) {
  1066. if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  1067. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  1068. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  1069. bool valid;
  1070. Variant v = ca->value.get_named(ib->name,&valid);
  1071. if (!valid) {
  1072. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  1073. error_line=op->line;
  1074. return op;
  1075. }
  1076. ConstantNode *cn = alloc_node<ConstantNode>();
  1077. cn->value=v;
  1078. return cn;
  1079. }
  1080. return op;
  1081. }
  1082. //validate assignment (don't assign to cosntant expression
  1083. switch(op->op) {
  1084. case OperatorNode::OP_ASSIGN:
  1085. case OperatorNode::OP_ASSIGN_ADD:
  1086. case OperatorNode::OP_ASSIGN_SUB:
  1087. case OperatorNode::OP_ASSIGN_MUL:
  1088. case OperatorNode::OP_ASSIGN_DIV:
  1089. case OperatorNode::OP_ASSIGN_MOD:
  1090. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1091. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1092. case OperatorNode::OP_ASSIGN_BIT_AND:
  1093. case OperatorNode::OP_ASSIGN_BIT_OR:
  1094. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1095. if (op->arguments[0]->type==Node::TYPE_CONSTANT) {
  1096. _set_error("Can't assign to constant",tokenizer->get_token_line()-1);
  1097. error_line=op->line;
  1098. return op;
  1099. }
  1100. } break;
  1101. default: { break; }
  1102. }
  1103. //now se if all are constants
  1104. if (!all_constants)
  1105. return op; //nothing to reduce from here on
  1106. #define _REDUCE_UNARY(m_vop)\
  1107. bool valid=false;\
  1108. Variant res;\
  1109. Variant::evaluate(m_vop,static_cast<ConstantNode*>(op->arguments[0])->value,Variant(),res,valid);\
  1110. if (!valid) {\
  1111. _set_error("Invalid operand for unary operator");\
  1112. error_line=op->line;\
  1113. return p_node;\
  1114. }\
  1115. ConstantNode *cn = alloc_node<ConstantNode>();\
  1116. cn->value=res;\
  1117. return cn;
  1118. #define _REDUCE_BINARY(m_vop)\
  1119. bool valid=false;\
  1120. Variant res;\
  1121. Variant::evaluate(m_vop,static_cast<ConstantNode*>(op->arguments[0])->value,static_cast<ConstantNode*>(op->arguments[1])->value,res,valid);\
  1122. if (!valid) {\
  1123. _set_error("Invalid operands for operator");\
  1124. error_line=op->line;\
  1125. return p_node;\
  1126. }\
  1127. ConstantNode *cn = alloc_node<ConstantNode>();\
  1128. cn->value=res;\
  1129. return cn;
  1130. switch(op->op) {
  1131. //unary operators
  1132. case OperatorNode::OP_NEG: { _REDUCE_UNARY(Variant::OP_NEGATE); } break;
  1133. case OperatorNode::OP_NOT: { _REDUCE_UNARY(Variant::OP_NOT); } break;
  1134. case OperatorNode::OP_BIT_INVERT: { _REDUCE_UNARY(Variant::OP_BIT_NEGATE); } break;
  1135. //binary operators (in precedence order)
  1136. case OperatorNode::OP_IN: { _REDUCE_BINARY(Variant::OP_IN); } break;
  1137. case OperatorNode::OP_EQUAL: { _REDUCE_BINARY(Variant::OP_EQUAL); } break;
  1138. case OperatorNode::OP_NOT_EQUAL: { _REDUCE_BINARY(Variant::OP_NOT_EQUAL); } break;
  1139. case OperatorNode::OP_LESS: { _REDUCE_BINARY(Variant::OP_LESS); } break;
  1140. case OperatorNode::OP_LESS_EQUAL: { _REDUCE_BINARY(Variant::OP_LESS_EQUAL); } break;
  1141. case OperatorNode::OP_GREATER: { _REDUCE_BINARY(Variant::OP_GREATER); } break;
  1142. case OperatorNode::OP_GREATER_EQUAL: { _REDUCE_BINARY(Variant::OP_GREATER_EQUAL); } break;
  1143. case OperatorNode::OP_AND: { _REDUCE_BINARY(Variant::OP_AND); } break;
  1144. case OperatorNode::OP_OR: { _REDUCE_BINARY(Variant::OP_OR); } break;
  1145. case OperatorNode::OP_ADD: { _REDUCE_BINARY(Variant::OP_ADD); } break;
  1146. case OperatorNode::OP_SUB: { _REDUCE_BINARY(Variant::OP_SUBSTRACT); } break;
  1147. case OperatorNode::OP_MUL: { _REDUCE_BINARY(Variant::OP_MULTIPLY); } break;
  1148. case OperatorNode::OP_DIV: { _REDUCE_BINARY(Variant::OP_DIVIDE); } break;
  1149. case OperatorNode::OP_MOD: { _REDUCE_BINARY(Variant::OP_MODULE); } break;
  1150. case OperatorNode::OP_SHIFT_LEFT: { _REDUCE_BINARY(Variant::OP_SHIFT_LEFT); } break;
  1151. case OperatorNode::OP_SHIFT_RIGHT: { _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT); } break;
  1152. case OperatorNode::OP_BIT_AND: { _REDUCE_BINARY(Variant::OP_BIT_AND); } break;
  1153. case OperatorNode::OP_BIT_OR: { _REDUCE_BINARY(Variant::OP_BIT_OR); } break;
  1154. case OperatorNode::OP_BIT_XOR: { _REDUCE_BINARY(Variant::OP_BIT_XOR); } break;
  1155. default: { ERR_FAIL_V(op); }
  1156. }
  1157. ERR_FAIL_V(op);
  1158. } break;
  1159. default: {
  1160. return p_node;
  1161. } break;
  1162. }
  1163. }
  1164. GDParser::Node* GDParser::_parse_and_reduce_expression(Node *p_parent,bool p_static,bool p_reduce_const,bool p_allow_assign) {
  1165. Node* expr=_parse_expression(p_parent,p_static,p_allow_assign,p_reduce_const);
  1166. if (!expr || error_set)
  1167. return NULL;
  1168. expr = _reduce_expression(expr,p_reduce_const);
  1169. if (!expr || error_set)
  1170. return NULL;
  1171. return expr;
  1172. }
  1173. bool GDParser::_recover_from_completion() {
  1174. if (!completion_found) {
  1175. return false; //can't recover if no completion
  1176. }
  1177. //skip stuff until newline
  1178. while(tokenizer->get_token()!=GDTokenizer::TK_NEWLINE && tokenizer->get_token()!=GDTokenizer::TK_EOF && tokenizer->get_token()!=GDTokenizer::TK_ERROR) {
  1179. tokenizer->advance();
  1180. }
  1181. completion_found=false;
  1182. error_set=false;
  1183. if(tokenizer->get_token() == GDTokenizer::TK_ERROR){
  1184. error_set = true;
  1185. }
  1186. return true;
  1187. }
  1188. void GDParser::_parse_block(BlockNode *p_block,bool p_static) {
  1189. int indent_level = tab_level.back()->get();
  1190. #ifdef DEBUG_ENABLED
  1191. NewLineNode *nl = alloc_node<NewLineNode>();
  1192. nl->line=tokenizer->get_token_line();
  1193. p_block->statements.push_back(nl);
  1194. #endif
  1195. while(true) {
  1196. GDTokenizer::Token token = tokenizer->get_token();
  1197. if (error_set)
  1198. return;
  1199. if (indent_level>tab_level.back()->get()) {
  1200. p_block->end_line=tokenizer->get_token_line();
  1201. return; //go back a level
  1202. }
  1203. if (pending_newline!=-1) {
  1204. NewLineNode *nl = alloc_node<NewLineNode>();
  1205. nl->line=pending_newline;
  1206. p_block->statements.push_back(nl);
  1207. pending_newline=-1;
  1208. }
  1209. switch(token) {
  1210. case GDTokenizer::TK_EOF:
  1211. p_block->end_line=tokenizer->get_token_line();
  1212. case GDTokenizer::TK_ERROR: {
  1213. return; //go back
  1214. //end of file!
  1215. } break;
  1216. case GDTokenizer::TK_NEWLINE: {
  1217. if (!_parse_newline()) {
  1218. if (!error_set) {
  1219. p_block->end_line=tokenizer->get_token_line();
  1220. pending_newline=p_block->end_line;
  1221. }
  1222. return;
  1223. }
  1224. NewLineNode *nl = alloc_node<NewLineNode>();
  1225. nl->line=tokenizer->get_token_line();
  1226. p_block->statements.push_back(nl);
  1227. } break;
  1228. case GDTokenizer::TK_CF_PASS: {
  1229. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE && tokenizer->get_token(1)!=GDTokenizer::TK_EOF) {
  1230. _set_error("Expected ';' or <NewLine>.");
  1231. return;
  1232. }
  1233. tokenizer->advance();
  1234. if(tokenizer->get_token()==GDTokenizer::TK_SEMICOLON) {
  1235. // Ignore semicolon after 'pass'
  1236. tokenizer->advance();
  1237. }
  1238. } break;
  1239. case GDTokenizer::TK_PR_VAR: {
  1240. //variale declaration and (eventual) initialization
  1241. tokenizer->advance();
  1242. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1243. _set_error("Expected identifier for local variable name.");
  1244. return;
  1245. }
  1246. StringName n = tokenizer->get_token_identifier();
  1247. tokenizer->advance();
  1248. p_block->variables.push_back(n); //line?
  1249. p_block->variable_lines.push_back(tokenizer->get_token_line());
  1250. //must know when the local variable is declared
  1251. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1252. lv->name=n;
  1253. p_block->statements.push_back(lv);
  1254. Node *assigned=NULL;
  1255. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1256. tokenizer->advance();
  1257. Node *subexpr=NULL;
  1258. subexpr = _parse_and_reduce_expression(p_block,p_static);
  1259. if (!subexpr) {
  1260. if (_recover_from_completion()) {
  1261. break;
  1262. }
  1263. return;
  1264. }
  1265. lv->assign=subexpr;
  1266. assigned=subexpr;
  1267. } else {
  1268. ConstantNode *c = alloc_node<ConstantNode>();
  1269. c->value=Variant();
  1270. assigned = c;
  1271. }
  1272. IdentifierNode *id = alloc_node<IdentifierNode>();
  1273. id->name=n;
  1274. OperatorNode *op = alloc_node<OperatorNode>();
  1275. op->op=OperatorNode::OP_ASSIGN;
  1276. op->arguments.push_back(id);
  1277. op->arguments.push_back(assigned);
  1278. p_block->statements.push_back(op);
  1279. if (!_end_statement()) {
  1280. _set_error("Expected end of statement (var)");
  1281. return;
  1282. }
  1283. } break;
  1284. case GDTokenizer::TK_CF_IF: {
  1285. tokenizer->advance();
  1286. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1287. if (!condition) {
  1288. if (_recover_from_completion()) {
  1289. break;
  1290. }
  1291. return;
  1292. }
  1293. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1294. cf_if->cf_type=ControlFlowNode::CF_IF;
  1295. cf_if->arguments.push_back(condition);
  1296. cf_if->body = alloc_node<BlockNode>();
  1297. cf_if->body->parent_block=p_block;
  1298. p_block->sub_blocks.push_back(cf_if->body);
  1299. if (!_enter_indent_block(cf_if->body)) {
  1300. _set_error("Expected intended block after 'if'");
  1301. p_block->end_line=tokenizer->get_token_line();
  1302. return;
  1303. }
  1304. current_block=cf_if->body;
  1305. _parse_block(cf_if->body,p_static);
  1306. current_block=p_block;
  1307. if (error_set)
  1308. return;
  1309. p_block->statements.push_back(cf_if);
  1310. while(true) {
  1311. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1312. tokenizer->advance();
  1313. }
  1314. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1315. p_block->end_line=tokenizer->get_token_line();
  1316. return;
  1317. }
  1318. if (tokenizer->get_token()==GDTokenizer::TK_CF_ELIF) {
  1319. if (tab_level.back()->get() > indent_level) {
  1320. _set_error("Invalid indent");
  1321. return;
  1322. }
  1323. tokenizer->advance();
  1324. cf_if->body_else=alloc_node<BlockNode>();
  1325. cf_if->body_else->parent_block=p_block;
  1326. p_block->sub_blocks.push_back(cf_if->body_else);
  1327. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1328. cf_else->cf_type=ControlFlowNode::CF_IF;
  1329. //condition
  1330. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1331. if (!condition) {
  1332. if (_recover_from_completion()) {
  1333. break;
  1334. }
  1335. return;
  1336. }
  1337. cf_else->arguments.push_back(condition);
  1338. cf_else->cf_type=ControlFlowNode::CF_IF;
  1339. cf_if->body_else->statements.push_back(cf_else);
  1340. cf_if=cf_else;
  1341. cf_if->body=alloc_node<BlockNode>();
  1342. cf_if->body->parent_block=p_block;
  1343. p_block->sub_blocks.push_back(cf_if->body);
  1344. if (!_enter_indent_block(cf_if->body)) {
  1345. _set_error("Expected indented block after 'elif'");
  1346. p_block->end_line=tokenizer->get_token_line();
  1347. return;
  1348. }
  1349. current_block=cf_else->body;
  1350. _parse_block(cf_else->body,p_static);
  1351. current_block=p_block;
  1352. if (error_set)
  1353. return;
  1354. } else if (tokenizer->get_token()==GDTokenizer::TK_CF_ELSE) {
  1355. if (tab_level.back()->get() > indent_level) {
  1356. _set_error("Invalid indent");
  1357. return;
  1358. }
  1359. tokenizer->advance();
  1360. cf_if->body_else=alloc_node<BlockNode>();
  1361. cf_if->body_else->parent_block=p_block;
  1362. p_block->sub_blocks.push_back(cf_if->body_else);
  1363. if (!_enter_indent_block(cf_if->body_else)) {
  1364. _set_error("Expected indented block after 'else'");
  1365. p_block->end_line=tokenizer->get_token_line();
  1366. return;
  1367. }
  1368. current_block=cf_if->body_else;
  1369. _parse_block(cf_if->body_else,p_static);
  1370. current_block=p_block;
  1371. if (error_set)
  1372. return;
  1373. break; //after else, exit
  1374. } else
  1375. break;
  1376. }
  1377. } break;
  1378. case GDTokenizer::TK_CF_WHILE: {
  1379. tokenizer->advance();
  1380. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1381. if (!condition) {
  1382. if (_recover_from_completion()) {
  1383. break;
  1384. }
  1385. return;
  1386. }
  1387. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  1388. cf_while->cf_type=ControlFlowNode::CF_WHILE;
  1389. cf_while->arguments.push_back(condition);
  1390. cf_while->body = alloc_node<BlockNode>();
  1391. cf_while->body->parent_block=p_block;
  1392. p_block->sub_blocks.push_back(cf_while->body);
  1393. if (!_enter_indent_block(cf_while->body)) {
  1394. _set_error("Expected indented block after 'while'");
  1395. p_block->end_line=tokenizer->get_token_line();
  1396. return;
  1397. }
  1398. current_block=cf_while->body;
  1399. _parse_block(cf_while->body,p_static);
  1400. current_block=p_block;
  1401. if (error_set)
  1402. return;
  1403. p_block->statements.push_back(cf_while);
  1404. } break;
  1405. case GDTokenizer::TK_CF_FOR: {
  1406. tokenizer->advance();
  1407. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1408. _set_error("identifier expected after 'for'");
  1409. }
  1410. IdentifierNode *id = alloc_node<IdentifierNode>();
  1411. id->name=tokenizer->get_token_identifier();
  1412. tokenizer->advance();
  1413. if (tokenizer->get_token()!=GDTokenizer::TK_OP_IN) {
  1414. _set_error("'in' expected after identifier");
  1415. return;
  1416. }
  1417. tokenizer->advance();
  1418. Node *container = _parse_and_reduce_expression(p_block,p_static);
  1419. if (!container) {
  1420. if (_recover_from_completion()) {
  1421. break;
  1422. }
  1423. return;
  1424. }
  1425. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  1426. cf_for->cf_type=ControlFlowNode::CF_FOR;
  1427. cf_for->arguments.push_back(id);
  1428. cf_for->arguments.push_back(container);
  1429. cf_for->body = alloc_node<BlockNode>();
  1430. cf_for->body->parent_block=p_block;
  1431. p_block->sub_blocks.push_back(cf_for->body);
  1432. if (!_enter_indent_block(cf_for->body)) {
  1433. _set_error("Expected indented block after 'for'");
  1434. p_block->end_line=tokenizer->get_token_line();
  1435. return;
  1436. }
  1437. current_block=cf_for->body;
  1438. _parse_block(cf_for->body,p_static);
  1439. current_block=p_block;
  1440. if (error_set)
  1441. return;
  1442. p_block->statements.push_back(cf_for);
  1443. } break;
  1444. case GDTokenizer::TK_CF_CONTINUE: {
  1445. tokenizer->advance();
  1446. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  1447. cf_continue->cf_type=ControlFlowNode::CF_CONTINUE;
  1448. p_block->statements.push_back(cf_continue);
  1449. if (!_end_statement()) {
  1450. _set_error("Expected end of statement (continue)");
  1451. return;
  1452. }
  1453. } break;
  1454. case GDTokenizer::TK_CF_BREAK: {
  1455. tokenizer->advance();
  1456. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  1457. cf_break->cf_type=ControlFlowNode::CF_BREAK;
  1458. p_block->statements.push_back(cf_break);
  1459. if (!_end_statement()) {
  1460. _set_error("Expected end of statement (break)");
  1461. return;
  1462. }
  1463. } break;
  1464. case GDTokenizer::TK_CF_RETURN: {
  1465. tokenizer->advance();
  1466. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  1467. cf_return->cf_type=ControlFlowNode::CF_RETURN;
  1468. if (tokenizer->get_token()==GDTokenizer::TK_SEMICOLON || tokenizer->get_token()==GDTokenizer::TK_NEWLINE || tokenizer->get_token()==GDTokenizer::TK_EOF) {
  1469. //expect end of statement
  1470. p_block->statements.push_back(cf_return);
  1471. if (!_end_statement()) {
  1472. return;
  1473. }
  1474. } else {
  1475. //expect expression
  1476. Node *retexpr = _parse_and_reduce_expression(p_block,p_static);
  1477. if (!retexpr) {
  1478. if (_recover_from_completion()) {
  1479. break;
  1480. }
  1481. return;
  1482. }
  1483. cf_return->arguments.push_back(retexpr);
  1484. p_block->statements.push_back(cf_return);
  1485. if (!_end_statement()) {
  1486. _set_error("Expected end of statement after return expression.");
  1487. return;
  1488. }
  1489. }
  1490. } break;
  1491. case GDTokenizer::TK_PR_ASSERT: {
  1492. tokenizer->advance();
  1493. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1494. if (!condition) {
  1495. if (_recover_from_completion()) {
  1496. break;
  1497. }
  1498. return;
  1499. }
  1500. AssertNode *an = alloc_node<AssertNode>();
  1501. an->condition=condition;
  1502. p_block->statements.push_back(an);
  1503. if (!_end_statement()) {
  1504. _set_error("Expected end of statement after assert.");
  1505. return;
  1506. }
  1507. } break;
  1508. case GDTokenizer::TK_PR_BREAKPOINT: {
  1509. tokenizer->advance();
  1510. BreakpointNode *bn = alloc_node<BreakpointNode>();
  1511. p_block->statements.push_back(bn);
  1512. if (!_end_statement()) {
  1513. _set_error("Expected end of statement after breakpoint.");
  1514. return;
  1515. }
  1516. } break;
  1517. default: {
  1518. Node *expression = _parse_and_reduce_expression(p_block,p_static,false,true);
  1519. if (!expression) {
  1520. if (_recover_from_completion()) {
  1521. break;
  1522. }
  1523. return;
  1524. }
  1525. p_block->statements.push_back(expression);
  1526. if (!_end_statement()) {
  1527. _set_error("Expected end of statement after expression.");
  1528. return;
  1529. }
  1530. } break;
  1531. /*
  1532. case GDTokenizer::TK_CF_LOCAL: {
  1533. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  1534. _set_error("Expected ';' or <NewLine>.");
  1535. }
  1536. tokenizer->advance();
  1537. } break;
  1538. */
  1539. }
  1540. }
  1541. }
  1542. bool GDParser::_parse_newline() {
  1543. if (tokenizer->get_token(1)!=GDTokenizer::TK_EOF && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
  1544. int indent = tokenizer->get_token_line_indent();
  1545. int current_indent = tab_level.back()->get();
  1546. if (indent>current_indent) {
  1547. _set_error("Unexpected indent.");
  1548. return false;
  1549. }
  1550. if (indent<current_indent) {
  1551. while(indent<current_indent) {
  1552. //exit block
  1553. if (tab_level.size()==1) {
  1554. _set_error("Invalid indent. BUG?");
  1555. return false;
  1556. }
  1557. tab_level.pop_back();
  1558. if (tab_level.back()->get()<indent) {
  1559. _set_error("Unindent does not match any outer indentation level.");
  1560. return false;
  1561. }
  1562. current_indent = tab_level.back()->get();
  1563. }
  1564. tokenizer->advance();
  1565. return false;
  1566. }
  1567. }
  1568. tokenizer->advance();
  1569. return true;
  1570. }
  1571. void GDParser::_parse_extends(ClassNode *p_class) {
  1572. if (p_class->extends_used) {
  1573. _set_error("'extends' already used for this class.");
  1574. return;
  1575. }
  1576. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  1577. _set_error("'extends' must be used before anything else.");
  1578. return;
  1579. }
  1580. p_class->extends_used=true;
  1581. tokenizer->advance();
  1582. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type()==Variant::OBJECT) {
  1583. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  1584. tokenizer->advance();
  1585. return;
  1586. }
  1587. // see if inheritance happens from a file
  1588. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT) {
  1589. Variant constant = tokenizer->get_token_constant();
  1590. if (constant.get_type()!=Variant::STRING) {
  1591. _set_error("'extends' constant must be a string.");
  1592. return;
  1593. }
  1594. if (((String)(constant)).begins_with("/")) {
  1595. _set_error("Paths cannot start with '/', absolute paths must start with \'res://\', \'user://\', or \'local://\'");
  1596. return;
  1597. }
  1598. p_class->extends_file=constant;
  1599. tokenizer->advance();
  1600. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD) {
  1601. return;
  1602. } else
  1603. tokenizer->advance();
  1604. }
  1605. while(true) {
  1606. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1607. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  1608. return;
  1609. }
  1610. StringName identifier=tokenizer->get_token_identifier();
  1611. p_class->extends_class.push_back(identifier);
  1612. tokenizer->advance(1);
  1613. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD)
  1614. return;
  1615. }
  1616. }
  1617. void GDParser::_parse_class(ClassNode *p_class) {
  1618. int indent_level = tab_level.back()->get();
  1619. while(true) {
  1620. GDTokenizer::Token token = tokenizer->get_token();
  1621. if (error_set)
  1622. return;
  1623. if (indent_level>tab_level.back()->get()) {
  1624. p_class->end_line=tokenizer->get_token_line();
  1625. return; //go back a level
  1626. }
  1627. switch(token) {
  1628. case GDTokenizer::TK_EOF:
  1629. p_class->end_line=tokenizer->get_token_line();
  1630. case GDTokenizer::TK_ERROR: {
  1631. return; //go back
  1632. //end of file!
  1633. } break;
  1634. case GDTokenizer::TK_NEWLINE: {
  1635. if (!_parse_newline()) {
  1636. if (!error_set) {
  1637. p_class->end_line=tokenizer->get_token_line();
  1638. }
  1639. return;
  1640. }
  1641. } break;
  1642. case GDTokenizer::TK_PR_EXTENDS: {
  1643. _parse_extends(p_class);
  1644. if (error_set)
  1645. return;
  1646. if (!_end_statement()) {
  1647. _set_error("Expected end of statement after extends");
  1648. return;
  1649. }
  1650. } break;
  1651. case GDTokenizer::TK_PR_TOOL: {
  1652. if (p_class->tool) {
  1653. _set_error("tool used more than once");
  1654. return;
  1655. }
  1656. p_class->tool=true;
  1657. tokenizer->advance();
  1658. } break;
  1659. case GDTokenizer::TK_PR_CLASS: {
  1660. //class inside class :D
  1661. StringName name;
  1662. StringName extends;
  1663. if (tokenizer->get_token(1)!=GDTokenizer::TK_IDENTIFIER) {
  1664. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  1665. return;
  1666. }
  1667. name = tokenizer->get_token_identifier(1);
  1668. tokenizer->advance(2);
  1669. ClassNode *newclass = alloc_node<ClassNode>();
  1670. newclass->initializer = alloc_node<BlockNode>();
  1671. newclass->initializer->parent_class=newclass;
  1672. newclass->ready = alloc_node<BlockNode>();
  1673. newclass->ready->parent_class=newclass;
  1674. newclass->name=name;
  1675. newclass->owner=p_class;
  1676. p_class->subclasses.push_back(newclass);
  1677. if (tokenizer->get_token()==GDTokenizer::TK_PR_EXTENDS) {
  1678. _parse_extends(newclass);
  1679. if (error_set)
  1680. return;
  1681. }
  1682. if (!_enter_indent_block()) {
  1683. _set_error("Indented block expected.");
  1684. return;
  1685. }
  1686. current_class=newclass;
  1687. _parse_class(newclass);
  1688. current_class=p_class;
  1689. } break;
  1690. /* this is for functions....
  1691. case GDTokenizer::TK_CF_PASS: {
  1692. tokenizer->advance(1);
  1693. } break;
  1694. */
  1695. case GDTokenizer::TK_PR_STATIC: {
  1696. tokenizer->advance();
  1697. if (tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  1698. _set_error("Expected 'func'.");
  1699. return;
  1700. }
  1701. }; //fallthrough to function
  1702. case GDTokenizer::TK_PR_FUNCTION: {
  1703. bool _static=false;
  1704. pending_newline=-1;
  1705. if (tokenizer->get_token(-1)==GDTokenizer::TK_PR_STATIC) {
  1706. _static=true;
  1707. }
  1708. tokenizer->advance();
  1709. StringName name;
  1710. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC,name)) {
  1711. }
  1712. if (name==StringName()) {
  1713. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  1714. return;
  1715. }
  1716. for(int i=0;i<p_class->functions.size();i++) {
  1717. if (p_class->functions[i]->name==name) {
  1718. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->functions[i]->line)+").");
  1719. }
  1720. }
  1721. for(int i=0;i<p_class->static_functions.size();i++) {
  1722. if (p_class->static_functions[i]->name==name) {
  1723. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->static_functions[i]->line)+").");
  1724. }
  1725. }
  1726. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1727. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  1728. return;
  1729. }
  1730. tokenizer->advance();
  1731. Vector<StringName> arguments;
  1732. Vector<Node*> default_values;
  1733. int fnline = tokenizer->get_token_line();
  1734. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1735. //has arguments
  1736. bool defaulting=false;
  1737. while(true) {
  1738. if (tokenizer->get_token()==GDTokenizer::TK_PR_VAR) {
  1739. tokenizer->advance(); //var before the identifier is allowed
  1740. }
  1741. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1742. _set_error("Expected identifier for argument.");
  1743. return;
  1744. }
  1745. StringName argname=tokenizer->get_token_identifier();
  1746. arguments.push_back(argname);
  1747. tokenizer->advance();
  1748. if (defaulting && tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  1749. _set_error("Default parameter expected.");
  1750. return;
  1751. }
  1752. //tokenizer->advance();
  1753. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1754. defaulting=true;
  1755. tokenizer->advance(1);
  1756. Node *defval=NULL;
  1757. defval=_parse_and_reduce_expression(p_class,_static);
  1758. if (!defval || error_set)
  1759. return;
  1760. OperatorNode *on = alloc_node<OperatorNode>();
  1761. on->op=OperatorNode::OP_ASSIGN;
  1762. IdentifierNode *in = alloc_node<IdentifierNode>();
  1763. in->name=argname;
  1764. on->arguments.push_back(in);
  1765. on->arguments.push_back(defval);
  1766. /* no ..
  1767. if (defval->type!=Node::TYPE_CONSTANT) {
  1768. _set_error("default argument must be constant");
  1769. }
  1770. */
  1771. default_values.push_back(on);
  1772. }
  1773. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1774. tokenizer->advance();
  1775. continue;
  1776. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1777. _set_error("Expected ',' or ')'.");
  1778. return;
  1779. }
  1780. break;
  1781. }
  1782. }
  1783. tokenizer->advance();
  1784. BlockNode *block = alloc_node<BlockNode>();
  1785. block->parent_class=p_class;
  1786. if (name=="_init") {
  1787. if (p_class->extends_used) {
  1788. OperatorNode *cparent = alloc_node<OperatorNode>();
  1789. cparent->op=OperatorNode::OP_PARENT_CALL;
  1790. block->statements.push_back(cparent);
  1791. IdentifierNode *id = alloc_node<IdentifierNode>();
  1792. id->name="_init";
  1793. cparent->arguments.push_back(id);
  1794. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1795. tokenizer->advance();
  1796. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1797. _set_error("expected '(' for parent constructor arguments.");
  1798. }
  1799. tokenizer->advance();
  1800. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1801. //has arguments
  1802. while(true) {
  1803. Node *arg = _parse_and_reduce_expression(p_class,_static);
  1804. cparent->arguments.push_back(arg);
  1805. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1806. tokenizer->advance();
  1807. continue;
  1808. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1809. _set_error("Expected ',' or ')'.");
  1810. return;
  1811. }
  1812. break;
  1813. }
  1814. }
  1815. tokenizer->advance();
  1816. }
  1817. } else {
  1818. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1819. _set_error("Parent constructor call found for a class without inheritance.");
  1820. return;
  1821. }
  1822. }
  1823. }
  1824. if (!_enter_indent_block(block)) {
  1825. _set_error("Indented block expected.");
  1826. return;
  1827. }
  1828. FunctionNode *function = alloc_node<FunctionNode>();
  1829. function->name=name;
  1830. function->arguments=arguments;
  1831. function->default_values=default_values;
  1832. function->_static=_static;
  1833. function->line=fnline;
  1834. function->rpc_mode=rpc_mode;
  1835. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  1836. if (_static)
  1837. p_class->static_functions.push_back(function);
  1838. else
  1839. p_class->functions.push_back(function);
  1840. current_function=function;
  1841. function->body=block;
  1842. current_block=block;
  1843. _parse_block(block,_static);
  1844. current_block=NULL;
  1845. //arguments
  1846. } break;
  1847. case GDTokenizer::TK_PR_SIGNAL: {
  1848. tokenizer->advance();
  1849. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1850. _set_error("Expected identifier after 'signal'.");
  1851. return;
  1852. }
  1853. ClassNode::Signal sig;
  1854. sig.name = tokenizer->get_token_identifier();
  1855. tokenizer->advance();
  1856. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1857. tokenizer->advance();
  1858. while(true) {
  1859. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1860. tokenizer->advance();
  1861. break;
  1862. }
  1863. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1864. _set_error("Expected identifier in signal argument.");
  1865. return;
  1866. }
  1867. sig.arguments.push_back(tokenizer->get_token_identifier());
  1868. tokenizer->advance();
  1869. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1870. tokenizer->advance();
  1871. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1872. _set_error("Expected ',' or ')' after signal parameter identifier.");
  1873. return;
  1874. }
  1875. }
  1876. }
  1877. p_class->_signals.push_back(sig);
  1878. if (!_end_statement()) {
  1879. _set_error("Expected end of statement (signal)");
  1880. return;
  1881. }
  1882. } break;
  1883. case GDTokenizer::TK_PR_EXPORT: {
  1884. tokenizer->advance();
  1885. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1886. tokenizer->advance();
  1887. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE) {
  1888. Variant::Type type = tokenizer->get_token_type();
  1889. if (type==Variant::NIL) {
  1890. _set_error("Can't export null type.");
  1891. return;
  1892. }
  1893. current_export.type=type;
  1894. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  1895. tokenizer->advance();
  1896. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1897. // hint expected next!
  1898. tokenizer->advance();
  1899. switch(current_export.type) {
  1900. case Variant::INT: {
  1901. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FLAGS") {
  1902. current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  1903. tokenizer->advance();
  1904. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1905. break;
  1906. }
  1907. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA)
  1908. {
  1909. _set_error("Expected ')' or ',' in bit flags hint.");
  1910. return;
  1911. }
  1912. current_export.hint=PROPERTY_HINT_FLAGS;
  1913. tokenizer->advance();
  1914. bool first = true;
  1915. while(true) {
  1916. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1917. current_export=PropertyInfo();
  1918. _set_error("Expected a string constant in named bit flags hint.");
  1919. return;
  1920. }
  1921. String c = tokenizer->get_token_constant();
  1922. if (!first)
  1923. current_export.hint_string+=",";
  1924. else
  1925. first=false;
  1926. current_export.hint_string+=c.xml_escape();
  1927. tokenizer->advance();
  1928. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1929. break;
  1930. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1931. current_export=PropertyInfo();
  1932. _set_error("Expected ')' or ',' in named bit flags hint.");
  1933. return;
  1934. }
  1935. tokenizer->advance();
  1936. }
  1937. break;
  1938. }
  1939. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  1940. //enumeration
  1941. current_export.hint=PROPERTY_HINT_ENUM;
  1942. bool first=true;
  1943. while(true) {
  1944. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1945. current_export=PropertyInfo();
  1946. _set_error("Expected a string constant in enumeration hint.");
  1947. return;
  1948. }
  1949. String c = tokenizer->get_token_constant();
  1950. if (!first)
  1951. current_export.hint_string+=",";
  1952. else
  1953. first=false;
  1954. current_export.hint_string+=c.xml_escape();
  1955. tokenizer->advance();
  1956. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1957. break;
  1958. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1959. current_export=PropertyInfo();
  1960. _set_error("Expected ')' or ',' in enumeration hint.");
  1961. return;
  1962. }
  1963. tokenizer->advance();
  1964. }
  1965. break;
  1966. }
  1967. }; //fallthrough to use the same
  1968. case Variant::REAL: {
  1969. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EASE") {
  1970. current_export.hint=PROPERTY_HINT_EXP_EASING;
  1971. tokenizer->advance();
  1972. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1973. _set_error("Expected ')' in hint.");
  1974. return;
  1975. }
  1976. break;
  1977. }
  1978. // range
  1979. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EXP") {
  1980. current_export.hint=PROPERTY_HINT_EXP_RANGE;
  1981. tokenizer->advance();
  1982. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1983. break;
  1984. else if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1985. _set_error("Expected ')' or ',' in exponential range hint.");
  1986. return;
  1987. }
  1988. tokenizer->advance();
  1989. }
  1990. else
  1991. current_export.hint=PROPERTY_HINT_RANGE;
  1992. float sign=1.0;
  1993. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1994. sign=-1;
  1995. tokenizer->advance();
  1996. }
  1997. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1998. current_export=PropertyInfo();
  1999. _set_error("Expected a range in numeric hint.");
  2000. return;
  2001. }
  2002. current_export.hint_string=rtos(sign*double(tokenizer->get_token_constant()));
  2003. tokenizer->advance();
  2004. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2005. current_export.hint_string="0,"+current_export.hint_string;
  2006. break;
  2007. }
  2008. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2009. current_export=PropertyInfo();
  2010. _set_error("Expected ',' or ')' in numeric range hint.");
  2011. return;
  2012. }
  2013. tokenizer->advance();
  2014. sign=1.0;
  2015. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  2016. sign=-1;
  2017. tokenizer->advance();
  2018. }
  2019. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2020. current_export=PropertyInfo();
  2021. _set_error("Expected a number as upper bound in numeric range hint.");
  2022. return;
  2023. }
  2024. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  2025. tokenizer->advance();
  2026. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2027. break;
  2028. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2029. current_export=PropertyInfo();
  2030. _set_error("Expected ',' or ')' in numeric range hint.");
  2031. return;
  2032. }
  2033. tokenizer->advance();
  2034. sign=1.0;
  2035. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  2036. sign=-1;
  2037. tokenizer->advance();
  2038. }
  2039. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2040. current_export=PropertyInfo();
  2041. _set_error("Expected a number as step in numeric range hint.");
  2042. return;
  2043. }
  2044. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  2045. tokenizer->advance();
  2046. } break;
  2047. case Variant::STRING: {
  2048. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  2049. //enumeration
  2050. current_export.hint=PROPERTY_HINT_ENUM;
  2051. bool first=true;
  2052. while(true) {
  2053. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2054. current_export=PropertyInfo();
  2055. _set_error("Expected a string constant in enumeration hint.");
  2056. return;
  2057. }
  2058. String c = tokenizer->get_token_constant();
  2059. if (!first)
  2060. current_export.hint_string+=",";
  2061. else
  2062. first=false;
  2063. current_export.hint_string+=c.xml_escape();
  2064. tokenizer->advance();
  2065. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2066. break;
  2067. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2068. current_export=PropertyInfo();
  2069. _set_error("Expected ')' or ',' in enumeration hint.");
  2070. return;
  2071. }
  2072. tokenizer->advance();
  2073. }
  2074. break;
  2075. }
  2076. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="DIR") {
  2077. tokenizer->advance();
  2078. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2079. current_export.hint=PROPERTY_HINT_DIR;
  2080. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA ) {
  2081. tokenizer->advance();
  2082. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier()=="GLOBAL")) {
  2083. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2084. return;
  2085. }
  2086. if (!p_class->tool) {
  2087. _set_error("Global filesystem hints may only be used in tool scripts.");
  2088. return;
  2089. }
  2090. current_export.hint=PROPERTY_HINT_GLOBAL_DIR;
  2091. tokenizer->advance();
  2092. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2093. _set_error("Expected ')' in hint.");
  2094. return;
  2095. }
  2096. }
  2097. else {
  2098. _set_error("Expected ')' or ',' in hint.");
  2099. return;
  2100. }
  2101. break;
  2102. }
  2103. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FILE") {
  2104. current_export.hint=PROPERTY_HINT_FILE;
  2105. tokenizer->advance();
  2106. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2107. tokenizer->advance();
  2108. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="GLOBAL") {
  2109. if (!p_class->tool) {
  2110. _set_error("Global filesystem hints may only be used in tool scripts.");
  2111. return;
  2112. }
  2113. current_export.hint=PROPERTY_HINT_GLOBAL_FILE;
  2114. tokenizer->advance();
  2115. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2116. break;
  2117. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA)
  2118. tokenizer->advance();
  2119. else {
  2120. _set_error("Expected ')' or ',' in hint.");
  2121. return;
  2122. }
  2123. }
  2124. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2125. if (current_export.hint==PROPERTY_HINT_GLOBAL_FILE)
  2126. _set_error("Expected string constant with filter");
  2127. else
  2128. _set_error("Expected 'GLOBAL' or string constant with filter");
  2129. return;
  2130. }
  2131. current_export.hint_string=tokenizer->get_token_constant();
  2132. tokenizer->advance();
  2133. }
  2134. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2135. _set_error("Expected ')' in hint.");
  2136. return;
  2137. }
  2138. break;
  2139. }
  2140. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="MULTILINE") {
  2141. current_export.hint=PROPERTY_HINT_MULTILINE_TEXT;
  2142. tokenizer->advance();
  2143. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2144. _set_error("Expected ')' in hint.");
  2145. return;
  2146. }
  2147. break;
  2148. }
  2149. } break;
  2150. case Variant::COLOR: {
  2151. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER ) {
  2152. current_export=PropertyInfo();
  2153. _set_error("Color type hint expects RGB or RGBA as hints");
  2154. return;
  2155. }
  2156. String identifier = tokenizer->get_token_identifier();
  2157. if (identifier=="RGB") {
  2158. current_export.hint=PROPERTY_HINT_COLOR_NO_ALPHA;
  2159. } else if (identifier=="RGBA") {
  2160. //none
  2161. } else {
  2162. current_export=PropertyInfo();
  2163. _set_error("Color type hint expects RGB or RGBA as hints");
  2164. return;
  2165. }
  2166. tokenizer->advance();
  2167. } break;
  2168. default: {
  2169. current_export=PropertyInfo();
  2170. _set_error("Type '"+Variant::get_type_name(type)+"' can't take hints.");
  2171. return;
  2172. } break;
  2173. }
  2174. }
  2175. } else if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  2176. String identifier = tokenizer->get_token_identifier();
  2177. if (!ObjectTypeDB::is_type(identifier,"Resource")) {
  2178. current_export=PropertyInfo();
  2179. _set_error("Export hint not a type or resource.");
  2180. }
  2181. current_export.type=Variant::OBJECT;
  2182. current_export.hint=PROPERTY_HINT_RESOURCE_TYPE;
  2183. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2184. current_export.hint_string=identifier;
  2185. tokenizer->advance();
  2186. }
  2187. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2188. current_export=PropertyInfo();
  2189. _set_error("Expected ')' or ',' after export hint.");
  2190. return;
  2191. }
  2192. tokenizer->advance();
  2193. }
  2194. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_ONREADY && tokenizer->get_token()!=GDTokenizer::TK_PR_REMOTE && tokenizer->get_token()!=GDTokenizer::TK_PR_MASTER && tokenizer->get_token()!=GDTokenizer::TK_PR_SLAVE && tokenizer->get_token()!=GDTokenizer::TK_PR_SYNC) {
  2195. current_export=PropertyInfo();
  2196. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave' or 'sync'.");
  2197. return;
  2198. }
  2199. continue;
  2200. } break;
  2201. case GDTokenizer::TK_PR_ONREADY: {
  2202. //may be fallthrough from export, ignore if so
  2203. tokenizer->advance();
  2204. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2205. _set_error("Expected 'var'.");
  2206. return;
  2207. }
  2208. continue;
  2209. } break;
  2210. case GDTokenizer::TK_PR_REMOTE: {
  2211. //may be fallthrough from export, ignore if so
  2212. tokenizer->advance();
  2213. if (current_export.type) {
  2214. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2215. _set_error("Expected 'var'.");
  2216. return;
  2217. }
  2218. } else {
  2219. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2220. _set_error("Expected 'var' or 'func'.");
  2221. return;
  2222. }
  2223. }
  2224. rpc_mode=ScriptInstance::RPC_MODE_REMOTE;
  2225. continue;
  2226. } break;
  2227. case GDTokenizer::TK_PR_MASTER: {
  2228. //may be fallthrough from export, ignore if so
  2229. tokenizer->advance();
  2230. if (current_export.type) {
  2231. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2232. _set_error("Expected 'var'.");
  2233. return;
  2234. }
  2235. } else {
  2236. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2237. _set_error("Expected 'var' or 'func'.");
  2238. return;
  2239. }
  2240. }
  2241. rpc_mode=ScriptInstance::RPC_MODE_MASTER;
  2242. continue;
  2243. } break;
  2244. case GDTokenizer::TK_PR_SLAVE: {
  2245. //may be fallthrough from export, ignore if so
  2246. tokenizer->advance();
  2247. if (current_export.type) {
  2248. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2249. _set_error("Expected 'var'.");
  2250. return;
  2251. }
  2252. } else {
  2253. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2254. _set_error("Expected 'var' or 'func'.");
  2255. return;
  2256. }
  2257. }
  2258. rpc_mode=ScriptInstance::RPC_MODE_SLAVE;
  2259. continue;
  2260. } break;
  2261. case GDTokenizer::TK_PR_SYNC: {
  2262. //may be fallthrough from export, ignore if so
  2263. tokenizer->advance();
  2264. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2265. if (current_export.type)
  2266. _set_error("Expected 'var'.");
  2267. else
  2268. _set_error("Expected 'var' or 'func'.");
  2269. return;
  2270. }
  2271. rpc_mode=ScriptInstance::RPC_MODE_SYNC;
  2272. continue;
  2273. } break;
  2274. case GDTokenizer::TK_PR_VAR: {
  2275. //variale declaration and (eventual) initialization
  2276. ClassNode::Member member;
  2277. bool autoexport = tokenizer->get_token(-1)==GDTokenizer::TK_PR_EXPORT;
  2278. if (current_export.type!=Variant::NIL) {
  2279. member._export=current_export;
  2280. current_export=PropertyInfo();
  2281. }
  2282. bool onready = tokenizer->get_token(-1)==GDTokenizer::TK_PR_ONREADY;
  2283. tokenizer->advance();
  2284. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2285. _set_error("Expected identifier for member variable name.");
  2286. return;
  2287. }
  2288. member.identifier=tokenizer->get_token_identifier();
  2289. member.expression=NULL;
  2290. member._export.name=member.identifier;
  2291. member.line=tokenizer->get_token_line();
  2292. member.rpc_mode=rpc_mode;
  2293. tokenizer->advance();
  2294. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  2295. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  2296. #ifdef DEBUG_ENABLED
  2297. int line = tokenizer->get_token_line();
  2298. #endif
  2299. tokenizer->advance();
  2300. Node *subexpr=NULL;
  2301. subexpr = _parse_and_reduce_expression(p_class,false,autoexport);
  2302. if (!subexpr) {
  2303. if (_recover_from_completion()) {
  2304. break;
  2305. }
  2306. return;
  2307. }
  2308. //discourage common error
  2309. if (!onready && subexpr->type==Node::TYPE_OPERATOR) {
  2310. OperatorNode *op=static_cast<OperatorNode*>(subexpr);
  2311. if (op->op==OperatorNode::OP_CALL && op->arguments[0]->type==Node::TYPE_SELF && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  2312. IdentifierNode *id=static_cast<IdentifierNode*>(op->arguments[1]);
  2313. if (id->name=="get_node") {
  2314. _set_error("Use 'onready var "+String(member.identifier)+" = get_node(..)' instead");
  2315. return;
  2316. }
  2317. }
  2318. }
  2319. member.expression=subexpr;
  2320. if (autoexport) {
  2321. if (1)/*(subexpr->type==Node::TYPE_ARRAY) {
  2322. member._export.type=Variant::ARRAY;
  2323. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  2324. member._export.type=Variant::DICTIONARY;
  2325. } else*/ {
  2326. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2327. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2328. return;
  2329. }
  2330. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2331. if (cn->value.get_type()==Variant::NIL) {
  2332. _set_error("Can't accept a null constant expression for infering export type.");
  2333. return;
  2334. }
  2335. member._export.type=cn->value.get_type();
  2336. member._export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2337. }
  2338. }
  2339. #ifdef TOOLS_ENABLED
  2340. if (subexpr->type==Node::TYPE_CONSTANT && member._export.type!=Variant::NIL) {
  2341. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2342. if (cn->value.get_type()!=Variant::NIL) {
  2343. member.default_value=cn->value;
  2344. }
  2345. }
  2346. #endif
  2347. IdentifierNode *id = alloc_node<IdentifierNode>();
  2348. id->name=member.identifier;
  2349. OperatorNode *op = alloc_node<OperatorNode>();
  2350. op->op=OperatorNode::OP_INIT_ASSIGN;
  2351. op->arguments.push_back(id);
  2352. op->arguments.push_back(subexpr);
  2353. #ifdef DEBUG_ENABLED
  2354. NewLineNode *nl = alloc_node<NewLineNode>();
  2355. nl->line=line;
  2356. if (onready)
  2357. p_class->ready->statements.push_back(nl);
  2358. else
  2359. p_class->initializer->statements.push_back(nl);
  2360. #endif
  2361. if (onready)
  2362. p_class->ready->statements.push_back(op);
  2363. else
  2364. p_class->initializer->statements.push_back(op);
  2365. } else {
  2366. if (autoexport) {
  2367. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2368. return;
  2369. }
  2370. }
  2371. if (tokenizer->get_token()==GDTokenizer::TK_PR_SETGET) {
  2372. tokenizer->advance();
  2373. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2374. //just comma means using only getter
  2375. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2376. _set_error("Expected identifier for setter function after 'notify'.");
  2377. }
  2378. member.setter=tokenizer->get_token_identifier();
  2379. tokenizer->advance();
  2380. }
  2381. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2382. //there is a getter
  2383. tokenizer->advance();
  2384. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2385. _set_error("Expected identifier for getter function after ','.");
  2386. }
  2387. member.getter=tokenizer->get_token_identifier();
  2388. tokenizer->advance();
  2389. }
  2390. }
  2391. p_class->variables.push_back(member);
  2392. if (!_end_statement()) {
  2393. _set_error("Expected end of statement (continue)");
  2394. return;
  2395. }
  2396. } break;
  2397. case GDTokenizer::TK_PR_CONST: {
  2398. //variale declaration and (eventual) initialization
  2399. ClassNode::Constant constant;
  2400. tokenizer->advance();
  2401. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2402. _set_error("Expected name (identifier) for constant.");
  2403. return;
  2404. }
  2405. constant.identifier=tokenizer->get_token_identifier();
  2406. tokenizer->advance();
  2407. if (tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  2408. _set_error("Constant expects assignment.");
  2409. return;
  2410. }
  2411. tokenizer->advance();
  2412. Node *subexpr=NULL;
  2413. subexpr = _parse_and_reduce_expression(p_class,true,true);
  2414. if (!subexpr) {
  2415. if (_recover_from_completion()) {
  2416. break;
  2417. }
  2418. return;
  2419. }
  2420. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2421. _set_error("Expected constant expression");
  2422. }
  2423. constant.expression=subexpr;
  2424. p_class->constant_expressions.push_back(constant);
  2425. if (!_end_statement()) {
  2426. _set_error("Expected end of statement (constant)");
  2427. return;
  2428. }
  2429. } break;
  2430. case GDTokenizer::TK_PR_ENUM: {
  2431. //mutiple constant declarations..
  2432. int last_assign = -1; // Incremented by 1 right before the assingment.
  2433. String enum_name;
  2434. Dictionary enum_dict;
  2435. tokenizer->advance();
  2436. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  2437. enum_name=tokenizer->get_token_identifier();
  2438. tokenizer->advance();
  2439. }
  2440. if (tokenizer->get_token()!=GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  2441. _set_error("Expected '{' in enum declaration");
  2442. return;
  2443. }
  2444. tokenizer->advance();
  2445. while(true) {
  2446. if(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  2447. tokenizer->advance(); // Ignore newlines
  2448. } else if (tokenizer->get_token()==GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2449. tokenizer->advance();
  2450. break; // End of enum
  2451. } else if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2452. if(tokenizer->get_token()==GDTokenizer::TK_EOF) {
  2453. _set_error("Unexpected end of file.");
  2454. } else {
  2455. _set_error(String("Unexpected ") + GDTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  2456. }
  2457. return;
  2458. } else { // tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER
  2459. ClassNode::Constant constant;
  2460. constant.identifier=tokenizer->get_token_identifier();
  2461. tokenizer->advance();
  2462. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  2463. tokenizer->advance();
  2464. Node *subexpr=NULL;
  2465. subexpr = _parse_and_reduce_expression(p_class,true,true);
  2466. if (!subexpr) {
  2467. if (_recover_from_completion()) {
  2468. break;
  2469. }
  2470. return;
  2471. }
  2472. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2473. _set_error("Expected constant expression");
  2474. }
  2475. const ConstantNode *subexpr_const = static_cast<const ConstantNode*>(subexpr);
  2476. if(subexpr_const->value.get_type() != Variant::INT) {
  2477. _set_error("Expected an int value for enum");
  2478. }
  2479. last_assign = subexpr_const->value;
  2480. constant.expression=subexpr;
  2481. } else {
  2482. last_assign = last_assign + 1;
  2483. ConstantNode *cn = alloc_node<ConstantNode>();
  2484. cn->value = last_assign;
  2485. constant.expression = cn;
  2486. }
  2487. if(tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2488. tokenizer->advance();
  2489. }
  2490. if(enum_name != "") {
  2491. const ConstantNode *cn = static_cast<const ConstantNode*>(constant.expression);
  2492. enum_dict[constant.identifier] = cn->value;
  2493. }
  2494. p_class->constant_expressions.push_back(constant);
  2495. }
  2496. }
  2497. if(enum_name != "") {
  2498. ClassNode::Constant enum_constant;
  2499. enum_constant.identifier=enum_name;
  2500. ConstantNode *cn = alloc_node<ConstantNode>();
  2501. cn->value = enum_dict;
  2502. enum_constant.expression=cn;
  2503. p_class->constant_expressions.push_back(enum_constant);
  2504. }
  2505. if (!_end_statement()) {
  2506. _set_error("Expected end of statement (enum)");
  2507. return;
  2508. }
  2509. } break;
  2510. default: {
  2511. _set_error(String()+"Unexpected token: "+tokenizer->get_token_name(tokenizer->get_token())+":"+tokenizer->get_token_identifier());
  2512. return;
  2513. } break;
  2514. }
  2515. }
  2516. }
  2517. void GDParser::_set_error(const String& p_error, int p_line, int p_column) {
  2518. if (error_set)
  2519. return; //allow no further errors
  2520. error=p_error;
  2521. error_line=p_line<0?tokenizer->get_token_line():p_line;
  2522. error_column=p_column<0?tokenizer->get_token_column():p_column;
  2523. error_set=true;
  2524. }
  2525. String GDParser::get_error() const {
  2526. return error;
  2527. }
  2528. int GDParser::get_error_line() const {
  2529. return error_line;
  2530. }
  2531. int GDParser::get_error_column() const {
  2532. return error_column;
  2533. }
  2534. Error GDParser::_parse(const String& p_base_path) {
  2535. base_path=p_base_path;
  2536. clear();
  2537. //assume class
  2538. ClassNode *main_class = alloc_node<ClassNode>();
  2539. main_class->initializer = alloc_node<BlockNode>();
  2540. main_class->initializer->parent_class=main_class;
  2541. main_class->ready = alloc_node<BlockNode>();
  2542. main_class->ready->parent_class=main_class;
  2543. current_class=main_class;
  2544. _parse_class(main_class);
  2545. if (tokenizer->get_token()==GDTokenizer::TK_ERROR) {
  2546. error_set=false;
  2547. _set_error("Parse Error: "+tokenizer->get_token_error());
  2548. }
  2549. if (error_set) {
  2550. return ERR_PARSE_ERROR;
  2551. }
  2552. return OK;
  2553. }
  2554. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path, const String &p_self_path) {
  2555. for_completion=false;
  2556. validating=false;
  2557. completion_type=COMPLETION_NONE;
  2558. completion_node=NULL;
  2559. completion_class=NULL;
  2560. completion_function=NULL;
  2561. completion_block=NULL;
  2562. completion_found=false;
  2563. current_block=NULL;
  2564. current_class=NULL;
  2565. current_function=NULL;
  2566. self_path=p_self_path;
  2567. GDTokenizerBuffer *tb = memnew( GDTokenizerBuffer );
  2568. tb->set_code_buffer(p_bytecode);
  2569. tokenizer=tb;
  2570. Error ret = _parse(p_base_path);
  2571. memdelete(tb);
  2572. tokenizer=NULL;
  2573. return ret;
  2574. }
  2575. Error GDParser::parse(const String& p_code, const String& p_base_path, bool p_just_validate, const String &p_self_path,bool p_for_completion) {
  2576. completion_type=COMPLETION_NONE;
  2577. completion_node=NULL;
  2578. completion_class=NULL;
  2579. completion_function=NULL;
  2580. completion_block=NULL;
  2581. completion_found=false;
  2582. current_block=NULL;
  2583. current_class=NULL;
  2584. current_function=NULL;
  2585. self_path=p_self_path;
  2586. GDTokenizerText *tt = memnew( GDTokenizerText );
  2587. tt->set_code(p_code);
  2588. validating=p_just_validate;
  2589. for_completion=p_for_completion;
  2590. tokenizer=tt;
  2591. Error ret = _parse(p_base_path);
  2592. memdelete(tt);
  2593. tokenizer=NULL;
  2594. return ret;
  2595. }
  2596. bool GDParser::is_tool_script() const {
  2597. return (head && head->type==Node::TYPE_CLASS && static_cast<const ClassNode*>(head)->tool);
  2598. }
  2599. const GDParser::Node *GDParser::get_parse_tree() const {
  2600. return head;
  2601. }
  2602. void GDParser::clear() {
  2603. while(list) {
  2604. Node *l=list;
  2605. list=list->next;
  2606. memdelete(l);
  2607. }
  2608. head=NULL;
  2609. list=NULL;
  2610. completion_type=COMPLETION_NONE;
  2611. completion_node=NULL;
  2612. completion_class=NULL;
  2613. completion_function=NULL;
  2614. completion_block=NULL;
  2615. current_block=NULL;
  2616. current_class=NULL;
  2617. completion_found=false;
  2618. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  2619. current_function=NULL;
  2620. validating=false;
  2621. for_completion=false;
  2622. error_set=false;
  2623. tab_level.clear();
  2624. tab_level.push_back(0);
  2625. error_line=0;
  2626. error_column=0;
  2627. pending_newline=-1;
  2628. parenthesis=0;
  2629. current_export.type=Variant::NIL;
  2630. error="";
  2631. }
  2632. GDParser::CompletionType GDParser::get_completion_type() {
  2633. return completion_type;
  2634. }
  2635. StringName GDParser::get_completion_cursor() {
  2636. return completion_cursor;
  2637. }
  2638. int GDParser::get_completion_line() {
  2639. return completion_line;
  2640. }
  2641. Variant::Type GDParser::get_completion_built_in_constant(){
  2642. return completion_built_in_constant;
  2643. }
  2644. GDParser::Node *GDParser::get_completion_node(){
  2645. return completion_node;
  2646. }
  2647. GDParser::BlockNode *GDParser::get_completion_block() {
  2648. return completion_block;
  2649. }
  2650. GDParser::ClassNode *GDParser::get_completion_class(){
  2651. return completion_class;
  2652. }
  2653. GDParser::FunctionNode *GDParser::get_completion_function(){
  2654. return completion_function;
  2655. }
  2656. int GDParser::get_completion_argument_index() {
  2657. return completion_argument;
  2658. }
  2659. int GDParser::get_completion_identifier_is_function() {
  2660. return completion_ident_is_call;
  2661. }
  2662. GDParser::GDParser() {
  2663. head=NULL;
  2664. list=NULL;
  2665. tokenizer=NULL;
  2666. pending_newline=-1;
  2667. clear();
  2668. }
  2669. GDParser::~GDParser() {
  2670. clear();
  2671. }