as_parser.cpp 86 KB

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