as_parser.cpp 97 KB

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