as_parser.cpp 96 KB

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