gd_parser.cpp 98 KB

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