gd_parser.cpp 97 KB

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