Case InsensitiveConstants: { Digit [0-9] Quote " Point . Int 1 Float 2 String 3}Modes: { COMMENT Exclusive DOG Inclusive}Rules: { -?{Digit}+ Store Int -?{Digit}*{Point}{Digit}+ Store Float {Quote}[^]*{Quote} Store String {- Mode <COMMENT> <COMMENT> -} Mode <> <COMMENT,DOG,> doggy { print "nada" }}Code: {;Anything in here is copied straight to the main body;so make sure it's valid BB code}
;===============================================================================;This function library provides an interface through which you can use the lexer;generated by BBLex, rather than calling it directly.Include "Regex.bb" ;Get this Include from: <a href="codearcs5392.html?code=2632" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=2632</a>;Extend this with any other useful info that can be read by your scanner,;for example, line numbers (remember to extend the constructor too!)Type BBLex_Token Field val$ Field tTypeEnd Type;Run the lexical scanner on the contents of the given fileFunction BBLex_ScanFile(filename$) Local sBank,tBank sBank=LoadFileBank(filename) tBank=BBLex_ScanData(sBank) FreeBank sBank Return tBankEnd Function;Get a token from the list by indexFunction BBLex_GetToken.BBLex_Token(tBank, index) Return Object.BBLex_Token(PeekInt(tBank, index * 4))End Function;Free the token list when done with itFunction BBLex_FreeTokenBank(tBank) Local i, token.BBLex_Token For i = 0 To BankSize(tBank) - 4 Step 4 token = Object.BBLex_Token(PeekInt(tBank, i)) Delete token Next FreeBank tBankEnd Function;Internal functions to the scanner - don't use separately;--------------------------------------------------------;Internal function to the scanner - clean up regex objects once doneFunction BBLex_DeleteRegexen(regexBank) Local i For i=0 To BankSize(regexBank)-4 Step 4 RegEx_Delete(Object.RegEx_Node(PeekInt(regexBank,i))) Next FreeBank regexBankEnd Function;Internal function to the scanner - clean up mode list when doneFunction BBLex_ClearModes(modeBank) Local i For i=0 To BankSize(modeBank)-4 Step 4 If PeekInt(modeBank,i) Then FreeBank PeekInt(modeBank,i) Next FreeBank modeBankEnd Function;Internal function to the scanner - check that a rule applies in the current modeFunction BBLex_ModeMatch(rule, mBank, cMode) Local i, ruleModes ruleModes = PeekInt(mBank, rule * 4) If ruleModes For i = 0 To BankSize(ruleModes) - 4 Step 4 If (cMode = 0 And PeekInt(ruleModes, i) < 1) Or cMode = PeekInt(ruleModes, i) Then Return True Next Else Return (cMode < 1) EndIfEnd Function;Add a token to the token list - used by the scannerFunction BBLex_StoreToken(tBank, tType, token$) ResizeBank tBank, BankSize(tBank) + 4 PokeInt tBank, BankSize(tBank) - 4, Handle(BBLex_MakeToken(token, tType))End Function;Add a token's type to the token list without keeping its valueFunction BBLex_StoreType(tBank, tType) ;This is mainly here for compatibility ResizeBank tBank,BankSize(tBank) + 4 PokeInt tBank, BankSize(tBank) - 4, Handle(BBLex_MakeToken("", tType))End Function;New token objectFunction BBLex_MakeToken.BBLex_Token(val$, tType) Local tok.BBLex_Token = New BBLex_Token tokval = val tok Type = tType Return tokEnd Function;===============================================================================;===============================================================================;General utility functions (not really connected to this library);===============================================================================Function StrToBank(s$) ;Return a bank containing the binary value of the given string Local i,bank bank=CreateBank(Len(s)) For i=0 To Len(s)-1 PokeByte bank,i,Asc(Mid(s,i+1,1)) Next Return bankEnd FunctionFunction BankToStr$(bank) ;Return a string containing the ASCII value of the given bank Local i,s$ For i=0 To BankSize(bank)-1 s=s+Chr(PeekByte(bank,i)) Next Return sEnd FunctionFunction LoadFileBank(filename$) ;Load a file straight into a bank Local bank,file file=ReadFile(filename) bank=CreateBank(FileSize(filename)) ReadBytes bank,file,0,BankSize(bank) CloseFile file Return bankEnd Function;===============================================================================;~IDEal Editor Parameters:;~F#B#12#1B#20#30#39#42#4F#55#5B#6C#75#7D;~C#Blitz3D
Local i, tokenBanktokenBank = BBLex_ScanFile("test.txt")For i = 0 To BankSize(tokenBank) / 4 - 1 Local tok.BBLex_Token = BBLex_GetToken(tokenBank, i) Print tok Type + " : " + tokvalNextWaitKeyEndInclude "testlex.bb"
12 345 56.45 doggy {- This is a comment and shouldn't be picked up -}"String literal 1!"nn"String literal 2!"doggy65 -12.34boogledoggy 35.8 "doggy as a string literal!"
Modes: { Comment Exclusive InString Exclusive}Rules: { ;Punctuation <= Store 1 >= Store 2 == Store 3 != Store 4 :: Store 5 ; Store 6 , Store 7 ! Store 8 * Store 9 / Store 10 ( Store 11 ) Store 12 - Store 13 + Store 14 = Store 15 [ Store 16 ] Store 17 { Store 18 } Store 19 . Store 20 < Store 21 > Store 22 # Store 23 &&? Store 24 ||? Store 25 ^ Store 26 % Store 27 : Store 28 ;Values [0-9]+ { StoreNumericToken tBank,29,token } [0-9]*.[0-9]+ { StoreNumericToken tBank,30,token } " Mode <InString> <InString> [^"]* Store 31 <InString> |" Mode <> ;Comments /* Mode <Comment> <Comment> */ Mode <> //[^]* ;Names [a-zA-Z_][a-zA-Z0-9_]* Store 32}Code: { ;If the previous token was a minus, check if it was subtraction or negation and store appropriately Function StoreNumericToken(tBank,numType,token$) Local i If BankSize(tBank) If BBLex_TokenType(tBank,(BankSize(tBank)/8)-1)=13 If BankSize(tBank)>8 If TokenSubtractible(BBLex_TokenType(tBank,(BankSize(tBank)/8)-2))=False RemoveLastToken(tBank) token="-"+token EndIf Else RemoveLastToken(tBank) token="-"+token EndIf EndIf EndIf BBLex_StoreToken tBank,numType,token End Function ;Removes the last token from the given token bank Function RemoveLastToken(tBank) Local i For i=0 To BankSize(tBank)/8-1 If PeekInt(tBank,BankSize(tBank)-8)=BBLex_TokenType(tBank,i) Then Exit Next If i=BankSize(tBank)/8 Then FreeBank PeekInt(tBank,BankSize(tBank)-4) ResizeBank tBank,BankSize(tBank)-8 End Function ;Take a token type and see if it's an operator or a term Function TokenSubtractible(tokenType) If tokenType>=29 Or tokenType=12 Or tokenType=17 Return True Else Return False EndIf End Function}
Constants: { OCT 0[0-9]+ DEC [1-9][0-9]+ HEX 0[xX][0-9a-fA-F]+ INTSUFFIX ([uU]|([lL][uU]?)) FLTSUFFIX ([fF]|([lL][fF]?)) CHAR '\?.'}Modes: { COMMENT Exclusive INSTRING Exclusive}Rules: { ;Constants {DEC}{INTSUFFIX}? { StoreNumericToken tBank,1,token } {OCT}{INTSUFFIX}? { StoreNumericToken tBank,1,token } {HEX}{INTSUFFIX}? { StoreNumericToken tBank,1,token } [0-9]*.[0-9]+([eE]-?{DEC})?{FLTSUFFIX}? { StoreNumericToken tBank,2,token } {CHAR} Store 3 L{CHAR} Store 4 " Mode <INSTRING> <INSTRING> [^"]* Store 5 <INSTRING> ["] Mode <> ;Comments /* Mode <COMMENT> <COMMENT> */ Mode <> //[^]* ;Punctuation ; Store 6 { Store 7 } Store 8 , Store 9 = Store 10 : Store 11 ( Store 12 ) Store 13 [ Store 14 ] Store 15 * Store 16 ... Store 17 *= Store 18 /= Store 19 %= Store 20 += Store 21 -= Store 22 <<= Store 23 >>= Store 24 &= Store 25 ^= Store 26 |= Store 27 ? Store 28 || Store 29 && Store 30 | Store 31 ^ Store 32 & Store 33 == Store 34 != Store 35 < Store 36 > Store 37 <= Store 38 >= Store 39 << Store 40 >> Store 41 + Store 42 - Store 43 * Store 44 / Store 45 % Store 46 ++ Store 47 -- Store 48 ~ Store 49 ! Store 50 . Store 51 -> Store 52 ;Keywords auto Store 53 register Store 54 static Store 55 extern Store 56 typedef Store 57 void Store 58 char Store 59 short Store 60 int Store 61 long Store 62 float Store 63 double Store 64 signed Store 65 unsigned Store 66 const Store 67 volatile Store 68 struct Store 69 union Store 70 enum Store 71 case Store 72 default Store 73 if Store 74 else Store 75 switch Store 76 while Store 77 do Store 78 for Store 79 goto Store 80 continue Store 81 break Store 82 return Store 83 sizeof Store 84 ;Identifiers [a-zA-Z_][a-zA-Z0-9_]* Store 85}Code: { ;If the previous token was a minus, check if it was subtraction or negation and store appropriately Function StoreNumericToken(tBank,numType,token$) Local i If BankSize(tBank) If BBLex_TokenType(tBank,(BankSize(tBank)/8)-1)=43 If BankSize(tBank)>8 If TokenSubtractible(BBLex_TokenType(tBank,(BankSize(tBank)/8)-2))=False RemoveLastToken(tBank) token="-"+token EndIf Else RemoveLastToken(tBank) token="-"+token EndIf EndIf EndIf BBLex_StoreToken tBank,numType,token End Function ;Removes the last token from the given token bank Function RemoveLastToken(tBank) Local i For i=0 To BankSize(tBank)/8-1 If PeekInt(tBank,BankSize(tBank)-8)=BBLex_TokenType(tBank,i) Then Exit Next If i=BankSize(tBank)/8 Then FreeBank PeekInt(tBank,BankSize(tBank)-4) ResizeBank tBank,BankSize(tBank)-8 End Function ;Take a token type and see if it's an operator or a term Function TokenSubtractible(tokenType) Select tokenType Case 1,2,3,4,13,15,85 Return True Default Return False End Select End Function}