as_parser.cpp 74 KB

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