as_parser.cpp 80 KB

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