gd_parser.cpp 101 KB

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