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("Parser bug..");
  807. }
  808. op->arguments.push_back(expression[next_op-1].node); //expression goes as left
  809. op->arguments.push_back(expression[next_op+1].node); //next expression goes as right
  810. //replace all 3 nodes by this operator and make it an expression
  811. expression[next_op-1].node=op;
  812. expression.remove(next_op);
  813. expression.remove(next_op);
  814. }
  815. }
  816. return expression[0].node;
  817. }
  818. GDParser::Node* GDParser::_reduce_expression(Node *p_node,bool p_to_const) {
  819. switch(p_node->type) {
  820. case Node::TYPE_BUILT_IN_FUNCTION: {
  821. //many may probably be optimizable
  822. return p_node;
  823. } break;
  824. case Node::TYPE_ARRAY: {
  825. ArrayNode *an = static_cast<ArrayNode*>(p_node);
  826. bool all_constants=true;
  827. for(int i=0;i<an->elements.size();i++) {
  828. an->elements[i]=_reduce_expression(an->elements[i],p_to_const);
  829. if (an->elements[i]->type!=Node::TYPE_CONSTANT)
  830. all_constants=false;
  831. }
  832. if (all_constants && p_to_const) {
  833. //reduce constant array expression
  834. ConstantNode *cn = alloc_node<ConstantNode>();
  835. Array arr(!p_to_const);
  836. //print_line("mk array "+itos(!p_to_const));
  837. arr.resize(an->elements.size());
  838. for(int i=0;i<an->elements.size();i++) {
  839. ConstantNode *acn = static_cast<ConstantNode*>(an->elements[i]);
  840. arr[i]=acn->value;
  841. }
  842. cn->value=arr;
  843. return cn;
  844. }
  845. return an;
  846. } break;
  847. case Node::TYPE_DICTIONARY: {
  848. DictionaryNode *dn = static_cast<DictionaryNode*>(p_node);
  849. bool all_constants=true;
  850. for(int i=0;i<dn->elements.size();i++) {
  851. dn->elements[i].key=_reduce_expression(dn->elements[i].key,p_to_const);
  852. if (dn->elements[i].key->type!=Node::TYPE_CONSTANT)
  853. all_constants=false;
  854. dn->elements[i].value=_reduce_expression(dn->elements[i].value,p_to_const);
  855. if (dn->elements[i].value->type!=Node::TYPE_CONSTANT)
  856. all_constants=false;
  857. }
  858. if (all_constants && p_to_const) {
  859. //reduce constant array expression
  860. ConstantNode *cn = alloc_node<ConstantNode>();
  861. Dictionary dict(!p_to_const);
  862. for(int i=0;i<dn->elements.size();i++) {
  863. ConstantNode *key_c = static_cast<ConstantNode*>(dn->elements[i].key);
  864. ConstantNode *value_c = static_cast<ConstantNode*>(dn->elements[i].value);
  865. dict[key_c->value]=value_c->value;
  866. }
  867. cn->value=dict;
  868. return cn;
  869. }
  870. return dn;
  871. } break;
  872. case Node::TYPE_OPERATOR: {
  873. OperatorNode *op=static_cast<OperatorNode*>(p_node);
  874. bool all_constants=true;
  875. int last_not_constant=-1;
  876. for(int i=0;i<op->arguments.size();i++) {
  877. op->arguments[i]=_reduce_expression(op->arguments[i],p_to_const);
  878. if (op->arguments[i]->type!=Node::TYPE_CONSTANT) {
  879. all_constants=false;
  880. last_not_constant=i;
  881. }
  882. }
  883. if (op->op==OperatorNode::OP_EXTENDS) {
  884. //nothing much
  885. return op;
  886. } if (op->op==OperatorNode::OP_PARENT_CALL) {
  887. //nothing much
  888. return op;
  889. } else if (op->op==OperatorNode::OP_CALL) {
  890. //can reduce base type constructors
  891. 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) {
  892. //native type constructor or intrinsic function
  893. const Variant **vptr=NULL;
  894. Vector<Variant*> ptrs;
  895. if (op->arguments.size()>1) {
  896. ptrs.resize(op->arguments.size()-1);
  897. for(int i=0;i<ptrs.size();i++) {
  898. ConstantNode *cn = static_cast<ConstantNode*>(op->arguments[i+1]);
  899. ptrs[i]=&cn->value;
  900. }
  901. vptr=(const Variant**)&ptrs[0];
  902. }
  903. Variant::CallError ce;
  904. Variant v;
  905. if (op->arguments[0]->type==Node::TYPE_TYPE) {
  906. TypeNode *tn = static_cast<TypeNode*>(op->arguments[0]);
  907. v = Variant::construct(tn->vtype,vptr,ptrs.size(),ce);
  908. } else {
  909. GDFunctions::Function func = static_cast<BuiltInFunctionNode*>(op->arguments[0])->function;
  910. GDFunctions::call(func,vptr,ptrs.size(),v,ce);
  911. }
  912. if (ce.error!=Variant::CallError::CALL_OK) {
  913. String errwhere;
  914. if (op->arguments[0]->type==Node::TYPE_TYPE) {
  915. TypeNode *tn = static_cast<TypeNode*>(op->arguments[0]);
  916. errwhere="'"+Variant::get_type_name(tn->vtype)+"'' constructor";
  917. } else {
  918. GDFunctions::Function func = static_cast<BuiltInFunctionNode*>(op->arguments[0])->function;
  919. errwhere=String("'")+GDFunctions::get_func_name(func)+"'' intrinsic function";
  920. }
  921. switch(ce.error) {
  922. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  923. _set_error("Invalid argument (#"+itos(ce.argument+1)+") for "+errwhere+".");
  924. } break;
  925. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  926. _set_error("Too many arguments for "+errwhere+".");
  927. } break;
  928. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  929. _set_error("Too few arguments for "+errwhere+".");
  930. } break;
  931. default: {
  932. _set_error("Invalid arguments for "+errwhere+".");
  933. } break;
  934. }
  935. return p_node;
  936. }
  937. ConstantNode *cn = alloc_node<ConstantNode>();
  938. cn->value=v;
  939. return cn;
  940. } else if (op->arguments[0]->type==Node::TYPE_BUILT_IN_FUNCTION && last_not_constant==0) {
  941. }
  942. return op; //don't reduce yet
  943. } else if (op->op==OperatorNode::OP_YIELD) {
  944. return op;
  945. } else if (op->op==OperatorNode::OP_INDEX) {
  946. //can reduce indices into constant arrays or dictionaries
  947. if (all_constants) {
  948. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  949. ConstantNode *cb = static_cast<ConstantNode*>(op->arguments[1]);
  950. bool valid;
  951. Variant v = ca->value.get(cb->value,&valid);
  952. if (!valid) {
  953. _set_error("invalid index in constant expression");
  954. return op;
  955. }
  956. ConstantNode *cn = alloc_node<ConstantNode>();
  957. cn->value=v;
  958. return cn;
  959. } /*else if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  960. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  961. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  962. bool valid;
  963. Variant v = ca->value.get_named(ib->name,&valid);
  964. if (!valid) {
  965. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  966. return op;
  967. }
  968. ConstantNode *cn = alloc_node<ConstantNode>();
  969. cn->value=v;
  970. return cn;
  971. }*/
  972. return op;
  973. } else if (op->op==OperatorNode::OP_INDEX_NAMED) {
  974. if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  975. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  976. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  977. bool valid;
  978. Variant v = ca->value.get_named(ib->name,&valid);
  979. if (!valid) {
  980. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  981. return op;
  982. }
  983. ConstantNode *cn = alloc_node<ConstantNode>();
  984. cn->value=v;
  985. return cn;
  986. }
  987. return op;
  988. }
  989. //validate assignment (don't assign to cosntant expression
  990. switch(op->op) {
  991. case OperatorNode::OP_ASSIGN:
  992. case OperatorNode::OP_ASSIGN_ADD:
  993. case OperatorNode::OP_ASSIGN_SUB:
  994. case OperatorNode::OP_ASSIGN_MUL:
  995. case OperatorNode::OP_ASSIGN_DIV:
  996. case OperatorNode::OP_ASSIGN_MOD:
  997. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  998. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  999. case OperatorNode::OP_ASSIGN_BIT_AND:
  1000. case OperatorNode::OP_ASSIGN_BIT_OR:
  1001. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1002. if (op->arguments[0]->type==Node::TYPE_CONSTANT) {
  1003. _set_error("Can't assign to constant",tokenizer->get_token_line()-1);
  1004. return op;
  1005. }
  1006. } break;
  1007. default: { break; }
  1008. }
  1009. //now se if all are constants
  1010. if (!all_constants)
  1011. return op; //nothing to reduce from here on
  1012. #define _REDUCE_UNARY(m_vop)\
  1013. bool valid=false;\
  1014. Variant res;\
  1015. Variant::evaluate(m_vop,static_cast<ConstantNode*>(op->arguments[0])->value,Variant(),res,valid);\
  1016. if (!valid) {\
  1017. _set_error("Invalid operand for unary operator");\
  1018. return p_node;\
  1019. }\
  1020. ConstantNode *cn = alloc_node<ConstantNode>();\
  1021. cn->value=res;\
  1022. return cn;
  1023. #define _REDUCE_BINARY(m_vop)\
  1024. bool valid=false;\
  1025. Variant res;\
  1026. Variant::evaluate(m_vop,static_cast<ConstantNode*>(op->arguments[0])->value,static_cast<ConstantNode*>(op->arguments[1])->value,res,valid);\
  1027. if (!valid) {\
  1028. _set_error("Invalid operands for operator");\
  1029. return p_node;\
  1030. }\
  1031. ConstantNode *cn = alloc_node<ConstantNode>();\
  1032. cn->value=res;\
  1033. return cn;
  1034. switch(op->op) {
  1035. //unary operators
  1036. case OperatorNode::OP_NEG: { _REDUCE_UNARY(Variant::OP_NEGATE); } break;
  1037. case OperatorNode::OP_NOT: { _REDUCE_UNARY(Variant::OP_NOT); } break;
  1038. case OperatorNode::OP_BIT_INVERT: { _REDUCE_UNARY(Variant::OP_BIT_NEGATE); } break;
  1039. //binary operators (in precedence order)
  1040. case OperatorNode::OP_IN: { _REDUCE_BINARY(Variant::OP_IN); } break;
  1041. case OperatorNode::OP_EQUAL: { _REDUCE_BINARY(Variant::OP_EQUAL); } break;
  1042. case OperatorNode::OP_NOT_EQUAL: { _REDUCE_BINARY(Variant::OP_NOT_EQUAL); } break;
  1043. case OperatorNode::OP_LESS: { _REDUCE_BINARY(Variant::OP_LESS); } break;
  1044. case OperatorNode::OP_LESS_EQUAL: { _REDUCE_BINARY(Variant::OP_LESS_EQUAL); } break;
  1045. case OperatorNode::OP_GREATER: { _REDUCE_BINARY(Variant::OP_GREATER); } break;
  1046. case OperatorNode::OP_GREATER_EQUAL: { _REDUCE_BINARY(Variant::OP_GREATER_EQUAL); } break;
  1047. case OperatorNode::OP_AND: { _REDUCE_BINARY(Variant::OP_AND); } break;
  1048. case OperatorNode::OP_OR: { _REDUCE_BINARY(Variant::OP_OR); } break;
  1049. case OperatorNode::OP_ADD: { _REDUCE_BINARY(Variant::OP_ADD); } break;
  1050. case OperatorNode::OP_SUB: { _REDUCE_BINARY(Variant::OP_SUBSTRACT); } break;
  1051. case OperatorNode::OP_MUL: { _REDUCE_BINARY(Variant::OP_MULTIPLY); } break;
  1052. case OperatorNode::OP_DIV: { _REDUCE_BINARY(Variant::OP_DIVIDE); } break;
  1053. case OperatorNode::OP_MOD: { _REDUCE_BINARY(Variant::OP_MODULE); } break;
  1054. case OperatorNode::OP_SHIFT_LEFT: { _REDUCE_BINARY(Variant::OP_SHIFT_LEFT); } break;
  1055. case OperatorNode::OP_SHIFT_RIGHT: { _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT); } break;
  1056. case OperatorNode::OP_BIT_AND: { _REDUCE_BINARY(Variant::OP_BIT_AND); } break;
  1057. case OperatorNode::OP_BIT_OR: { _REDUCE_BINARY(Variant::OP_BIT_OR); } break;
  1058. case OperatorNode::OP_BIT_XOR: { _REDUCE_BINARY(Variant::OP_BIT_XOR); } break;
  1059. default: { ERR_FAIL_V(op); }
  1060. }
  1061. ERR_FAIL_V(op);
  1062. } break;
  1063. default: {
  1064. return p_node;
  1065. } break;
  1066. }
  1067. }
  1068. GDParser::Node* GDParser::_parse_and_reduce_expression(Node *p_parent,bool p_static,bool p_reduce_const,bool p_allow_assign) {
  1069. Node* expr=_parse_expression(p_parent,p_static,p_allow_assign);
  1070. if (!expr || error_set)
  1071. return NULL;
  1072. expr = _reduce_expression(expr,p_reduce_const);
  1073. if (!expr || error_set)
  1074. return NULL;
  1075. return expr;
  1076. }
  1077. bool GDParser::_recover_from_completion() {
  1078. if (!completion_found) {
  1079. return false; //can't recover if no completion
  1080. }
  1081. //skip stuff until newline
  1082. while(tokenizer->get_token()!=GDTokenizer::TK_NEWLINE && tokenizer->get_token()!=GDTokenizer::TK_EOF && tokenizer->get_token()!=GDTokenizer::TK_ERROR) {
  1083. tokenizer->advance();
  1084. }
  1085. completion_found=false;
  1086. error_set=false;
  1087. if(tokenizer->get_token() == GDTokenizer::TK_ERROR){
  1088. error_set = true;
  1089. }
  1090. return true;
  1091. }
  1092. void GDParser::_parse_block(BlockNode *p_block,bool p_static) {
  1093. int indent_level = tab_level.back()->get();
  1094. #ifdef DEBUG_ENABLED
  1095. NewLineNode *nl = alloc_node<NewLineNode>();
  1096. nl->line=tokenizer->get_token_line();
  1097. p_block->statements.push_back(nl);
  1098. #endif
  1099. while(true) {
  1100. GDTokenizer::Token token = tokenizer->get_token();
  1101. if (error_set)
  1102. return;
  1103. if (indent_level>tab_level.back()->get()) {
  1104. p_block->end_line=tokenizer->get_token_line();
  1105. return; //go back a level
  1106. }
  1107. if (pending_newline!=-1) {
  1108. NewLineNode *nl = alloc_node<NewLineNode>();
  1109. nl->line=pending_newline;
  1110. p_block->statements.push_back(nl);
  1111. pending_newline=-1;
  1112. }
  1113. switch(token) {
  1114. case GDTokenizer::TK_EOF:
  1115. p_block->end_line=tokenizer->get_token_line();
  1116. case GDTokenizer::TK_ERROR: {
  1117. return; //go back
  1118. //end of file!
  1119. } break;
  1120. case GDTokenizer::TK_NEWLINE: {
  1121. if (!_parse_newline()) {
  1122. if (!error_set) {
  1123. p_block->end_line=tokenizer->get_token_line();
  1124. pending_newline=p_block->end_line;
  1125. }
  1126. return;
  1127. }
  1128. NewLineNode *nl = alloc_node<NewLineNode>();
  1129. nl->line=tokenizer->get_token_line();
  1130. p_block->statements.push_back(nl);
  1131. } break;
  1132. case GDTokenizer::TK_CF_PASS: {
  1133. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE && tokenizer->get_token(1)!=GDTokenizer::TK_EOF) {
  1134. _set_error("Expected ';' or <NewLine>.");
  1135. return;
  1136. }
  1137. tokenizer->advance();
  1138. } break;
  1139. case GDTokenizer::TK_PR_VAR: {
  1140. //variale declaration and (eventual) initialization
  1141. tokenizer->advance();
  1142. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1143. _set_error("Expected identifier for local variable name.");
  1144. return;
  1145. }
  1146. StringName n = tokenizer->get_token_identifier();
  1147. tokenizer->advance();
  1148. p_block->variables.push_back(n); //line?
  1149. p_block->variable_lines.push_back(tokenizer->get_token_line());
  1150. //must know when the local variable is declared
  1151. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1152. lv->name=n;
  1153. p_block->statements.push_back(lv);
  1154. Node *assigned=NULL;
  1155. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1156. tokenizer->advance();
  1157. Node *subexpr=NULL;
  1158. subexpr = _parse_and_reduce_expression(p_block,p_static);
  1159. if (!subexpr) {
  1160. if (_recover_from_completion()) {
  1161. break;
  1162. }
  1163. return;
  1164. }
  1165. lv->assign=subexpr;
  1166. assigned=subexpr;
  1167. } else {
  1168. ConstantNode *c = alloc_node<ConstantNode>();
  1169. c->value=Variant();
  1170. assigned = c;
  1171. }
  1172. IdentifierNode *id = alloc_node<IdentifierNode>();
  1173. id->name=n;
  1174. OperatorNode *op = alloc_node<OperatorNode>();
  1175. op->op=OperatorNode::OP_ASSIGN;
  1176. op->arguments.push_back(id);
  1177. op->arguments.push_back(assigned);
  1178. p_block->statements.push_back(op);
  1179. if (!_end_statement()) {
  1180. _set_error("Expected end of statement (var)");
  1181. return;
  1182. }
  1183. } break;
  1184. case GDTokenizer::TK_CF_IF: {
  1185. tokenizer->advance();
  1186. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1187. if (!condition) {
  1188. if (_recover_from_completion()) {
  1189. break;
  1190. }
  1191. return;
  1192. }
  1193. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1194. cf_if->cf_type=ControlFlowNode::CF_IF;
  1195. cf_if->arguments.push_back(condition);
  1196. cf_if->body = alloc_node<BlockNode>();
  1197. cf_if->body->parent_block=p_block;
  1198. p_block->sub_blocks.push_back(cf_if->body);
  1199. if (!_enter_indent_block(cf_if->body)) {
  1200. _set_error("Expected intended block after 'if'");
  1201. p_block->end_line=tokenizer->get_token_line();
  1202. return;
  1203. }
  1204. current_block=cf_if->body;
  1205. _parse_block(cf_if->body,p_static);
  1206. current_block=p_block;
  1207. if (error_set)
  1208. return;
  1209. p_block->statements.push_back(cf_if);
  1210. while(true) {
  1211. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1212. tokenizer->advance();
  1213. }
  1214. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1215. p_block->end_line=tokenizer->get_token_line();
  1216. return;
  1217. }
  1218. if (tokenizer->get_token()==GDTokenizer::TK_CF_ELIF) {
  1219. if (tab_level.back()->get() > indent_level) {
  1220. _set_error("Invalid indent");
  1221. return;
  1222. }
  1223. tokenizer->advance();
  1224. cf_if->body_else=alloc_node<BlockNode>();
  1225. cf_if->body_else->parent_block=p_block;
  1226. p_block->sub_blocks.push_back(cf_if->body_else);
  1227. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1228. cf_else->cf_type=ControlFlowNode::CF_IF;
  1229. //condition
  1230. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1231. if (!condition) {
  1232. if (_recover_from_completion()) {
  1233. break;
  1234. }
  1235. return;
  1236. }
  1237. cf_else->arguments.push_back(condition);
  1238. cf_else->cf_type=ControlFlowNode::CF_IF;
  1239. cf_if->body_else->statements.push_back(cf_else);
  1240. cf_if=cf_else;
  1241. cf_if->body=alloc_node<BlockNode>();
  1242. cf_if->body->parent_block=p_block;
  1243. p_block->sub_blocks.push_back(cf_if->body);
  1244. if (!_enter_indent_block(cf_if->body)) {
  1245. _set_error("Expected indented block after 'elif'");
  1246. p_block->end_line=tokenizer->get_token_line();
  1247. return;
  1248. }
  1249. current_block=cf_else->body;
  1250. _parse_block(cf_else->body,p_static);
  1251. current_block=p_block;
  1252. if (error_set)
  1253. return;
  1254. } else if (tokenizer->get_token()==GDTokenizer::TK_CF_ELSE) {
  1255. if (tab_level.back()->get() > indent_level) {
  1256. _set_error("Invalid indent");
  1257. return;
  1258. }
  1259. tokenizer->advance();
  1260. cf_if->body_else=alloc_node<BlockNode>();
  1261. cf_if->body_else->parent_block=p_block;
  1262. p_block->sub_blocks.push_back(cf_if->body_else);
  1263. if (!_enter_indent_block(cf_if->body_else)) {
  1264. _set_error("Expected indented block after 'else'");
  1265. p_block->end_line=tokenizer->get_token_line();
  1266. return;
  1267. }
  1268. current_block=cf_if->body_else;
  1269. _parse_block(cf_if->body_else,p_static);
  1270. current_block=p_block;
  1271. if (error_set)
  1272. return;
  1273. break; //after else, exit
  1274. } else
  1275. break;
  1276. }
  1277. } break;
  1278. case GDTokenizer::TK_CF_WHILE: {
  1279. tokenizer->advance();
  1280. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1281. if (!condition) {
  1282. if (_recover_from_completion()) {
  1283. break;
  1284. }
  1285. return;
  1286. }
  1287. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  1288. cf_while->cf_type=ControlFlowNode::CF_WHILE;
  1289. cf_while->arguments.push_back(condition);
  1290. cf_while->body = alloc_node<BlockNode>();
  1291. cf_while->body->parent_block=p_block;
  1292. p_block->sub_blocks.push_back(cf_while->body);
  1293. if (!_enter_indent_block(cf_while->body)) {
  1294. _set_error("Expected indented block after 'while'");
  1295. p_block->end_line=tokenizer->get_token_line();
  1296. return;
  1297. }
  1298. current_block=cf_while->body;
  1299. _parse_block(cf_while->body,p_static);
  1300. current_block=p_block;
  1301. if (error_set)
  1302. return;
  1303. p_block->statements.push_back(cf_while);
  1304. } break;
  1305. case GDTokenizer::TK_CF_FOR: {
  1306. tokenizer->advance();
  1307. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1308. _set_error("identifier expected after 'for'");
  1309. }
  1310. IdentifierNode *id = alloc_node<IdentifierNode>();
  1311. id->name=tokenizer->get_token_identifier();
  1312. tokenizer->advance();
  1313. if (tokenizer->get_token()!=GDTokenizer::TK_OP_IN) {
  1314. _set_error("'in' expected after identifier");
  1315. return;
  1316. }
  1317. tokenizer->advance();
  1318. Node *container = _parse_and_reduce_expression(p_block,p_static);
  1319. if (!container) {
  1320. if (_recover_from_completion()) {
  1321. break;
  1322. }
  1323. return;
  1324. }
  1325. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  1326. cf_for->cf_type=ControlFlowNode::CF_FOR;
  1327. cf_for->arguments.push_back(id);
  1328. cf_for->arguments.push_back(container);
  1329. cf_for->body = alloc_node<BlockNode>();
  1330. cf_for->body->parent_block=p_block;
  1331. p_block->sub_blocks.push_back(cf_for->body);
  1332. if (!_enter_indent_block(cf_for->body)) {
  1333. _set_error("Expected indented block after 'for'");
  1334. p_block->end_line=tokenizer->get_token_line();
  1335. return;
  1336. }
  1337. current_block=cf_for->body;
  1338. _parse_block(cf_for->body,p_static);
  1339. current_block=p_block;
  1340. if (error_set)
  1341. return;
  1342. p_block->statements.push_back(cf_for);
  1343. } break;
  1344. case GDTokenizer::TK_CF_CONTINUE: {
  1345. tokenizer->advance();
  1346. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  1347. cf_continue->cf_type=ControlFlowNode::CF_CONTINUE;
  1348. p_block->statements.push_back(cf_continue);
  1349. if (!_end_statement()) {
  1350. _set_error("Expected end of statement (continue)");
  1351. return;
  1352. }
  1353. } break;
  1354. case GDTokenizer::TK_CF_BREAK: {
  1355. tokenizer->advance();
  1356. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  1357. cf_break->cf_type=ControlFlowNode::CF_BREAK;
  1358. p_block->statements.push_back(cf_break);
  1359. if (!_end_statement()) {
  1360. _set_error("Expected end of statement (break)");
  1361. return;
  1362. }
  1363. } break;
  1364. case GDTokenizer::TK_CF_RETURN: {
  1365. tokenizer->advance();
  1366. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  1367. cf_return->cf_type=ControlFlowNode::CF_RETURN;
  1368. if (tokenizer->get_token()==GDTokenizer::TK_SEMICOLON || tokenizer->get_token()==GDTokenizer::TK_NEWLINE || tokenizer->get_token()==GDTokenizer::TK_EOF) {
  1369. //expect end of statement
  1370. p_block->statements.push_back(cf_return);
  1371. if (!_end_statement()) {
  1372. return;
  1373. }
  1374. } else {
  1375. //expect expression
  1376. Node *retexpr = _parse_and_reduce_expression(p_block,p_static);
  1377. if (!retexpr) {
  1378. if (_recover_from_completion()) {
  1379. break;
  1380. }
  1381. return;
  1382. }
  1383. cf_return->arguments.push_back(retexpr);
  1384. p_block->statements.push_back(cf_return);
  1385. if (!_end_statement()) {
  1386. _set_error("Expected end of statement after return expression.");
  1387. return;
  1388. }
  1389. }
  1390. } break;
  1391. case GDTokenizer::TK_PR_ASSERT: {
  1392. tokenizer->advance();
  1393. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1394. if (!condition) {
  1395. if (_recover_from_completion()) {
  1396. break;
  1397. }
  1398. return;
  1399. }
  1400. AssertNode *an = alloc_node<AssertNode>();
  1401. an->condition=condition;
  1402. p_block->statements.push_back(an);
  1403. if (!_end_statement()) {
  1404. _set_error("Expected end of statement after assert.");
  1405. return;
  1406. }
  1407. } break;
  1408. case GDTokenizer::TK_PR_BREAKPOINT: {
  1409. tokenizer->advance();
  1410. BreakpointNode *bn = alloc_node<BreakpointNode>();
  1411. p_block->statements.push_back(bn);
  1412. if (!_end_statement()) {
  1413. _set_error("Expected end of statement after breakpoint.");
  1414. return;
  1415. }
  1416. } break;
  1417. default: {
  1418. Node *expression = _parse_and_reduce_expression(p_block,p_static,false,true);
  1419. if (!expression) {
  1420. if (_recover_from_completion()) {
  1421. break;
  1422. }
  1423. return;
  1424. }
  1425. p_block->statements.push_back(expression);
  1426. if (!_end_statement()) {
  1427. _set_error("Expected end of statement after expression.");
  1428. return;
  1429. }
  1430. } break;
  1431. /*
  1432. case GDTokenizer::TK_CF_LOCAL: {
  1433. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  1434. _set_error("Expected ';' or <NewLine>.");
  1435. }
  1436. tokenizer->advance();
  1437. } break;
  1438. */
  1439. }
  1440. }
  1441. }
  1442. bool GDParser::_parse_newline() {
  1443. if (tokenizer->get_token(1)!=GDTokenizer::TK_EOF && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
  1444. int indent = tokenizer->get_token_line_indent();
  1445. int current_indent = tab_level.back()->get();
  1446. if (indent>current_indent) {
  1447. _set_error("Unexpected indent.");
  1448. return false;
  1449. }
  1450. if (indent<current_indent) {
  1451. while(indent<current_indent) {
  1452. //exit block
  1453. if (tab_level.size()==1) {
  1454. _set_error("Invalid indent. BUG?");
  1455. return false;
  1456. }
  1457. tab_level.pop_back();
  1458. if (tab_level.back()->get()<indent) {
  1459. _set_error("Unindent does not match any outer indentation level.");
  1460. return false;
  1461. }
  1462. current_indent = tab_level.back()->get();
  1463. }
  1464. tokenizer->advance();
  1465. return false;
  1466. }
  1467. }
  1468. tokenizer->advance();
  1469. return true;
  1470. }
  1471. void GDParser::_parse_extends(ClassNode *p_class) {
  1472. if (p_class->extends_used) {
  1473. _set_error("'extends' already used for this class.");
  1474. return;
  1475. }
  1476. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  1477. _set_error("'extends' must be used before anything else.");
  1478. return;
  1479. }
  1480. p_class->extends_used=true;
  1481. tokenizer->advance();
  1482. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type()==Variant::OBJECT) {
  1483. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  1484. tokenizer->advance();
  1485. return;
  1486. }
  1487. // see if inheritance happens from a file
  1488. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT) {
  1489. Variant constant = tokenizer->get_token_constant();
  1490. if (constant.get_type()!=Variant::STRING) {
  1491. _set_error("'extends' constant must be a string.");
  1492. return;
  1493. }
  1494. p_class->extends_file=constant;
  1495. tokenizer->advance();
  1496. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD) {
  1497. return;
  1498. } else
  1499. tokenizer->advance();
  1500. }
  1501. while(true) {
  1502. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1503. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  1504. return;
  1505. }
  1506. StringName identifier=tokenizer->get_token_identifier();
  1507. p_class->extends_class.push_back(identifier);
  1508. tokenizer->advance(1);
  1509. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD)
  1510. return;
  1511. }
  1512. }
  1513. void GDParser::_parse_class(ClassNode *p_class) {
  1514. int indent_level = tab_level.back()->get();
  1515. while(true) {
  1516. GDTokenizer::Token token = tokenizer->get_token();
  1517. if (error_set)
  1518. return;
  1519. if (indent_level>tab_level.back()->get()) {
  1520. p_class->end_line=tokenizer->get_token_line();
  1521. return; //go back a level
  1522. }
  1523. switch(token) {
  1524. case GDTokenizer::TK_EOF:
  1525. p_class->end_line=tokenizer->get_token_line();
  1526. case GDTokenizer::TK_ERROR: {
  1527. return; //go back
  1528. //end of file!
  1529. } break;
  1530. case GDTokenizer::TK_NEWLINE: {
  1531. if (!_parse_newline()) {
  1532. if (!error_set) {
  1533. p_class->end_line=tokenizer->get_token_line();
  1534. }
  1535. return;
  1536. }
  1537. } break;
  1538. case GDTokenizer::TK_PR_EXTENDS: {
  1539. _parse_extends(p_class);
  1540. if (error_set)
  1541. return;
  1542. if (!_end_statement()) {
  1543. _set_error("Expected end of statement after extends");
  1544. return;
  1545. }
  1546. } break;
  1547. case GDTokenizer::TK_PR_TOOL: {
  1548. if (p_class->tool) {
  1549. _set_error("tool used more than once");
  1550. return;
  1551. }
  1552. p_class->tool=true;
  1553. tokenizer->advance();
  1554. } break;
  1555. case GDTokenizer::TK_PR_CLASS: {
  1556. //class inside class :D
  1557. StringName name;
  1558. StringName extends;
  1559. if (tokenizer->get_token(1)!=GDTokenizer::TK_IDENTIFIER) {
  1560. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  1561. return;
  1562. }
  1563. name = tokenizer->get_token_identifier(1);
  1564. tokenizer->advance(2);
  1565. ClassNode *newclass = alloc_node<ClassNode>();
  1566. newclass->initializer = alloc_node<BlockNode>();
  1567. newclass->initializer->parent_class=newclass;
  1568. newclass->ready = alloc_node<BlockNode>();
  1569. newclass->ready->parent_class=newclass;
  1570. newclass->name=name;
  1571. newclass->owner=p_class;
  1572. p_class->subclasses.push_back(newclass);
  1573. if (tokenizer->get_token()==GDTokenizer::TK_PR_EXTENDS) {
  1574. _parse_extends(newclass);
  1575. if (error_set)
  1576. return;
  1577. }
  1578. if (!_enter_indent_block()) {
  1579. _set_error("Indented block expected.");
  1580. return;
  1581. }
  1582. current_class=newclass;
  1583. _parse_class(newclass);
  1584. current_class=p_class;
  1585. } break;
  1586. /* this is for functions....
  1587. case GDTokenizer::TK_CF_PASS: {
  1588. tokenizer->advance(1);
  1589. } break;
  1590. */
  1591. case GDTokenizer::TK_PR_STATIC: {
  1592. tokenizer->advance();
  1593. if (tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  1594. _set_error("Expected 'func'.");
  1595. return;
  1596. }
  1597. }; //fallthrough to function
  1598. case GDTokenizer::TK_PR_FUNCTION: {
  1599. bool _static=false;
  1600. pending_newline=-1;
  1601. if (tokenizer->get_token(-1)==GDTokenizer::TK_PR_STATIC) {
  1602. _static=true;
  1603. }
  1604. tokenizer->advance();
  1605. StringName name;
  1606. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC,name)) {
  1607. }
  1608. if (name==StringName()) {
  1609. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  1610. return;
  1611. }
  1612. for(int i=0;i<p_class->functions.size();i++) {
  1613. if (p_class->functions[i]->name==name) {
  1614. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->functions[i]->line)+").");
  1615. }
  1616. }
  1617. for(int i=0;i<p_class->static_functions.size();i++) {
  1618. if (p_class->static_functions[i]->name==name) {
  1619. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->static_functions[i]->line)+").");
  1620. }
  1621. }
  1622. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1623. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  1624. return;
  1625. }
  1626. tokenizer->advance();
  1627. Vector<StringName> arguments;
  1628. Vector<Node*> default_values;
  1629. int fnline = tokenizer->get_token_line();
  1630. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1631. //has arguments
  1632. bool defaulting=false;
  1633. while(true) {
  1634. if (tokenizer->get_token()==GDTokenizer::TK_PR_VAR) {
  1635. tokenizer->advance(); //var before the identifier is allowed
  1636. }
  1637. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1638. _set_error("Expected identifier for argument.");
  1639. return;
  1640. }
  1641. StringName argname=tokenizer->get_token_identifier();
  1642. arguments.push_back(argname);
  1643. tokenizer->advance();
  1644. if (defaulting && tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  1645. _set_error("Default parameter expected.");
  1646. return;
  1647. }
  1648. //tokenizer->advance();
  1649. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1650. defaulting=true;
  1651. tokenizer->advance(1);
  1652. Node *defval=NULL;
  1653. defval=_parse_and_reduce_expression(p_class,_static);
  1654. if (!defval || error_set)
  1655. return;
  1656. OperatorNode *on = alloc_node<OperatorNode>();
  1657. on->op=OperatorNode::OP_ASSIGN;
  1658. IdentifierNode *in = alloc_node<IdentifierNode>();
  1659. in->name=argname;
  1660. on->arguments.push_back(in);
  1661. on->arguments.push_back(defval);
  1662. /* no ..
  1663. if (defval->type!=Node::TYPE_CONSTANT) {
  1664. _set_error("default argument must be constant");
  1665. }
  1666. */
  1667. default_values.push_back(on);
  1668. }
  1669. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1670. tokenizer->advance();
  1671. continue;
  1672. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1673. _set_error("Expected ',' or ')'.");
  1674. return;
  1675. }
  1676. break;
  1677. }
  1678. }
  1679. tokenizer->advance();
  1680. BlockNode *block = alloc_node<BlockNode>();
  1681. block->parent_class=p_class;
  1682. if (name=="_init") {
  1683. if (p_class->extends_used) {
  1684. OperatorNode *cparent = alloc_node<OperatorNode>();
  1685. cparent->op=OperatorNode::OP_PARENT_CALL;
  1686. block->statements.push_back(cparent);
  1687. IdentifierNode *id = alloc_node<IdentifierNode>();
  1688. id->name="_init";
  1689. cparent->arguments.push_back(id);
  1690. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1691. tokenizer->advance();
  1692. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1693. _set_error("expected '(' for parent constructor arguments.");
  1694. }
  1695. tokenizer->advance();
  1696. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1697. //has arguments
  1698. while(true) {
  1699. Node *arg = _parse_and_reduce_expression(p_class,_static);
  1700. cparent->arguments.push_back(arg);
  1701. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1702. tokenizer->advance();
  1703. continue;
  1704. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1705. _set_error("Expected ',' or ')'.");
  1706. return;
  1707. }
  1708. break;
  1709. }
  1710. }
  1711. tokenizer->advance();
  1712. }
  1713. } else {
  1714. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1715. _set_error("Parent constructor call found for a class without inheritance.");
  1716. return;
  1717. }
  1718. }
  1719. }
  1720. if (!_enter_indent_block(block)) {
  1721. _set_error("Indented block expected.");
  1722. return;
  1723. }
  1724. FunctionNode *function = alloc_node<FunctionNode>();
  1725. function->name=name;
  1726. function->arguments=arguments;
  1727. function->default_values=default_values;
  1728. function->_static=_static;
  1729. function->line=fnline;
  1730. if (_static)
  1731. p_class->static_functions.push_back(function);
  1732. else
  1733. p_class->functions.push_back(function);
  1734. current_function=function;
  1735. function->body=block;
  1736. current_block=block;
  1737. _parse_block(block,_static);
  1738. current_block=NULL;
  1739. //arguments
  1740. } break;
  1741. case GDTokenizer::TK_PR_SIGNAL: {
  1742. tokenizer->advance();
  1743. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1744. _set_error("Expected identifier after 'signal'.");
  1745. return;
  1746. }
  1747. ClassNode::Signal sig;
  1748. sig.name = tokenizer->get_token_identifier();
  1749. tokenizer->advance();
  1750. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1751. tokenizer->advance();
  1752. while(true) {
  1753. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1754. tokenizer->advance();
  1755. break;
  1756. }
  1757. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1758. _set_error("Expected identifier in signal argument.");
  1759. return;
  1760. }
  1761. sig.arguments.push_back(tokenizer->get_token_identifier());
  1762. tokenizer->advance();
  1763. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1764. tokenizer->advance();
  1765. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1766. _set_error("Expected ',' or ')' after signal parameter identifier.");
  1767. return;
  1768. }
  1769. }
  1770. }
  1771. p_class->_signals.push_back(sig);
  1772. if (!_end_statement()) {
  1773. _set_error("Expected end of statement (signal)");
  1774. return;
  1775. }
  1776. } break;
  1777. case GDTokenizer::TK_PR_EXPORT: {
  1778. tokenizer->advance();
  1779. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1780. tokenizer->advance();
  1781. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE) {
  1782. Variant::Type type = tokenizer->get_token_type();
  1783. if (type==Variant::NIL) {
  1784. _set_error("Can't export null type.");
  1785. return;
  1786. }
  1787. current_export.type=type;
  1788. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  1789. tokenizer->advance();
  1790. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1791. // hint expected next!
  1792. tokenizer->advance();
  1793. switch(current_export.type) {
  1794. case Variant::INT: {
  1795. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FLAGS") {
  1796. current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  1797. tokenizer->advance();
  1798. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1799. break;
  1800. }
  1801. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA)
  1802. {
  1803. _set_error("Expected ')' or ',' in bit flags hint.");
  1804. return;
  1805. }
  1806. current_export.hint=PROPERTY_HINT_FLAGS;
  1807. tokenizer->advance();
  1808. bool first = true;
  1809. while(true) {
  1810. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1811. current_export=PropertyInfo();
  1812. _set_error("Expected a string constant in named bit flags hint.");
  1813. return;
  1814. }
  1815. String c = tokenizer->get_token_constant();
  1816. if (!first)
  1817. current_export.hint_string+=",";
  1818. else
  1819. first=false;
  1820. current_export.hint_string+=c.xml_escape();
  1821. tokenizer->advance();
  1822. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1823. break;
  1824. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1825. current_export=PropertyInfo();
  1826. _set_error("Expected ')' or ',' in named bit flags hint.");
  1827. return;
  1828. }
  1829. tokenizer->advance();
  1830. }
  1831. break;
  1832. }
  1833. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  1834. //enumeration
  1835. current_export.hint=PROPERTY_HINT_ENUM;
  1836. bool first=true;
  1837. while(true) {
  1838. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1839. current_export=PropertyInfo();
  1840. _set_error("Expected a string constant in enumeration hint.");
  1841. return;
  1842. }
  1843. String c = tokenizer->get_token_constant();
  1844. if (!first)
  1845. current_export.hint_string+=",";
  1846. else
  1847. first=false;
  1848. current_export.hint_string+=c.xml_escape();
  1849. tokenizer->advance();
  1850. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1851. break;
  1852. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1853. current_export=PropertyInfo();
  1854. _set_error("Expected ')' or ',' in enumeration hint.");
  1855. return;
  1856. }
  1857. tokenizer->advance();
  1858. }
  1859. break;
  1860. }
  1861. }; //fallthrough to use the same
  1862. case Variant::REAL: {
  1863. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EASE") {
  1864. current_export.hint=PROPERTY_HINT_EXP_EASING;
  1865. tokenizer->advance();
  1866. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1867. _set_error("Expected ')' in hint.");
  1868. return;
  1869. }
  1870. break;
  1871. }
  1872. // range
  1873. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EXP") {
  1874. current_export.hint=PROPERTY_HINT_EXP_RANGE;
  1875. tokenizer->advance();
  1876. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1877. break;
  1878. else if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1879. _set_error("Expected ')' or ',' in exponential range hint.");
  1880. return;
  1881. }
  1882. tokenizer->advance();
  1883. }
  1884. else
  1885. current_export.hint=PROPERTY_HINT_RANGE;
  1886. float sign=1.0;
  1887. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1888. sign=-1;
  1889. tokenizer->advance();
  1890. }
  1891. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1892. current_export=PropertyInfo();
  1893. _set_error("Expected a range in numeric hint.");
  1894. return;
  1895. }
  1896. current_export.hint_string=rtos(sign*double(tokenizer->get_token_constant()));
  1897. tokenizer->advance();
  1898. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1899. current_export.hint_string="0,"+current_export.hint_string;
  1900. break;
  1901. }
  1902. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1903. current_export=PropertyInfo();
  1904. _set_error("Expected ',' or ')' in numeric range hint.");
  1905. return;
  1906. }
  1907. tokenizer->advance();
  1908. sign=1.0;
  1909. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1910. sign=-1;
  1911. tokenizer->advance();
  1912. }
  1913. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1914. current_export=PropertyInfo();
  1915. _set_error("Expected a number as upper bound in numeric range hint.");
  1916. return;
  1917. }
  1918. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  1919. tokenizer->advance();
  1920. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1921. break;
  1922. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1923. current_export=PropertyInfo();
  1924. _set_error("Expected ',' or ')' in numeric range hint.");
  1925. return;
  1926. }
  1927. tokenizer->advance();
  1928. sign=1.0;
  1929. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1930. sign=-1;
  1931. tokenizer->advance();
  1932. }
  1933. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1934. current_export=PropertyInfo();
  1935. _set_error("Expected a number as step in numeric range hint.");
  1936. return;
  1937. }
  1938. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  1939. tokenizer->advance();
  1940. } break;
  1941. case Variant::STRING: {
  1942. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  1943. //enumeration
  1944. current_export.hint=PROPERTY_HINT_ENUM;
  1945. bool first=true;
  1946. while(true) {
  1947. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1948. current_export=PropertyInfo();
  1949. _set_error("Expected a string constant in enumeration hint.");
  1950. return;
  1951. }
  1952. String c = tokenizer->get_token_constant();
  1953. if (!first)
  1954. current_export.hint_string+=",";
  1955. else
  1956. first=false;
  1957. current_export.hint_string+=c.xml_escape();
  1958. tokenizer->advance();
  1959. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1960. break;
  1961. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1962. current_export=PropertyInfo();
  1963. _set_error("Expected ')' or ',' in enumeration hint.");
  1964. return;
  1965. }
  1966. tokenizer->advance();
  1967. }
  1968. break;
  1969. }
  1970. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="DIR") {
  1971. tokenizer->advance();
  1972. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1973. current_export.hint=PROPERTY_HINT_DIR;
  1974. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA ) {
  1975. tokenizer->advance();
  1976. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier()=="GLOBAL")) {
  1977. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  1978. return;
  1979. }
  1980. if (!p_class->tool) {
  1981. _set_error("Global filesystem hints may only be used in tool scripts.");
  1982. return;
  1983. }
  1984. current_export.hint=PROPERTY_HINT_GLOBAL_DIR;
  1985. tokenizer->advance();
  1986. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1987. _set_error("Expected ')' in hint.");
  1988. return;
  1989. }
  1990. }
  1991. else {
  1992. _set_error("Expected ')' or ',' in hint.");
  1993. return;
  1994. }
  1995. break;
  1996. }
  1997. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FILE") {
  1998. current_export.hint=PROPERTY_HINT_FILE;
  1999. tokenizer->advance();
  2000. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2001. tokenizer->advance();
  2002. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="GLOBAL") {
  2003. if (!p_class->tool) {
  2004. _set_error("Global filesystem hints may only be used in tool scripts.");
  2005. return;
  2006. }
  2007. current_export.hint=PROPERTY_HINT_GLOBAL_FILE;
  2008. tokenizer->advance();
  2009. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2010. break;
  2011. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA)
  2012. tokenizer->advance();
  2013. else {
  2014. _set_error("Expected ')' or ',' in hint.");
  2015. return;
  2016. }
  2017. }
  2018. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2019. if (current_export.hint==PROPERTY_HINT_GLOBAL_FILE)
  2020. _set_error("Expected string constant with filter");
  2021. else
  2022. _set_error("Expected 'GLOBAL' or string constant with filter");
  2023. return;
  2024. }
  2025. current_export.hint_string=tokenizer->get_token_constant();
  2026. tokenizer->advance();
  2027. }
  2028. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2029. _set_error("Expected ')' in hint.");
  2030. return;
  2031. }
  2032. break;
  2033. }
  2034. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="MULTILINE") {
  2035. current_export.hint=PROPERTY_HINT_MULTILINE_TEXT;
  2036. tokenizer->advance();
  2037. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2038. _set_error("Expected ')' in hint.");
  2039. return;
  2040. }
  2041. break;
  2042. }
  2043. } break;
  2044. case Variant::COLOR: {
  2045. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER ) {
  2046. current_export=PropertyInfo();
  2047. _set_error("Color type hint expects RGB or RGBA as hints");
  2048. return;
  2049. }
  2050. String identifier = tokenizer->get_token_identifier();
  2051. if (identifier=="RGB") {
  2052. current_export.hint=PROPERTY_HINT_COLOR_NO_ALPHA;
  2053. } else if (identifier=="RGBA") {
  2054. //none
  2055. } else {
  2056. current_export=PropertyInfo();
  2057. _set_error("Color type hint expects RGB or RGBA as hints");
  2058. return;
  2059. }
  2060. tokenizer->advance();
  2061. } break;
  2062. default: {
  2063. current_export=PropertyInfo();
  2064. _set_error("Type '"+Variant::get_type_name(type)+"' can't take hints.");
  2065. return;
  2066. } break;
  2067. }
  2068. }
  2069. } else if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  2070. String identifier = tokenizer->get_token_identifier();
  2071. if (!ObjectTypeDB::is_type(identifier,"Resource")) {
  2072. current_export=PropertyInfo();
  2073. _set_error("Export hint not a type or resource.");
  2074. }
  2075. current_export.type=Variant::OBJECT;
  2076. current_export.hint=PROPERTY_HINT_RESOURCE_TYPE;
  2077. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2078. current_export.hint_string=identifier;
  2079. tokenizer->advance();
  2080. }
  2081. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2082. current_export=PropertyInfo();
  2083. _set_error("Expected ')' or ',' after export hint.");
  2084. return;
  2085. }
  2086. tokenizer->advance();
  2087. }
  2088. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2089. current_export=PropertyInfo();
  2090. _set_error("Expected 'var'.");
  2091. return;
  2092. }
  2093. }; //fallthrough to var
  2094. case GDTokenizer::TK_PR_ONREADY: {
  2095. if (token==GDTokenizer::TK_PR_ONREADY) {
  2096. //may be fallthrough from export, ignore if so
  2097. tokenizer->advance();
  2098. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2099. _set_error("Expected 'var'.");
  2100. return;
  2101. }
  2102. }
  2103. }; //fallthrough to var
  2104. case GDTokenizer::TK_PR_VAR: {
  2105. //variale declaration and (eventual) initialization
  2106. ClassNode::Member member;
  2107. bool autoexport = tokenizer->get_token(-1)==GDTokenizer::TK_PR_EXPORT;
  2108. if (current_export.type!=Variant::NIL) {
  2109. member._export=current_export;
  2110. current_export=PropertyInfo();
  2111. }
  2112. bool onready = tokenizer->get_token(-1)==GDTokenizer::TK_PR_ONREADY;
  2113. tokenizer->advance();
  2114. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2115. _set_error("Expected identifier for member variable name.");
  2116. return;
  2117. }
  2118. member.identifier=tokenizer->get_token_identifier();
  2119. member.expression=NULL;
  2120. member._export.name=member.identifier;
  2121. member.line=tokenizer->get_token_line();
  2122. tokenizer->advance();
  2123. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  2124. #ifdef DEBUG_ENABLED
  2125. int line = tokenizer->get_token_line();
  2126. #endif
  2127. tokenizer->advance();
  2128. Node *subexpr=NULL;
  2129. subexpr = _parse_and_reduce_expression(p_class,false,autoexport);
  2130. if (!subexpr) {
  2131. if (_recover_from_completion()) {
  2132. break;
  2133. }
  2134. return;
  2135. }
  2136. //discourage common error
  2137. if (!onready && subexpr->type==Node::TYPE_OPERATOR) {
  2138. OperatorNode *op=static_cast<OperatorNode*>(subexpr);
  2139. if (op->op==OperatorNode::OP_CALL && op->arguments[0]->type==Node::TYPE_SELF && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  2140. IdentifierNode *id=static_cast<IdentifierNode*>(op->arguments[1]);
  2141. if (id->name=="get_node") {
  2142. _set_error("Use 'onready var "+String(member.identifier)+" = get_node(..)' instead");
  2143. return;
  2144. }
  2145. }
  2146. }
  2147. member.expression=subexpr;
  2148. if (autoexport) {
  2149. if (1)/*(subexpr->type==Node::TYPE_ARRAY) {
  2150. member._export.type=Variant::ARRAY;
  2151. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  2152. member._export.type=Variant::DICTIONARY;
  2153. } else*/ {
  2154. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2155. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2156. return;
  2157. }
  2158. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2159. if (cn->value.get_type()==Variant::NIL) {
  2160. _set_error("Can't accept a null constant expression for infering export type.");
  2161. return;
  2162. }
  2163. member._export.type=cn->value.get_type();
  2164. member._export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2165. }
  2166. }
  2167. #ifdef TOOLS_ENABLED
  2168. if (subexpr->type==Node::TYPE_CONSTANT && member._export.type!=Variant::NIL) {
  2169. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2170. if (cn->value.get_type()!=Variant::NIL) {
  2171. member.default_value=cn->value;
  2172. }
  2173. }
  2174. #endif
  2175. IdentifierNode *id = alloc_node<IdentifierNode>();
  2176. id->name=member.identifier;
  2177. OperatorNode *op = alloc_node<OperatorNode>();
  2178. op->op=OperatorNode::OP_INIT_ASSIGN;
  2179. op->arguments.push_back(id);
  2180. op->arguments.push_back(subexpr);
  2181. #ifdef DEBUG_ENABLED
  2182. NewLineNode *nl = alloc_node<NewLineNode>();
  2183. nl->line=line;
  2184. if (onready)
  2185. p_class->ready->statements.push_back(nl);
  2186. else
  2187. p_class->initializer->statements.push_back(nl);
  2188. #endif
  2189. if (onready)
  2190. p_class->ready->statements.push_back(op);
  2191. else
  2192. p_class->initializer->statements.push_back(op);
  2193. } else {
  2194. if (autoexport) {
  2195. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2196. return;
  2197. }
  2198. }
  2199. if (tokenizer->get_token()==GDTokenizer::TK_PR_SETGET) {
  2200. tokenizer->advance();
  2201. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2202. //just comma means using only getter
  2203. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2204. _set_error("Expected identifier for setter function after 'notify'.");
  2205. }
  2206. member.setter=tokenizer->get_token_identifier();
  2207. tokenizer->advance();
  2208. }
  2209. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2210. //there is a getter
  2211. tokenizer->advance();
  2212. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2213. _set_error("Expected identifier for getter function after ','.");
  2214. }
  2215. member.getter=tokenizer->get_token_identifier();
  2216. tokenizer->advance();
  2217. }
  2218. }
  2219. p_class->variables.push_back(member);
  2220. if (!_end_statement()) {
  2221. _set_error("Expected end of statement (continue)");
  2222. return;
  2223. }
  2224. } break;
  2225. case GDTokenizer::TK_PR_CONST: {
  2226. //variale declaration and (eventual) initialization
  2227. ClassNode::Constant constant;
  2228. tokenizer->advance();
  2229. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2230. _set_error("Expected name (identifier) for constant.");
  2231. return;
  2232. }
  2233. constant.identifier=tokenizer->get_token_identifier();
  2234. tokenizer->advance();
  2235. if (tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  2236. _set_error("Constant expects assignment.");
  2237. return;
  2238. }
  2239. tokenizer->advance();
  2240. Node *subexpr=NULL;
  2241. subexpr = _parse_and_reduce_expression(p_class,true,true);
  2242. if (!subexpr) {
  2243. if (_recover_from_completion()) {
  2244. break;
  2245. }
  2246. return;
  2247. }
  2248. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2249. _set_error("Expected constant expression");
  2250. }
  2251. constant.expression=subexpr;
  2252. p_class->constant_expressions.push_back(constant);
  2253. if (!_end_statement()) {
  2254. _set_error("Expected end of statement (constant)");
  2255. return;
  2256. }
  2257. } break;
  2258. default: {
  2259. _set_error(String()+"Unexpected token: "+tokenizer->get_token_name(tokenizer->get_token())+":"+tokenizer->get_token_identifier());
  2260. return;
  2261. } break;
  2262. }
  2263. }
  2264. }
  2265. void GDParser::_set_error(const String& p_error, int p_line, int p_column) {
  2266. if (error_set)
  2267. return; //allow no further errors
  2268. error=p_error;
  2269. error_line=p_line<0?tokenizer->get_token_line():p_line;
  2270. error_column=p_column<0?tokenizer->get_token_column():p_column;
  2271. error_set=true;
  2272. }
  2273. String GDParser::get_error() const {
  2274. return error;
  2275. }
  2276. int GDParser::get_error_line() const {
  2277. return error_line;
  2278. }
  2279. int GDParser::get_error_column() const {
  2280. return error_column;
  2281. }
  2282. Error GDParser::_parse(const String& p_base_path) {
  2283. base_path=p_base_path;
  2284. clear();
  2285. //assume class
  2286. ClassNode *main_class = alloc_node<ClassNode>();
  2287. main_class->initializer = alloc_node<BlockNode>();
  2288. main_class->initializer->parent_class=main_class;
  2289. main_class->ready = alloc_node<BlockNode>();
  2290. main_class->ready->parent_class=main_class;
  2291. current_class=main_class;
  2292. _parse_class(main_class);
  2293. if (tokenizer->get_token()==GDTokenizer::TK_ERROR) {
  2294. error_set=false;
  2295. _set_error("Parse Error: "+tokenizer->get_token_error());
  2296. }
  2297. if (error_set) {
  2298. return ERR_PARSE_ERROR;
  2299. }
  2300. return OK;
  2301. }
  2302. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path, const String &p_self_path) {
  2303. for_completion=false;
  2304. validating=false;
  2305. completion_type=COMPLETION_NONE;
  2306. completion_node=NULL;
  2307. completion_class=NULL;
  2308. completion_function=NULL;
  2309. completion_block=NULL;
  2310. completion_found=false;
  2311. current_block=NULL;
  2312. current_class=NULL;
  2313. current_function=NULL;
  2314. self_path=p_self_path;
  2315. GDTokenizerBuffer *tb = memnew( GDTokenizerBuffer );
  2316. tb->set_code_buffer(p_bytecode);
  2317. tokenizer=tb;
  2318. Error ret = _parse(p_base_path);
  2319. memdelete(tb);
  2320. tokenizer=NULL;
  2321. return ret;
  2322. }
  2323. 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) {
  2324. completion_type=COMPLETION_NONE;
  2325. completion_node=NULL;
  2326. completion_class=NULL;
  2327. completion_function=NULL;
  2328. completion_block=NULL;
  2329. completion_found=false;
  2330. current_block=NULL;
  2331. current_class=NULL;
  2332. current_function=NULL;
  2333. self_path=p_self_path;
  2334. GDTokenizerText *tt = memnew( GDTokenizerText );
  2335. tt->set_code(p_code);
  2336. validating=p_just_validate;
  2337. for_completion=p_for_completion;
  2338. tokenizer=tt;
  2339. Error ret = _parse(p_base_path);
  2340. memdelete(tt);
  2341. tokenizer=NULL;
  2342. return ret;
  2343. }
  2344. bool GDParser::is_tool_script() const {
  2345. return (head && head->type==Node::TYPE_CLASS && static_cast<const ClassNode*>(head)->tool);
  2346. }
  2347. const GDParser::Node *GDParser::get_parse_tree() const {
  2348. return head;
  2349. }
  2350. void GDParser::clear() {
  2351. while(list) {
  2352. Node *l=list;
  2353. list=list->next;
  2354. memdelete(l);
  2355. }
  2356. head=NULL;
  2357. list=NULL;
  2358. completion_type=COMPLETION_NONE;
  2359. completion_node=NULL;
  2360. completion_class=NULL;
  2361. completion_function=NULL;
  2362. completion_block=NULL;
  2363. current_block=NULL;
  2364. current_class=NULL;
  2365. completion_found=false;
  2366. current_function=NULL;
  2367. validating=false;
  2368. for_completion=false;
  2369. error_set=false;
  2370. tab_level.clear();
  2371. tab_level.push_back(0);
  2372. error_line=0;
  2373. error_column=0;
  2374. pending_newline=-1;
  2375. parenthesis=0;
  2376. current_export.type=Variant::NIL;
  2377. error="";
  2378. }
  2379. GDParser::CompletionType GDParser::get_completion_type() {
  2380. return completion_type;
  2381. }
  2382. StringName GDParser::get_completion_cursor() {
  2383. return completion_cursor;
  2384. }
  2385. int GDParser::get_completion_line() {
  2386. return completion_line;
  2387. }
  2388. Variant::Type GDParser::get_completion_built_in_constant(){
  2389. return completion_built_in_constant;
  2390. }
  2391. GDParser::Node *GDParser::get_completion_node(){
  2392. return completion_node;
  2393. }
  2394. GDParser::BlockNode *GDParser::get_completion_block() {
  2395. return completion_block;
  2396. }
  2397. GDParser::ClassNode *GDParser::get_completion_class(){
  2398. return completion_class;
  2399. }
  2400. GDParser::FunctionNode *GDParser::get_completion_function(){
  2401. return completion_function;
  2402. }
  2403. int GDParser::get_completion_argument_index() {
  2404. return completion_argument;
  2405. }
  2406. GDParser::GDParser() {
  2407. head=NULL;
  2408. list=NULL;
  2409. tokenizer=NULL;
  2410. pending_newline=-1;
  2411. clear();
  2412. }
  2413. GDParser::~GDParser() {
  2414. clear();
  2415. }