Function bbToDecls(file$, ExportType%=False,ExportConst%=False,ExportGlobal%=False,ComFunc%=False,ComType%=False,ComConst%=False,ComGlobal%=False) Local ops$ = ".=()[]#$%;:/+-*.,?" Local emptyops$ = " "+Chr(9) Local strops$ = Chr(34) If Lower(Right(file,3))<>".bb" Then Return False Local o$ = Left(file,Len(file)-3)+".decls" ; same name replacing ".bb" with ".decls" ; read the inputfile (return if not "readable") Local in = ReadFile(file) : If Not(in) Then Return False ; write the output decls Local out = WriteFile(o) : If Not(out) Then Return False ; add the decoration WriteLine out, ".lib "+Chr(34)+" "+Chr(34) WriteLine out, "" Local LId% = 0 Local lastcomment$="" ; parse the bb-file While Not(Eof(in)) ; read the lines (remove spaces before and after) Local l$=Trim(ReadLine(in)) LId = LId+1 If Len(l) Local ts.TokenSet = Tokenize(l,ops,emptyops,strops) If TokenSetCount(ts) ; assert there is tokens (not an empty line ... should not happen due to the trim ... but anyway.) ; find function signature (decoration) Select Lower(TokenValue(ts,1)) Case "function" If TokenSetCount(ts)<4 Then RuntimeError "Error at line "+LId+" : in the function declaration '"+l+"'" ; tokenize the line ; set all to default. Local fname$="" Local freturn$="" Local freturntype$="" ; actually, the start of the parameters can't be smaller than "3" ; Function Name([...]) ; if can start at 4 ; Function Name%([...]) ; if can also start at 5 ; Function Name .ReturnType([...]) ; I don't think it can start later... Local fparamstart%=3 ; so, let's start grabing the "name" and the "return" thing ; let's assume the file is a "valid" bb-file (we won't deal with errors in the file) ; pos 1 = function ; pos 2 = name ; pos 3 = "." or % or "$" or "#" or "(" ; so the name is at pos 2 ! fname=TokenValue(ts,2) ; for the pos 3, we have to check wether it's a "(" or something else Select TokenValue(ts,3) ; so we found the first bracket Case "(" ; no return type freturn="" ; and the first argument start at pos 4 (if any) fparamstart=3 Case "." freturn="" ; do not mark the 'type' return (decls does not support them) ; eventually if we wanted to catch the type : (for document or else) freturntype = TokenValue(ts,4) fparamstart=5 ; go after the bracket Case "%","$","#" freturn=TokenValue(ts,3) fparamstart=4 Default ; maybe we could check what else happend here RuntimeError "Error at line "+LId+" : This symbol is Not supported : ["+TokenValue(ts,3)+"] as a Function decoration" ; we should not be here as there is a RuntimeError call ... ; but if the user override the RuntimeError Function ?! ... I know someone who ..." End ; Hey, maybe someone override the "End" too >.< what knid of person do that ?! Return False ; I hope the return can't be override ... damn, I think I'm paranoid DebugLog "Are you serious ? He !" Print "You think I'm paranoid, don't you ?!" End Select ; so we have the name and the return style, let's parse the arguments. Local fargs$="(", lastarg$="" For n = fparamstart To tscount Select TokenType(ts,n) Case TOKEN_WORD lastarg = TokenValue(ts,n) fargs=fargs+lastarg Case TOKEN_OP Select TokenValue(ts,n) Case "," ; start of a new argument ; check if there is no previous argument ... if so, its' a syntax error. If lastarg="" RuntimeError "Error at line "+LId+" : Illegal declaration in function '"+fname+"' missing argument before comma" EndIf fargs=fargs+", " Case "%","#","$" ; add the type specifier fargs=fargs+TokenValue(ts,n) Case "[" ; a static array as parameter, Yes Sir ! ; arrays are marked with a "*" for pointers, but we have to remove the type specifier (if any) Select TokenValue(ts,n-1) Case "%","#","$" fargs=Left(fargs,Len(fargs)-1) End Select ; add the "*" fargs=fargs+"*" ; jump to the end of the array decoration n2 = n+1 For n=n2 To TokenSetCount(ts) If TokenValue(ts,n)="]" Then Exit Next Case "." ; it's a Blitz-type > marked with a "*" in decls files. (but we have to skyp the type-name) fargs = fargs+"*" ; skip the next token (should be the typename, but if it's not there is an error) If TokenType(ts,n+1)=TOKEN_WORD n=n+1 Else RuntimeError "Error at line "+LId+" : Illegal declaration of BlitzType in Function '"+fname+"'" EndIf Case "=" ; start of optionnal value of the current argument If TokenSetCount(ts)>n+1 ; skip the optional value and check if the next token is a valid one. (end of arguments, or separator) Select TokenValue(ts,n+2) Case ")", "," n=n+1 Default RuntimeError "Error at line "+LId+" : Unknown decoration for argument '"+lastarg+"'" End Select Else ; not enough space : function is not defined correctly RuntimeError "Error at line "+LId+" : Unknown error in end of Function declaration '"+fname+"'" EndIf ; The ending brackets, we did it ! Case ")" fargs=fargs+")" Exit End Select Case TOKEN_NUM ; as the "=value..." is skiped, we should not find any numbers. RuntimeError "Error at line "+LId+" : Unexpected Number in function declaration '"+fname+"'" Case TOKEN_STR ; we should not find any strings either. RuntimeError "Error at line "+LId+" : Unexpected string in function declaration '"+fname+"'" End Select Next ; assert the arguments are closed ! If Right(fargs,1)<>")" RuntimeError "Error at line "+LId+" : missing bracket ')' in function declaration '"+fname+"'" EndIf If ComFunc And lastcomment<>"" Then WriteLine out, lastcomment WriteLine out, fname+" "+freturn+" "+fargs lastcomment = "" ; manage the comments ? Case ";" lastcomment=l Case "type" ; export types as function the currently won't be function, but they will be highlighted) If ExportType If TokenSetCount(ts)>1 If ComType And lastcomment<>"" Then WriteLine out, lastcomment WriteLine out, TokenValue(ts,2)+"()" Else RuntimeError "Error at line "+LId+" : missing Type specifier" EndIf EndIf lastcomment = "" Case "global" ; export globals as function the currently won't be function, but they will be highlighted) If ExportGlobal If TokenSetCount(ts)>4 If TokenType(ts,2)=TOKEN_WORD If ComGlobal And lastcomment<>"" Then WriteLine out, lastcomment Select TokenValue(ts,3) Case "%","$","#" WriteLine out, TokenValue(ts,2)+TokenValue(ts,3)+"()" Default WriteLine out, TokenValue(ts,2)+"()" End Select Else RuntimeError "Error at line "+LId+" : missing Type specifier" EndIf Else RuntimeError "Error at line "+LId+" : missing Type specifier" EndIf EndIf lastcomment = "" Case "const" ; export const as function the currently won't be function, but they will be highlighted) If ExportConst If TokenSetCount(ts)>4 If TokenType(ts,2)=TOKEN_WORD If ComConst And lastcomment<>"" Then WriteLine out, lastcomment Select TokenValue(ts,3) Case "%","$","#" WriteLine out, TokenValue(ts,2)+TokenValue(ts,3)+"()" Default WriteLine out, TokenValue(ts,2)+"()" End Select Else RuntimeError "Error at line "+LId+" : missing Type specifier" EndIf Else RuntimeError "Error at line "+LId+" : missing Type specifier" EndIf EndIf lastcomment = "" ; else ... skip the line. Default ; here we should get all tokens and catch the "function" words inside the set ; some people use the ":" to stick lines on the same line ; ex : Function f1():dothings:End Function : Function F2(): blablabla : End Function ; but whatever, this small version does not deal with it at the moment. ; hey ! it's just a sample :) lastcomment = "" End Select EndIf FreeTokenSet(ts) EndIf Wend CloseFile In CloseFile Out End FunctionbbToDecls("YOUR_BB_FILE_HERE.bb",True,True,True,True,False,False,False)