as_parser.cpp 95 KB

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