Results 1 to 4 of 4

Thread: Tiny Token Scanner v1.2 Update

Threaded View

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    637

    Tiny Token Scanner v1.2 Update

    Hi was out today did some shopping then went to the library so I thought I have a play on there computers anyway found my self in Microsoft Word and decided to play with the macro editor anyway this is what I came up with a basic small token scanner I made for scanning tokens in a string hope it comes in handy you could more than likely use something like this for a expression parser or if your building a interpter. Anyway the code is very basic but easy to understand. If i get time I may try and add more features to it

    Comments suggestions welcome.

    Update 1.2
    Here a small update agian of the scanner now with quoted string support
    Bug fix for unkown tokens.

    vb Code:
    1. 'Updated 18:56 28-Jan-12
    2. 'Added quoted string support
    3. 'Fixed bug with unknown token should not hang program now
    4.  
    5. Option Explicit
    6.  
    7. Private Enum TTypes
    8.     TUnknown = 0
    9.     TAlpha = 1
    10.     TQuoteStr = 2
    11.     TDigit = 3
    12.     TSymbol = 4
    13.     EOL = 5
    14.     EOP = 6
    15. End Enum
    16.  
    17. Private Const VbQuote = """"
    18. Dim Expr As String          'Expression / Code to scan
    19. Dim Token As String         'Current Token
    20. Dim TokType As TTypes       'Token Type see TTypes
    21. Private Pos As Integer      'Char Position
    22.  
    23. Private Sub Inc(Optional IncVal As Integer = 1)
    24.     Pos = (Pos + IncVal)
    25. End Sub
    26.  
    27. Private Function IsSymbol(c As String) As Boolean
    28.     'Return true if we have a symbol.
    29.     If InStr(" :,;<>+-/*%^=[]()&", c) Or c = vbCr Then IsSymbol = True
    30. End Function
    31.  
    32. Private Function GetChar() As String
    33.     'Fetch next char from string.
    34.     GetChar = Mid$(Expr, Pos, 1)
    35. End Function
    36.  
    37. Private Function IsWhite(c As String) As Boolean
    38.     'Is white check
    39.     IsWhite = (c = " ") Or (c = vbTab)
    40. End Function
    41.  
    42. Private Function IsAlpha(c As String) As Boolean
    43.     'Return true if string is of alpha
    44.     Select Case UCase$(c)
    45.         Case "A" To "Z"
    46.             IsAlpha = True
    47.         Case Else
    48.             IsAlpha = False
    49.         End Select
    50. End Function
    51.  
    52. Private Function IsDigit(c As String) As Boolean
    53. 'Return if we have numbers
    54.     Select Case c
    55.         Case 0 To 9
    56.             IsDigit = True
    57.         Case Else
    58.             IsDigit = False
    59.     End Select
    60. End Function
    61.  
    62. Private Sub GetToken()
    63.     'Set token type None
    64.     'Eat token
    65.     Token = vbNullString
    66.    
    67.     'Check for end of string
    68.     If (Pos > Len(Expr)) Then
    69.         TokType = EOP
    70.         Pos = Len(Expr)
    71.         Exit Sub
    72.     End If
    73.    
    74.     'Skip white
    75.     While IsWhite(GetChar)
    76.         Call Inc
    77.     Wend
    78.    
    79.     'Check for vbcr
    80.     If GetChar = vbCr Then
    81.         Call Inc(2)
    82.         Token = vbCr
    83.         TokType = EOL
    84.         Exit Sub
    85.     End If
    86.    
    87.     'Check for symbol
    88.     If IsSymbol(GetChar) Then
    89.         'Build token.
    90.         Token = Token & GetChar
    91.         Call Inc
    92.         TokType = TSymbol
    93.     ElseIf IsAlpha(GetChar) Then
    94.         While Not IsSymbol(GetChar)
    95.             'Build token.
    96.             Token = Token & GetChar
    97.             Call Inc
    98.         Wend
    99.         TokType = TAlpha
    100.     'Check for number
    101.     ElseIf IsDigit(GetChar) Then
    102.         While Not IsSymbol(GetChar)
    103.             'Build token.
    104.             Token = Token & GetChar
    105.             Call Inc
    106.         Wend
    107.         TokType = TDigit
    108.     'Check for quoted string
    109.     ElseIf (GetChar = VbQuote) Then
    110.         'Move along one
    111.         Call Inc
    112.         While Not (GetChar = VbQuote)
    113.             Token = Token + GetChar
    114.             Call Inc
    115.         Wend
    116.         TokType = TQuoteStr
    117.         'Inc by one
    118.         Call Inc
    119.     Else
    120.         'Unknown token type
    121.         TokType = TUnknown
    122.         If (Pos > Len(Expr)) Then Exit Sub
    123.     End If
    124.    
    125. End Sub
    126.  
    127. Private Sub Init()
    128.     'Test string example
    129.     Expr = "Var x String = 5" & vbCrLf
    130.     Expr = Expr & "x = Hello + Str(5 + 5)" & vbCrLf
    131.     Expr = Expr & "Print x" & vbCrLf
    132.     Expr = Expr & "Print " + VbQuote + "Hello'-'World" + VbQuote + vbCrLf
    133.     Expr = Expr & "End." & vbCrLf
    134.    
    135.     'Init vars
    136.     Pos = 1
    137. End Sub
    138.  
    139.  
    140. Private Sub Command1_Click()
    141.     Call Init
    142.     'Get First token
    143.     Call GetToken
    144.    
    145.     'Read the tokens until end of source
    146.     While TokType <> EOP
    147.         'Test for unkown token
    148.         If (TokType = TUnknown) Then Exit Sub
    149.        
    150.         'Display token type and token
    151.         Call MsgBox("Token Type =" & TokType & vbCrLf & "Token=" & Token)
    152.         Call GetToken
    153.     Wend
    154.    
    155. End Sub
    Last edited by BenJones; Jan 28th, 2012 at 02:01 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width