as_parser.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_parser.cpp
  25. //
  26. // This class parses the script code and builds a tree for compilation
  27. //
  28. #include "as_config.h"
  29. #include "as_parser.h"
  30. #include "as_tokendef.h"
  31. #include "as_texts.h"
  32. #include "as_debug.h"
  33. #ifdef _MSC_VER
  34. #pragma warning(disable:4702) // unreachable code
  35. #endif
  36. BEGIN_AS_NAMESPACE
  37. asCParser::asCParser(asCBuilder *builder)
  38. {
  39. this->builder = builder;
  40. this->engine = builder->engine;
  41. script = 0;
  42. scriptNode = 0;
  43. checkValidTypes = false;
  44. isParsingAppInterface = false;
  45. }
  46. asCParser::~asCParser()
  47. {
  48. Reset();
  49. }
  50. void asCParser::Reset()
  51. {
  52. errorWhileParsing = false;
  53. isSyntaxError = false;
  54. checkValidTypes = false;
  55. isParsingAppInterface = false;
  56. sourcePos = 0;
  57. if( scriptNode )
  58. {
  59. scriptNode->Destroy(engine);
  60. }
  61. scriptNode = 0;
  62. script = 0;
  63. lastToken.pos = size_t(-1);
  64. }
  65. asCScriptNode *asCParser::GetScriptNode()
  66. {
  67. return scriptNode;
  68. }
  69. int asCParser::ParseFunctionDefinition(asCScriptCode *script, bool expectListPattern)
  70. {
  71. Reset();
  72. // Set flag that permits ? as datatype for parameters
  73. isParsingAppInterface = true;
  74. this->script = script;
  75. scriptNode = ParseFunctionDefinition();
  76. if( expectListPattern )
  77. scriptNode->AddChildLast(ParseListPattern());
  78. // The declaration should end after the definition
  79. if( !isSyntaxError )
  80. {
  81. sToken t;
  82. GetToken(&t);
  83. if( t.type != ttEnd )
  84. {
  85. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEnd)), &t);
  86. return -1;
  87. }
  88. }
  89. if( errorWhileParsing )
  90. return -1;
  91. return 0;
  92. }
  93. asCScriptNode *asCParser::CreateNode(eScriptNode type)
  94. {
  95. void *ptr = engine->memoryMgr.AllocScriptNode();
  96. if( ptr == 0 )
  97. {
  98. // Out of memory
  99. errorWhileParsing = true;
  100. return 0;
  101. }
  102. return new(ptr) asCScriptNode(type);
  103. }
  104. int asCParser::ParseDataType(asCScriptCode *script, bool isReturnType)
  105. {
  106. Reset();
  107. this->script = script;
  108. scriptNode = CreateNode(snDataType);
  109. if( scriptNode == 0 ) return -1;
  110. scriptNode->AddChildLast(ParseType(true));
  111. if( isSyntaxError ) return -1;
  112. if( isReturnType )
  113. {
  114. scriptNode->AddChildLast(ParseTypeMod(false));
  115. if( isSyntaxError ) return -1;
  116. }
  117. // The declaration should end after the type
  118. sToken t;
  119. GetToken(&t);
  120. if( t.type != ttEnd )
  121. {
  122. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEnd)), &t);
  123. return -1;
  124. }
  125. if( errorWhileParsing )
  126. return -1;
  127. return 0;
  128. }
  129. // Parse a template declaration: IDENTIFIER '<' 'class'? IDENTIFIER '>'
  130. int asCParser::ParseTemplateDecl(asCScriptCode *script)
  131. {
  132. Reset();
  133. this->script = script;
  134. scriptNode = CreateNode(snUndefined);
  135. if( scriptNode == 0 ) return -1;
  136. scriptNode->AddChildLast(ParseIdentifier());
  137. if( isSyntaxError ) return -1;
  138. sToken t;
  139. GetToken(&t);
  140. if( t.type != ttLessThan )
  141. {
  142. Error(ExpectedToken(asCTokenizer::GetDefinition(ttLessThan)), &t);
  143. return -1;
  144. }
  145. // The class token is optional
  146. GetToken(&t);
  147. if( t.type != ttClass )
  148. RewindTo(&t);
  149. scriptNode->AddChildLast(ParseIdentifier());
  150. if( isSyntaxError ) return -1;
  151. // There can be multiple sub types
  152. GetToken(&t);
  153. // Parse template types by list separator
  154. while(t.type == ttListSeparator)
  155. {
  156. GetToken(&t);
  157. if( t.type != ttClass )
  158. RewindTo(&t);
  159. scriptNode->AddChildLast(ParseIdentifier());
  160. if( isSyntaxError ) return -1;
  161. GetToken(&t);
  162. }
  163. if( t.type != ttGreaterThan )
  164. {
  165. Error(ExpectedToken(asCTokenizer::GetDefinition(ttGreaterThan)), &t);
  166. return -1;
  167. }
  168. GetToken(&t);
  169. if( t.type != ttEnd )
  170. {
  171. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEnd)), &t);
  172. return -1;
  173. }
  174. if( errorWhileParsing )
  175. return -1;
  176. return 0;
  177. }
  178. int asCParser::ParsePropertyDeclaration(asCScriptCode *script)
  179. {
  180. Reset();
  181. this->script = script;
  182. scriptNode = CreateNode(snDeclaration);
  183. if( scriptNode == 0 ) return -1;
  184. scriptNode->AddChildLast(ParseType(true));
  185. if( isSyntaxError ) return -1;
  186. // Allow optional namespace to be defined before the identifier in case
  187. // the declaration is to be used for searching for an existing property
  188. ParseOptionalScope(scriptNode);
  189. scriptNode->AddChildLast(ParseIdentifier());
  190. if( isSyntaxError ) return -1;
  191. // The declaration should end after the identifier
  192. sToken t;
  193. GetToken(&t);
  194. if( t.type != ttEnd )
  195. {
  196. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEnd)), &t);
  197. return -1;
  198. }
  199. return 0;
  200. }
  201. void asCParser::ParseOptionalScope(asCScriptNode *node)
  202. {
  203. sToken t1, t2;
  204. GetToken(&t1);
  205. GetToken(&t2);
  206. if( t1.type == ttScope )
  207. {
  208. RewindTo(&t1);
  209. node->AddChildLast(ParseToken(ttScope));
  210. GetToken(&t1);
  211. GetToken(&t2);
  212. }
  213. while( t1.type == ttIdentifier && t2.type == ttScope )
  214. {
  215. RewindTo(&t1);
  216. node->AddChildLast(ParseIdentifier());
  217. node->AddChildLast(ParseToken(ttScope));
  218. GetToken(&t1);
  219. GetToken(&t2);
  220. }
  221. RewindTo(&t1);
  222. }
  223. asCScriptNode *asCParser::ParseFunctionDefinition()
  224. {
  225. asCScriptNode *node = CreateNode(snFunction);
  226. if( node == 0 ) return 0;
  227. node->AddChildLast(ParseType(true));
  228. if( isSyntaxError ) return node;
  229. node->AddChildLast(ParseTypeMod(false));
  230. if( isSyntaxError ) return node;
  231. ParseOptionalScope(node);
  232. node->AddChildLast(ParseIdentifier());
  233. if( isSyntaxError ) return node;
  234. node->AddChildLast(ParseParameterList());
  235. if( isSyntaxError ) return node;
  236. // Parse an optional const after the function definition (used for object methods)
  237. sToken t1;
  238. GetToken(&t1);
  239. RewindTo(&t1);
  240. if( t1.type == ttConst )
  241. node->AddChildLast(ParseToken(ttConst));
  242. return node;
  243. }
  244. asCScriptNode *asCParser::ParseTypeMod(bool isParam)
  245. {
  246. asCScriptNode *node = CreateNode(snDataType);
  247. if( node == 0 ) return 0;
  248. sToken t;
  249. // Parse possible & token
  250. GetToken(&t);
  251. RewindTo(&t);
  252. if( t.type == ttAmp )
  253. {
  254. node->AddChildLast(ParseToken(ttAmp));
  255. if( isSyntaxError ) return node;
  256. if( isParam )
  257. {
  258. GetToken(&t);
  259. RewindTo(&t);
  260. if( t.type == ttIn || t.type == ttOut || t.type == ttInOut )
  261. {
  262. int tokens[3] = {ttIn, ttOut, ttInOut};
  263. node->AddChildLast(ParseOneOf(tokens, 3));
  264. }
  265. }
  266. }
  267. // Parse possible + token
  268. GetToken(&t);
  269. RewindTo(&t);
  270. if( t.type == ttPlus )
  271. {
  272. node->AddChildLast(ParseToken(ttPlus));
  273. if( isSyntaxError ) return node;
  274. }
  275. return node;
  276. }
  277. asCScriptNode *asCParser::ParseType(bool allowConst, bool allowVariableType)
  278. {
  279. asCScriptNode *node = CreateNode(snDataType);
  280. if( node == 0 ) return 0;
  281. sToken t;
  282. if( allowConst )
  283. {
  284. GetToken(&t);
  285. RewindTo(&t);
  286. if( t.type == ttConst )
  287. {
  288. node->AddChildLast(ParseToken(ttConst));
  289. if( isSyntaxError ) return node;
  290. }
  291. }
  292. // Parse scope prefix
  293. ParseOptionalScope(node);
  294. // Parse the actual type
  295. node->AddChildLast(ParseDataType(allowVariableType));
  296. // If the datatype is a template type, then parse the subtype within the < >
  297. asCScriptNode *type = node->lastChild;
  298. tempString.Assign(&script->code[type->tokenPos], type->tokenLength);
  299. if( engine->IsTemplateType(tempString.AddressOf()) )
  300. {
  301. GetToken(&t);
  302. if( t.type != ttLessThan )
  303. {
  304. Error(ExpectedToken(asCTokenizer::GetDefinition(ttLessThan)), &t);
  305. return node;
  306. }
  307. node->AddChildLast(ParseType(true, false));
  308. if( isSyntaxError ) return node;
  309. GetToken(&t);
  310. // Parse template types by list separator
  311. while(t.type == ttListSeparator)
  312. {
  313. node->AddChildLast(ParseType(true, false));
  314. if( isSyntaxError ) return node;
  315. GetToken(&t);
  316. }
  317. // Accept >> and >>> tokens too. But then force the tokenizer to move
  318. // only 1 character ahead (thus splitting the token in two).
  319. if( script->code[t.pos] != '>' )
  320. {
  321. Error(ExpectedToken(asCTokenizer::GetDefinition(ttGreaterThan)), &t);
  322. return node;
  323. }
  324. else
  325. {
  326. // Break the token so that only the first > is parsed
  327. SetPos(t.pos + 1);
  328. }
  329. }
  330. // Parse [] and @
  331. GetToken(&t);
  332. RewindTo(&t);
  333. while( t.type == ttOpenBracket || t.type == ttHandle)
  334. {
  335. if( t.type == ttOpenBracket )
  336. {
  337. node->AddChildLast(ParseToken(ttOpenBracket));
  338. if( isSyntaxError ) return node;
  339. GetToken(&t);
  340. if( t.type != ttCloseBracket )
  341. {
  342. Error(ExpectedToken("]"), &t);
  343. return node;
  344. }
  345. }
  346. else
  347. {
  348. node->AddChildLast(ParseToken(ttHandle));
  349. if( isSyntaxError ) return node;
  350. }
  351. GetToken(&t);
  352. RewindTo(&t);
  353. }
  354. return node;
  355. }
  356. asCScriptNode *asCParser::ParseToken(int token)
  357. {
  358. asCScriptNode *node = CreateNode(snUndefined);
  359. if( node == 0 ) return 0;
  360. sToken t1;
  361. GetToken(&t1);
  362. if( t1.type != token )
  363. {
  364. Error(ExpectedToken(asCTokenizer::GetDefinition(token)), &t1);
  365. return node;
  366. }
  367. node->SetToken(&t1);
  368. node->UpdateSourcePos(t1.pos, t1.length);
  369. return node;
  370. }
  371. asCScriptNode *asCParser::ParseOneOf(int *tokens, int count)
  372. {
  373. asCScriptNode *node = CreateNode(snUndefined);
  374. if( node == 0 ) return 0;
  375. sToken t1;
  376. GetToken(&t1);
  377. int n;
  378. for( n = 0; n < count; n++ )
  379. {
  380. if( tokens[n] == t1.type )
  381. break;
  382. }
  383. if( n == count )
  384. {
  385. Error(ExpectedOneOf(tokens, count), &t1);
  386. return node;
  387. }
  388. node->SetToken(&t1);
  389. node->UpdateSourcePos(t1.pos, t1.length);
  390. return node;
  391. }
  392. asCScriptNode *asCParser::ParseDataType(bool allowVariableType)
  393. {
  394. asCScriptNode *node = CreateNode(snDataType);
  395. if( node == 0 ) return 0;
  396. sToken t1;
  397. GetToken(&t1);
  398. if( !IsDataType(t1) && !(allowVariableType && t1.type == ttQuestion) )
  399. {
  400. if( t1.type == ttIdentifier )
  401. {
  402. asCString errMsg;
  403. tempString.Assign(&script->code[t1.pos], t1.length);
  404. errMsg.Format(TXT_IDENTIFIER_s_NOT_DATA_TYPE, tempString.AddressOf());
  405. Error(errMsg, &t1);
  406. }
  407. else
  408. Error(TXT_EXPECTED_DATA_TYPE, &t1);
  409. return node;
  410. }
  411. node->SetToken(&t1);
  412. node->UpdateSourcePos(t1.pos, t1.length);
  413. return node;
  414. }
  415. asCScriptNode *asCParser::ParseRealType()
  416. {
  417. asCScriptNode *node = CreateNode(snDataType);
  418. if( node == 0 ) return 0;
  419. sToken t1;
  420. GetToken(&t1);
  421. if( !IsRealType(t1.type) )
  422. {
  423. Error(TXT_EXPECTED_DATA_TYPE, &t1);
  424. return node;
  425. }
  426. node->SetToken(&t1);
  427. node->UpdateSourcePos(t1.pos, t1.length);
  428. return node;
  429. }
  430. asCScriptNode *asCParser::ParseIdentifier()
  431. {
  432. asCScriptNode *node = CreateNode(snIdentifier);
  433. if( node == 0 ) return 0;
  434. sToken t1;
  435. GetToken(&t1);
  436. if( t1.type != ttIdentifier )
  437. {
  438. Error(TXT_EXPECTED_IDENTIFIER, &t1);
  439. return node;
  440. }
  441. node->SetToken(&t1);
  442. node->UpdateSourcePos(t1.pos, t1.length);
  443. return node;
  444. }
  445. asCScriptNode *asCParser::ParseParameterList()
  446. {
  447. asCScriptNode *node = CreateNode(snParameterList);
  448. if( node == 0 ) return 0;
  449. sToken t1;
  450. GetToken(&t1);
  451. if( t1.type != ttOpenParanthesis )
  452. {
  453. Error(ExpectedToken("("), &t1);
  454. return node;
  455. }
  456. node->UpdateSourcePos(t1.pos, t1.length);
  457. GetToken(&t1);
  458. if( t1.type == ttCloseParanthesis )
  459. {
  460. node->UpdateSourcePos(t1.pos, t1.length);
  461. // Statement block is finished
  462. return node;
  463. }
  464. else
  465. {
  466. // If the parameter list is just (void) then the void token should be ignored
  467. if( t1.type == ttVoid )
  468. {
  469. sToken t2;
  470. GetToken(&t2);
  471. if( t2.type == ttCloseParanthesis )
  472. {
  473. node->UpdateSourcePos(t2.pos, t2.length);
  474. return node;
  475. }
  476. }
  477. RewindTo(&t1);
  478. for(;;)
  479. {
  480. // Parse data type
  481. node->AddChildLast(ParseType(true, isParsingAppInterface));
  482. if( isSyntaxError ) return node;
  483. node->AddChildLast(ParseTypeMod(true));
  484. if( isSyntaxError ) return node;
  485. // Parse optional identifier
  486. GetToken(&t1);
  487. if( t1.type == ttIdentifier )
  488. {
  489. RewindTo(&t1);
  490. node->AddChildLast(ParseIdentifier());
  491. if( isSyntaxError ) return node;
  492. GetToken(&t1);
  493. }
  494. // Parse optional expression for the default arg
  495. if( t1.type == ttAssignment )
  496. {
  497. // Do a superficial parsing of the default argument
  498. // The actual parsing will be done when the argument is compiled for a function call
  499. node->AddChildLast(SuperficiallyParseExpression());
  500. if( isSyntaxError ) return node;
  501. GetToken(&t1);
  502. }
  503. // Check if list continues
  504. if( t1.type == ttCloseParanthesis )
  505. {
  506. node->UpdateSourcePos(t1.pos, t1.length);
  507. return node;
  508. }
  509. else if( t1.type == ttListSeparator )
  510. continue;
  511. else
  512. {
  513. Error(ExpectedTokens(")", ","), &t1);
  514. return node;
  515. }
  516. }
  517. }
  518. UNREACHABLE_RETURN;
  519. }
  520. asCScriptNode *asCParser::SuperficiallyParseExpression()
  521. {
  522. asCScriptNode *node = CreateNode(snExpression);
  523. if( node == 0 ) return 0;
  524. // Simply parse everything until the first , or ), whichever comes first.
  525. // Keeping in mind that () and {} can group expressions.
  526. sToken start;
  527. GetToken(&start);
  528. RewindTo(&start);
  529. asCString stack;
  530. sToken t;
  531. for(;;)
  532. {
  533. GetToken(&t);
  534. if( t.type == ttOpenParanthesis )
  535. stack += "(";
  536. else if( t.type == ttCloseParanthesis )
  537. {
  538. if( stack == "" )
  539. {
  540. // Expression has ended. This token is not part of expression
  541. RewindTo(&t);
  542. break;
  543. }
  544. else if( stack[stack.GetLength()-1] == '(' )
  545. {
  546. // Group has ended
  547. stack.SetLength(stack.GetLength()-1);
  548. }
  549. else
  550. {
  551. // Wrong syntax
  552. RewindTo(&t);
  553. asCString str;
  554. str.Format(TXT_UNEXPECTED_TOKEN_s, ")");
  555. Error(str, &t);
  556. return node;
  557. }
  558. }
  559. else if( t.type == ttListSeparator )
  560. {
  561. if( stack == "" )
  562. {
  563. // Expression has ended. This token is not part of expression
  564. RewindTo(&t);
  565. break;
  566. }
  567. }
  568. else if( t.type == ttStartStatementBlock )
  569. stack += "{";
  570. else if( t.type == ttEndStatementBlock )
  571. {
  572. if( stack == "" || stack[stack.GetLength()-1] != '{' )
  573. {
  574. // Wrong syntax
  575. RewindTo(&t);
  576. asCString str;
  577. str.Format(TXT_UNEXPECTED_TOKEN_s, "}");
  578. Error(str, &t);
  579. return node;
  580. }
  581. else
  582. {
  583. // Group has ended
  584. stack.SetLength(stack.GetLength()-1);
  585. }
  586. }
  587. else if( t.type == ttEndStatement )
  588. {
  589. // Wrong syntax (since we're parsing a default arg expression)
  590. RewindTo(&t);
  591. asCString str;
  592. str.Format(TXT_UNEXPECTED_TOKEN_s, ";");
  593. Error(str, &t);
  594. return node;
  595. }
  596. else if( t.type == ttNonTerminatedStringConstant )
  597. {
  598. RewindTo(&t);
  599. Error(TXT_NONTERMINATED_STRING, &t);
  600. return node;
  601. }
  602. else if( t.type == ttEnd )
  603. {
  604. // Wrong syntax
  605. RewindTo(&t);
  606. Error(TXT_UNEXPECTED_END_OF_FILE, &t);
  607. Info(TXT_WHILE_PARSING_EXPRESSION, &start);
  608. return node;
  609. }
  610. // Include the token in the node
  611. node->UpdateSourcePos(t.pos, t.length);
  612. }
  613. return node;
  614. }
  615. void asCParser::GetToken(sToken *token)
  616. {
  617. // Check if the token has already been parsed
  618. if( lastToken.pos == sourcePos )
  619. {
  620. *token = lastToken;
  621. sourcePos += token->length;
  622. if( token->type == ttWhiteSpace ||
  623. token->type == ttOnelineComment ||
  624. token->type == ttMultilineComment )
  625. GetToken(token);
  626. return;
  627. }
  628. // Parse new token
  629. size_t sourceLength = script->codeLength;
  630. do
  631. {
  632. if( sourcePos >= sourceLength )
  633. {
  634. token->type = ttEnd;
  635. token->length = 0;
  636. }
  637. else
  638. token->type = engine->tok.GetToken(&script->code[sourcePos], sourceLength - sourcePos, &token->length);
  639. token->pos = sourcePos;
  640. // Update state
  641. sourcePos += token->length;
  642. }
  643. // Filter out whitespace and comments
  644. while( token->type == ttWhiteSpace ||
  645. token->type == ttOnelineComment ||
  646. token->type == ttMultilineComment );
  647. }
  648. void asCParser::SetPos(size_t pos)
  649. {
  650. lastToken.pos = size_t(-1);
  651. sourcePos = pos;
  652. }
  653. void asCParser::RewindTo(const sToken *token)
  654. {
  655. // TODO: optimize: Perhaps we can optimize this further by having the parser
  656. // set an explicit return point, after which each token will
  657. // be stored. That way not just one token will be reused but
  658. // no token will have to be tokenized more than once.
  659. // Store the token so it doesn't have to be tokenized again
  660. lastToken = *token;
  661. sourcePos = token->pos;
  662. }
  663. void asCParser::Error(const asCString &text, sToken *token)
  664. {
  665. RewindTo(token);
  666. isSyntaxError = true;
  667. errorWhileParsing = true;
  668. int row, col;
  669. script->ConvertPosToRowCol(token->pos, &row, &col);
  670. if( builder )
  671. builder->WriteError(script->name, text, row, col);
  672. }
  673. void asCParser::Info(const asCString &text, sToken *token)
  674. {
  675. RewindTo(token);
  676. isSyntaxError = true;
  677. errorWhileParsing = true;
  678. int row, col;
  679. script->ConvertPosToRowCol(token->pos, &row, &col);
  680. if( builder )
  681. builder->WriteInfo(script->name, text, row, col, false);
  682. }
  683. bool asCParser::IsRealType(int tokenType)
  684. {
  685. if( tokenType == ttVoid ||
  686. tokenType == ttInt ||
  687. tokenType == ttInt8 ||
  688. tokenType == ttInt16 ||
  689. tokenType == ttInt64 ||
  690. tokenType == ttUInt ||
  691. tokenType == ttUInt8 ||
  692. tokenType == ttUInt16 ||
  693. tokenType == ttUInt64 ||
  694. tokenType == ttFloat ||
  695. tokenType == ttBool ||
  696. tokenType == ttDouble )
  697. return true;
  698. return false;
  699. }
  700. bool asCParser::IsDataType(const sToken &token)
  701. {
  702. if( token.type == ttIdentifier )
  703. {
  704. if( checkValidTypes )
  705. {
  706. // Check if this is an existing type, regardless of namespace
  707. tempString.Assign(&script->code[token.pos], token.length);
  708. if( !builder->DoesTypeExist(tempString.AddressOf()) )
  709. return false;
  710. }
  711. return true;
  712. }
  713. if( IsRealType(token.type) )
  714. return true;
  715. return false;
  716. }
  717. asCString asCParser::ExpectedToken(const char *token)
  718. {
  719. asCString str;
  720. str.Format(TXT_EXPECTED_s, token);
  721. return str;
  722. }
  723. asCString asCParser::ExpectedTokens(const char *t1, const char *t2)
  724. {
  725. asCString str;
  726. str.Format(TXT_EXPECTED_s_OR_s, t1, t2);
  727. return str;
  728. }
  729. asCString asCParser::ExpectedOneOf(int *tokens, int count)
  730. {
  731. asCString str;
  732. str = TXT_EXPECTED_ONE_OF;
  733. for( int n = 0; n < count; n++ )
  734. {
  735. str += asCTokenizer::GetDefinition(tokens[n]);
  736. if( n < count-1 )
  737. str += ", ";
  738. }
  739. return str;
  740. }
  741. asCString asCParser::ExpectedOneOf(const char **tokens, int count)
  742. {
  743. asCString str;
  744. str = TXT_EXPECTED_ONE_OF;
  745. for( int n = 0; n < count; n++ )
  746. {
  747. str += tokens[n];
  748. if( n < count-1 )
  749. str += ", ";
  750. }
  751. return str;
  752. }
  753. asCScriptNode *asCParser::ParseListPattern()
  754. {
  755. asCScriptNode *node = CreateNode(snListPattern);
  756. if( node == 0 ) return 0;
  757. sToken t1;
  758. GetToken(&t1);
  759. if( t1.type != ttStartStatementBlock )
  760. {
  761. Error(ExpectedToken("{"), &t1);
  762. return node;
  763. }
  764. node->UpdateSourcePos(t1.pos, t1.length);
  765. sToken start = t1;
  766. bool isBeginning = true;
  767. bool afterType = false;
  768. while( !isSyntaxError )
  769. {
  770. GetToken(&t1);
  771. if( t1.type == ttEndStatementBlock )
  772. {
  773. if( !afterType )
  774. Error(TXT_EXPECTED_DATA_TYPE, &t1);
  775. break;
  776. }
  777. else if( t1.type == ttStartStatementBlock )
  778. {
  779. if( afterType )
  780. {
  781. asCString msg;
  782. msg.Format(TXT_EXPECTED_s_OR_s, ",", "}");
  783. Error(msg.AddressOf(), &t1);
  784. }
  785. RewindTo(&t1);
  786. node->AddChildLast(ParseListPattern());
  787. afterType = true;
  788. }
  789. else if( t1.type == ttIdentifier && IdentifierIs(t1, "repeat") )
  790. {
  791. if( !isBeginning )
  792. {
  793. asCString msg;
  794. msg.Format(TXT_UNEXPECTED_TOKEN_s, "repeat");
  795. Error(msg.AddressOf(), &t1);
  796. }
  797. RewindTo(&t1);
  798. node->AddChildLast(ParseIdentifier());
  799. }
  800. else if( t1.type == ttEnd )
  801. {
  802. Error(TXT_UNEXPECTED_END_OF_FILE, &t1);
  803. Info(TXT_WHILE_PARSING_STATEMENT_BLOCK, &start);
  804. break;
  805. }
  806. else if( t1.type == ttListSeparator )
  807. {
  808. if( !afterType )
  809. Error(TXT_EXPECTED_DATA_TYPE, &t1);
  810. afterType = false;
  811. }
  812. else
  813. {
  814. if( afterType )
  815. {
  816. asCString msg;
  817. msg.Format(TXT_EXPECTED_s_OR_s, ",", "}");
  818. Error(msg.AddressOf(), &t1);
  819. }
  820. RewindTo(&t1);
  821. node->AddChildLast(ParseType(true, true));
  822. afterType = true;
  823. }
  824. isBeginning = false;
  825. }
  826. node->UpdateSourcePos(t1.pos, t1.length);
  827. return node;
  828. }
  829. bool asCParser::IdentifierIs(const sToken &t, const char *str)
  830. {
  831. if( t.type != ttIdentifier )
  832. return false;
  833. return script->TokenEquals(t.pos, t.length, str);
  834. }
  835. #ifndef AS_NO_COMPILER
  836. bool asCParser::CheckTemplateType(sToken &t)
  837. {
  838. // Is this a template type?
  839. tempString.Assign(&script->code[t.pos], t.length);
  840. if( engine->IsTemplateType(tempString.AddressOf()) )
  841. {
  842. // Expect the sub type within < >
  843. GetToken(&t);
  844. if( t.type != ttLessThan )
  845. return false;
  846. for(;;)
  847. {
  848. // There might optionally be a 'const'
  849. GetToken(&t);
  850. if( t.type == ttConst )
  851. GetToken(&t);
  852. // The type may be initiated with the scope operator
  853. if( t.type == ttScope )
  854. GetToken(&t);
  855. // There may be multiple levels of scope operators
  856. sToken t2;
  857. GetToken(&t2);
  858. while( t.type == ttIdentifier && t2.type == ttScope )
  859. {
  860. GetToken(&t);
  861. GetToken(&t2);
  862. }
  863. RewindTo(&t2);
  864. // Now there must be a data type
  865. if( !IsDataType(t) )
  866. return false;
  867. if( !CheckTemplateType(t) )
  868. return false;
  869. GetToken(&t);
  870. // Is it a handle or array?
  871. while( t.type == ttHandle || t.type == ttOpenBracket )
  872. {
  873. if( t.type == ttOpenBracket )
  874. {
  875. GetToken(&t);
  876. if( t.type != ttCloseBracket )
  877. return false;
  878. }
  879. GetToken(&t);
  880. }
  881. // Was this the last template subtype?
  882. if( t.type != ttListSeparator )
  883. break;
  884. }
  885. // Accept >> and >>> tokens too. But then force the tokenizer to move
  886. // only 1 character ahead (thus splitting the token in two).
  887. if( script->code[t.pos] != '>' )
  888. return false;
  889. else if( t.length != 1 )
  890. {
  891. // We need to break the token, so that only the first character is parsed
  892. SetPos(t.pos + 1);
  893. }
  894. }
  895. return true;
  896. }
  897. asCScriptNode *asCParser::ParseCast()
  898. {
  899. asCScriptNode *node = CreateNode(snCast);
  900. if( node == 0 ) return 0;
  901. sToken t1;
  902. GetToken(&t1);
  903. if( t1.type != ttCast )
  904. {
  905. Error(ExpectedToken("cast"), &t1);
  906. return node;
  907. }
  908. node->UpdateSourcePos(t1.pos, t1.length);
  909. GetToken(&t1);
  910. if( t1.type != ttLessThan )
  911. {
  912. Error(ExpectedToken("<"), &t1);
  913. return node;
  914. }
  915. // Parse the data type
  916. node->AddChildLast(ParseType(true));
  917. if( isSyntaxError ) return node;
  918. node->AddChildLast(ParseTypeMod(false));
  919. if( isSyntaxError ) return node;
  920. GetToken(&t1);
  921. if( t1.type != ttGreaterThan )
  922. {
  923. Error(ExpectedToken(">"), &t1);
  924. return node;
  925. }
  926. GetToken(&t1);
  927. if( t1.type != ttOpenParanthesis )
  928. {
  929. Error(ExpectedToken("("), &t1);
  930. return node;
  931. }
  932. node->AddChildLast(ParseAssignment());
  933. if( isSyntaxError ) return node;
  934. GetToken(&t1);
  935. if( t1.type != ttCloseParanthesis )
  936. {
  937. Error(ExpectedToken(")"), &t1);
  938. return node;
  939. }
  940. node->UpdateSourcePos(t1.pos, t1.length);
  941. return node;
  942. }
  943. asCScriptNode *asCParser::ParseExprValue()
  944. {
  945. asCScriptNode *node = CreateNode(snExprValue);
  946. if( node == 0 ) return 0;
  947. sToken t1, t2;
  948. GetToken(&t1);
  949. GetToken(&t2);
  950. RewindTo(&t1);
  951. // 'void' is a special expression that doesn't do anything (normally used for skipping output arguments)
  952. if( t1.type == ttVoid )
  953. node->AddChildLast(ParseToken(ttVoid));
  954. else if( IsRealType(t1.type) )
  955. node->AddChildLast(ParseConstructCall());
  956. else if( t1.type == ttIdentifier || t1.type == ttScope )
  957. {
  958. // Determine the last identifier in order to check if it is a type
  959. sToken t;
  960. if( t1.type == ttScope ) t = t2; else t = t1;
  961. RewindTo(&t);
  962. GetToken(&t2);
  963. while( t.type == ttIdentifier )
  964. {
  965. t2 = t;
  966. GetToken(&t);
  967. if( t.type == ttScope )
  968. GetToken(&t);
  969. else
  970. break;
  971. }
  972. // Rewind so the real parsing can be done, after deciding what to parse
  973. RewindTo(&t1);
  974. // Check if this is a construct call
  975. if( IsDataType(t2) && (t.type == ttOpenParanthesis ||
  976. t.type == ttLessThan ||
  977. t.type == ttOpenBracket) )
  978. node->AddChildLast(ParseConstructCall());
  979. else if( IsFunctionCall() )
  980. node->AddChildLast(ParseFunctionCall());
  981. else
  982. node->AddChildLast(ParseVariableAccess());
  983. }
  984. else if( t1.type == ttCast )
  985. node->AddChildLast(ParseCast());
  986. else if( IsConstant(t1.type) )
  987. node->AddChildLast(ParseConstant());
  988. else if( t1.type == ttOpenParanthesis )
  989. {
  990. GetToken(&t1);
  991. node->UpdateSourcePos(t1.pos, t1.length);
  992. node->AddChildLast(ParseAssignment());
  993. if( isSyntaxError ) return node;
  994. GetToken(&t1);
  995. if( t1.type != ttCloseParanthesis )
  996. Error(ExpectedToken(")"), &t1);
  997. node->UpdateSourcePos(t1.pos, t1.length);
  998. }
  999. else
  1000. Error(TXT_EXPECTED_EXPRESSION_VALUE, &t1);
  1001. return node;
  1002. }
  1003. asCScriptNode *asCParser::ParseConstant()
  1004. {
  1005. asCScriptNode *node = CreateNode(snConstant);
  1006. if( node == 0 ) return 0;
  1007. sToken t;
  1008. GetToken(&t);
  1009. if( !IsConstant(t.type) )
  1010. {
  1011. Error(TXT_EXPECTED_CONSTANT, &t);
  1012. return node;
  1013. }
  1014. node->SetToken(&t);
  1015. node->UpdateSourcePos(t.pos, t.length);
  1016. // We want to gather a list of string constants to concatenate as children
  1017. if( t.type == ttStringConstant || t.type == ttMultilineStringConstant || t.type == ttHeredocStringConstant )
  1018. RewindTo(&t);
  1019. while( t.type == ttStringConstant || t.type == ttMultilineStringConstant || t.type == ttHeredocStringConstant )
  1020. {
  1021. node->AddChildLast(ParseStringConstant());
  1022. GetToken(&t);
  1023. RewindTo(&t);
  1024. }
  1025. return node;
  1026. }
  1027. asCScriptNode *asCParser::ParseStringConstant()
  1028. {
  1029. asCScriptNode *node = CreateNode(snConstant);
  1030. if( node == 0 ) return 0;
  1031. sToken t;
  1032. GetToken(&t);
  1033. if( t.type != ttStringConstant && t.type != ttMultilineStringConstant && t.type != ttHeredocStringConstant )
  1034. {
  1035. Error(TXT_EXPECTED_STRING, &t);
  1036. return node;
  1037. }
  1038. node->SetToken(&t);
  1039. node->UpdateSourcePos(t.pos, t.length);
  1040. return node;
  1041. }
  1042. asCScriptNode *asCParser::ParseFunctionCall()
  1043. {
  1044. asCScriptNode *node = CreateNode(snFunctionCall);
  1045. if( node == 0 ) return 0;
  1046. // Parse scope prefix
  1047. ParseOptionalScope(node);
  1048. // Parse the function name followed by the argument list
  1049. node->AddChildLast(ParseIdentifier());
  1050. if( isSyntaxError ) return node;
  1051. node->AddChildLast(ParseArgList());
  1052. return node;
  1053. }
  1054. asCScriptNode *asCParser::ParseVariableAccess()
  1055. {
  1056. asCScriptNode *node = CreateNode(snVariableAccess);
  1057. if( node == 0 ) return 0;
  1058. // Parse scope prefix
  1059. ParseOptionalScope(node);
  1060. // Parse the variable name
  1061. node->AddChildLast(ParseIdentifier());
  1062. return node;
  1063. }
  1064. asCScriptNode *asCParser::ParseConstructCall()
  1065. {
  1066. asCScriptNode *node = CreateNode(snConstructCall);
  1067. if( node == 0 ) return 0;
  1068. node->AddChildLast(ParseType(false));
  1069. if( isSyntaxError ) return node;
  1070. node->AddChildLast(ParseArgList());
  1071. return node;
  1072. }
  1073. asCScriptNode *asCParser::ParseArgList()
  1074. {
  1075. asCScriptNode *node = CreateNode(snArgList);
  1076. if( node == 0 ) return 0;
  1077. sToken t1;
  1078. GetToken(&t1);
  1079. if( t1.type != ttOpenParanthesis )
  1080. {
  1081. Error(ExpectedToken("("), &t1);
  1082. return node;
  1083. }
  1084. node->UpdateSourcePos(t1.pos, t1.length);
  1085. GetToken(&t1);
  1086. if( t1.type == ttCloseParanthesis )
  1087. {
  1088. node->UpdateSourcePos(t1.pos, t1.length);
  1089. // Statement block is finished
  1090. return node;
  1091. }
  1092. else
  1093. {
  1094. RewindTo(&t1);
  1095. for(;;)
  1096. {
  1097. node->AddChildLast(ParseAssignment());
  1098. if( isSyntaxError ) return node;
  1099. // Check if list continues
  1100. GetToken(&t1);
  1101. if( t1.type == ttCloseParanthesis )
  1102. {
  1103. node->UpdateSourcePos(t1.pos, t1.length);
  1104. return node;
  1105. }
  1106. else if( t1.type == ttListSeparator )
  1107. continue;
  1108. else
  1109. {
  1110. Error(ExpectedTokens(")", ","), &t1);
  1111. return node;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. bool asCParser::IsFunctionCall()
  1117. {
  1118. sToken s;
  1119. sToken t1, t2;
  1120. GetToken(&s);
  1121. t1 = s;
  1122. // A function call may be prefixed with scope resolution
  1123. if( t1.type == ttScope )
  1124. GetToken(&t1);
  1125. GetToken(&t2);
  1126. while( t1.type == ttIdentifier && t2.type == ttScope )
  1127. {
  1128. GetToken(&t1);
  1129. GetToken(&t2);
  1130. }
  1131. // A function call starts with an identifier followed by an argument list
  1132. if( t1.type != ttIdentifier || IsDataType(t1) )
  1133. {
  1134. RewindTo(&s);
  1135. return false;
  1136. }
  1137. if( t2.type == ttOpenParanthesis )
  1138. {
  1139. RewindTo(&s);
  1140. return true;
  1141. }
  1142. RewindTo(&s);
  1143. return false;
  1144. }
  1145. asCScriptNode *asCParser::ParseAssignment()
  1146. {
  1147. asCScriptNode *node = CreateNode(snAssignment);
  1148. if( node == 0 ) return 0;
  1149. node->AddChildLast(ParseCondition());
  1150. if( isSyntaxError ) return node;
  1151. sToken t;
  1152. GetToken(&t);
  1153. RewindTo(&t);
  1154. if( IsAssignOperator(t.type) )
  1155. {
  1156. node->AddChildLast(ParseAssignOperator());
  1157. if( isSyntaxError ) return node;
  1158. node->AddChildLast(ParseAssignment());
  1159. if( isSyntaxError ) return node;
  1160. }
  1161. return node;
  1162. }
  1163. asCScriptNode *asCParser::ParseCondition()
  1164. {
  1165. asCScriptNode *node = CreateNode(snCondition);
  1166. if( node == 0 ) return 0;
  1167. node->AddChildLast(ParseExpression());
  1168. if( isSyntaxError ) return node;
  1169. sToken t;
  1170. GetToken(&t);
  1171. if( t.type == ttQuestion )
  1172. {
  1173. node->AddChildLast(ParseAssignment());
  1174. if( isSyntaxError ) return node;
  1175. GetToken(&t);
  1176. if( t.type != ttColon )
  1177. {
  1178. Error(ExpectedToken(":"), &t);
  1179. return node;
  1180. }
  1181. node->AddChildLast(ParseAssignment());
  1182. if( isSyntaxError ) return node;
  1183. }
  1184. else
  1185. RewindTo(&t);
  1186. return node;
  1187. }
  1188. asCScriptNode *asCParser::ParseExpression()
  1189. {
  1190. asCScriptNode *node = CreateNode(snExpression);
  1191. if( node == 0 ) return 0;
  1192. node->AddChildLast(ParseExprTerm());
  1193. if( isSyntaxError ) return node;
  1194. for(;;)
  1195. {
  1196. sToken t;
  1197. GetToken(&t);
  1198. RewindTo(&t);
  1199. if( !IsOperator(t.type) )
  1200. return node;
  1201. node->AddChildLast(ParseExprOperator());
  1202. if( isSyntaxError ) return node;
  1203. node->AddChildLast(ParseExprTerm());
  1204. if( isSyntaxError ) return node;
  1205. }
  1206. UNREACHABLE_RETURN;
  1207. }
  1208. asCScriptNode *asCParser::ParseExprTerm()
  1209. {
  1210. asCScriptNode *node = CreateNode(snExprTerm);
  1211. if( node == 0 ) return 0;
  1212. for(;;)
  1213. {
  1214. sToken t;
  1215. GetToken(&t);
  1216. RewindTo(&t);
  1217. if( !IsPreOperator(t.type) )
  1218. break;
  1219. node->AddChildLast(ParseExprPreOp());
  1220. if( isSyntaxError ) return node;
  1221. }
  1222. node->AddChildLast(ParseExprValue());
  1223. if( isSyntaxError ) return node;
  1224. for(;;)
  1225. {
  1226. sToken t;
  1227. GetToken(&t);
  1228. RewindTo(&t);
  1229. if( !IsPostOperator(t.type) )
  1230. return node;
  1231. node->AddChildLast(ParseExprPostOp());
  1232. if( isSyntaxError ) return node;
  1233. }
  1234. UNREACHABLE_RETURN;
  1235. }
  1236. asCScriptNode *asCParser::ParseExprPreOp()
  1237. {
  1238. asCScriptNode *node = CreateNode(snExprPreOp);
  1239. if( node == 0 ) return 0;
  1240. sToken t;
  1241. GetToken(&t);
  1242. if( !IsPreOperator(t.type) )
  1243. {
  1244. Error(TXT_EXPECTED_PRE_OPERATOR, &t);
  1245. return node;
  1246. }
  1247. node->SetToken(&t);
  1248. node->UpdateSourcePos(t.pos, t.length);
  1249. return node;
  1250. }
  1251. asCScriptNode *asCParser::ParseExprPostOp()
  1252. {
  1253. asCScriptNode *node = CreateNode(snExprPostOp);
  1254. if( node == 0 ) return 0;
  1255. sToken t;
  1256. GetToken(&t);
  1257. if( !IsPostOperator(t.type) )
  1258. {
  1259. Error(TXT_EXPECTED_POST_OPERATOR, &t);
  1260. return node;
  1261. }
  1262. node->SetToken(&t);
  1263. node->UpdateSourcePos(t.pos, t.length);
  1264. if( t.type == ttDot )
  1265. {
  1266. sToken t1, t2;
  1267. GetToken(&t1);
  1268. GetToken(&t2);
  1269. RewindTo(&t1);
  1270. if( t2.type == ttOpenParanthesis )
  1271. node->AddChildLast(ParseFunctionCall());
  1272. else
  1273. node->AddChildLast(ParseIdentifier());
  1274. }
  1275. else if( t.type == ttOpenBracket )
  1276. {
  1277. node->AddChildLast(ParseAssignment());
  1278. GetToken(&t);
  1279. if( t.type != ttCloseBracket )
  1280. {
  1281. Error(ExpectedToken("]"), &t);
  1282. return node;
  1283. }
  1284. node->UpdateSourcePos(t.pos, t.length);
  1285. }
  1286. else if( t.type == ttOpenParanthesis )
  1287. {
  1288. RewindTo(&t);
  1289. node->AddChildLast(ParseArgList());
  1290. }
  1291. return node;
  1292. }
  1293. asCScriptNode *asCParser::ParseExprOperator()
  1294. {
  1295. asCScriptNode *node = CreateNode(snExprOperator);
  1296. if( node == 0 ) return 0;
  1297. sToken t;
  1298. GetToken(&t);
  1299. if( !IsOperator(t.type) )
  1300. {
  1301. Error(TXT_EXPECTED_OPERATOR, &t);
  1302. return node;
  1303. }
  1304. node->SetToken(&t);
  1305. node->UpdateSourcePos(t.pos, t.length);
  1306. return node;
  1307. }
  1308. asCScriptNode *asCParser::ParseAssignOperator()
  1309. {
  1310. asCScriptNode *node = CreateNode(snExprOperator);
  1311. if( node == 0 ) return 0;
  1312. sToken t;
  1313. GetToken(&t);
  1314. if( !IsAssignOperator(t.type) )
  1315. {
  1316. Error(TXT_EXPECTED_OPERATOR, &t);
  1317. return node;
  1318. }
  1319. node->SetToken(&t);
  1320. node->UpdateSourcePos(t.pos, t.length);
  1321. return node;
  1322. }
  1323. bool asCParser::IsOperator(int tokenType)
  1324. {
  1325. if( tokenType == ttPlus ||
  1326. tokenType == ttMinus ||
  1327. tokenType == ttStar ||
  1328. tokenType == ttSlash ||
  1329. tokenType == ttPercent ||
  1330. tokenType == ttAnd ||
  1331. tokenType == ttOr ||
  1332. tokenType == ttXor ||
  1333. tokenType == ttEqual ||
  1334. tokenType == ttNotEqual ||
  1335. tokenType == ttLessThan ||
  1336. tokenType == ttLessThanOrEqual ||
  1337. tokenType == ttGreaterThan ||
  1338. tokenType == ttGreaterThanOrEqual ||
  1339. tokenType == ttAmp ||
  1340. tokenType == ttBitOr ||
  1341. tokenType == ttBitXor ||
  1342. tokenType == ttBitShiftLeft ||
  1343. tokenType == ttBitShiftRight ||
  1344. tokenType == ttBitShiftRightArith ||
  1345. tokenType == ttIs ||
  1346. tokenType == ttNotIs )
  1347. return true;
  1348. return false;
  1349. }
  1350. bool asCParser::IsAssignOperator(int tokenType)
  1351. {
  1352. if( tokenType == ttAssignment ||
  1353. tokenType == ttAddAssign ||
  1354. tokenType == ttSubAssign ||
  1355. tokenType == ttMulAssign ||
  1356. tokenType == ttDivAssign ||
  1357. tokenType == ttModAssign ||
  1358. tokenType == ttAndAssign ||
  1359. tokenType == ttOrAssign ||
  1360. tokenType == ttXorAssign ||
  1361. tokenType == ttShiftLeftAssign ||
  1362. tokenType == ttShiftRightLAssign ||
  1363. tokenType == ttShiftRightAAssign )
  1364. return true;
  1365. return false;
  1366. }
  1367. bool asCParser::IsPreOperator(int tokenType)
  1368. {
  1369. if( tokenType == ttMinus ||
  1370. tokenType == ttPlus ||
  1371. tokenType == ttNot ||
  1372. tokenType == ttInc ||
  1373. tokenType == ttDec ||
  1374. tokenType == ttBitNot ||
  1375. tokenType == ttHandle )
  1376. return true;
  1377. return false;
  1378. }
  1379. bool asCParser::IsPostOperator(int tokenType)
  1380. {
  1381. if( tokenType == ttInc || // post increment
  1382. tokenType == ttDec || // post decrement
  1383. tokenType == ttDot || // member access
  1384. tokenType == ttOpenBracket || // index operator
  1385. tokenType == ttOpenParanthesis ) // argument list for call on function pointer
  1386. return true;
  1387. return false;
  1388. }
  1389. bool asCParser::IsConstant(int tokenType)
  1390. {
  1391. if( tokenType == ttIntConstant ||
  1392. tokenType == ttFloatConstant ||
  1393. tokenType == ttDoubleConstant ||
  1394. tokenType == ttStringConstant ||
  1395. tokenType == ttMultilineStringConstant ||
  1396. tokenType == ttHeredocStringConstant ||
  1397. tokenType == ttTrue ||
  1398. tokenType == ttFalse ||
  1399. tokenType == ttBitsConstant ||
  1400. tokenType == ttNull )
  1401. return true;
  1402. return false;
  1403. }
  1404. int asCParser::ParseScript(asCScriptCode *script)
  1405. {
  1406. Reset();
  1407. this->script = script;
  1408. scriptNode = ParseScript(false);
  1409. if( errorWhileParsing )
  1410. return -1;
  1411. return 0;
  1412. }
  1413. int asCParser::ParseExpression(asCScriptCode *script)
  1414. {
  1415. Reset();
  1416. this->script = script;
  1417. checkValidTypes = true;
  1418. scriptNode = ParseExpression();
  1419. if( errorWhileParsing )
  1420. return -1;
  1421. return 0;
  1422. }
  1423. asCScriptNode *asCParser::ParseImport()
  1424. {
  1425. asCScriptNode *node = CreateNode(snImport);
  1426. if( node == 0 ) return 0;
  1427. sToken t;
  1428. GetToken(&t);
  1429. if( t.type != ttImport )
  1430. {
  1431. Error(ExpectedToken(asCTokenizer::GetDefinition(ttImport)), &t);
  1432. return node;
  1433. }
  1434. node->SetToken(&t);
  1435. node->UpdateSourcePos(t.pos, t.length);
  1436. node->AddChildLast(ParseFunctionDefinition());
  1437. if( isSyntaxError ) return node;
  1438. GetToken(&t);
  1439. if( t.type != ttIdentifier )
  1440. {
  1441. Error(ExpectedToken(FROM_TOKEN), &t);
  1442. return node;
  1443. }
  1444. tempString.Assign(&script->code[t.pos], t.length);
  1445. if( tempString != FROM_TOKEN )
  1446. {
  1447. Error(ExpectedToken(FROM_TOKEN), &t);
  1448. return node;
  1449. }
  1450. node->UpdateSourcePos(t.pos, t.length);
  1451. GetToken(&t);
  1452. if( t.type != ttStringConstant )
  1453. {
  1454. Error(TXT_EXPECTED_STRING, &t);
  1455. return node;
  1456. }
  1457. asCScriptNode *mod = CreateNode(snConstant);
  1458. if( mod == 0 ) return 0;
  1459. node->AddChildLast(mod);
  1460. mod->SetToken(&t);
  1461. mod->UpdateSourcePos(t.pos, t.length);
  1462. GetToken(&t);
  1463. if( t.type != ttEndStatement )
  1464. {
  1465. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEndStatement)), &t);
  1466. return node;
  1467. }
  1468. node->UpdateSourcePos(t.pos, t.length);
  1469. return node;
  1470. }
  1471. asCScriptNode *asCParser::ParseScript(bool inBlock)
  1472. {
  1473. asCScriptNode *node = CreateNode(snScript);
  1474. if( node == 0 ) return 0;
  1475. // Determine type of node
  1476. sToken t1, t2;
  1477. for(;;)
  1478. {
  1479. while( !isSyntaxError )
  1480. {
  1481. GetToken(&t1);
  1482. GetToken(&t2);
  1483. RewindTo(&t1);
  1484. if( t1.type == ttImport )
  1485. node->AddChildLast(ParseImport());
  1486. else if( t1.type == ttEnum || (IdentifierIs(t1, SHARED_TOKEN) && t2.type == ttEnum) )
  1487. node->AddChildLast(ParseEnumeration()); // Handle enumerations
  1488. else if( t1.type == ttTypedef )
  1489. node->AddChildLast(ParseTypedef()); // Handle primitive typedefs
  1490. else if( t1.type == ttClass ||
  1491. ((IdentifierIs(t1, SHARED_TOKEN) || IdentifierIs(t1, FINAL_TOKEN)) && t2.type == ttClass) ||
  1492. (IdentifierIs(t1, SHARED_TOKEN) && IdentifierIs(t2, FINAL_TOKEN)) )
  1493. node->AddChildLast(ParseClass());
  1494. else if( t1.type == ttMixin )
  1495. node->AddChildLast(ParseMixin());
  1496. else if( t1.type == ttInterface || (t1.type == ttIdentifier && t2.type == ttInterface) )
  1497. node->AddChildLast(ParseInterface());
  1498. else if( t1.type == ttFuncDef )
  1499. node->AddChildLast(ParseFuncDef());
  1500. else if( t1.type == ttConst || t1.type == ttScope || IsDataType(t1) )
  1501. {
  1502. if( IsVirtualPropertyDecl() )
  1503. node->AddChildLast(ParseVirtualPropertyDecl(false, false));
  1504. else if( IsVarDecl() )
  1505. node->AddChildLast(ParseDeclaration(false, true));
  1506. else
  1507. node->AddChildLast(ParseFunction());
  1508. }
  1509. else if( t1.type == ttEndStatement )
  1510. {
  1511. // Ignore a semicolon by itself
  1512. GetToken(&t1);
  1513. }
  1514. else if( t1.type == ttNamespace )
  1515. node->AddChildLast(ParseNamespace());
  1516. else if( t1.type == ttEnd )
  1517. return node;
  1518. else if( inBlock && t1.type == ttEndStatementBlock )
  1519. return node;
  1520. else
  1521. {
  1522. asCString str;
  1523. const char *t = asCTokenizer::GetDefinition(t1.type);
  1524. if( t == 0 ) t = "<unknown token>";
  1525. str.Format(TXT_UNEXPECTED_TOKEN_s, t);
  1526. Error(str, &t1);
  1527. }
  1528. }
  1529. if( isSyntaxError )
  1530. {
  1531. // Search for either ';' or '{' or end
  1532. GetToken(&t1);
  1533. while( t1.type != ttEndStatement && t1.type != ttEnd &&
  1534. t1.type != ttStartStatementBlock )
  1535. GetToken(&t1);
  1536. if( t1.type == ttStartStatementBlock )
  1537. {
  1538. // Find the end of the block and skip nested blocks
  1539. int level = 1;
  1540. while( level > 0 )
  1541. {
  1542. GetToken(&t1);
  1543. if( t1.type == ttStartStatementBlock ) level++;
  1544. if( t1.type == ttEndStatementBlock ) level--;
  1545. if( t1.type == ttEnd ) break;
  1546. }
  1547. }
  1548. isSyntaxError = false;
  1549. }
  1550. }
  1551. UNREACHABLE_RETURN;
  1552. }
  1553. asCScriptNode *asCParser::ParseNamespace()
  1554. {
  1555. asCScriptNode *node = CreateNode(snNamespace);
  1556. if( node == 0 ) return 0;
  1557. sToken t1;
  1558. GetToken(&t1);
  1559. if( t1.type == ttNamespace )
  1560. node->UpdateSourcePos(t1.pos, t1.length);
  1561. else
  1562. Error(ExpectedToken(asCTokenizer::GetDefinition(ttNamespace)), &t1);
  1563. // TODO: namespace: Allow declaration of multiple nested namespace with namespace A::B::C { }
  1564. node->AddChildLast(ParseIdentifier());
  1565. if( isSyntaxError ) return node;
  1566. GetToken(&t1);
  1567. if( t1.type == ttStartStatementBlock )
  1568. node->UpdateSourcePos(t1.pos, t1.length);
  1569. else
  1570. {
  1571. Error(ExpectedToken(asCTokenizer::GetDefinition(ttStartStatementBlock)), &t1);
  1572. return node;
  1573. }
  1574. sToken start = t1;
  1575. node->AddChildLast(ParseScript(true));
  1576. if( !isSyntaxError )
  1577. {
  1578. GetToken(&t1);
  1579. if( t1.type == ttEndStatementBlock )
  1580. node->UpdateSourcePos(t1.pos, t1.length);
  1581. else
  1582. {
  1583. if( t1.type == ttEnd )
  1584. Error(TXT_UNEXPECTED_END_OF_FILE, &t1);
  1585. else
  1586. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEndStatementBlock)), &t1);
  1587. Info(TXT_WHILE_PARSING_NAMESPACE, &start);
  1588. return node;
  1589. }
  1590. }
  1591. return node;
  1592. }
  1593. int asCParser::ParseStatementBlock(asCScriptCode *script, asCScriptNode *block)
  1594. {
  1595. TimeIt("asCParser::ParseStatementBlock");
  1596. Reset();
  1597. // Tell the parser to validate the identifiers as valid types
  1598. checkValidTypes = true;
  1599. this->script = script;
  1600. sourcePos = block->tokenPos;
  1601. scriptNode = ParseStatementBlock();
  1602. if( isSyntaxError || errorWhileParsing )
  1603. return -1;
  1604. return 0;
  1605. }
  1606. asCScriptNode *asCParser::ParseEnumeration()
  1607. {
  1608. asCScriptNode *ident;
  1609. asCScriptNode *dataType;
  1610. asCScriptNode *node = CreateNode(snEnum);
  1611. if( node == 0 ) return 0;
  1612. sToken token;
  1613. // Optional 'shared' token
  1614. GetToken(&token);
  1615. if( IdentifierIs(token, SHARED_TOKEN) )
  1616. {
  1617. RewindTo(&token);
  1618. node->AddChildLast(ParseIdentifier());
  1619. if( isSyntaxError ) return node;
  1620. GetToken(&token);
  1621. }
  1622. // Check for enum
  1623. if( token.type != ttEnum )
  1624. {
  1625. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEnum)), &token);
  1626. return node;
  1627. }
  1628. node->SetToken(&token);
  1629. node->UpdateSourcePos(token.pos, token.length);
  1630. // Get the identifier
  1631. GetToken(&token);
  1632. if(ttIdentifier != token.type)
  1633. {
  1634. Error(TXT_EXPECTED_IDENTIFIER, &token);
  1635. return node;
  1636. }
  1637. dataType = CreateNode(snDataType);
  1638. if( dataType == 0 ) return 0;
  1639. node->AddChildLast(dataType);
  1640. ident = CreateNode(snIdentifier);
  1641. if( ident == 0 ) return 0;
  1642. ident->SetToken(&token);
  1643. ident->UpdateSourcePos(token.pos, token.length);
  1644. dataType->AddChildLast(ident);
  1645. // check for the start of the declaration block
  1646. GetToken(&token);
  1647. if( token.type != ttStartStatementBlock )
  1648. {
  1649. RewindTo(&token);
  1650. Error(ExpectedToken(asCTokenizer::GetDefinition(token.type)), &token);
  1651. return node;
  1652. }
  1653. while(ttEnd != token.type)
  1654. {
  1655. GetToken(&token);
  1656. if( ttEndStatementBlock == token.type )
  1657. {
  1658. RewindTo(&token);
  1659. break;
  1660. }
  1661. if(ttIdentifier != token.type)
  1662. {
  1663. Error(TXT_EXPECTED_IDENTIFIER, &token);
  1664. return node;
  1665. }
  1666. // Add the enum element
  1667. ident = CreateNode(snIdentifier);
  1668. if( ident == 0 ) return 0;
  1669. ident->SetToken(&token);
  1670. ident->UpdateSourcePos(token.pos, token.length);
  1671. node->AddChildLast(ident);
  1672. GetToken(&token);
  1673. if( token.type == ttAssignment )
  1674. {
  1675. asCScriptNode *tmp;
  1676. RewindTo(&token);
  1677. tmp = SuperficiallyParseVarInit();
  1678. node->AddChildLast(tmp);
  1679. if( isSyntaxError ) return node;
  1680. GetToken(&token);
  1681. }
  1682. if(ttListSeparator != token.type)
  1683. {
  1684. RewindTo(&token);
  1685. break;
  1686. }
  1687. }
  1688. // check for the end of the declaration block
  1689. GetToken(&token);
  1690. if( token.type != ttEndStatementBlock )
  1691. {
  1692. RewindTo(&token);
  1693. Error(ExpectedToken(asCTokenizer::GetDefinition(token.type)), &token);
  1694. return node;
  1695. }
  1696. // Parse the declarations
  1697. return node;
  1698. }
  1699. bool asCParser::IsVarDecl()
  1700. {
  1701. // Set start point so that we can rewind
  1702. sToken t;
  1703. GetToken(&t);
  1704. RewindTo(&t);
  1705. // A class property decl can be preceded by 'private'
  1706. sToken t1;
  1707. GetToken(&t1);
  1708. if( t1.type != ttPrivate )
  1709. RewindTo(&t1);
  1710. // A variable decl can start with a const
  1711. GetToken(&t1);
  1712. if( t1.type == ttConst )
  1713. GetToken(&t1);
  1714. // The type may be initiated with the scope operator
  1715. if( t1.type == ttScope )
  1716. GetToken(&t1);
  1717. // The type may be preceeded with a multilevel scope
  1718. sToken t2;
  1719. GetToken(&t2);
  1720. while( t1.type == ttIdentifier && t2.type == ttScope )
  1721. {
  1722. GetToken(&t1);
  1723. GetToken(&t2);
  1724. }
  1725. RewindTo(&t2);
  1726. // We don't validate if the identifier is an actual declared type at this moment
  1727. // as it may wrongly identify the statement as a non-declaration if the user typed
  1728. // the name incorrectly. The real type is validated in ParseDeclaration where a
  1729. // proper error message can be given.
  1730. if( !IsRealType(t1.type) && t1.type != ttIdentifier )
  1731. {
  1732. RewindTo(&t);
  1733. return false;
  1734. }
  1735. if( !CheckTemplateType(t1) )
  1736. {
  1737. RewindTo(&t);
  1738. return false;
  1739. }
  1740. // Object handles can be interleaved with the array brackets
  1741. GetToken(&t2);
  1742. while( t2.type == ttHandle || t2.type == ttOpenBracket )
  1743. {
  1744. if( t2.type == ttOpenBracket )
  1745. {
  1746. GetToken(&t2);
  1747. if( t2.type != ttCloseBracket )
  1748. {
  1749. RewindTo(&t);
  1750. return false;
  1751. }
  1752. }
  1753. GetToken(&t2);
  1754. }
  1755. if( t2.type != ttIdentifier )
  1756. {
  1757. RewindTo(&t);
  1758. return false;
  1759. }
  1760. GetToken(&t2);
  1761. if( t2.type == ttEndStatement || t2.type == ttAssignment || t2.type == ttListSeparator )
  1762. {
  1763. RewindTo(&t);
  1764. return true;
  1765. }
  1766. if( t2.type == ttOpenParanthesis )
  1767. {
  1768. // If the closing paranthesis is followed by a statement
  1769. // block or end-of-file, then treat it as a function. A
  1770. // function decl may have nested paranthesis so we need to
  1771. // check for this too.
  1772. int nest = 0;
  1773. while( t2.type != ttEnd )
  1774. {
  1775. if( t2.type == ttOpenParanthesis )
  1776. nest++;
  1777. else if( t2.type == ttCloseParanthesis )
  1778. {
  1779. nest--;
  1780. if( nest == 0 )
  1781. break;
  1782. }
  1783. GetToken(&t2);
  1784. }
  1785. if( t2.type == ttEnd )
  1786. return false;
  1787. else
  1788. {
  1789. GetToken(&t1);
  1790. RewindTo(&t);
  1791. if( t1.type == ttStartStatementBlock || t1.type == ttEnd )
  1792. return false;
  1793. }
  1794. RewindTo(&t);
  1795. return true;
  1796. }
  1797. RewindTo(&t);
  1798. return false;
  1799. }
  1800. bool asCParser::IsVirtualPropertyDecl()
  1801. {
  1802. // Set start point so that we can rewind
  1803. sToken t;
  1804. GetToken(&t);
  1805. RewindTo(&t);
  1806. // A class property decl can be preceded by 'private'
  1807. sToken t1;
  1808. GetToken(&t1);
  1809. if( t1.type != ttPrivate )
  1810. RewindTo(&t1);
  1811. // A variable decl can start with a const
  1812. GetToken(&t1);
  1813. if( t1.type == ttConst )
  1814. GetToken(&t1);
  1815. // We don't validate if the identifier is an actual declared type at this moment
  1816. // as it may wrongly identify the statement as a non-declaration if the user typed
  1817. // the name incorrectly. The real type is validated in ParseDeclaration where a
  1818. // proper error message can be given.
  1819. if( !IsRealType(t1.type) && t1.type != ttIdentifier )
  1820. {
  1821. RewindTo(&t);
  1822. return false;
  1823. }
  1824. if( !CheckTemplateType(t1) )
  1825. {
  1826. RewindTo(&t);
  1827. return false;
  1828. }
  1829. // Object handles can be interleaved with the array brackets
  1830. sToken t2;
  1831. GetToken(&t2);
  1832. while( t2.type == ttHandle || t2.type == ttOpenBracket )
  1833. {
  1834. if( t2.type == ttOpenBracket )
  1835. {
  1836. GetToken(&t2);
  1837. if( t2.type != ttCloseBracket )
  1838. {
  1839. RewindTo(&t);
  1840. return false;
  1841. }
  1842. }
  1843. GetToken(&t2);
  1844. }
  1845. if( t2.type != ttIdentifier )
  1846. {
  1847. RewindTo(&t);
  1848. return false;
  1849. }
  1850. GetToken(&t2);
  1851. if( t2.type == ttStartStatementBlock )
  1852. {
  1853. RewindTo(&t);
  1854. return true;
  1855. }
  1856. RewindTo(&t);
  1857. return false;
  1858. }
  1859. bool asCParser::IsFuncDecl(bool isMethod)
  1860. {
  1861. // Set start point so that we can rewind
  1862. sToken t;
  1863. GetToken(&t);
  1864. RewindTo(&t);
  1865. if( isMethod )
  1866. {
  1867. // A class method decl can be preceded by 'private'
  1868. sToken t1, t2;
  1869. GetToken(&t1);
  1870. if( t1.type != ttPrivate )
  1871. RewindTo(&t1);
  1872. // A class constructor starts with identifier followed by parenthesis
  1873. // A class destructor starts with the ~ token
  1874. GetToken(&t1);
  1875. GetToken(&t2);
  1876. RewindTo(&t1);
  1877. if( (t1.type == ttIdentifier && t2.type == ttOpenParanthesis) || t1.type == ttBitNot )
  1878. {
  1879. RewindTo(&t);
  1880. return true;
  1881. }
  1882. }
  1883. // A function decl can start with a const
  1884. sToken t1;
  1885. GetToken(&t1);
  1886. if( t1.type == ttConst )
  1887. GetToken(&t1);
  1888. // The return type can be optionally preceeded by a scope
  1889. if( t1.type == ttScope )
  1890. GetToken(&t1);
  1891. while( t1.type == ttIdentifier )
  1892. {
  1893. sToken t2;
  1894. GetToken(&t2);
  1895. if( t2.type == ttScope )
  1896. GetToken(&t1);
  1897. else
  1898. {
  1899. RewindTo(&t2);
  1900. break;
  1901. }
  1902. }
  1903. if( !IsDataType(t1) )
  1904. {
  1905. RewindTo(&t);
  1906. return false;
  1907. }
  1908. // If the type is a template type, then skip the angle brackets holding the subtype
  1909. if( !CheckTemplateType(t1) )
  1910. {
  1911. RewindTo(&t);
  1912. return false;
  1913. }
  1914. // Object handles can be interleaved with the array brackets
  1915. sToken t2;
  1916. GetToken(&t2);
  1917. while( t2.type == ttHandle || t2.type == ttOpenBracket )
  1918. {
  1919. if( t2.type == ttOpenBracket )
  1920. {
  1921. GetToken(&t2);
  1922. if( t2.type != ttCloseBracket )
  1923. {
  1924. RewindTo(&t);
  1925. return false;
  1926. }
  1927. }
  1928. GetToken(&t2);
  1929. }
  1930. // There can be an ampersand if the function returns a reference
  1931. if( t2.type == ttAmp )
  1932. {
  1933. RewindTo(&t);
  1934. return true;
  1935. }
  1936. if( t2.type != ttIdentifier )
  1937. {
  1938. RewindTo(&t);
  1939. return false;
  1940. }
  1941. GetToken(&t2);
  1942. if( t2.type == ttOpenParanthesis )
  1943. {
  1944. // If the closing parenthesis is not followed by a
  1945. // statement block then it is not a function.
  1946. // It's possible that there are nested parenthesis due to default
  1947. // arguments so this should be checked for.
  1948. int nest = 0;
  1949. GetToken(&t2);
  1950. while( (nest || t2.type != ttCloseParanthesis) && t2.type != ttEnd )
  1951. {
  1952. if( t2.type == ttOpenParanthesis )
  1953. nest++;
  1954. if( t2.type == ttCloseParanthesis )
  1955. nest--;
  1956. GetToken(&t2);
  1957. }
  1958. if( t2.type == ttEnd )
  1959. return false;
  1960. else
  1961. {
  1962. if( isMethod )
  1963. {
  1964. // A class method can have a 'const' token after the parameter list
  1965. GetToken(&t1);
  1966. if( t1.type != ttConst )
  1967. RewindTo(&t1);
  1968. // A class method may also have any number of additional inheritance behavior specifiers
  1969. for( ; ; )
  1970. {
  1971. GetToken(&t2);
  1972. if( !IdentifierIs(t2, FINAL_TOKEN) && !IdentifierIs(t2, OVERRIDE_TOKEN) )
  1973. {
  1974. RewindTo(&t2);
  1975. break;
  1976. }
  1977. }
  1978. }
  1979. GetToken(&t1);
  1980. RewindTo(&t);
  1981. if( t1.type == ttStartStatementBlock )
  1982. return true;
  1983. }
  1984. RewindTo(&t);
  1985. return false;
  1986. }
  1987. RewindTo(&t);
  1988. return false;
  1989. }
  1990. asCScriptNode *asCParser::ParseFuncDef()
  1991. {
  1992. asCScriptNode *node = CreateNode(snFuncDef);
  1993. if( node == 0 ) return 0;
  1994. sToken t1;
  1995. GetToken(&t1);
  1996. if( t1.type != ttFuncDef )
  1997. {
  1998. Error(asCTokenizer::GetDefinition(ttFuncDef), &t1);
  1999. return node;
  2000. }
  2001. node->SetToken(&t1);
  2002. node->AddChildLast(ParseType(true));
  2003. if( isSyntaxError ) return node;
  2004. node->AddChildLast(ParseTypeMod(false));
  2005. if( isSyntaxError ) return node;
  2006. node->AddChildLast(ParseIdentifier());
  2007. if( isSyntaxError ) return node;
  2008. node->AddChildLast(ParseParameterList());
  2009. if( isSyntaxError ) return node;
  2010. GetToken(&t1);
  2011. if( t1.type != ttEndStatement )
  2012. {
  2013. Error(ExpectedToken(asCTokenizer::GetDefinition(ttEndStatement)), &t1);
  2014. return node;
  2015. }
  2016. node->UpdateSourcePos(t1.pos, t1.length);
  2017. return node;
  2018. }
  2019. asCScriptNode *asCParser::ParseFunction(bool isMethod)
  2020. {
  2021. asCScriptNode *node = CreateNode(snFunction);
  2022. if( node == 0 ) return 0;
  2023. sToken t1,t2;
  2024. GetToken(&t1);
  2025. GetToken(&t2);
  2026. RewindTo(&t1);
  2027. // A class method can start with private
  2028. if( isMethod && t1.type == ttPrivate )
  2029. {
  2030. node->AddChildLast(ParseToken(ttPrivate));
  2031. if( isSyntaxError ) return node;
  2032. }
  2033. // A global function can be marked as shared
  2034. if( !isMethod && IdentifierIs(t1, SHARED_TOKEN) )
  2035. {
  2036. node->AddChildLast(ParseIdentifier());
  2037. if( isSyntaxError ) return node;
  2038. }
  2039. // If it is a global function, or a method, except constructor and destructor, then the return type is parsed
  2040. if( !isMethod || (t1.type != ttBitNot && t2.type != ttOpenParanthesis) )
  2041. {
  2042. node->AddChildLast(ParseType(true));
  2043. if( isSyntaxError ) return node;
  2044. node->AddChildLast(ParseTypeMod(false));
  2045. if( isSyntaxError ) return node;
  2046. }
  2047. // If this is a class destructor then it starts with ~, and no return type is declared
  2048. if( isMethod && t1.type == ttBitNot )
  2049. {
  2050. node->AddChildLast(ParseToken(ttBitNot));
  2051. if( isSyntaxError ) return node;
  2052. }
  2053. node->AddChildLast(ParseIdentifier());
  2054. if( isSyntaxError ) return node;
  2055. node->AddChildLast(ParseParameterList());
  2056. if( isSyntaxError ) return node;
  2057. if( isMethod )
  2058. {
  2059. GetToken(&t1);
  2060. RewindTo(&t1);
  2061. // Is the method a const?
  2062. if( t1.type == ttConst )
  2063. node->AddChildLast(ParseToken(ttConst));
  2064. ParseMethodOverrideBehaviors(node);
  2065. if( isSyntaxError ) return node;
  2066. }
  2067. // We should just find the end of the statement block here. The statements
  2068. // will be parsed on request by the compiler once it starts the compilation.
  2069. node->AddChildLast(SuperficiallyParseStatementBlock());
  2070. return node;
  2071. }
  2072. asCScriptNode *asCParser::ParseInterfaceMethod()
  2073. {
  2074. asCScriptNode *node = CreateNode(snFunction);
  2075. if( node == 0 ) return 0;
  2076. node->AddChildLast(ParseType(true));
  2077. if( isSyntaxError ) return node;
  2078. node->AddChildLast(ParseTypeMod(false));
  2079. if( isSyntaxError ) return node;
  2080. node->AddChildLast(ParseIdentifier());
  2081. if( isSyntaxError ) return node;
  2082. node->AddChildLast(ParseParameterList());
  2083. if( isSyntaxError ) return node;
  2084. // Parse an optional const after the method definition
  2085. sToken t1;
  2086. GetToken(&t1);
  2087. RewindTo(&t1);
  2088. if( t1.type == ttConst )
  2089. node->AddChildLast(ParseToken(ttConst));
  2090. GetToken(&t1);
  2091. if( t1.type != ttEndStatement )
  2092. {
  2093. Error(ExpectedToken(";"), &t1);
  2094. return node;
  2095. }
  2096. node->UpdateSourcePos(t1.pos, t1.length);
  2097. return node;
  2098. }
  2099. asCScriptNode *asCParser::ParseVirtualPropertyDecl(bool isMethod, bool isInterface)
  2100. {
  2101. asCScriptNode *node = CreateNode(snVirtualProperty);
  2102. if( node == 0 ) return 0;
  2103. sToken t1,t2;
  2104. GetToken(&t1);
  2105. GetToken(&t2);
  2106. RewindTo(&t1);
  2107. // A class method can start with private
  2108. if( isMethod && t1.type == ttPrivate )
  2109. {
  2110. node->AddChildLast(ParseToken(ttPrivate));
  2111. if( isSyntaxError ) return node;
  2112. }
  2113. node->AddChildLast(ParseType(true));
  2114. if( isSyntaxError ) return node;
  2115. node->AddChildLast(ParseTypeMod(false));
  2116. if( isSyntaxError ) return node;
  2117. node->AddChildLast(ParseIdentifier());
  2118. if( isSyntaxError ) return node;
  2119. GetToken(&t1);
  2120. if( t1.type != ttStartStatementBlock )
  2121. {
  2122. Error(ExpectedToken("{"), &t1);
  2123. return node;
  2124. }
  2125. for(;;)
  2126. {
  2127. GetToken(&t1);
  2128. asCScriptNode *accessorNode = 0;
  2129. if( IdentifierIs(t1, GET_TOKEN) || IdentifierIs(t1, SET_TOKEN) )
  2130. {
  2131. accessorNode = CreateNode(snVirtualProperty);
  2132. if( accessorNode == 0 ) return 0;
  2133. node->AddChildLast(accessorNode);
  2134. RewindTo(&t1);
  2135. accessorNode->AddChildLast(ParseIdentifier());
  2136. if( isMethod )
  2137. {
  2138. GetToken(&t1);
  2139. RewindTo(&t1);
  2140. if( t1.type == ttConst )
  2141. accessorNode->AddChildLast(ParseToken(ttConst));
  2142. if( !isInterface )
  2143. {
  2144. ParseMethodOverrideBehaviors(accessorNode);
  2145. if( isSyntaxError ) return node;
  2146. }
  2147. }
  2148. if( !isInterface )
  2149. {
  2150. GetToken(&t1);
  2151. if( t1.type == ttStartStatementBlock )
  2152. {
  2153. RewindTo(&t1);
  2154. accessorNode->AddChildLast(SuperficiallyParseStatementBlock());
  2155. if( isSyntaxError ) return node;
  2156. }
  2157. else if( t1.type != ttEndStatement )
  2158. {
  2159. Error(ExpectedTokens(";", "{"), &t1);
  2160. return node;
  2161. }
  2162. }
  2163. else
  2164. {
  2165. GetToken(&t1);
  2166. if( t1.type != ttEndStatement )
  2167. {
  2168. Error(ExpectedToken(";"), &t1);
  2169. return node;
  2170. }
  2171. }
  2172. }
  2173. else if( t1.type == ttEndStatementBlock )
  2174. break;
  2175. else
  2176. {
  2177. const char *tokens[] = { GET_TOKEN, SET_TOKEN, asCTokenizer::GetDefinition(ttEndStatementBlock) };
  2178. Error(ExpectedOneOf(tokens, 3), &t1);
  2179. return node;
  2180. }
  2181. }
  2182. return node;
  2183. }
  2184. asCScriptNode *asCParser::ParseInterface()
  2185. {
  2186. asCScriptNode *node = CreateNode(snInterface);
  2187. if( node == 0 ) return 0;
  2188. sToken t;
  2189. GetToken(&t);
  2190. // Allow keyword 'shared' before 'interface'
  2191. if( t.type == ttIdentifier )
  2192. {
  2193. tempString.Assign(&script->code[t.pos], t.length);
  2194. if( tempString != SHARED_TOKEN )
  2195. {
  2196. Error(ExpectedToken(SHARED_TOKEN), &t);
  2197. return node;
  2198. }
  2199. RewindTo(&t);
  2200. node->AddChildLast(ParseIdentifier());
  2201. GetToken(&t);
  2202. }
  2203. if( t.type != ttInterface )
  2204. {
  2205. Error(ExpectedToken("interface"), &t);
  2206. return node;
  2207. }
  2208. node->SetToken(&t);
  2209. node->AddChildLast(ParseIdentifier());
  2210. // Can optionally have a list of interfaces that are inherited
  2211. GetToken(&t);
  2212. if( t.type == ttColon )
  2213. {
  2214. asCScriptNode *inherit = CreateNode(snIdentifier);
  2215. node->AddChildLast(inherit);
  2216. ParseOptionalScope(inherit);
  2217. inherit->AddChildLast(ParseIdentifier());
  2218. GetToken(&t);
  2219. while( t.type == ttListSeparator )
  2220. {
  2221. inherit = CreateNode(snIdentifier);
  2222. node->AddChildLast(inherit);
  2223. ParseOptionalScope(inherit);
  2224. inherit->AddChildLast(ParseIdentifier());
  2225. GetToken(&t);
  2226. }
  2227. }
  2228. if( t.type != ttStartStatementBlock )
  2229. {
  2230. Error(ExpectedToken("{"), &t);
  2231. return node;
  2232. }
  2233. // Parse interface methods
  2234. GetToken(&t);
  2235. RewindTo(&t);
  2236. while( t.type != ttEndStatementBlock && t.type != ttEnd )
  2237. {
  2238. if( IsVirtualPropertyDecl() )
  2239. node->AddChildLast(ParseVirtualPropertyDecl(true, true));
  2240. else if( t.type == ttEndStatement )
  2241. // Skip empty declarations
  2242. GetToken(&t);
  2243. else
  2244. // Parse the method signature
  2245. node->AddChildLast(ParseInterfaceMethod());
  2246. if( isSyntaxError ) return node;
  2247. GetToken(&t);
  2248. RewindTo(&t);
  2249. }
  2250. GetToken(&t);
  2251. if( t.type != ttEndStatementBlock )
  2252. {
  2253. Error(ExpectedToken("}"), &t);
  2254. return node;
  2255. }
  2256. node->UpdateSourcePos(t.pos, t.length);
  2257. return node;
  2258. }
  2259. asCScriptNode *asCParser::ParseMixin()
  2260. {
  2261. asCScriptNode *node = CreateNode(snMixin);
  2262. if( node == 0 ) return 0;
  2263. sToken t;
  2264. GetToken(&t);
  2265. if( t.type != ttMixin )
  2266. {
  2267. Error(ExpectedToken("mixin"), &t);
  2268. return node;
  2269. }
  2270. node->SetToken(&t);
  2271. // A mixin token must be followed by a class declaration
  2272. node->AddChildLast(ParseClass());
  2273. return node;
  2274. }
  2275. asCScriptNode *asCParser::ParseClass()
  2276. {
  2277. asCScriptNode *node = CreateNode(snClass);
  2278. if( node == 0 ) return 0;
  2279. sToken t;
  2280. GetToken(&t);
  2281. // Allow the keyword 'shared' before 'class'
  2282. if( IdentifierIs(t, SHARED_TOKEN) )
  2283. {
  2284. RewindTo(&t);
  2285. node->AddChildLast(ParseIdentifier());
  2286. GetToken(&t);
  2287. }
  2288. if( IdentifierIs(t, FINAL_TOKEN) )
  2289. {
  2290. RewindTo(&t);
  2291. node->AddChildLast(ParseIdentifier());
  2292. GetToken(&t);
  2293. }
  2294. if( t.type != ttClass )
  2295. {
  2296. Error(ExpectedToken("class"), &t);
  2297. return node;
  2298. }
  2299. node->SetToken(&t);
  2300. if( engine->ep.allowImplicitHandleTypes )
  2301. {
  2302. // Parse 'implicit handle class' construct
  2303. GetToken(&t);
  2304. if ( t.type == ttHandle )
  2305. node->SetToken(&t);
  2306. else
  2307. RewindTo(&t);
  2308. }
  2309. node->AddChildLast(ParseIdentifier());
  2310. GetToken(&t);
  2311. // Optional list of interfaces that are being implemented and classes that are being inherited
  2312. if( t.type == ttColon )
  2313. {
  2314. asCScriptNode *inherit = CreateNode(snIdentifier);
  2315. node->AddChildLast(inherit);
  2316. ParseOptionalScope(inherit);
  2317. inherit->AddChildLast(ParseIdentifier());
  2318. GetToken(&t);
  2319. while( t.type == ttListSeparator )
  2320. {
  2321. inherit = CreateNode(snIdentifier);
  2322. node->AddChildLast(inherit);
  2323. ParseOptionalScope(inherit);
  2324. inherit->AddChildLast(ParseIdentifier());
  2325. GetToken(&t);
  2326. }
  2327. }
  2328. if( t.type != ttStartStatementBlock )
  2329. {
  2330. Error(ExpectedToken("{"), &t);
  2331. return node;
  2332. }
  2333. // Parse properties
  2334. GetToken(&t);
  2335. RewindTo(&t);
  2336. while( t.type != ttEndStatementBlock && t.type != ttEnd )
  2337. {
  2338. // Is it a property or a method?
  2339. if( IsFuncDecl(true) )
  2340. node->AddChildLast(ParseFunction(true));
  2341. else if( IsVirtualPropertyDecl() )
  2342. node->AddChildLast(ParseVirtualPropertyDecl(true, false));
  2343. else if( IsVarDecl() )
  2344. node->AddChildLast(ParseDeclaration(true));
  2345. else if( t.type == ttEndStatement )
  2346. // Skip empty declarations
  2347. GetToken(&t);
  2348. else
  2349. {
  2350. Error(TXT_EXPECTED_METHOD_OR_PROPERTY, &t);
  2351. return node;
  2352. }
  2353. GetToken(&t);
  2354. RewindTo(&t);
  2355. }
  2356. GetToken(&t);
  2357. if( t.type != ttEndStatementBlock )
  2358. {
  2359. Error(ExpectedToken("}"), &t);
  2360. return node;
  2361. }
  2362. node->UpdateSourcePos(t.pos, t.length);
  2363. return node;
  2364. }
  2365. int asCParser::ParseVarInit(asCScriptCode *script, asCScriptNode *init)
  2366. {
  2367. Reset();
  2368. // Tell the parser to validate the identifiers as valid types
  2369. checkValidTypes = true;
  2370. this->script = script;
  2371. sourcePos = init->tokenPos;
  2372. // If next token is assignment, parse expression
  2373. sToken t;
  2374. GetToken(&t);
  2375. if( t.type == ttAssignment )
  2376. {
  2377. GetToken(&t);
  2378. RewindTo(&t);
  2379. if( t.type == ttStartStatementBlock )
  2380. scriptNode = ParseInitList();
  2381. else
  2382. scriptNode = ParseAssignment();
  2383. }
  2384. else if( t.type == ttOpenParanthesis )
  2385. {
  2386. RewindTo(&t);
  2387. scriptNode = ParseArgList();
  2388. }
  2389. else
  2390. {
  2391. int tokens[] = {ttAssignment, ttOpenParanthesis};
  2392. Error(ExpectedOneOf(tokens, 2), &t);
  2393. }
  2394. // Don't allow any more tokens after the expression
  2395. GetToken(&t);
  2396. if( t.type != ttEnd && t.type != ttEndStatement && t.type != ttListSeparator && t.type != ttEndStatementBlock )
  2397. {
  2398. asCString msg;
  2399. msg.Format(TXT_UNEXPECTED_TOKEN_s, asCTokenizer::GetDefinition(t.type));
  2400. Error(msg, &t);
  2401. }
  2402. if( isSyntaxError || errorWhileParsing )
  2403. return -1;
  2404. return 0;
  2405. }
  2406. asCScriptNode *asCParser::SuperficiallyParseVarInit()
  2407. {
  2408. asCScriptNode *node = CreateNode(snAssignment);
  2409. if( node == 0 ) return 0;
  2410. sToken t;
  2411. GetToken(&t);
  2412. node->UpdateSourcePos(t.pos, t.length);
  2413. if( t.type == ttAssignment )
  2414. {
  2415. GetToken(&t);
  2416. if( t.type == ttStartStatementBlock )
  2417. {
  2418. sToken start = t;
  2419. // Find the end of the initialization list
  2420. int indent = 1;
  2421. while( indent )
  2422. {
  2423. GetToken(&t);
  2424. if( t.type == ttStartStatementBlock )
  2425. indent++;
  2426. else if( t.type == ttEndStatementBlock )
  2427. indent--;
  2428. else if( t.type == ttNonTerminatedStringConstant )
  2429. {
  2430. Error(TXT_NONTERMINATED_STRING, &t);
  2431. break;
  2432. }
  2433. else if( t.type == ttEnd )
  2434. {
  2435. Error(TXT_UNEXPECTED_END_OF_FILE, &t);
  2436. Info(TXT_WHILE_PARSING_INIT_LIST, &start);
  2437. break;
  2438. }
  2439. }
  2440. }
  2441. else
  2442. {
  2443. sToken start = t;
  2444. // Find the end of the expression
  2445. int indent = 0;
  2446. while( indent || (t.type != ttListSeparator && t.type != ttEndStatement && t.type != ttEndStatementBlock) )
  2447. {
  2448. if( t.type == ttOpenParanthesis )
  2449. indent++;
  2450. else if( t.type == ttCloseParanthesis )
  2451. indent--;
  2452. else if( t.type == ttNonTerminatedStringConstant )
  2453. {
  2454. Error(TXT_NONTERMINATED_STRING, &t);
  2455. break;
  2456. }
  2457. else if( t.type == ttEnd )
  2458. {
  2459. Error(TXT_UNEXPECTED_END_OF_FILE, &t);
  2460. Info(TXT_WHILE_PARSING_EXPRESSION, &start);
  2461. break;
  2462. }
  2463. GetToken(&t);
  2464. }
  2465. // Rewind so that the next token read is the list separator, end statement, or end statement block
  2466. RewindTo(&t);
  2467. }
  2468. }
  2469. else if( t.type == ttOpenParanthesis )
  2470. {
  2471. sToken start = t;
  2472. // Find the end of the argument list
  2473. int indent = 1;
  2474. while( indent )
  2475. {
  2476. GetToken(&t);
  2477. if( t.type == ttOpenParanthesis )
  2478. indent++;
  2479. else if( t.type == ttCloseParanthesis )
  2480. indent--;
  2481. else if( t.type == ttNonTerminatedStringConstant )
  2482. {
  2483. Error(TXT_NONTERMINATED_STRING, &t);
  2484. break;
  2485. }
  2486. else if( t.type == ttEnd )
  2487. {
  2488. Error(TXT_UNEXPECTED_END_OF_FILE, &t);
  2489. Info(TXT_WHILE_PARSING_ARG_LIST, &start);
  2490. break;
  2491. }
  2492. }
  2493. }
  2494. else
  2495. {
  2496. int tokens[] = {ttAssignment, ttOpenParanthesis};
  2497. Error(ExpectedOneOf(tokens, 2), &t);
  2498. }
  2499. return node;
  2500. }
  2501. asCScriptNode *asCParser::SuperficiallyParseStatementBlock()
  2502. {
  2503. asCScriptNode *node = CreateNode(snStatementBlock);
  2504. if( node == 0 ) return 0;
  2505. // This function will only superficially parse the statement block in order to find the end of it
  2506. sToken t1;
  2507. GetToken(&t1);
  2508. if( t1.type != ttStartStatementBlock )
  2509. {
  2510. Error(ExpectedToken("{"), &t1);
  2511. return node;
  2512. }
  2513. node->UpdateSourcePos(t1.pos, t1.length);
  2514. sToken start = t1;
  2515. int level = 1;
  2516. while( level > 0 && !isSyntaxError )
  2517. {
  2518. GetToken(&t1);
  2519. if( t1.type == ttEndStatementBlock )
  2520. level--;
  2521. else if( t1.type == ttStartStatementBlock )
  2522. level++;
  2523. else if( t1.type == ttNonTerminatedStringConstant )
  2524. {
  2525. Error(TXT_NONTERMINATED_STRING, &t1);
  2526. break;
  2527. }
  2528. else if( t1.type == ttEnd )
  2529. {
  2530. Error(TXT_UNEXPECTED_END_OF_FILE, &t1);
  2531. Info(TXT_WHILE_PARSING_STATEMENT_BLOCK, &start);
  2532. break;
  2533. }
  2534. }
  2535. node->UpdateSourcePos(t1.pos, t1.length);
  2536. return node;
  2537. }
  2538. asCScriptNode *asCParser::ParseStatementBlock()
  2539. {
  2540. asCScriptNode *node = CreateNode(snStatementBlock);
  2541. if( node == 0 ) return 0;
  2542. sToken t1;
  2543. GetToken(&t1);
  2544. if( t1.type != ttStartStatementBlock )
  2545. {
  2546. Error(ExpectedToken("{"), &t1);
  2547. return node;
  2548. }
  2549. sToken start = t1;
  2550. node->UpdateSourcePos(t1.pos, t1.length);
  2551. for(;;)
  2552. {
  2553. while( !isSyntaxError )
  2554. {
  2555. GetToken(&t1);
  2556. if( t1.type == ttEndStatementBlock )
  2557. {
  2558. node->UpdateSourcePos(t1.pos, t1.length);
  2559. // Statement block is finished
  2560. return node;
  2561. }
  2562. else
  2563. {
  2564. RewindTo(&t1);
  2565. if( IsVarDecl() )
  2566. node->AddChildLast(ParseDeclaration());
  2567. else
  2568. node->AddChildLast(ParseStatement());
  2569. }
  2570. }
  2571. if( isSyntaxError )
  2572. {
  2573. // Search for either ';', '{', '}', or end
  2574. GetToken(&t1);
  2575. while( t1.type != ttEndStatement && t1.type != ttEnd &&
  2576. t1.type != ttStartStatementBlock && t1.type != ttEndStatementBlock )
  2577. {
  2578. GetToken(&t1);
  2579. }
  2580. // Skip this statement block
  2581. if( t1.type == ttStartStatementBlock )
  2582. {
  2583. // Find the end of the block and skip nested blocks
  2584. int level = 1;
  2585. while( level > 0 )
  2586. {
  2587. GetToken(&t1);
  2588. if( t1.type == ttStartStatementBlock ) level++;
  2589. if( t1.type == ttEndStatementBlock ) level--;
  2590. if( t1.type == ttEnd ) break;
  2591. }
  2592. }
  2593. else if( t1.type == ttEndStatementBlock )
  2594. {
  2595. RewindTo(&t1);
  2596. }
  2597. else if( t1.type == ttEnd )
  2598. {
  2599. Error(TXT_UNEXPECTED_END_OF_FILE, &t1);
  2600. Info(TXT_WHILE_PARSING_STATEMENT_BLOCK, &start);
  2601. return node;
  2602. }
  2603. isSyntaxError = false;
  2604. }
  2605. }
  2606. UNREACHABLE_RETURN;
  2607. }
  2608. asCScriptNode *asCParser::ParseInitList()
  2609. {
  2610. asCScriptNode *node = CreateNode(snInitList);
  2611. if( node == 0 ) return 0;
  2612. sToken t1;
  2613. GetToken(&t1);
  2614. if( t1.type != ttStartStatementBlock )
  2615. {
  2616. Error(ExpectedToken("{"), &t1);
  2617. return node;
  2618. }
  2619. node->UpdateSourcePos(t1.pos, t1.length);
  2620. GetToken(&t1);
  2621. if( t1.type == ttEndStatementBlock )
  2622. {
  2623. node->UpdateSourcePos(t1.pos, t1.length);
  2624. // Statement block is finished
  2625. return node;
  2626. }
  2627. else
  2628. {
  2629. RewindTo(&t1);
  2630. for(;;)
  2631. {
  2632. GetToken(&t1);
  2633. if( t1.type == ttListSeparator )
  2634. {
  2635. // No expression
  2636. node->AddChildLast(CreateNode(snUndefined));
  2637. GetToken(&t1);
  2638. if( t1.type == ttEndStatementBlock )
  2639. {
  2640. // No expression
  2641. node->AddChildLast(CreateNode(snUndefined));
  2642. node->UpdateSourcePos(t1.pos, t1.length);
  2643. return node;
  2644. }
  2645. RewindTo(&t1);
  2646. }
  2647. else if( t1.type == ttEndStatementBlock )
  2648. {
  2649. // No expression
  2650. node->AddChildLast(CreateNode(snUndefined));
  2651. node->UpdateSourcePos(t1.pos, t1.length);
  2652. // Statement block is finished
  2653. return node;
  2654. }
  2655. else if( t1.type == ttStartStatementBlock )
  2656. {
  2657. RewindTo(&t1);
  2658. node->AddChildLast(ParseInitList());
  2659. if( isSyntaxError ) return node;
  2660. GetToken(&t1);
  2661. if( t1.type == ttListSeparator )
  2662. continue;
  2663. else if( t1.type == ttEndStatementBlock )
  2664. {
  2665. node->UpdateSourcePos(t1.pos, t1.length);
  2666. // Statement block is finished
  2667. return node;
  2668. }
  2669. else
  2670. {
  2671. Error(ExpectedTokens("}", ","), &t1);
  2672. return node;
  2673. }
  2674. }
  2675. else
  2676. {
  2677. RewindTo(&t1);
  2678. node->AddChildLast(ParseAssignment());
  2679. if( isSyntaxError ) return node;
  2680. GetToken(&t1);
  2681. if( t1.type == ttListSeparator )
  2682. continue;
  2683. else if( t1.type == ttEndStatementBlock )
  2684. {
  2685. node->UpdateSourcePos(t1.pos, t1.length);
  2686. // Statement block is finished
  2687. return node;
  2688. }
  2689. else
  2690. {
  2691. Error(ExpectedTokens("}", ","), &t1);
  2692. return node;
  2693. }
  2694. }
  2695. }
  2696. }
  2697. UNREACHABLE_RETURN;
  2698. }
  2699. asCScriptNode *asCParser::ParseDeclaration(bool isClassProp, bool isGlobalVar)
  2700. {
  2701. asCScriptNode *node = CreateNode(snDeclaration);
  2702. if( node == 0 ) return 0;
  2703. sToken t;
  2704. GetToken(&t);
  2705. RewindTo(&t);
  2706. // A class property can be preceeded by private
  2707. if( t.type == ttPrivate && isClassProp )
  2708. node->AddChildLast(ParseToken(ttPrivate));
  2709. // Parse data type
  2710. node->AddChildLast(ParseType(true));
  2711. if( isSyntaxError ) return node;
  2712. for(;;)
  2713. {
  2714. // Parse identifier
  2715. node->AddChildLast(ParseIdentifier());
  2716. if( isSyntaxError ) return node;
  2717. if( isClassProp || isGlobalVar )
  2718. {
  2719. // Only superficially parse the initialization info for the class property
  2720. GetToken(&t);
  2721. RewindTo(&t);
  2722. if( t.type == ttAssignment || t.type == ttOpenParanthesis )
  2723. {
  2724. node->AddChildLast(SuperficiallyParseVarInit());
  2725. if( isSyntaxError ) return node;
  2726. }
  2727. }
  2728. else
  2729. {
  2730. // If next token is assignment, parse expression
  2731. GetToken(&t);
  2732. if( t.type == ttOpenParanthesis )
  2733. {
  2734. RewindTo(&t);
  2735. node->AddChildLast(ParseArgList());
  2736. if( isSyntaxError ) return node;
  2737. }
  2738. else if( t.type == ttAssignment )
  2739. {
  2740. GetToken(&t);
  2741. RewindTo(&t);
  2742. if( t.type == ttStartStatementBlock )
  2743. {
  2744. node->AddChildLast(ParseInitList());
  2745. if( isSyntaxError ) return node;
  2746. }
  2747. else
  2748. {
  2749. node->AddChildLast(ParseAssignment());
  2750. if( isSyntaxError ) return node;
  2751. }
  2752. }
  2753. else
  2754. RewindTo(&t);
  2755. }
  2756. // continue if list separator, else terminate with end statement
  2757. GetToken(&t);
  2758. if( t.type == ttListSeparator )
  2759. continue;
  2760. else if( t.type == ttEndStatement )
  2761. {
  2762. node->UpdateSourcePos(t.pos, t.length);
  2763. return node;
  2764. }
  2765. else
  2766. {
  2767. Error(ExpectedTokens(",", ";"), &t);
  2768. return node;
  2769. }
  2770. }
  2771. UNREACHABLE_RETURN;
  2772. }
  2773. asCScriptNode *asCParser::ParseStatement()
  2774. {
  2775. sToken t1;
  2776. GetToken(&t1);
  2777. RewindTo(&t1);
  2778. if( t1.type == ttIf )
  2779. return ParseIf();
  2780. else if( t1.type == ttFor )
  2781. return ParseFor();
  2782. else if( t1.type == ttWhile )
  2783. return ParseWhile();
  2784. else if( t1.type == ttReturn )
  2785. return ParseReturn();
  2786. else if( t1.type == ttStartStatementBlock )
  2787. return ParseStatementBlock();
  2788. else if( t1.type == ttBreak )
  2789. return ParseBreak();
  2790. else if( t1.type == ttContinue )
  2791. return ParseContinue();
  2792. else if( t1.type == ttDo )
  2793. return ParseDoWhile();
  2794. else if( t1.type == ttSwitch )
  2795. return ParseSwitch();
  2796. else
  2797. return ParseExpressionStatement();
  2798. }
  2799. asCScriptNode *asCParser::ParseExpressionStatement()
  2800. {
  2801. asCScriptNode *node = CreateNode(snExpressionStatement);
  2802. if( node == 0 ) return 0;
  2803. sToken t;
  2804. GetToken(&t);
  2805. if( t.type == ttEndStatement )
  2806. {
  2807. node->UpdateSourcePos(t.pos, t.length);
  2808. return node;
  2809. }
  2810. RewindTo(&t);
  2811. node->AddChildLast(ParseAssignment());
  2812. if( isSyntaxError ) return node;
  2813. GetToken(&t);
  2814. if( t.type != ttEndStatement )
  2815. {
  2816. Error(ExpectedToken(";"), &t);
  2817. return node;
  2818. }
  2819. node->UpdateSourcePos(t.pos, t.length);
  2820. return node;
  2821. }
  2822. asCScriptNode *asCParser::ParseSwitch()
  2823. {
  2824. asCScriptNode *node = CreateNode(snSwitch);
  2825. if( node == 0 ) return 0;
  2826. sToken t;
  2827. GetToken(&t);
  2828. if( t.type != ttSwitch )
  2829. {
  2830. Error(ExpectedToken("switch"), &t);
  2831. return node;
  2832. }
  2833. node->UpdateSourcePos(t.pos, t.length);
  2834. GetToken(&t);
  2835. if( t.type != ttOpenParanthesis )
  2836. {
  2837. Error(ExpectedToken("("), &t);
  2838. return node;
  2839. }
  2840. node->AddChildLast(ParseAssignment());
  2841. if( isSyntaxError ) return node;
  2842. GetToken(&t);
  2843. if( t.type != ttCloseParanthesis )
  2844. {
  2845. Error(ExpectedToken(")"), &t);
  2846. return node;
  2847. }
  2848. GetToken(&t);
  2849. if( t.type != ttStartStatementBlock )
  2850. {
  2851. Error(ExpectedToken("{"), &t);
  2852. return node;
  2853. }
  2854. while( !isSyntaxError )
  2855. {
  2856. GetToken(&t);
  2857. if( t.type == ttEndStatementBlock )
  2858. break;
  2859. RewindTo(&t);
  2860. if( t.type != ttCase && t.type != ttDefault )
  2861. {
  2862. const char *tokens[] = {"case", "default"};
  2863. Error(ExpectedOneOf(tokens, 2), &t);
  2864. return node;
  2865. }
  2866. node->AddChildLast(ParseCase());
  2867. if( isSyntaxError ) return node;
  2868. }
  2869. if( t.type != ttEndStatementBlock )
  2870. {
  2871. Error(ExpectedToken("}"), &t);
  2872. return node;
  2873. }
  2874. return node;
  2875. }
  2876. asCScriptNode *asCParser::ParseCase()
  2877. {
  2878. asCScriptNode *node = CreateNode(snCase);
  2879. if( node == 0 ) return 0;
  2880. sToken t;
  2881. GetToken(&t);
  2882. if( t.type != ttCase && t.type != ttDefault )
  2883. {
  2884. Error(ExpectedTokens("case", "default"), &t);
  2885. return node;
  2886. }
  2887. node->UpdateSourcePos(t.pos, t.length);
  2888. if(t.type == ttCase)
  2889. {
  2890. node->AddChildLast(ParseExpression());
  2891. }
  2892. GetToken(&t);
  2893. if( t.type != ttColon )
  2894. {
  2895. Error(ExpectedToken(":"), &t);
  2896. return node;
  2897. }
  2898. // Parse statements until we find either of }, case, default, and break
  2899. GetToken(&t);
  2900. RewindTo(&t);
  2901. while( t.type != ttCase &&
  2902. t.type != ttDefault &&
  2903. t.type != ttEndStatementBlock &&
  2904. t.type != ttBreak )
  2905. {
  2906. if( IsVarDecl() )
  2907. // Variable declarations are not allowed, but we parse it anyway to give a good error message
  2908. node->AddChildLast(ParseDeclaration());
  2909. else
  2910. node->AddChildLast(ParseStatement());
  2911. if( isSyntaxError ) return node;
  2912. GetToken(&t);
  2913. RewindTo(&t);
  2914. }
  2915. // If the case was ended with a break statement, add it to the node
  2916. if( t.type == ttBreak )
  2917. node->AddChildLast(ParseBreak());
  2918. return node;
  2919. }
  2920. asCScriptNode *asCParser::ParseIf()
  2921. {
  2922. asCScriptNode *node = CreateNode(snIf);
  2923. if( node == 0 ) return 0;
  2924. sToken t;
  2925. GetToken(&t);
  2926. if( t.type != ttIf )
  2927. {
  2928. Error(ExpectedToken("if"), &t);
  2929. return node;
  2930. }
  2931. node->UpdateSourcePos(t.pos, t.length);
  2932. GetToken(&t);
  2933. if( t.type != ttOpenParanthesis )
  2934. {
  2935. Error(ExpectedToken("("), &t);
  2936. return node;
  2937. }
  2938. node->AddChildLast(ParseAssignment());
  2939. if( isSyntaxError ) return node;
  2940. GetToken(&t);
  2941. if( t.type != ttCloseParanthesis )
  2942. {
  2943. Error(ExpectedToken(")"), &t);
  2944. return node;
  2945. }
  2946. node->AddChildLast(ParseStatement());
  2947. if( isSyntaxError ) return node;
  2948. GetToken(&t);
  2949. if( t.type != ttElse )
  2950. {
  2951. // No else statement return already
  2952. RewindTo(&t);
  2953. return node;
  2954. }
  2955. node->AddChildLast(ParseStatement());
  2956. return node;
  2957. }
  2958. asCScriptNode *asCParser::ParseFor()
  2959. {
  2960. asCScriptNode *node = CreateNode(snFor);
  2961. if( node == 0 ) return 0;
  2962. sToken t;
  2963. GetToken(&t);
  2964. if( t.type != ttFor )
  2965. {
  2966. Error(ExpectedToken("for"), &t);
  2967. return node;
  2968. }
  2969. node->UpdateSourcePos(t.pos, t.length);
  2970. GetToken(&t);
  2971. if( t.type != ttOpenParanthesis )
  2972. {
  2973. Error(ExpectedToken("("), &t);
  2974. return node;
  2975. }
  2976. if( IsVarDecl() )
  2977. node->AddChildLast(ParseDeclaration());
  2978. else
  2979. node->AddChildLast(ParseExpressionStatement());
  2980. if( isSyntaxError ) return node;
  2981. node->AddChildLast(ParseExpressionStatement());
  2982. if( isSyntaxError ) return node;
  2983. GetToken(&t);
  2984. if( t.type != ttCloseParanthesis )
  2985. {
  2986. RewindTo(&t);
  2987. asCScriptNode *n = CreateNode(snExpressionStatement);
  2988. if( n == 0 ) return 0;
  2989. node->AddChildLast(n);
  2990. n->AddChildLast(ParseAssignment());
  2991. if( isSyntaxError ) return node;
  2992. GetToken(&t);
  2993. if( t.type != ttCloseParanthesis )
  2994. {
  2995. Error(ExpectedToken(")"), &t);
  2996. return node;
  2997. }
  2998. }
  2999. node->AddChildLast(ParseStatement());
  3000. return node;
  3001. }
  3002. asCScriptNode *asCParser::ParseWhile()
  3003. {
  3004. asCScriptNode *node = CreateNode(snWhile);
  3005. if( node == 0 ) return 0;
  3006. sToken t;
  3007. GetToken(&t);
  3008. if( t.type != ttWhile )
  3009. {
  3010. Error(ExpectedToken("while"), &t);
  3011. return node;
  3012. }
  3013. node->UpdateSourcePos(t.pos, t.length);
  3014. GetToken(&t);
  3015. if( t.type != ttOpenParanthesis )
  3016. {
  3017. Error(ExpectedToken("("), &t);
  3018. return node;
  3019. }
  3020. node->AddChildLast(ParseAssignment());
  3021. if( isSyntaxError ) return node;
  3022. GetToken(&t);
  3023. if( t.type != ttCloseParanthesis )
  3024. {
  3025. Error(ExpectedToken(")"), &t);
  3026. return node;
  3027. }
  3028. node->AddChildLast(ParseStatement());
  3029. return node;
  3030. }
  3031. asCScriptNode *asCParser::ParseDoWhile()
  3032. {
  3033. asCScriptNode *node = CreateNode(snDoWhile);
  3034. if( node == 0 ) return 0;
  3035. sToken t;
  3036. GetToken(&t);
  3037. if( t.type != ttDo )
  3038. {
  3039. Error(ExpectedToken("do"), &t);
  3040. return node;
  3041. }
  3042. node->UpdateSourcePos(t.pos, t.length);
  3043. node->AddChildLast(ParseStatement());
  3044. if( isSyntaxError ) return node;
  3045. GetToken(&t);
  3046. if( t.type != ttWhile )
  3047. {
  3048. Error(ExpectedToken("while"), &t);
  3049. return node;
  3050. }
  3051. GetToken(&t);
  3052. if( t.type != ttOpenParanthesis )
  3053. {
  3054. Error(ExpectedToken("("), &t);
  3055. return node;
  3056. }
  3057. node->AddChildLast(ParseAssignment());
  3058. if( isSyntaxError ) return node;
  3059. GetToken(&t);
  3060. if( t.type != ttCloseParanthesis )
  3061. {
  3062. Error(ExpectedToken(")"), &t);
  3063. return node;
  3064. }
  3065. GetToken(&t);
  3066. if( t.type != ttEndStatement )
  3067. {
  3068. Error(ExpectedToken(";"), &t);
  3069. return node;
  3070. }
  3071. node->UpdateSourcePos(t.pos, t.length);
  3072. return node;
  3073. }
  3074. asCScriptNode *asCParser::ParseReturn()
  3075. {
  3076. asCScriptNode *node = CreateNode(snReturn);
  3077. if( node == 0 ) return 0;
  3078. sToken t;
  3079. GetToken(&t);
  3080. if( t.type != ttReturn )
  3081. {
  3082. Error(ExpectedToken("return"), &t);
  3083. return node;
  3084. }
  3085. node->UpdateSourcePos(t.pos, t.length);
  3086. GetToken(&t);
  3087. if( t.type == ttEndStatement )
  3088. {
  3089. node->UpdateSourcePos(t.pos, t.length);
  3090. return node;
  3091. }
  3092. RewindTo(&t);
  3093. node->AddChildLast(ParseAssignment());
  3094. if( isSyntaxError ) return node;
  3095. GetToken(&t);
  3096. if( t.type != ttEndStatement )
  3097. {
  3098. Error(ExpectedToken(";"), &t);
  3099. return node;
  3100. }
  3101. node->UpdateSourcePos(t.pos, t.length);
  3102. return node;
  3103. }
  3104. asCScriptNode *asCParser::ParseBreak()
  3105. {
  3106. asCScriptNode *node = CreateNode(snBreak);
  3107. if( node == 0 ) return 0;
  3108. sToken t;
  3109. GetToken(&t);
  3110. if( t.type != ttBreak )
  3111. {
  3112. Error(ExpectedToken("break"), &t);
  3113. return node;
  3114. }
  3115. node->UpdateSourcePos(t.pos, t.length);
  3116. GetToken(&t);
  3117. if( t.type != ttEndStatement )
  3118. Error(ExpectedToken(";"), &t);
  3119. node->UpdateSourcePos(t.pos, t.length);
  3120. return node;
  3121. }
  3122. asCScriptNode *asCParser::ParseContinue()
  3123. {
  3124. asCScriptNode *node = CreateNode(snContinue);
  3125. if( node == 0 ) return 0;
  3126. sToken t;
  3127. GetToken(&t);
  3128. if( t.type != ttContinue )
  3129. {
  3130. Error(ExpectedToken("continue"), &t);
  3131. return node;
  3132. }
  3133. node->UpdateSourcePos(t.pos, t.length);
  3134. GetToken(&t);
  3135. if( t.type != ttEndStatement )
  3136. Error(ExpectedToken(";"), &t);
  3137. node->UpdateSourcePos(t.pos, t.length);
  3138. return node;
  3139. }
  3140. // TODO: typedef: Typedefs should accept complex types as well
  3141. asCScriptNode *asCParser::ParseTypedef()
  3142. {
  3143. // Create the typedef node
  3144. asCScriptNode *node = CreateNode(snTypedef);
  3145. if( node == 0 ) return 0;
  3146. sToken token;
  3147. GetToken(&token);
  3148. if( token.type != ttTypedef)
  3149. {
  3150. Error(ExpectedToken(asCTokenizer::GetDefinition(token.type)), &token);
  3151. return node;
  3152. }
  3153. node->SetToken(&token);
  3154. node->UpdateSourcePos(token.pos, token.length);
  3155. // Parse the base type
  3156. GetToken(&token);
  3157. RewindTo(&token);
  3158. // Make sure it is a primitive type (except ttVoid)
  3159. if( !IsRealType(token.type) || token.type == ttVoid )
  3160. {
  3161. asCString str;
  3162. str.Format(TXT_UNEXPECTED_TOKEN_s, asCTokenizer::GetDefinition(token.type));
  3163. Error(str, &token);
  3164. return node;
  3165. }
  3166. node->AddChildLast(ParseRealType());
  3167. node->AddChildLast(ParseIdentifier());
  3168. // Check for the end of the typedef
  3169. GetToken(&token);
  3170. if( token.type != ttEndStatement )
  3171. {
  3172. RewindTo(&token);
  3173. Error(ExpectedToken(asCTokenizer::GetDefinition(token.type)), &token);
  3174. }
  3175. return node;
  3176. }
  3177. void asCParser::ParseMethodOverrideBehaviors(asCScriptNode *funcNode)
  3178. {
  3179. sToken t1;
  3180. for(;;)
  3181. {
  3182. GetToken(&t1);
  3183. RewindTo(&t1);
  3184. if( IdentifierIs(t1, FINAL_TOKEN) || IdentifierIs(t1, OVERRIDE_TOKEN) )
  3185. funcNode->AddChildLast(ParseIdentifier());
  3186. else
  3187. break;
  3188. }
  3189. }
  3190. #endif
  3191. END_AS_NAMESPACE