gd_parser.cpp 98 KB

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