gd_parser.cpp 86 KB

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