gd_parser.cpp 91 KB

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