2
0

gd_parser.cpp 99 KB

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