gd_parser.cpp 83 KB

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