gd_parser.cpp 86 KB

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