as_parser.cpp 87 KB

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