gd_parser.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960
  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. if (!_end_statement()) {
  1121. _set_error("Expected end of statement (var)");
  1122. return;
  1123. }
  1124. } break;
  1125. case GDTokenizer::TK_CF_IF: {
  1126. tokenizer->advance();
  1127. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1128. if (!condition)
  1129. return;
  1130. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1131. cf_if->cf_type=ControlFlowNode::CF_IF;
  1132. cf_if->arguments.push_back(condition);
  1133. cf_if->body = alloc_node<BlockNode>();
  1134. cf_if->body->parent_block=p_block;
  1135. p_block->sub_blocks.push_back(cf_if->body);
  1136. if (!_enter_indent_block(cf_if->body)) {
  1137. p_block->end_line=tokenizer->get_token_line();
  1138. return;
  1139. }
  1140. current_block=cf_if->body;
  1141. _parse_block(cf_if->body,p_static);
  1142. current_block=p_block;
  1143. if (error_set)
  1144. return;
  1145. p_block->statements.push_back(cf_if);
  1146. while(true) {
  1147. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1148. tokenizer->advance();
  1149. }
  1150. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1151. p_block->end_line=tokenizer->get_token_line();
  1152. return;
  1153. }
  1154. if (tokenizer->get_token()==GDTokenizer::TK_CF_ELIF) {
  1155. if (tab_level.back()->get() > indent_level) {
  1156. _set_error("Invalid indent");
  1157. return;
  1158. }
  1159. tokenizer->advance();
  1160. cf_if->body_else=alloc_node<BlockNode>();
  1161. cf_if->body_else->parent_block=p_block;
  1162. p_block->sub_blocks.push_back(cf_if->body_else);
  1163. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1164. cf_else->cf_type=ControlFlowNode::CF_IF;
  1165. //condition
  1166. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1167. if (!condition)
  1168. return;
  1169. cf_else->arguments.push_back(condition);
  1170. cf_else->cf_type=ControlFlowNode::CF_IF;
  1171. cf_if->body_else->statements.push_back(cf_else);
  1172. cf_if=cf_else;
  1173. cf_if->body=alloc_node<BlockNode>();
  1174. cf_if->body->parent_block=p_block;
  1175. p_block->sub_blocks.push_back(cf_if->body);
  1176. if (!_enter_indent_block(cf_if->body)) {
  1177. p_block->end_line=tokenizer->get_token_line();
  1178. return;
  1179. }
  1180. current_block=cf_else->body;
  1181. _parse_block(cf_else->body,p_static);
  1182. current_block=p_block;
  1183. if (error_set)
  1184. return;
  1185. } else if (tokenizer->get_token()==GDTokenizer::TK_CF_ELSE) {
  1186. if (tab_level.back()->get() > indent_level) {
  1187. _set_error("Invalid indent");
  1188. return;
  1189. }
  1190. tokenizer->advance();
  1191. cf_if->body_else=alloc_node<BlockNode>();
  1192. cf_if->body_else->parent_block=p_block;
  1193. p_block->sub_blocks.push_back(cf_if->body_else);
  1194. if (!_enter_indent_block(cf_if->body_else)) {
  1195. p_block->end_line=tokenizer->get_token_line();
  1196. return;
  1197. }
  1198. current_block=cf_if->body_else;
  1199. _parse_block(cf_if->body_else,p_static);
  1200. current_block=p_block;
  1201. if (error_set)
  1202. return;
  1203. break; //after else, exit
  1204. } else
  1205. break;
  1206. }
  1207. } break;
  1208. case GDTokenizer::TK_CF_WHILE: {
  1209. tokenizer->advance();
  1210. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1211. if (!condition)
  1212. return;
  1213. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  1214. cf_while->cf_type=ControlFlowNode::CF_WHILE;
  1215. cf_while->arguments.push_back(condition);
  1216. cf_while->body = alloc_node<BlockNode>();
  1217. cf_while->body->parent_block=p_block;
  1218. p_block->sub_blocks.push_back(cf_while->body);
  1219. if (!_enter_indent_block(cf_while->body)) {
  1220. p_block->end_line=tokenizer->get_token_line();
  1221. return;
  1222. }
  1223. current_block=cf_while->body;
  1224. _parse_block(cf_while->body,p_static);
  1225. current_block=p_block;
  1226. if (error_set)
  1227. return;
  1228. p_block->statements.push_back(cf_while);
  1229. } break;
  1230. case GDTokenizer::TK_CF_FOR: {
  1231. tokenizer->advance();
  1232. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1233. _set_error("identifier expected after 'for'");
  1234. }
  1235. IdentifierNode *id = alloc_node<IdentifierNode>();
  1236. id->name=tokenizer->get_token_identifier();
  1237. tokenizer->advance();
  1238. if (tokenizer->get_token()!=GDTokenizer::TK_OP_IN) {
  1239. _set_error("'in' expected after identifier");
  1240. return;
  1241. }
  1242. tokenizer->advance();
  1243. Node *container = _parse_and_reduce_expression(p_block,p_static);
  1244. if (!container)
  1245. return;
  1246. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  1247. cf_for->cf_type=ControlFlowNode::CF_FOR;
  1248. cf_for->arguments.push_back(id);
  1249. cf_for->arguments.push_back(container);
  1250. cf_for->body = alloc_node<BlockNode>();
  1251. cf_for->body->parent_block=p_block;
  1252. p_block->sub_blocks.push_back(cf_for->body);
  1253. if (!_enter_indent_block(cf_for->body)) {
  1254. p_block->end_line=tokenizer->get_token_line();
  1255. return;
  1256. }
  1257. current_block=cf_for->body;
  1258. _parse_block(cf_for->body,p_static);
  1259. current_block=p_block;
  1260. if (error_set)
  1261. return;
  1262. p_block->statements.push_back(cf_for);
  1263. } break;
  1264. case GDTokenizer::TK_CF_CONTINUE: {
  1265. tokenizer->advance();
  1266. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  1267. cf_continue->cf_type=ControlFlowNode::CF_CONTINUE;
  1268. p_block->statements.push_back(cf_continue);
  1269. if (!_end_statement()) {
  1270. _set_error("Expected end of statement (continue)");
  1271. return;
  1272. }
  1273. } break;
  1274. case GDTokenizer::TK_CF_BREAK: {
  1275. tokenizer->advance();
  1276. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  1277. cf_break->cf_type=ControlFlowNode::CF_BREAK;
  1278. p_block->statements.push_back(cf_break);
  1279. if (!_end_statement()) {
  1280. _set_error("Expected end of statement (break)");
  1281. return;
  1282. }
  1283. } break;
  1284. case GDTokenizer::TK_CF_RETURN: {
  1285. tokenizer->advance();
  1286. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  1287. cf_return->cf_type=ControlFlowNode::CF_RETURN;
  1288. if (tokenizer->get_token()==GDTokenizer::TK_SEMICOLON || tokenizer->get_token()==GDTokenizer::TK_NEWLINE || tokenizer->get_token()==GDTokenizer::TK_EOF) {
  1289. //expect end of statement
  1290. p_block->statements.push_back(cf_return);
  1291. if (!_end_statement()) {
  1292. return;
  1293. }
  1294. } else {
  1295. //expect expression
  1296. Node *retexpr = _parse_and_reduce_expression(p_block,p_static);
  1297. if (!retexpr)
  1298. return;
  1299. cf_return->arguments.push_back(retexpr);
  1300. p_block->statements.push_back(cf_return);
  1301. if (!_end_statement()) {
  1302. _set_error("Expected end of statement after return expression.");
  1303. return;
  1304. }
  1305. }
  1306. } break;
  1307. case GDTokenizer::TK_PR_ASSERT: {
  1308. tokenizer->advance();
  1309. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1310. if (!condition)
  1311. return;
  1312. AssertNode *an = alloc_node<AssertNode>();
  1313. an->condition=condition;
  1314. p_block->statements.push_back(an);
  1315. if (!_end_statement()) {
  1316. _set_error("Expected end of statement after assert.");
  1317. return;
  1318. }
  1319. } break;
  1320. default: {
  1321. Node *expression = _parse_and_reduce_expression(p_block,p_static,false,true);
  1322. if (!expression)
  1323. return;
  1324. p_block->statements.push_back(expression);
  1325. if (!_end_statement()) {
  1326. _set_error("Expected end of statement after expression.");
  1327. return;
  1328. }
  1329. } break;
  1330. /*
  1331. case GDTokenizer::TK_CF_LOCAL: {
  1332. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  1333. _set_error("Expected ';' or <NewLine>.");
  1334. }
  1335. tokenizer->advance();
  1336. } break;
  1337. */
  1338. }
  1339. }
  1340. }
  1341. bool GDParser::_parse_newline() {
  1342. if (tokenizer->get_token(1)!=GDTokenizer::TK_EOF && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
  1343. int indent = tokenizer->get_token_line_indent();
  1344. int current_indent = tab_level.back()->get();
  1345. if (indent>current_indent) {
  1346. _set_error("Unexpected indent.");
  1347. return false;
  1348. }
  1349. if (indent<current_indent) {
  1350. while(indent<current_indent) {
  1351. //exit block
  1352. if (tab_level.size()==1) {
  1353. _set_error("Invalid indent. BUG?");
  1354. return false;
  1355. }
  1356. tab_level.pop_back();
  1357. if (tab_level.back()->get()<indent) {
  1358. _set_error("Unindent does not match any outer indentation level.");
  1359. return false;
  1360. }
  1361. current_indent = tab_level.back()->get();
  1362. }
  1363. tokenizer->advance();
  1364. return false;
  1365. }
  1366. }
  1367. tokenizer->advance();
  1368. return true;
  1369. }
  1370. void GDParser::_parse_extends(ClassNode *p_class) {
  1371. if (p_class->extends_used) {
  1372. _set_error("'extends' already used for this class.");
  1373. return;
  1374. }
  1375. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  1376. _set_error("'extends' must be used before anything else.");
  1377. return;
  1378. }
  1379. p_class->extends_used=true;
  1380. //see if inheritance happens from a file
  1381. tokenizer->advance();
  1382. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT) {
  1383. Variant constant = tokenizer->get_token_constant();
  1384. if (constant.get_type()!=Variant::STRING) {
  1385. _set_error("'extends' constant must be a string.");
  1386. return;
  1387. }
  1388. p_class->extends_file=constant;
  1389. tokenizer->advance();
  1390. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD) {
  1391. return;
  1392. } else
  1393. tokenizer->advance();
  1394. }
  1395. while(true) {
  1396. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1397. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  1398. return;
  1399. }
  1400. StringName identifier=tokenizer->get_token_identifier();
  1401. p_class->extends_class.push_back(identifier);
  1402. tokenizer->advance(1);
  1403. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD)
  1404. return;
  1405. }
  1406. }
  1407. void GDParser::_parse_class(ClassNode *p_class) {
  1408. int indent_level = tab_level.back()->get();
  1409. while(true) {
  1410. GDTokenizer::Token token = tokenizer->get_token();
  1411. if (error_set)
  1412. return;
  1413. if (indent_level>tab_level.back()->get()) {
  1414. p_class->end_line=tokenizer->get_token_line();
  1415. return; //go back a level
  1416. }
  1417. switch(token) {
  1418. case GDTokenizer::TK_EOF:
  1419. p_class->end_line=tokenizer->get_token_line();
  1420. case GDTokenizer::TK_ERROR: {
  1421. return; //go back
  1422. //end of file!
  1423. } break;
  1424. case GDTokenizer::TK_NEWLINE: {
  1425. if (!_parse_newline()) {
  1426. if (!error_set) {
  1427. p_class->end_line=tokenizer->get_token_line();
  1428. }
  1429. return;
  1430. }
  1431. } break;
  1432. case GDTokenizer::TK_PR_EXTENDS: {
  1433. _parse_extends(p_class);
  1434. if (error_set)
  1435. return;
  1436. if (!_end_statement()) {
  1437. _set_error("Expected end of statement after extends");
  1438. return;
  1439. }
  1440. } break;
  1441. case GDTokenizer::TK_PR_TOOL: {
  1442. if (p_class->tool) {
  1443. _set_error("tool used more than once");
  1444. return;
  1445. }
  1446. p_class->tool=true;
  1447. tokenizer->advance();
  1448. } break;
  1449. case GDTokenizer::TK_PR_CLASS: {
  1450. //class inside class :D
  1451. StringName name;
  1452. StringName extends;
  1453. if (tokenizer->get_token(1)!=GDTokenizer::TK_IDENTIFIER) {
  1454. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  1455. return;
  1456. }
  1457. name = tokenizer->get_token_identifier(1);
  1458. tokenizer->advance(2);
  1459. ClassNode *newclass = alloc_node<ClassNode>();
  1460. newclass->initializer = alloc_node<BlockNode>();
  1461. newclass->initializer->parent_class=newclass;
  1462. newclass->name=name;
  1463. newclass->owner=p_class;
  1464. p_class->subclasses.push_back(newclass);
  1465. if (tokenizer->get_token()==GDTokenizer::TK_PR_EXTENDS) {
  1466. _parse_extends(newclass);
  1467. if (error_set)
  1468. return;
  1469. }
  1470. if (!_enter_indent_block()) {
  1471. _set_error("Indented block expected.");
  1472. return;
  1473. }
  1474. current_class=newclass;
  1475. _parse_class(newclass);
  1476. current_class=p_class;
  1477. } break;
  1478. /* this is for functions....
  1479. case GDTokenizer::TK_CF_PASS: {
  1480. tokenizer->advance(1);
  1481. } break;
  1482. */
  1483. case GDTokenizer::TK_PR_STATIC: {
  1484. tokenizer->advance();
  1485. if (tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  1486. _set_error("Expected 'func'.");
  1487. return;
  1488. }
  1489. }; //fallthrough to function
  1490. case GDTokenizer::TK_PR_FUNCTION: {
  1491. bool _static=false;
  1492. pending_newline=-1;
  1493. if (tokenizer->get_token(-1)==GDTokenizer::TK_PR_STATIC) {
  1494. _static=true;
  1495. }
  1496. tokenizer->advance();
  1497. StringName name;
  1498. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC,name)) {
  1499. }
  1500. if (name==StringName()) {
  1501. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  1502. return;
  1503. }
  1504. for(int i=0;i<p_class->functions.size();i++) {
  1505. if (p_class->functions[i]->name==name) {
  1506. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->functions[i]->line)+").");
  1507. }
  1508. }
  1509. for(int i=0;i<p_class->static_functions.size();i++) {
  1510. if (p_class->static_functions[i]->name==name) {
  1511. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->static_functions[i]->line)+").");
  1512. }
  1513. }
  1514. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1515. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  1516. return;
  1517. }
  1518. tokenizer->advance();
  1519. Vector<StringName> arguments;
  1520. Vector<Node*> default_values;
  1521. int fnline = tokenizer->get_token_line();
  1522. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1523. //has arguments
  1524. bool defaulting=false;
  1525. while(true) {
  1526. if (tokenizer->get_token()==GDTokenizer::TK_PR_VAR) {
  1527. tokenizer->advance(); //var before the identifier is allowed
  1528. }
  1529. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1530. _set_error("Expected identifier for argument.");
  1531. return;
  1532. }
  1533. StringName argname=tokenizer->get_token_identifier();
  1534. arguments.push_back(argname);
  1535. tokenizer->advance();
  1536. if (defaulting && tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  1537. _set_error("Default parameter expected.");
  1538. return;
  1539. }
  1540. //tokenizer->advance();
  1541. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1542. defaulting=true;
  1543. tokenizer->advance(1);
  1544. Node *defval=NULL;
  1545. defval=_parse_and_reduce_expression(p_class,_static);
  1546. if (!defval || error_set)
  1547. return;
  1548. OperatorNode *on = alloc_node<OperatorNode>();
  1549. on->op=OperatorNode::OP_ASSIGN;
  1550. IdentifierNode *in = alloc_node<IdentifierNode>();
  1551. in->name=argname;
  1552. on->arguments.push_back(in);
  1553. on->arguments.push_back(defval);
  1554. /* no ..
  1555. if (defval->type!=Node::TYPE_CONSTANT) {
  1556. _set_error("default argument must be constant");
  1557. }
  1558. */
  1559. default_values.push_back(on);
  1560. }
  1561. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1562. tokenizer->advance();
  1563. continue;
  1564. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1565. _set_error("Expected ',' or ')'.");
  1566. return;
  1567. }
  1568. break;
  1569. }
  1570. }
  1571. tokenizer->advance();
  1572. BlockNode *block = alloc_node<BlockNode>();
  1573. block->parent_class=p_class;
  1574. if (name=="_init") {
  1575. if (p_class->extends_used) {
  1576. OperatorNode *cparent = alloc_node<OperatorNode>();
  1577. cparent->op=OperatorNode::OP_PARENT_CALL;
  1578. block->statements.push_back(cparent);
  1579. IdentifierNode *id = alloc_node<IdentifierNode>();
  1580. id->name="_init";
  1581. cparent->arguments.push_back(id);
  1582. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1583. tokenizer->advance();
  1584. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1585. _set_error("expected '(' for parent constructor arguments.");
  1586. }
  1587. tokenizer->advance();
  1588. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1589. //has arguments
  1590. while(true) {
  1591. Node *arg = _parse_and_reduce_expression(p_class,_static);
  1592. cparent->arguments.push_back(arg);
  1593. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1594. tokenizer->advance();
  1595. continue;
  1596. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1597. _set_error("Expected ',' or ')'.");
  1598. return;
  1599. }
  1600. break;
  1601. }
  1602. }
  1603. tokenizer->advance();
  1604. }
  1605. } else {
  1606. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1607. _set_error("Parent constructor call found for a class without inheritance.");
  1608. return;
  1609. }
  1610. }
  1611. }
  1612. if (!_enter_indent_block(block)) {
  1613. _set_error("Indented block expected.");
  1614. return;
  1615. }
  1616. FunctionNode *function = alloc_node<FunctionNode>();
  1617. function->name=name;
  1618. function->arguments=arguments;
  1619. function->default_values=default_values;
  1620. function->_static=_static;
  1621. function->line=fnline;
  1622. if (_static)
  1623. p_class->static_functions.push_back(function);
  1624. else
  1625. p_class->functions.push_back(function);
  1626. current_function=function;
  1627. function->body=block;
  1628. current_block=block;
  1629. _parse_block(block,_static);
  1630. current_block=NULL;
  1631. //arguments
  1632. } break;
  1633. case GDTokenizer::TK_PR_SIGNAL: {
  1634. tokenizer->advance();
  1635. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1636. _set_error("Expected identifier after 'signal'.");
  1637. return;
  1638. }
  1639. ClassNode::Signal sig;
  1640. sig.name = tokenizer->get_token_identifier();
  1641. tokenizer->advance();
  1642. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1643. tokenizer->advance();
  1644. while(true) {
  1645. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1646. tokenizer->advance();
  1647. break;
  1648. }
  1649. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1650. _set_error("Expected identifier in signal argument.");
  1651. return;
  1652. }
  1653. sig.arguments.push_back(tokenizer->get_token_identifier());
  1654. tokenizer->advance();
  1655. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1656. tokenizer->advance();
  1657. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1658. _set_error("Expected ',' or ')' after signal parameter identifier.");
  1659. return;
  1660. }
  1661. }
  1662. }
  1663. p_class->_signals.push_back(sig);
  1664. if (!_end_statement()) {
  1665. _set_error("Expected end of statement (signal)");
  1666. return;
  1667. }
  1668. } break;
  1669. case GDTokenizer::TK_PR_EXPORT: {
  1670. tokenizer->advance();
  1671. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1672. tokenizer->advance();
  1673. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE) {
  1674. Variant::Type type = tokenizer->get_token_type();
  1675. if (type==Variant::NIL) {
  1676. _set_error("Can't export null type.");
  1677. return;
  1678. }
  1679. current_export.type=type;
  1680. tokenizer->advance();
  1681. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1682. // hint expected next!
  1683. tokenizer->advance();
  1684. switch(current_export.type) {
  1685. case Variant::INT: {
  1686. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  1687. //enumeration
  1688. current_export.hint=PROPERTY_HINT_ENUM;
  1689. bool first=true;
  1690. while(true) {
  1691. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1692. current_export=PropertyInfo();
  1693. _set_error("Expected a string constant in enumeration hint.");
  1694. return;
  1695. }
  1696. String c = tokenizer->get_token_constant();
  1697. if (!first)
  1698. current_export.hint_string+=",";
  1699. else
  1700. first=false;
  1701. current_export.hint_string+=c.xml_escape();
  1702. tokenizer->advance();
  1703. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1704. break;
  1705. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1706. current_export=PropertyInfo();
  1707. _set_error("Expected ')' or ',' in enumeration hint.");
  1708. return;
  1709. }
  1710. tokenizer->advance();
  1711. }
  1712. break;
  1713. }
  1714. }; //fallthrough to use the same
  1715. case Variant::REAL: {
  1716. float sign=1.0;
  1717. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1718. sign=-1;
  1719. tokenizer->advance();
  1720. }
  1721. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1722. current_export=PropertyInfo();
  1723. _set_error("Expected a range in numeric hint.");
  1724. return;
  1725. }
  1726. //enumeration
  1727. current_export.hint=PROPERTY_HINT_RANGE;
  1728. current_export.hint_string=rtos(sign*double(tokenizer->get_token_constant()));
  1729. tokenizer->advance();
  1730. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1731. current_export.hint_string="0,"+current_export.hint_string;
  1732. break;
  1733. }
  1734. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1735. current_export=PropertyInfo();
  1736. _set_error("Expected ',' or ')' in numeric range hint.");
  1737. return;
  1738. }
  1739. tokenizer->advance();
  1740. sign=1.0;
  1741. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1742. sign=-1;
  1743. tokenizer->advance();
  1744. }
  1745. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1746. current_export=PropertyInfo();
  1747. _set_error("Expected a number as upper bound in numeric range hint.");
  1748. return;
  1749. }
  1750. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  1751. tokenizer->advance();
  1752. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1753. break;
  1754. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1755. current_export=PropertyInfo();
  1756. _set_error("Expected ',' or ')' in numeric range hint.");
  1757. return;
  1758. }
  1759. tokenizer->advance();
  1760. sign=1.0;
  1761. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1762. sign=-1;
  1763. tokenizer->advance();
  1764. }
  1765. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1766. current_export=PropertyInfo();
  1767. _set_error("Expected a number as step in numeric range hint.");
  1768. return;
  1769. }
  1770. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  1771. tokenizer->advance();
  1772. } break;
  1773. case Variant::STRING: {
  1774. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  1775. //enumeration
  1776. current_export.hint=PROPERTY_HINT_ENUM;
  1777. bool first=true;
  1778. while(true) {
  1779. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1780. current_export=PropertyInfo();
  1781. _set_error("Expected a string constant in enumeration hint.");
  1782. return;
  1783. }
  1784. String c = tokenizer->get_token_constant();
  1785. if (!first)
  1786. current_export.hint_string+=",";
  1787. else
  1788. first=false;
  1789. current_export.hint_string+=c.xml_escape();
  1790. tokenizer->advance();
  1791. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1792. break;
  1793. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1794. current_export=PropertyInfo();
  1795. _set_error("Expected ')' or ',' in enumeration hint.");
  1796. return;
  1797. }
  1798. tokenizer->advance();
  1799. }
  1800. break;
  1801. }
  1802. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="DIR") {
  1803. current_export.hint=PROPERTY_HINT_DIR;
  1804. tokenizer->advance();
  1805. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1806. _set_error("Expected ')' in hint.");
  1807. return;
  1808. }
  1809. break;
  1810. }
  1811. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FILE") {
  1812. current_export.hint=PROPERTY_HINT_FILE;
  1813. tokenizer->advance();
  1814. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1815. tokenizer->advance();
  1816. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1817. _set_error("Expected string constant with filter");
  1818. return;
  1819. }
  1820. current_export.hint_string=tokenizer->get_token_constant();
  1821. tokenizer->advance();
  1822. }
  1823. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1824. _set_error("Expected ')' in hint.");
  1825. return;
  1826. }
  1827. break;
  1828. }
  1829. } break;
  1830. case Variant::COLOR: {
  1831. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER ) {
  1832. current_export=PropertyInfo();
  1833. _set_error("Color type hint expects RGB or RGBA as hints");
  1834. return;
  1835. }
  1836. String identifier = tokenizer->get_token_identifier();
  1837. if (identifier=="RGB") {
  1838. current_export.hint=PROPERTY_HINT_COLOR_NO_ALPHA;
  1839. } else if (identifier=="RGBA") {
  1840. //none
  1841. } else {
  1842. current_export=PropertyInfo();
  1843. _set_error("Color type hint expects RGB or RGBA as hints");
  1844. return;
  1845. }
  1846. tokenizer->advance();
  1847. } break;
  1848. default: {
  1849. current_export=PropertyInfo();
  1850. _set_error("Type '"+Variant::get_type_name(type)+"' can't take hints.");
  1851. return;
  1852. } break;
  1853. }
  1854. }
  1855. } else if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  1856. String identifier = tokenizer->get_token_identifier();
  1857. if (!ObjectTypeDB::is_type(identifier,"Resource")) {
  1858. current_export=PropertyInfo();
  1859. _set_error("Export hint not a type or resource.");
  1860. }
  1861. current_export.type=Variant::OBJECT;
  1862. current_export.hint=PROPERTY_HINT_RESOURCE_TYPE;
  1863. current_export.hint_string=identifier;
  1864. tokenizer->advance();
  1865. }
  1866. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1867. current_export=PropertyInfo();
  1868. _set_error("Expected ')' or ',' after export hint.");
  1869. return;
  1870. }
  1871. tokenizer->advance();
  1872. }
  1873. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  1874. current_export=PropertyInfo();
  1875. _set_error("Expected 'var'.");
  1876. return;
  1877. }
  1878. }; //fallthrough to var
  1879. case GDTokenizer::TK_PR_VAR: {
  1880. //variale declaration and (eventual) initialization
  1881. ClassNode::Member member;
  1882. bool autoexport = tokenizer->get_token(-1)==GDTokenizer::TK_PR_EXPORT;
  1883. if (current_export.type!=Variant::NIL) {
  1884. member._export=current_export;
  1885. current_export=PropertyInfo();
  1886. }
  1887. tokenizer->advance();
  1888. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1889. _set_error("Expected identifier for member variable name.");
  1890. return;
  1891. }
  1892. member.identifier=tokenizer->get_token_identifier();
  1893. member.expression=NULL;
  1894. member._export.name=member.identifier;
  1895. member.line=tokenizer->get_token_line();
  1896. tokenizer->advance();
  1897. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1898. #ifdef DEBUG_ENABLED
  1899. int line = tokenizer->get_token_line();
  1900. #endif
  1901. tokenizer->advance();
  1902. Node *subexpr=NULL;
  1903. subexpr = _parse_and_reduce_expression(p_class,false);
  1904. if (!subexpr)
  1905. return;
  1906. member.expression=subexpr;
  1907. if (autoexport) {
  1908. if (subexpr->type==Node::TYPE_ARRAY) {
  1909. member._export.type=Variant::ARRAY;
  1910. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  1911. member._export.type=Variant::DICTIONARY;
  1912. } else {
  1913. if (subexpr->type!=Node::TYPE_CONSTANT) {
  1914. _set_error("Type-less export needs a constant expression assigned to infer type.");
  1915. return;
  1916. }
  1917. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  1918. if (cn->value.get_type()==Variant::NIL) {
  1919. _set_error("Can't accept a null constant expression for infering export type.");
  1920. return;
  1921. }
  1922. member._export.type=cn->value.get_type();
  1923. }
  1924. }
  1925. #ifdef TOOLS_ENABLED
  1926. if (subexpr->type==Node::TYPE_CONSTANT && member._export.type!=Variant::NIL) {
  1927. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  1928. if (cn->value.get_type()!=Variant::NIL) {
  1929. member.default_value=cn->value;
  1930. }
  1931. }
  1932. #endif
  1933. IdentifierNode *id = alloc_node<IdentifierNode>();
  1934. id->name=member.identifier;
  1935. OperatorNode *op = alloc_node<OperatorNode>();
  1936. op->op=OperatorNode::OP_INIT_ASSIGN;
  1937. op->arguments.push_back(id);
  1938. op->arguments.push_back(subexpr);
  1939. #ifdef DEBUG_ENABLED
  1940. NewLineNode *nl = alloc_node<NewLineNode>();
  1941. nl->line=line;
  1942. p_class->initializer->statements.push_back(nl);
  1943. #endif
  1944. p_class->initializer->statements.push_back(op);
  1945. } else {
  1946. if (autoexport) {
  1947. _set_error("Type-less export needs a constant expression assigned to infer type.");
  1948. return;
  1949. }
  1950. }
  1951. if (tokenizer->get_token()==GDTokenizer::TK_PR_SETGET) {
  1952. tokenizer->advance();
  1953. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1954. //just comma means using only getter
  1955. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1956. _set_error("Expected identifier for setter function after 'notify'.");
  1957. }
  1958. member.setter=tokenizer->get_token_identifier();
  1959. tokenizer->advance();
  1960. }
  1961. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1962. //there is a getter
  1963. tokenizer->advance();
  1964. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1965. _set_error("Expected identifier for getter function after ','.");
  1966. }
  1967. member.getter=tokenizer->get_token_identifier();
  1968. tokenizer->advance();
  1969. }
  1970. }
  1971. p_class->variables.push_back(member);
  1972. if (!_end_statement()) {
  1973. _set_error("Expected end of statement (continue)");
  1974. return;
  1975. }
  1976. } break;
  1977. case GDTokenizer::TK_PR_CONST: {
  1978. //variale declaration and (eventual) initialization
  1979. ClassNode::Constant constant;
  1980. tokenizer->advance();
  1981. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1982. _set_error("Expected name (identifier) for constant.");
  1983. return;
  1984. }
  1985. constant.identifier=tokenizer->get_token_identifier();
  1986. tokenizer->advance();
  1987. if (tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  1988. _set_error("Constant expects assignment.");
  1989. return;
  1990. }
  1991. tokenizer->advance();
  1992. Node *subexpr=NULL;
  1993. subexpr = _parse_and_reduce_expression(p_class,true,true);
  1994. if (!subexpr)
  1995. return;
  1996. if (subexpr->type!=Node::TYPE_CONSTANT) {
  1997. _set_error("Expected constant expression");
  1998. }
  1999. constant.expression=subexpr;
  2000. p_class->constant_expressions.push_back(constant);
  2001. if (!_end_statement()) {
  2002. _set_error("Expected end of statement (constant)");
  2003. return;
  2004. }
  2005. } break;
  2006. default: {
  2007. _set_error(String()+"Unexpected token: "+tokenizer->get_token_name(tokenizer->get_token())+":"+tokenizer->get_token_identifier());
  2008. return;
  2009. } break;
  2010. }
  2011. }
  2012. }
  2013. void GDParser::_set_error(const String& p_error, int p_line, int p_column) {
  2014. if (error_set)
  2015. return; //allow no further errors
  2016. error=p_error;
  2017. error_line=p_line<0?tokenizer->get_token_line():p_line;
  2018. error_column=p_column<0?tokenizer->get_token_column():p_column;
  2019. error_set=true;
  2020. }
  2021. String GDParser::get_error() const {
  2022. return error;
  2023. }
  2024. int GDParser::get_error_line() const {
  2025. return error_line;
  2026. }
  2027. int GDParser::get_error_column() const {
  2028. return error_column;
  2029. }
  2030. Error GDParser::_parse(const String& p_base_path) {
  2031. base_path=p_base_path;
  2032. clear();
  2033. //assume class
  2034. ClassNode *main_class = alloc_node<ClassNode>();
  2035. main_class->initializer = alloc_node<BlockNode>();
  2036. main_class->initializer->parent_class=main_class;
  2037. current_class=main_class;
  2038. _parse_class(main_class);
  2039. if (tokenizer->get_token()==GDTokenizer::TK_ERROR) {
  2040. error_set=false;
  2041. _set_error("Parse Error: "+tokenizer->get_token_error());
  2042. }
  2043. if (error_set) {
  2044. return ERR_PARSE_ERROR;
  2045. }
  2046. return OK;
  2047. }
  2048. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path, const String &p_self_path) {
  2049. completion_type=COMPLETION_NONE;
  2050. completion_node=NULL;
  2051. completion_class=NULL;
  2052. completion_function=NULL;
  2053. completion_block=NULL;
  2054. current_block=NULL;
  2055. current_class=NULL;
  2056. current_function=NULL;
  2057. self_path=p_self_path;
  2058. GDTokenizerBuffer *tb = memnew( GDTokenizerBuffer );
  2059. tb->set_code_buffer(p_bytecode);
  2060. tokenizer=tb;
  2061. Error ret = _parse(p_base_path);
  2062. memdelete(tb);
  2063. tokenizer=NULL;
  2064. return ret;
  2065. }
  2066. Error GDParser::parse(const String& p_code, const String& p_base_path, bool p_just_validate, const String &p_self_path) {
  2067. completion_type=COMPLETION_NONE;
  2068. completion_node=NULL;
  2069. completion_class=NULL;
  2070. completion_function=NULL;
  2071. completion_block=NULL;
  2072. current_block=NULL;
  2073. current_class=NULL;
  2074. current_function=NULL;
  2075. self_path=p_self_path;
  2076. GDTokenizerText *tt = memnew( GDTokenizerText );
  2077. tt->set_code(p_code);
  2078. validating=p_just_validate;
  2079. tokenizer=tt;
  2080. Error ret = _parse(p_base_path);
  2081. memdelete(tt);
  2082. tokenizer=NULL;
  2083. return ret;
  2084. }
  2085. const GDParser::Node *GDParser::get_parse_tree() const {
  2086. return head;
  2087. }
  2088. void GDParser::clear() {
  2089. while(list) {
  2090. Node *l=list;
  2091. list=list->next;
  2092. memdelete(l);
  2093. }
  2094. head=NULL;
  2095. list=NULL;
  2096. completion_type=COMPLETION_NONE;
  2097. completion_node=NULL;
  2098. completion_class=NULL;
  2099. completion_function=NULL;
  2100. completion_block=NULL;
  2101. current_block=NULL;
  2102. current_class=NULL;
  2103. current_function=NULL;
  2104. validating=false;
  2105. error_set=false;
  2106. tab_level.clear();
  2107. tab_level.push_back(0);
  2108. error_line=0;
  2109. error_column=0;
  2110. pending_newline=-1;
  2111. parenthesis=0;
  2112. current_export.type=Variant::NIL;
  2113. error="";
  2114. }
  2115. GDParser::CompletionType GDParser::get_completion_type() {
  2116. return completion_type;
  2117. }
  2118. StringName GDParser::get_completion_cursor() {
  2119. return completion_cursor;
  2120. }
  2121. int GDParser::get_completion_line() {
  2122. return completion_line;
  2123. }
  2124. Variant::Type GDParser::get_completion_built_in_constant(){
  2125. return completion_built_in_constant;
  2126. }
  2127. GDParser::Node *GDParser::get_completion_node(){
  2128. return completion_node;
  2129. }
  2130. GDParser::BlockNode *GDParser::get_completion_block() {
  2131. return completion_block;
  2132. }
  2133. GDParser::ClassNode *GDParser::get_completion_class(){
  2134. return completion_class;
  2135. }
  2136. GDParser::FunctionNode *GDParser::get_completion_function(){
  2137. return completion_function;
  2138. }
  2139. int GDParser::get_completion_argument_index() {
  2140. return completion_argument;
  2141. }
  2142. GDParser::GDParser() {
  2143. head=NULL;
  2144. list=NULL;
  2145. tokenizer=NULL;
  2146. pending_newline=-1;
  2147. clear();
  2148. }
  2149. GDParser::~GDParser() {
  2150. clear();
  2151. }