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