gd_parser.cpp 87 KB

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