gd_parser.cpp 85 KB

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