gd_parser.cpp 75 KB

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