gd_parser.cpp 90 KB

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