gd_parser.cpp 84 KB

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