gd_parser.cpp 98 KB

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