parser

package
v0.0.0-...-cf91f57 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrExpectedLineEnd = fmt.Errorf("expected line end")
)

Functions

func ExtractExpectedStructure

func ExtractExpectedStructure(stream *lexer.TokenStream, dest *[]*lexer.Token, exp *Expectation) error

ExtractExpectedStructure attempts to extract a subset of Tokens from a TokenStream, storing the results in dest. dest must have a capacity large enough to store all the expected tokens.

Because append is used, dest MUST NOT need to grow.

func ExtractOneOfExpectedStructure

func ExtractOneOfExpectedStructure(stream *lexer.TokenStream, dest *[]*lexer.Token, exps *OneOfExpectations) error

ExtractOneOfExpectedStructure functions similar to ExtractExpectedStructure, but instead extracts the first matching expectation from OneOfExpectations.

func TokenizeAll

func TokenizeAll(data []byte) ([]*lexer.Token, error)

TokenizeAll converts the given data into a slice of Tokens

Types

type Expectation

type Expectation struct {
	// contains filtered or unexported fields
}

Expectation is an aggregate of ExpectationEntries that matches multiple expected Tokens.

func NewExpectation

func NewExpectation(description string, entries ...ExpectationEntry) *Expectation

func (*Expectation) Description

func (e *Expectation) Description() string

func (*Expectation) Extract

func (e *Expectation) Extract(stream *lexer.TokenStream, dest *[]*lexer.Token) error

func (*Expectation) ExtractionCount

func (e *Expectation) ExtractionCount() int

ExtractionCount returns the number of captured (e.g. not ignored) Tokens that its pattern describes

type ExpectationEntry

type ExpectationEntry interface {
	Matches(kind lexer.TokenKind) bool
	Keep() bool
	Describe() string
}

ExpectationEntry describes a possible token expectation that can be matched within stream of many Tokens.

func Expect

func Expect(kind lexer.TokenKind) ExpectationEntry

Expect returns an ExpectationEntry that captures an expected TokenKind

func ExpectAny

func ExpectAny(kinds ...lexer.TokenKind) ExpectationEntry

ExpectAny returns an ExpectationEntry that captures one of any given TokenKind

func ExpectAnyIgnore

func ExpectAnyIgnore(kinds ...lexer.TokenKind) ExpectationEntry

ExpectAnyIgnore returns an ExpectationEntry that expects but ignores one of any given TokenKind

func ExpectIgnore

func ExpectIgnore(kind lexer.TokenKind) ExpectationEntry

ExpectIgnore returns an ExpectationEntry that expects but ignores a TokenKind

type Extractable

type Extractable interface {
	Extract(stream *lexer.TokenStream, dest *[]*lexer.Token) error
	ExtractionCount() int
	Description() string
}

type ExtractionError

type ExtractionError struct {
	// contains filtered or unexported fields
}

func (*ExtractionError) Error

func (ee *ExtractionError) Error() string

type OneOfExpectations

type OneOfExpectations struct {
	// contains filtered or unexported fields
}

func OneOf

func OneOf(expectations ...*Expectation) *OneOfExpectations

func (*OneOfExpectations) Description

func (o *OneOfExpectations) Description() string

func (*OneOfExpectations) Extract

func (o *OneOfExpectations) Extract(stream *lexer.TokenStream, dest *[]*lexer.Token) error

func (*OneOfExpectations) ExtractionCount

func (o *OneOfExpectations) ExtractionCount() int

type Relocator

type Relocator interface {
	AddressFor(label *lexer.Token) (uint32, bool)
	OffsetFor(label *lexer.Token) (uint16, error)
	SignedOffsetFor(label *lexer.Token) (int16, error)
}

type Statement

type Statement struct {
	Body []*lexer.Token
	Kind StatementKind
}

Statement is the most basic element of an Orange assembly program.

It can describe:

  1. An instruction (e.g. ADD r3, r2, r1)
  2. A directive (e.g. a label declaration)

func ParseTokens

func ParseTokens(tokens []*lexer.Token) ([]*Statement, error)

ParseTokens aggregates tokens from the input stream into statements.

We make the following classification of all complete statements within Orange assembly:

All (meaningful) tokens belong to one of two types:

  1. Instructions
  2. Directives

Thus, we will iterate over the input token stream and aggregate each token to a specific instruction or directive.

For example, given the input token stream state of

[MOVZ, REGISTER, COMMA, BASE_10_IMM, COMMENT, LINE_END, ...]

We will extract MOVZ, inspect the token, determine that the instruction should contain a register and an immediate, and remove them from the stream.

The LINE_END token is used to enforce assembly programs to have each instruction on a separate line. This is not a limitation of the parsing, but a deliberate design choice. Therefore, we must reach a LINE_END token before beginning parsing of another Statement.

type StatementKind

type StatementKind uint8
const (
	InstructionStatement StatementKind = iota
	DirectiveStatement
)

Jump to

Keyboard shortcuts

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