I'm trying to create a compiler and I'm working on the Lexical Analyzer and the Parser. My plan is to create an analyzer and a parser for each step, ie - a parser and analyzer for the declaration/initialization, a separate analyzer and parser for the conditional logic, and so on and so on. So what I'm doing right now is creating a base analyzer and base parser class that I'm setting as must inherit. My question comes with read-only properties. Every parser should have an error collection, but I don't want the user to modify that error collection. So right now this is what I'm doing:
Base parser
Declaration and Initialization specific parser
Currently that works ok, but I'm wanting to know if that's how it should be done? Or should it be done a different way?
Base parser
Code:
Public MustInherit Class Parser
#Region "Globals"
Private m_analyzer As Analyzer
Private m_errors As List(Of Exception)
#End Region
#Region "Properties"
Public Property Analyzer() As Analyzer
Get
Return m_analyzer
End Get
Set(ByVal value As Analyzer)
m_analyzer = value
End Set
End Property
Public Property Errors() As List(Of Exception)
Get
Return m_errors
End Get
Set(value As List(Of Exception))
m_errors = value
End Set
End Property
#End Region
End Class
Code:
Public Class Parser_Declaration
#Region "Globals"
Private m_errors As List(Of Exception)
#End Region
#Region "Properties"
Public Overloads ReadOnly Property Errors() As List(Of Exception)
Get
Return m_errors
End Get
End Property
#End Region
#Region "Methods"
Public Function CanParse() As Boolean
Dim parsable As Boolean
m_errors.Clear()
'Do paring stuff and add errors as it goes
Return parsable
End Function
#End Region
End Class