gd_parser.cpp 97 KB

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