as_parser.cpp 64 KB

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