gd_parser.cpp 97 KB

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