gd_parser.cpp 96 KB

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