Documentation ¶
Overview ¶
Package core provides foundation features, the token type-system, and the error handling mechanism for the PL/0 compiler. This combination enables the error handler to connect an error to a location in the token stream. Package core is required to depend on Go standard library packages only.
Index ¶
- Constants
- Variables
- func NewGeneralError(component Component, failureMap map[Failure]string, severity Severity, ...) error
- func NewLineColumnError(component Component, failureMap map[Failure]string, severity Severity, ...) error
- func NewSourceError(component Component, failureMap map[Failure]string, severity Severity, ...) error
- func NewTokenError(component Component, failureMap map[Failure]string, severity Severity, ...) error
- type Component
- type ErrorHandler
- type ExportFormat
- type Exporter
- type Failure
- type Severity
- type Token
- type TokenDescription
- type TokenHandler
- type TokenSet
- type TokenStream
- type Tokens
Constants ¶
const ( Unknown = Token(iota) Identifier Number Plus Minus Times Divide Equal NotEqual Less LessEqual Greater GreaterEqual LeftParenthesis RightParenthesis Comma Colon Semicolon ProgramEnd Becomes Read Write OddWord BeginWord EndWord IfWord ThenWord WhileWord DoWord CallWord ConstWord VarWord ProcedureWord )
Tokens of the PL/0 programming language.
const ( Json = ExportFormat(iota) Text Binary )
Export formats for the compiler which can be used to export intermediate results.
Variables ¶
var ( // Empty is an empty token set. Empty = Tokens{} // TokenNames maps tokens to their string representation. TokenNames = map[Token]string{ Unknown: "unknown", Identifier: "identifier", Number: "number", Plus: "plus", Minus: "minus", Times: "times", Divide: "divide", Equal: "equal", NotEqual: "notEqual", Less: "less", LessEqual: "lessEqual", Greater: "greater", GreaterEqual: "greaterEqual", LeftParenthesis: "leftParenthesis", RightParenthesis: "rightParenthesis", Comma: "comma", Colon: "colon", Semicolon: "semicolon", ProgramEnd: "programEnd", Becomes: "becomes", Read: "read", Write: "write", OddWord: "odd", BeginWord: "begin", EndWord: "end", IfWord: "if", ThenWord: "then", WhileWord: "while", DoWord: "do", CallWord: "call", ConstWord: "const", VarWord: "var", ProcedureWord: "procedure", } )
Functions ¶
func NewGeneralError ¶
func NewGeneralError(component Component, failureMap map[Failure]string, severity Severity, code Failure, value any, inner error) error
Create a new general error with a severity level (a general error can wrap any other error).
func NewLineColumnError ¶
func NewLineColumnError(component Component, failureMap map[Failure]string, severity Severity, code Failure, value any, line, column int32) error
Create a new line-column error with a severity level and a line and column number.
func NewSourceError ¶
func NewSourceError(component Component, failureMap map[Failure]string, severity Severity, code Failure, value any, line, column int32, sourceCode []byte) error
Create a new source error with a severity level, a line and column number, and the source code where the error occurred.
func NewTokenError ¶
func NewTokenError(component Component, failureMap map[Failure]string, severity Severity, code Failure, value any, tokenStream TokenStream, index int) error
Create a new token error with a severity level and a token stream that is used to connect errors to a location in the source code.
Types ¶
type Component ¶
type Component uint64
Component describes packages of the compiler which can generate errors.
type ErrorHandler ¶
type ErrorHandler interface { AppendError(err error) error Count(severity Severity, component Component) int HasErrors() bool HasWarnings() bool HasRemarks() bool Print(print io.Writer, args ...any) error Export(format ExportFormat, print io.Writer) error }
ErrorHandler is an interface that provides methods for error handling and printing.
func NewErrorHandler ¶
func NewErrorHandler(tokenStream TokenStream) ErrorHandler
Return the public interface to a new error handler.
type Exporter ¶
type Exporter interface { Print(print io.Writer, args ...any) error Export(format ExportFormat, print io.Writer) error }
Exporter is an interface that provides methods for exporting intermediate results.
type Token ¶
type Token int32
Token is a type that represents a token in the source code.
type TokenDescription ¶
type TokenDescription struct { Token Token `json:"token"` // token kind TokenName string `json:"token_name"` // token name TokenValue string `json:"token_value"` // token value Line int32 `json:"line"` // line position in the source code Column int32 `json:"column"` // column position in the source code CurrentLine []byte `json:"-"` // source code line }
Describes a token with its kind, name, value, datatype, and position in the source code.
func (*TokenDescription) MarshalJSON ¶
func (td *TokenDescription) MarshalJSON() ([]byte, error)
Marshal the token description to a JSON object.
func (*TokenDescription) UnmarshalJSON ¶
func (td *TokenDescription) UnmarshalJSON(raw []byte) error
Unmarshal the token description from a JSON object.
type TokenHandler ¶
type TokenHandler interface { NextTokenDescription() bool LastToken() Token LastTokenName() string LastTokenValue() string LastTokenIndex() int Recover(code Failure, expected, fallback Tokens) bool IsFullyParsed() bool SetFullyParsed() NewError(severity Severity, code Failure, value any) error NewErrorOnIndex(severity Severity, code Failure, value any, index int) error AppendError(err error) error ReplaceComponent(component Component, failureMap map[Failure]string) }
TokenHandler is an interface that provides methods for handling tokens in the token stream.
func NewTokenHandler ¶
func NewTokenHandler(tokenStream TokenStream, errorHandler ErrorHandler, component Component, failureMap map[Failure]string) TokenHandler
Return the public interface to a new token handler.
type TokenSet ¶
type TokenSet interface {
ToTokens() Tokens
}
TokenSet is an interface that is used for types that can be converted to the 'Tokens' type.
type TokenStream ¶
type TokenStream []TokenDescription
The token stream table of token descriptions is the result of the lexical analysis of the source code.
func (TokenStream) Export ¶
func (ts TokenStream) Export(format ExportFormat, print io.Writer) error
Export the token stream to a writer in the specified format.