gd_parser.cpp 85 KB

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