gd_parser.cpp 85 KB

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