as_parser.cpp 74 KB

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