gd_parser.cpp 96 KB

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