as_parser.cpp 65 KB

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