core

package
v2.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

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

View Source
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.

View Source
const (
	Json = ExportFormat(iota)
	Text
	Binary
)

Export formats for the compiler which can be used to export intermediate results.

Variables

View Source
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.

const (
	Core Component = 1 << iota
	Scanner
	Parser
	AbstractSyntaxTree
	Analyzer
	Intermediate
	Emulator
	AllComponents = Component(^uint64(0))
)

Packages of the compiler which can generate errors as a bit-mask enumeration.

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 ExportFormat

type ExportFormat int

Export formats for the compiler.

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 Failure

type Failure int32

Failure is a type for codes that can be mapped to messages.

type Severity

type Severity uint64

Error levels that are used to categorize errors.

const (
	Remark Severity = 1 << iota
	Warning
	Error
	Fatal
	AllSeverities = Severity(^uint64(0))
)

Severity is a bit-mask enumeration of different error levels.

type Token

type Token int32

Token is a type that represents a token in the source code.

func (Token) In

func (token Token) In(set Tokens) bool

In returns true if the token is in the given tokens.

func (Token) ToTokens

func (t Token) ToTokens() Tokens

Token.ToTokens concerts a token to a token set to satisfy the TokenSet interface.

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.

func (TokenStream) Print

func (ts TokenStream) Print(print io.Writer, args ...any) error

Print the token stream to a writer and print from the bottom of the token stream if the bottom flag is set.

type Tokens

type Tokens []Token

Tokens represents a set of tokens.

func Set

func Set(tss ...TokenSet) Tokens

Set returns a joined slice of all tokens within the given TokenSet interfaces. Redundant tokens are removed.

func (Tokens) ToTokens

func (t Tokens) ToTokens() Tokens

Tokens.ToTokens simply returns the token set that is passed in to satisfy the TokenSet interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL