as_parser.cpp 77 KB

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