as_parser.cpp 63 KB

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