as_parser.cpp 97 KB

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