gd_parser.cpp 85 KB

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