gd_parser.cpp 84 KB

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