123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653 |
- ' Copyright (c) 2013-2016 Bruce A Henderson
- '
- ' Based on the public domain Monkey "trans" by Mark Sibly
- '
- ' This software is provided 'as-is', without any express or implied
- ' warranty. In no event will the authors be held liable for any damages
- ' arising from the use of this software.
- '
- ' Permission is granted to anyone to use this software for any purpose,
- ' including commercial applications, and to alter it and redistribute it
- ' freely, subject to the following restrictions:
- '
- ' 1. The origin of this software must not be misrepresented; you must not
- ' claim that you wrote the original software. If you use this software
- ' in a product, an acknowledgment in the product documentation would be
- ' appreciated but is not required.
- '
- ' 2. Altered source versions must be plainly marked as such, and must not be
- ' misrepresented as being the original software.
- '
- ' 3. This notice may not be removed or altered from any source
- ' distribution.
- '
- SuperStrict
- Import BRL.MaxUtil
- Import "toker.bmx"
- Const DECL_GLOBAL:Int = $10
- Const DECL_FIELD:Int = $20
- Const DECL_CONST:Int = $40
- Const DECL_LOCAL:Int = $80
- Type TIParser
- Field _toker:TToker
- Field _toke$
- Field _tokeType:Int
- Field _tokeSpace:Int
- Field _tokerStack:TList=New TList'<TToker>
- Method ParseModuleImport:Int(pmod:TModuleDecl, modpath:String, path:String, imp:String = Null, iData:String = Null, attrs:Int = 0, relPath:String = "", isFileImport:Int = 0)
- Const STATE_CLASS:Int = 1
- If Not modpath Then
- modpath = imp
- End If
- ' already imported??
- If _appInstance.IsImported(modpath)
- ' add import to the scope (so we can find decls in it later)
- ' but don't add it if pmod is the apps' main module
- If _appInstance.mainModule <> pmod Then
- pmod.AddImport(modpath, _appInstance.globalImports.ValueForKey(modpath))
- End If
- Return False
- Else If imp Then
- ' if "imp" is set, this is a file import. We need to check for it too, or we may end up importing it twice.
- If _appInstance.IsImported(imp)
- ' add import to the scope (so we can find decls in it later)
- ' but don't add it if pmod is the apps' main module
- If _appInstance.mainModule <> pmod Then
- pmod.AddImport(imp, _appInstance.globalImports.ValueForKey(imp))
- End If
- Return False
- End If
- End If
-
- Local prefix:String
- If isFileImport And opt_buildtype = BUILDTYPE_APP Then
- prefix = "m_"
- End If
- Local _mod:TModuleDecl = New TModuleDecl.Create(prefix + modpath, "bb" + modpath, path, attrs)
- Select modpath
- Case "brl.classes", "brl.blitzkeywords"
- _mod.UpdateFilePath(_mod.filepath + "." + modpath)
- End Select
- _mod.declImported = True
- _mod.relpath = relPath
- _mod.pmod = pmod
- If modpath = "brl.blitz" Then
- If pmod.imported.Contains(modpath) Then
- _mod = TModuleDecl(pmod.imported.ValueForKey(modpath))
- Else
- pmod.AddImport(modpath, _mod)
- End If
-
- ' import Object and String definitions
- Local par:TIParser = New TIParser
- If FileType(modulepath("brl.blitz") + "\blitz_classes." + opt_platform + ".i") Then
- par.ParseModuleImport(_mod, "brl.classes", modulepath("brl.blitz"), modulepath("brl.blitz") + "\blitz_classes." + opt_platform + ".i")
- Else
- par.ParseModuleImport(_mod, "brl.classes", modulepath("brl.blitz"), modulepath("brl.blitz") + "\blitz_classes.i")
- End If
-
- ' set up built-in keywords
- par = New TIParser
- par.ParseModuleImport(_mod, "brl.blitzkeywords", "", "", MakeKeywords())
- Else
- pmod.AddImport(modpath, _mod)
- End If
- _appInstance.globalImports.Insert(modpath, _mod)
-
- Local ipath:String
-
- 'Local ipath:String = path + "\" + ModuleIdent(modpath) + ".release.macos.x86.i"
- If imp Then
- ipath = imp
- ' add to imports
- pmod.AddImport(ipath, _mod)
- _appInstance.globalImports.Insert(ipath, _mod)
- Else
- ipath = path + "/" + ModuleIdent(modpath) + FileMung() + ".i"
- End If
-
- If Not iData Then
- If Not FileType(ipath) Then
- Err "Can't find interface for module '" + modpath + "'"
- Return False
- End If
- 'Local ifile:String[] = LoadString(ipath).Split("~n")
- _toker = New TToker.Create( ipath,LoadString( ipath ) )
- Else
- _toker = New TToker.Create( ipath, iData)
- End If
- Local toker:TToker = _toker
-
-
- Repeat
- Local pos:Int
-
- pos = toker._tokePos
- toker.NextToke
-
-
- Local line:Int
- Local state:Int
- Local class:TClassDecl
- Local stm:String
-
-
-
- Select toker.Toke().ToLower()
- Case "superstrict"
- _mod.attrs :| MODULE_SUPERSTRICT
- Continue
- Case "import"
-
- toker.NextToke()
- If toker.TokeType()=TOKE_SPACE toker.NextToke()
-
- ' skip non-module imports
- If toker.TokeType()=TOKE_STRINGLIT
- Local iRelPath:String = ParseStringLit()
- SetErr
-
- If iRelPath.EndsWith(".bmx") Then
-
- Local origPath:String = RealPath(ExtractDir(path) + "/" + iRelPath)
- Local iPath:String = OutputFilePath(origPath, FileMung(), "i")
-
- If FileType( iPath )<>FILETYPE_FILE
- Err "File '"+ iPath +"' not found."
- EndIf
-
-
- If _mod.imported.Contains( iPath ) Continue
-
- Local modpath:String
- If opt_buildtype = BUILDTYPE_MODULE Then
- Local dir:String = ExtractDir(origPath).ToLower()
- dir = dir[dir.findLast("/") + 1..]
- If dir.EndsWith(".mod") Then
- dir = ""
- Else
- dir :+ "_"
- End If
- Local file:String = StripDir(origPath).ToLower()
-
- modpath = opt_modulename + "_" + dir + StripExt(file)
- 'sanitize the path, remove non-allowed chars
- modpath = TStringHelper.Sanitize(modpath.ToLower())
- Else
- ' todo file imports for apps
- 'internalErr
- End If
- ' Local mdecl:TDecl=TDecl(pmod.GetDecl( modpath ))
- ' If Not mdecl
- New TIParser.ParseModuleImport( _mod, modpath, origPath, iPath, , , iRelPath)
- ' Else
- ' _mod.imported.Insert(modpath, mdecl)
- ' EndIf
- Else
- If iRelPath.StartsWith("-") Then
- If Not _mod.fileImports.Contains(iRelPath) Then
- _mod.fileImports.AddLast(iRelPath)
- End If
- End If
- End If
- Else
-
- Local m:String = toker._toke
- toker.NextToke()
-
- Parse(".")
-
- m :+ "." + toker._toke
-
- SetErr
- Local mdecl:TDecl=TDecl(pmod.GetDecl( m ))
-
- If Not mdecl
- Local path:String = modulepath(m)
- ' parse the imported module
- New TIParser.ParseModuleImport( _mod, m, path )
- Else
- _mod.AddImport(m, mdecl)
- EndIf
- End If
-
- Continue
-
-
- Case "moduleinfo"
- toker.nextToke()
- If toker.TokeType()=TOKE_SPACE toker.NextToke()
- Continue
- Case "~r", "~n"
- Continue
- Default
- stm = toker.Toke()
-
- Local v:String = toker.NextToke()
- Select v
- Case "^"
- toker.rollback(pos)
- toker.NextToke()
- ' class decl
- class = ParseClassDecl( stm,0 )
- class.declImported = True
- _mod.InsertDecl(class)
- If CParse("F")
- class.attrs :| DECL_FINAL
- Else If CParse("A")
- class.attrs :| DECL_ABSTRACT
- Else If CParse("S")
- class.attrs :| CLASS_STRUCT
- Else If CParse("AF")
- class.attrs :| DECL_ABSTRACT | DECL_FINAL
- Else If CParse("E")
- class.attrs :| DECL_EXTERN
- ApplyFunctionAttributes(class, DECL_EXTERN)
- Else If CParse("EW")
- class.attrs :| DECL_EXTERN | DECL_API_WIN32
- ApplyFunctionAttributes(class, DECL_EXTERN | DECL_API_WIN32)
-
- Else If CParse("AI")
- class.attrs :| CLASS_INTERFACE | DECL_ABSTRACT
- ApplyFunctionAttributes(class, DECL_ABSTRACT)
- Else If CParse("EI")
- class.attrs :| DECL_EXTERN | CLASS_INTERFACE
- ApplyFunctionAttributes(class, DECL_EXTERN | DECL_ABSTRACT)
- Else If CParse("EIW")
- class.attrs :| DECL_EXTERN | CLASS_INTERFACE | DECL_API_WIN32
- ApplyFunctionAttributes(class, DECL_EXTERN | DECL_ABSTRACT | DECL_API_WIN32)
- Else If CParse("ES")
- class.attrs :| DECL_EXTERN | CLASS_STRUCT
- Else If CParse("ESW")
- class.attrs :| DECL_EXTERN | CLASS_STRUCT | DECL_API_WIN32
- End If
- 'DebugStop
- If CParse( "=" )
- 'DebugStop
- If Not class.IsExtern() Then
- class.munged=ParseStringLit()
- If class.ident <> "String" Then
- For Local fdecl:TFieldDecl = EachIn class._decls
- fdecl.munged = "_" + class.munged + "_" + fdecl.ident
- fdecl.munged = fdecl.munged.ToLower()
- Next
- End If
- Else
- Parse "0"
- If Not class.munged Then
- class.munged = class.ident
-
- End If
- End If
- EndIf
- 'state = STATE_CLASS
- 'Exit
- ' Case "%"
- Default
- If toker._tokeType = TOKE_EOF
- 'DebugStop
- Exit
- End If
- Local a:Int
- Local ty:TType = ParseDeclType(a)
- If CParse("(") Then
- toker.rollback(pos)
- toker.NextToke()
- Local decl:TFuncDecl = ParseFuncDecl( _toke, 0 )
- decl.declImported = True
- ' an array of function pointers?
- If CParse( "&" ) Then
- End If
- While IsArrayDef()
- ty = ParseArrayType(ty)
-
- If CParse( "&" ) Then
- End If
- Wend
-
- If decl.attrs & FUNC_PTR Then
- Local fpty:TType = New TFunctionPtrType
- TFunctionPtrType(fpty).func = decl
-
- If TArrayType(ty) Then
- TArrayType(ty).elemType = fpty
- Else
- ty = fpty
- End If
-
- 'Local declInit:TExpr = decl.declInit
- 'decl.declInit = Null
- Local gdecl:TGlobalDecl = New TGlobalDecl.Create( decl.ident,ty, Null, DECL_GLOBAL )
- gdecl.munged = decl.munged
- _mod.InsertDecl gdecl
- gdecl.declImported = True
-
- If CParse( "=" )
-
- If CParse("mem")
-
- If CParse(":")
- If CParse("p")
- If CParse("(") Then
-
- gdecl.munged = ParseStringLit()
-
- ' for function pointers, ensure actual function reference is set too.
- 'If TFunctionPtrType(gdecl.declTy) Then
- ' TFunctionPtrType(gdecl.declTy).func.munged = gdecl.munged
- 'Else If TArrayType(gdecl.declTy) Then
- '
- 'End If
- TFunctionPtrType(fpty).func.munged = gdecl.munged
-
- Parse(")")
-
- EndIf
- End If
- Else
- If CParse("(") Then
-
- gdecl.munged = ParseStringLit()
-
- Parse(")")
-
- EndIf
- End If
- Else
- If TStringType(ty)
- If CParse("$") Then
- gdecl.declInit = ParseUnaryExpr()
- End If
- Else
- ' a default value ?
- gdecl.declInit = ParseUnaryExpr()
- End If
- End If
- End If
-
- Else
- _mod.InsertDecl decl
- End If
-
- Else
- toker.rollback(pos)
- toker.NextToke()
- Local decl:TDecl = ParseDecl( _toke, DECL_CONST | DECL_EXTERN)'DECL_GLOBAL | DECL_EXTERN )
- _mod.InsertDecl decl
- decl.declImported = True
- End If
- End Select
-
- End Select
- line :+ 1
-
- Forever
-
-
- Return True
-
- End Method
- Method ParseUnaryExpr:TExpr()
- SkipEols
-
- Local op$=_toke
- Select op
- Case "+","-","~~","not"
- NextToke
- Local expr:TExpr=ParseUnaryExpr()
- Return New TUnaryExpr.Create( op,expr )
- End Select
- Return ParsePrimaryExpr( False )
- End Method
- Method ParsePrimaryExpr:TExpr( stmt:Int )
- Local expr:TExpr
- Select _tokeType
- 'Case TOKE_IDENT
- ' expr=New TIdentExpr.Create( ParseIdent() )
- Case TOKE_INTLIT
- expr=New TConstExpr.Create( New TIntType,_toke )
- NextToke
- Case TOKE_LONGLIT
- expr=New TConstExpr.Create( New TLongType,_toke )
- NextToke
- Case TOKE_FLOATLIT
- Local value:String = _toke
- NextToke
- If CParse("!") Then
- expr=New TConstExpr.Create( New TDoubleType,value )
- Else
- CParse("#")
- expr=New TConstExpr.Create( New TFloatType,value )
- End If
- Case TOKE_STRINGLIT
- expr=New TConstExpr.Create( New TStringType,BmxUnquote( _toke, True ) )
- NextToke
- Case TOKE_IDENT
- If _toke = "nan" Or _toke = "inf" Then
- Local value:String = _toke
- NextToke
- If CParse("!") Then
- expr=New TConstExpr.Create( New TDoubleType,value )
- Else
- CParse("#")
- expr=New TConstExpr.Create( New TFloatType,value )
- End If
- Else
- Err "Syntax error - unexpected token '"+_toke+"'"
- End If
- Default
- Err "Syntax error - unexpected token '"+_toke+"'"
- End Select
- Return expr
-
- End Method
- Method ParseClassDecl:TClassDecl( toke$,attrs:Int )
- SetErr
- 'If toke Parse toke
-
- Local id$=ParseIdent()
- Local args:String[]
- Local superTy:TIdentType
- Local imps:TIdentType[]
- If CParse( "^" )
- If CParse( "null" )
-
- superTy=Null
-
- Else
- superTy=ParseIdentType()
- 'If superTy.ident <> "Object" Then
- ' superTy = TIdentType(superTy.Semant())
- 'EndIf
- EndIf
- Else
- superTy = New TIdentType.Create( "brl.classes.object" )
- EndIf
- ' implements
- If CParse("@") Then
- Local nimps:Int
- Repeat
- If imps.Length=nimps imps=imps + New TIdentType[10]
- imps[nimps]=ParseIdentType()
- nimps:+1
- Until Not CParse(",")
- imps=imps[..nimps]
- End If
-
- Local classDecl:TClassDecl=New TClassDecl.Create( id,args,superTy,imps,attrs )
-
- If classDecl.IsExtern()
- classDecl.munged=classDecl.ident
- If CParse( "=" ) classDecl.munged=ParseStringLit()
- EndIf
-
- 'If classDecl.IsTemplateArg() Return classDecl
- Local decl_attrs:Int=(attrs & DECL_EXTERN)
-
- Local method_attrs:Int=decl_attrs
- If attrs & CLASS_INTERFACE method_attrs:|DECL_ABSTRACT
- Repeat
- SkipEols
- 'If IsSpace(Asc(_toker._toke))
- ' _toker.NextToke
- 'End If
-
- Select _toker._toke
- Case "{"
- '_toker.
- NextToke
- Case "}"
- '_toker.
- NextToke
- Exit
- Case "-" ' method
- 'DebugStop
- '_toker.
- NextToke
-
- Local decl:TFuncDecl = ParseFuncDecl( _toke,method_attrs|FUNC_METHOD )
- 'If decl.IsCtor() decl.retTypeExpr=New TObjectType.Create( classDecl )
- classDecl.InsertDecl decl
-
- Case "+" ' function
- NextToke
-
- Local decl:TFuncDecl = ParseFuncDecl( _toke,method_attrs )
- 'If decl.IsCtor() decl.retTypeExpr=New TObjectType.Create( classDecl )
- classDecl.InsertDecl decl
- Case "." ' field
- NextToke
- decl_attrs :| DECL_FIELD
- Local decl:TDecl= ParseDecl( _toke,decl_attrs )
- classDecl.InsertDecl decl
- Rem
- Case "private"
- NextToke
- decl_attrs=decl_attrs | DECL_PRIVATE
- Case "public"
- NextToke
- decl_attrs=decl_attrs & ~DECL_PRIVATE
- Case "const","global","field"
- If (attrs & CLASS_INTERFACE) And _toke<>"const"
- Err "Interfaces can only contain constants and methods."
- EndIf
- classDecl.InsertDecls ParseDecls( _toke,decl_attrs )
- Case "method"
- Local decl:TFuncDecl=ParseFuncDecl( _toke,method_attrs )
- If decl.IsCtor() decl.retTypeExpr=New TObjectType.Create( classDecl )
- classDecl.InsertDecl decl
- Case "function"
- If (attrs & CLASS_INTERFACE) And _toke<>"const"
- Err "Interfaces can only contain constants and methods."
- EndIf
- Local decl:TFuncDecl=ParseFuncDecl( _toke,decl_attrs )
- classDecl.InsertDecl decl
- End Rem
- 'Default
- ' Err "Syntax error - expecting class member declaration."
- End Select
-
- If _toker._tokeType = TOKE_IDENT Then
- ' Const / Global?
- 'NextToke
- 'decl_attrs :| DECL_CONST
-
- Local decl:TDecl= ParseDecl( _toke,decl_attrs | DECL_CONST)
- classDecl.InsertDecl decl
- End If
-
- Forever
-
- If toke CParse toke
- Return classDecl
- End Method
- Method Parse( toke$ )
- If Not CParse( toke )
- Err "Syntax error - expecting '"+toke+"'."
- EndIf
- End Method
- Method ParseIdent$()
- Select _toker._toke.tolower()
- Case "@" _toker.NextToke
- Case "string","___array","object"
- Default
- If _toker._tokeType<>TOKE_IDENT Err "Syntax error - expecting identifier."
- End Select
- Local id$=_toker._toke
- NextToke
- Return id
- End Method
- Method ParseIdentType:TIdentType()
- Local id$=ParseIdent()
-
- While CParse( "." )
- id:+"."+ParseIdent()
- Wend
- Local args:TIdentType[]
- Return New TIdentType.Create( id,args )
- End Method
- Method NextToke$()
- Local toke$=_toke
-
- _tokeSpace=False
-
- Repeat
- _toke=_toker.NextToke()
- _tokeType=_toker.TokeType()
- If _tokeType<>TOKE_SPACE Exit
- _tokeSpace=True
- Forever
-
- If _tokeType=TOKE_KEYWORD _toke=_toke.ToLower()
- If toke="," SkipEols
- Return _toke
- End Method
-
- Method CParse:Int( toke$ )
- If _toker._toke.tolower()<>toke.tolower()
- Return False
- EndIf
- '_toker.
- NextToke
- Return True
- End Method
- Method CParseToker:Int( toker:TToker, toke$ )
- If toker._toke.ToLower()<>toke
- Return False
- EndIf
- NextTokeToker(toker)
- Return True
- End Method
- Method NextTokeToker$(toker:TToker)
- Repeat
- toker.NextToke()
- Until toker.tokeType()<>TOKE_SPACE
- Return toker._toke
- End Method
- Method SkipEols()
- Local found:Int = True
- While found
- found = False
- If CParse( "~n" )
- found = True
- End If
- If CParse("~r")
- found = True
- End If
- Wend
-
- SetErr
- End Method
- Method ParseStringLit$()
- If _toker._tokeType<>TOKE_STRINGLIT Err "Expecting string literal."
- Local str$=BmxUnquote( _toker._toke, True )
- '_toker.
- NextToke
- Return str
- End Method
- Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int, returnType:TType = Null )
- SetErr
- 'If toke Parse toke
-
- Local id$
- Local ty:TType
- Local meth:Int = attrs & FUNC_METHOD
- If Not returnType Then
- If attrs & FUNC_METHOD
- If _toker._toke.tolower() = "new"
- If attrs & DECL_EXTERN
- Err "Extern classes cannot have constructors"
- EndIf
- id=_toker._toke
- NextToke
- attrs:|FUNC_CTOR
- attrs:&~FUNC_METHOD
- ty=ParseDeclType(attrs, True)
- Else
- If _toker._tokeType = TOKE_STRINGLIT Then
- id = ParseStringLit()
- Else
- id=ParseIdent()
- End If
- ty=ParseDeclType(attrs, True)
- EndIf
- Else
- id=ParseIdent()
- ty=ParseDeclType(attrs, True)
- EndIf
- End If
-
- Local args:TArgDecl[]
-
- Parse "("
- SkipEols
- If _toker._toke<>")"
- Local nargs:Int
- Repeat
- Local pos:Int, tokeType:Int
- pos = _toker._tokePos
- tokeType = _toker._tokeType
- 'DebugStop
- Local id$=ParseIdent()
- 'If id = "compareFunc" DebugStop
- Local ty:TType=ParseDeclType(attrs)
- Local init:TExpr
- If CParse( "(") Then
- 'DebugStop
- ' function pointer
- _toker.rollback(pos, tokeType)
- _toker._toke = id
- '_toker.NextToke()
- Local decl:TFuncDecl = ParseFuncDecl( id, FUNC_PTR | FUNC_INIT )
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = decl
-
- End If
-
- If CParse("Var") Then
- ty = TType.MapToVarType(ty)
- End If
- If CParse( "=" ) Then
- 'DebugLog "TODO : parse default values..."
- If CParse("$") Then
- ' a string default
- init = ParseUnaryExpr()
- Else
- If Not TFunctionPtrType(ty) Then
- init = ParseUnaryExpr()
- If TArrayType(ty) Then
- If TConstExpr(init) And TConstExpr(init).value="bbEmptyArray" Then
- init = New TNullExpr.Create(TType.nullObjectType)
- End If
- Else If TObjectType(ty) Or TIdentType(ty) Then
- If TConstExpr(init) And TConstExpr(init).value="bbNullObject" Then
- init = New TNullExpr.Create(TType.nullObjectType)
- End If
- End If
- Else
- ' munged reference to default function pointer
- Local defaultFunc:String = ParseStringLit()
- Local func:TFuncDecl = TFuncDecl(TFunctionPtrType(ty).func.Copy())
- init = New TInvokeExpr.Create(func)
- func.munged = defaultFunc
- init.exprType = ty
-
- End If
- End If
- ' has a default value
- 'DebugStop
- 'init=ParseExpr()
- End If
-
- Local arg:TArgDecl=New TArgDecl.Create( id,ty,init )
- If args.Length=nargs args=args + New TArgDecl[10]
- args[nargs]=arg
- nargs:+1
- If _toker._toke=")" Exit
- Parse ","
- Forever
- args=args[..nargs]
- EndIf
- Parse ")"
-
- If returnType Then
- Return New TFuncDecl.CreateF(Null, returnType, args, 0)
- End If
- Local fdecl:TFuncDecl
- ' wait.. so everything until now was a function pointer return type, and we still have to process the function declaration...
- If _toke = "(" Then
- Local retTy:TType = New TFunctionPtrType
- TFunctionPtrType(retTy).func = New TFuncDecl.CreateF("",ty,args,attrs )
- TFunctionPtrType(retTy).func.attrs :| FUNC_PTR
- fdecl = ParseFuncDecl("", attrs, retTy)
- ty = retTy
- End If
- Repeat
- If CParse( "F" )
- attrs:|DECL_FINAL
- Else If CParse( "FW" )
- attrs:|DECL_FINAL | DECL_API_WIN32
- Else If CParse( "FP" )
- attrs:|DECL_FINAL|DECL_PRIVATE
- Else If CParse( "FPW" )
- attrs:|DECL_FINAL|DECL_PRIVATE| DECL_API_WIN32
- Else If CParse( "FR" )
- attrs:|DECL_FINAL|DECL_PROTECTED
- Else If CParse( "FRW" )
- attrs:|DECL_FINAL|DECL_PROTECTED| DECL_API_WIN32
- Else If CParse( "A" )
- attrs:|DECL_ABSTRACT
- Else If CParse( "AW" )
- attrs:|DECL_ABSTRACT | DECL_API_WIN32
- Else If CParse( "AP" )
- attrs:|DECL_ABSTRACT|DECL_PRIVATE
- Else If CParse( "APW" )
- attrs:|DECL_ABSTRACT|DECL_PRIVATE| DECL_API_WIN32
- Else If CParse( "AR" )
- attrs:|DECL_ABSTRACT|DECL_PROTECTED
- Else If CParse( "ARW" )
- attrs:|DECL_ABSTRACT|DECL_PROTECTED| DECL_API_WIN32
- Else If CParse( "W" )
- attrs:|DECL_API_WIN32
- Else If CParse( "O" )
- attrs:|FUNC_OPERATOR
- Else If CParse( "OW" )
- attrs:|FUNC_OPERATOR| DECL_API_WIN32
- Else If CParse( "OP" )
- attrs:|FUNC_OPERATOR|DECL_PRIVATE
- Else If CParse( "OPW" )
- attrs:|FUNC_OPERATOR|DECL_PRIVATE| DECL_API_WIN32
- Else If CParse( "OR" )
- attrs:|FUNC_OPERATOR|DECL_PROTECTED
- Else If CParse( "ORW" )
- attrs:|FUNC_OPERATOR|DECL_PROTECTED| DECL_API_WIN32
- Else If CParse( "P" )
- attrs:|DECL_PRIVATE
- Else If CParse( "PW" )
- attrs:|DECL_PRIVATE| DECL_API_WIN32
- Else If CParse( "R" )
- attrs:|DECL_PROTECTED
- Else If CParse( "RW" )
- attrs:|DECL_PROTECTED| DECL_API_WIN32
- Else If CParse( "FO" )
- attrs:|DECL_FINAL|FUNC_OPERATOR
- Else If CParse( "FOW" )
- attrs:|DECL_FINAL|FUNC_OPERATOR| DECL_API_WIN32
- Else If CParse( "FOP" )
- attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PRIVATE
- Else If CParse( "FOPW" )
- attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PRIVATE| DECL_API_WIN32
- Else If CParse( "FOR" )
- attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PROTECTED
- Else If CParse( "FORW" )
- attrs:|DECL_FINAL|FUNC_OPERATOR|DECL_PROTECTED| DECL_API_WIN32
- Else If CParse( "AO" )
- attrs:|DECL_ABSTRACT|FUNC_OPERATOR
- Else If CParse( "AOW" )
- attrs:|DECL_ABSTRACT|FUNC_OPERATOR| DECL_API_WIN32
- Else If CParse( "AOP" )
- attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PRIVATE
- Else If CParse( "AOPW" )
- attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PRIVATE| DECL_API_WIN32
- Else If CParse( "AOR" )
- attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PROTECTED
- Else If CParse( "AORW" )
- attrs:|DECL_ABSTRACT|FUNC_OPERATOR|DECL_PROTECTED| DECL_API_WIN32
- 'Else If CParse( "property" )
- ' If attrs & FUNC_METHOD
- ' attrs:|FUNC_PROPERTY
- ' Else
- ' Err "Only methods can be properties."
- ' EndIf
- Else
- Exit
- EndIf
- Forever
-
- Local funcDecl:TFuncDecl
- If attrs & FUNC_CTOR Then
- funcDecl = New TNewDecl.CreateF( id,ty,args,attrs )
- Else
- If fdecl Then
- funcDecl = fdecl
- funcDecl.ident = id
- Else
- funcDecl = New TFuncDecl.CreateF( id,ty,args,attrs )
- End If
- End If
-
- funcDecl.retType = ty
-
- If CParse("&") Then
- funcDecl.attrs :| DECL_POINTER
- End If
- 'If funcDecl.IsExtern()
- If Not (funcDecl.attrs & (FUNC_PTR | FUNC_INIT)) Then
- ' funcDecl.munged=funcDecl.ident
- If CParse( "=" )
- If CParse("mem")
- If CParse(":")
- If CParse("p")
- If CParse("(") Then
-
- funcDecl.munged = ParseStringLit()
-
- Cparse(")")
- EndIf
- End If
- End If
- Else
- funcDecl.munged=ParseStringLit()
- End If
- End If
- End If
-
- ' read function cast stuff
- If CParse(":") Then
- ' ret type
- Local rt$=_toker._toke
- If CParse("unsigned") Then
- rt :+ " " + _toker._toke
- End If
- NextToke
- If CParse("*") Then
- rt:+ "*"
-
- If CParse("*") Then
- rt:+ "*"
- End If
- End If
-
- funcDecl.castTo = rt
- ' fname
- Local fn$=_toker._toke
- NextToke
- ' args
- Parse("(")
- If Not CParse(")") Then
- Local i:Int = 0
- Repeat
- Local at$=_toker._toke
-
- If CParse("const") Then
- at :+ " " + _toker._toke
- End If
-
- If CParse("unsigned") Then
- at :+ " " + _toker._toke
- End If
- NextToke
- If CParse("*") Then
- at:+ "*"
-
- If CParse("*") Then
- at:+ "*"
- End If
- End If
- ' function pointer
- If CParse("(") Then
- Parse("*")
- Parse(")")
- at :+ "(*)"
-
- Parse("(")
- at :+ "("
-
- While Not CParse(")")
- NextToke
- at :+ _toker._toke
- Wend
-
- at :+ ")"
- End If
- args[i].castTo = at
-
-
- If _toker._toke=")" Exit
- Parse ","
-
- i:+ 1
- Forever
- End If
-
- End If
-
- ' Return funcDecl
- 'EndIf
-
- If funcDecl.attrs & DECL_POINTER Then
- funcDecl.attrs :| FUNC_PTR
- End If
-
- 'If funcDecl.IsAbstract() Return funcDecl
- Return funcDecl
-
- End Method
-
- Method ParseDecl:TDecl( toke$,attrs:Int )
- SetErr
- Local pos:Int, tokeType:Int
- pos = _toker._tokePos
- tokeType = _toker._tokeType
- Local id$=ParseIdent()
- Local ty:TType
- Local init:TExpr
- If attrs & DECL_EXTERN
- ty=ParseDeclType(attrs)
- 'Else If CParse( ":=" )
- ' init=ParseExpr()
- Else
- ty=ParseDeclType(attrs)
-
-
- If CParse( "(") Then
- 'DebugStop
- ' function pointer
- _toker.rollback(pos, tokeType)
- _toker._toke = id
- '_toker.NextToke()
- Local decl:TFuncDecl = ParseFuncDecl( id, FUNC_PTR | FUNC_INIT )
- ty = New TFunctionPtrType
- TFunctionPtrType(ty).func = decl
-
- If attrs & DECL_FIELD Then
- decl.attrs :| FUNC_METHOD
- End If
-
- ' an array of function pointers?
- If CParse( "&" ) Then
- End If
- While IsArrayDef()
- ty = ParseArrayType(ty)
-
- If CParse( "&" ) Then
- End If
- Wend
- End If
-
- If CParse("`") Then
- If CParse("`") Then
- attrs :| DECL_PROTECTED
- Else
- attrs :| DECL_PRIVATE
- End If
- End If
-
- Rem
- If CParse( "=" )
- ' TODO init=ParseExpr()
- If CParse("$") Then
- ' string value
- init = ParseUnaryExpr()
- Else
- init = ParseUnaryExpr()
- End If
- 'DebugLog "TODO : ParseExpression"
- Else If CParse( "[" )
- 'Local ln:TExpr=ParseExpr()
- Parse "]"
- 'While CParse( "[]" )
- ' ty=New TArrayType.Create(ty)
- 'Wend
- 'init=New TNewArrayExpr.Create( ty,ln)
- 'ty=New TArrayType.Create( ty )
- Else If toke<>"const"
- init=New TConstExpr.Create( ty,"" )
- Else
- Err "Constants must be initialized."
- EndIf
- End Rem
- EndIf
- Local decl:TValDecl
-
- If attrs & DECL_GLOBAL
- decl=New TGlobalDecl.Create( id,ty,init,attrs )
- Else If attrs & DECL_FIELD
- decl=New TFieldDecl.Create( id,ty,init,attrs )
- Else If attrs & DECL_CONST
- decl=New TConstDecl.Create( id,ty,init,attrs )
- Else If attrs & DECL_LOCAL
- decl=New TLocalDecl.Create( id,ty,init,attrs )
- EndIf
- 'DebugStop
- ' If decl.IsExtern()
- If CParse( "=" )
- If CParse("mem")
- ' Change to global
- ' Until this point, it was "probably" a const, but now we know for sure
- ' that it must be a global.
- If attrs & DECL_CONST Then
- attrs :| DECL_GLOBAL
- attrs :~ DECL_CONST
- decl=New TGlobalDecl.Create( id,ty,init,attrs )
- End If
-
-
- If CParse(":")
- If CParse("p")
- If CParse("(") Then
- decl.munged = ParseStringLit()
- ' for function pointers, ensure actual function reference is set too.
- If TFunctionPtrType(decl.declTy) Then
- TFunctionPtrType(decl.declTy).func.munged = decl.munged
- End If
- Parse(")")
- EndIf
- End If
- Else
- If CParse("(") Then
- decl.munged = ParseStringLit()
- Parse(")")
- EndIf
- End If
- Else
- If TStringType(ty)
- If CParse("$") Then
- decl.declInit = ParseUnaryExpr()
- End If
- Else
- ' a default value ?
- decl.declInit = ParseUnaryExpr()
- End If
- End If
-
-
-
- Else
- decl.munged=decl.ident
- EndIf
- ' EndIf
- Return decl
- End Method
- ' replaces While CParse( "[]" ) sections, with support for multi-dimension arrays
- Method ParseArrayType:TType(ty:TType)
- While True
- Local dims:Int = 1
-
- If CParse("[]") Then
- ty=New TArrayType.Create( ty )
- Exit
- End If
-
- If Not CParse("[") Then
- Exit
- End If
-
- While CParse( ",")
- dims :+ 1
- Wend
-
- Parse "]"
-
- ty=New TArrayType.Create( ty, dims )
- Exit
- Wend
- Return ty
- End Method
- Method IsArrayDef:Int()
- Local isDef:Int = True
- Local toker:TToker=New TToker.Copy(_toker)
- While True
- If CParseToker(toker, "[]") Then
- Exit
- End If
-
- If Not CParseToker(toker, "[") Then
- isDef = False
- Exit
- End If
-
- While CParseToker(toker, ",")
- Wend
-
- If Not CParseToker(toker, "]") Then
- isDef = False
- Exit
- End If
- Exit
- Wend
- Return isDef
- End Method
- Method ParseDeclType:TType(attrs:Int Var, fn:Int = False)
- Local ty:TType
- Select _toker._toke
- 'Case "?"
- ' NextToke
- ' ty=TType.boolType
- Case "%"
- NextToke
- ty=New TIntType
- If CParse("%") Then
- ty = New TLongType
- ElseIf CParse("z") Then
- ty = New TSizetType
- ElseIf CParse("j") Then
- ty = New TInt128Type
- ElseIf CParse("w") Then
- ty = New TWParamType
- ElseIf CParse("x") Then
- ty = New TLParamType
- End If
-
- If CParse("&") And Not (attrs & DECL_FIELD) Then
- attrs :| DECL_GLOBAL
- attrs :~ DECL_CONST
- End If
- ' pointer
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
-
- Case "|"
- NextToke
- ty=New TUIntType
- If CParse("|") Then
- ty = New TULongType
- End If
-
- If CParse("&") And Not (attrs & DECL_FIELD) Then
- attrs :| DECL_GLOBAL
- attrs :~ DECL_CONST
- End If
- ' pointer
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
-
- Case "#"
- NextToke
- ty=New TFloatType
- If CParse("&") And Not (attrs & DECL_FIELD) Then
- attrs :| DECL_GLOBAL
- attrs :~ DECL_CONST
- End If
- ' pointer
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
-
- Case "$"
- NextToke
- ty=New TStringType
- If CParse("z") Then
- ty._flags :| TType.T_CHAR_PTR
- Else If CParse("w") Then
- ty._flags :| TType.T_SHORT_PTR
- End If
- If CParse( "&" ) And Not (attrs & DECL_FIELD)
- attrs :| DECL_GLOBAL
- attrs :~ DECL_CONST
- End If
- Case "!"
- NextToke
- ty=New TDoubleType
-
- If CParse("k") Then
- ty = New TFloat128Type
- Else If CParse("m") Then
- ty = New TDouble128Type
- Else If CParse("h") Then
- ty = New TFloat64Type
- End If
- If CParse("&") And Not (attrs & DECL_FIELD) Then
- attrs :| DECL_GLOBAL
- attrs :~ DECL_CONST
- End If
- ' pointer
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Case ":"
- NextToke
- ty=ParseNewType()
-
- If CParse("*") Then
- If TIdentType(ty) Then
- ty = TType.MapToPointerType(ty)
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- End If
- End If
-
- CParse("&")
- Case "?"
- NextToke
-
- attrs :| DECL_EXTERN
-
- If CParse("?") Then
- attrs :| CLASS_INTERFACE
- End If
-
- ty=ParseNewType()
-
- If CParse("*") Then
- If TIdentType(ty) Then
- ty = TType.MapToPointerType(ty)
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- End If
- End If
-
- CParse("&")
- Case "~~"
- NextToke
-
- attrs :| DECL_EXTERN | CLASS_STRUCT
-
- ty=ParseNewType()
-
- If CParse("*") Then
- If TIdentType(ty) Then
- ty = TType.MapToPointerType(ty)
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- End If
- End If
-
- CParse("&")
- Case "@"
- NextToke
- ty=New TByteType
-
- If CParse("@") Then
- ty = New TShortType
- End If
-
- If CParse( "&" )
- 'DebugStop
- End If
-
- ' pointer
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- ' TODO
- ' Case "!" ' BaH Double
- ' NextToke
- ' ty=TType.doubleType
- Default
- 'If _module.IsStrict() Err "Illegal type expression."
- 'DebugStop
- If Not fn Then
- ty=New TIntType
- End If
- End Select
- If CParse( "&" ) Then
- End If
- While IsArrayDef()
- ty = ParseArrayType(ty)
- If CParse( "&" ) Then
- End If
- Wend
- If CParse( "&" ) Then
- End If
-
- Return ty
- End Method
- Method ParseNewType:TType()
- If CParse( "byte" ) Or CParse( "@" )
- Local ty:TType = New TByteType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "short" )
- Local ty:TType = New TShortType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "int" ) Or CParse( "%" )
- Local ty:TType = New TIntType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "uint" ) Or CParse( "|" )
- Local ty:TType = New TUIntType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "float" )
- Local ty:TType = New TFloatType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "string" ) Return New TStringType
- If CParse( "object" ) Return New TIdentType.Create( "brl.classes.object" )
- If CParse( "long" )
- Local ty:TType = New TLongType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "ulong" )
- Local ty:TType = New TULongType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "double" )
- Local ty:TType = New TDoubleType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "size_t" )
- Local ty:TType = New TSizeTType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "int128" )
- Local ty:TType = New TInt128Type
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "float64" )
- Local ty:TType = New TFloat64Type
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "float128" )
- Local ty:TType = New TFloat128Type
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "double128" )
- Local ty:TType = New TDouble128Type
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "wparam" )
- Local ty:TType = New TWParamType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- If CParse( "lparam" )
- Local ty:TType = New TLParamType
- While CParse("ptr")
- ty = TType.MapToPointerType(ty)
- Wend
- While CParse( "*" )
- ty = TType.MapToPointerType(ty)
- Wend
- Return ty
- End If
- Return ParseIdentType()
- End Method
-
- Method ApplyFunctionAttributes(classDecl:TClassDecl, attrs:Int)
- For Local decl:TFuncDecl = EachIn classDecl._decls
- decl.attrs :| attrs
- Next
- End Method
- Method SetErr()
- If _toker.Path()
- _errInfo=FormatError(_toker.Path(), _toker.Line(), 0)
- EndIf
- End Method
- End Type
|