gd_parser.cpp 102 KB

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