gd_parser.cpp 92 KB

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