as_parser.cpp 94 KB

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