Example: Visual Basic - ActiveX

 

##TEMPLATE-NAME 'Visual Basic - ActiveX DLL'
##LANGUAGE 'Visual Basic'
##ENGINE-NAME 'ActiveX DLL'
##AUTHOR 'Devin Cook'
##FILE-EXTENSION 'bas'
##NOTES
This template creates a Visual Basic program for use with the ActiveX DLL
The code will work with both Visual Basic 5 and 6.
##END-NOTES
##ID-CASE Propercase
##ID-SEPARATOR '_'
##ID-SYMBOL-PREFIX 'Symbol'
##ID-RULE-PREFIX 'Rule'
Option Explicit

Public Parser As New GOLDParserEngine.GOLDParser

Public Enum SymbolConstants
##SYMBOLS
    %ID.Padded% = %Value% ' %Description%
##END-SYMBOLS
End Enum

Enum RuleConstants
##RULES
    %ID.Padded% = %Value% ' %Description%
##END-RULES
End Enum

public Sub DoParse(ByVal Source As String)
   'This procedure starts the GOLD Parser Engine and handles each of the
   'messages it returns. Each time a reduction is made, a new custom object
   'can be created and used to store the rule. Otherwise, the system will use
   'the Reduction object that was returned.
   '
   'The resulting tree will be a pure representation of the language
   'and will be ready to implement.

   Dim Response As GOLDParserEngine.GPMessageConstants
   Dim Result As Object
   Dim Done As Boolean, Success As Boolean       'Controls when we leave the loop

   Success = False    'Unless the program is accepted by the parser

   With Parser
      If .LoadCompiledGrammar(App.Path & "\grammar.cgt") Then
         .OpenTextString Source
         .TrimReductions = True

         Done = False
         Do Until Done
            Response = .Parse()

            Select Case Response
            Case gpMsgLexicalError
               'Cannot recognize token
               Done = True

            Case gpMsgSyntaxError
               'Expecting a different token
                Done = True

            Case gpMsgReduction
               'You can create a new customized object and replace the
               'CurrentReduction with it. This saves memory and allows
               'easier interpretation.
               '
               '   Set .CurrentReduction = Result
               '
               'Of course, you can just let the system create a parse
               'tree of Reduction objects. Remember: The value of the
               'token is accessed through the Data property.

               With .CurrentReduction                 'Easy notation
                  Select Case .ParentRule.TableIndex
##RULES
                  Case %ID%
                     ' %Description%
##END-RULES
                  End Select
               End With

               'Set .CurrentReduction = Result

            Case gpMsgAccept
               'Success!
               'Set Program = .CurrentReduction  'The root node!
               Done = True
               Success = True

            Case gpMsgTokenRead
               'You don't have to do anything here.

            Case gpMsgInternalError
               'INTERNAL ERROR! Something is horribly wrong.
               Done = True

            Case gpMsgNotLoadedError
               'Due to the if-statement above, this case statement should never be true
               Done = True

            Case gpMsgCommentError
               'COMMENT ERROR! Unexpected end of file
               Done = True
            End Select
         Loop
      Else
         'Could not load the CGT file
      End If
   End With

End Sub

Public Sub Setup
   'Call this function before calling DoParse
   Parser.LoadCompiledGrammar(App.Path & "\grammar.cgt")
End Sub