gd_parser.cpp 79 KB

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