as_parser.cpp 80 KB

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