gd_parser.cpp 96 KB

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