gd_parser.cpp 86 KB

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