ast

package
v0.0.0-...-b4fbdbd Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: Apache-2.0 Imports: 13 Imported by: 95

Documentation

Overview

Package ast parses and formats sys files.

Index

Constants

View Source
const (
	OperatorCompareEq = iota + 1
	OperatorCompareNeq
	OperatorBinaryAnd
	OperatorOr
)
View Source
const BuiltinFile = "BUILTINS"

Variables

This section is empty.

Functions

func Format

func Format(desc *Description) []byte

func FormatInt

func FormatInt(v uint64, format IntFmt) string

func FormatStr

func FormatStr(v string, format StrFmt) string

func FormatWriter

func FormatWriter(w io.Writer, desc *Description)

func LoggingHandler

func LoggingHandler(pos Pos, msg string)

func PostRecursive

func PostRecursive(cb func(Node)) func(Node)

func Recursive

func Recursive(cb func(Node) bool) func(Node)

func SerializeNode

func SerializeNode(n Node) string

Types

type BinaryExpression

type BinaryExpression struct {
	Pos      Pos
	Operator Operator
	Left     *Type
	Right    *Type
}

func (*BinaryExpression) Clone

func (n *BinaryExpression) Clone() Node

func (*BinaryExpression) Info

func (n *BinaryExpression) Info() (Pos, string, string)

type Call

type Call struct {
	Pos      Pos
	Name     *Ident
	CallName string
	NR       uint64
	Args     []*Field
	Ret      *Type
	Attrs    []*Type
}

func (*Call) Clone

func (n *Call) Clone() Node

func (*Call) Info

func (n *Call) Info() (Pos, string, string)

type Comment

type Comment struct {
	Pos  Pos
	Text string
}

func (*Comment) Clone

func (n *Comment) Clone() Node

func (*Comment) Info

func (n *Comment) Info() (Pos, string, string)

type Define

type Define struct {
	Pos   Pos
	Name  *Ident
	Value *Int
}

func (*Define) Clone

func (n *Define) Clone() Node

func (*Define) Info

func (n *Define) Info() (Pos, string, string)

type Description

type Description struct {
	Nodes []Node
}

Description contains top-level nodes of a parsed sys description.

func Parse

func Parse(data []byte, filename string, errorHandler ErrorHandler) *Description

Parse parses sys description into AST and returns top-level nodes. If any errors are encountered, returns nil.

func ParseGlob

func ParseGlob(glob string, errorHandler ErrorHandler) *Description

func (*Description) Clone

func (desc *Description) Clone() *Description

func (*Description) Filter

func (desc *Description) Filter(predicate func(Node) bool) *Description

func (*Description) Walk

func (desc *Description) Walk(cb func(Node))

Walk calls callback cb for every top-level node in description. Note: it's not recursive. Use Recursive/PostRecursive helpers for recursive walk.

type ErrorHandler

type ErrorHandler func(pos Pos, msg string)

type ErrorMatcher

type ErrorMatcher struct {
	Data []byte
	// contains filtered or unexported fields
}

func NewErrorMatcher

func NewErrorMatcher(t *testing.T, file string) *ErrorMatcher

func (*ErrorMatcher) Check

func (em *ErrorMatcher) Check()

func (*ErrorMatcher) Count

func (em *ErrorMatcher) Count() int

func (*ErrorMatcher) DumpErrors

func (em *ErrorMatcher) DumpErrors()

func (*ErrorMatcher) ErrorHandler

func (em *ErrorMatcher) ErrorHandler(pos Pos, msg string)

type Field

type Field struct {
	Pos      Pos
	Name     *Ident
	Type     *Type
	Attrs    []*Type
	NewBlock bool // separated from previous fields by a new line
	Comments []*Comment
}

func (*Field) Clone

func (n *Field) Clone() Node

func (*Field) Info

func (n *Field) Info() (Pos, string, string)

type FlagValue

type FlagValue interface {
	GetName() string
}

type Flags

type Flags[T FlagValue] interface {
	SetValues(values []T)
	GetValues() []T
	GetPos() Pos
}

type Ident

type Ident struct {
	Pos  Pos
	Name string
}

func (*Ident) Clone

func (n *Ident) Clone() Node

func (*Ident) Info

func (n *Ident) Info() (Pos, string, string)

type Incdir

type Incdir struct {
	Pos Pos
	Dir *String
}

func (*Incdir) Clone

func (n *Incdir) Clone() Node

func (*Incdir) Info

func (n *Incdir) Info() (Pos, string, string)

type Include

type Include struct {
	Pos  Pos
	File *String
}

func (*Include) Clone

func (n *Include) Clone() Node

func (*Include) Info

func (n *Include) Info() (Pos, string, string)

type Int

type Int struct {
	Pos Pos
	// Only one of Value, Ident, CExpr is filled.
	Value    uint64
	ValueFmt IntFmt
	Ident    string
	CExpr    string
}

func (*Int) Clone

func (n *Int) Clone() Node

func (*Int) GetName

func (n *Int) GetName() string

func (*Int) Info

func (n *Int) Info() (Pos, string, string)

type IntFlags

type IntFlags struct {
	Pos    Pos
	Name   *Ident
	Values []*Int
}

func (*IntFlags) Clone

func (n *IntFlags) Clone() Node

func (*IntFlags) GetPos

func (n *IntFlags) GetPos() Pos

func (*IntFlags) GetValues

func (n *IntFlags) GetValues() []*Int

func (*IntFlags) Info

func (n *IntFlags) Info() (Pos, string, string)

func (*IntFlags) SetValues

func (n *IntFlags) SetValues(values []*Int)

type IntFmt

type IntFmt int
const (
	IntFmtDec IntFmt = iota
	IntFmtNeg
	IntFmtHex
	IntFmtChar
)

type Meta

type Meta struct {
	Pos   Pos
	Value *Type
}

func (*Meta) Clone

func (n *Meta) Clone() Node

func (*Meta) Info

func (n *Meta) Info() (Pos, string, string)

type NewLine

type NewLine struct {
	Pos Pos
}

func (*NewLine) Clone

func (n *NewLine) Clone() Node

func (*NewLine) Info

func (n *NewLine) Info() (Pos, string, string)

type Node

type Node interface {
	Info() (pos Pos, typ, name string)
	// Clone makes a deep copy of the node.
	Clone() Node
	// contains filtered or unexported methods
}

Node is AST node interface.

type Operator

type Operator int

type Pos

type Pos struct {
	File string
	Off  int // byte offset, starting at 0
	Line int // line number, starting at 1
	Col  int // column number, starting at 1 (byte count)
}

Pos represents source info for AST nodes.

func (Pos) Builtin

func (pos Pos) Builtin() bool

func (Pos) String

func (pos Pos) String() string

type Resource

type Resource struct {
	Pos    Pos
	Name   *Ident
	Base   *Type
	Values []*Int
}

func (*Resource) Clone

func (n *Resource) Clone() Node

func (*Resource) Info

func (n *Resource) Info() (Pos, string, string)

type StrFlags

type StrFlags struct {
	Pos    Pos
	Name   *Ident
	Values []*String
}

func (*StrFlags) Clone

func (n *StrFlags) Clone() Node

func (*StrFlags) GetPos

func (n *StrFlags) GetPos() Pos

func (*StrFlags) GetValues

func (n *StrFlags) GetValues() []*String

func (*StrFlags) Info

func (n *StrFlags) Info() (Pos, string, string)

func (*StrFlags) SetValues

func (n *StrFlags) SetValues(values []*String)

type StrFmt

type StrFmt int
const (
	StrFmtRaw StrFmt = iota
	StrFmtHex
	StrFmtIdent
)

type String

type String struct {
	Pos   Pos
	Value string
	Fmt   StrFmt
}

func (*String) Clone

func (n *String) Clone() Node

func (*String) GetName

func (n *String) GetName() string

func (*String) Info

func (n *String) Info() (Pos, string, string)

type Struct

type Struct struct {
	Pos      Pos
	Name     *Ident
	Fields   []*Field
	Attrs    []*Type
	Comments []*Comment
	IsUnion  bool
}

func (*Struct) Clone

func (n *Struct) Clone() Node

func (*Struct) Info

func (n *Struct) Info() (Pos, string, string)

type Type

type Type struct {
	Pos Pos
	// Only one of Value, Ident, String, Expression is filled.
	Value      uint64
	ValueFmt   IntFmt
	Ident      string
	String     string
	StringFmt  StrFmt
	HasString  bool
	Expression *BinaryExpression
	// Parts after COLON (for ranges and bitfields).
	Colon []*Type
	// Sub-types in [].
	Args []*Type
}

func (*Type) Clone

func (n *Type) Clone() Node

func (*Type) Info

func (n *Type) Info() (Pos, string, string)

type TypeDef

type TypeDef struct {
	Pos  Pos
	Name *Ident
	// Non-template type aliases have only Type filled.
	// Templates have Args and either Type or Struct filled.
	Args   []*Ident
	Type   *Type
	Struct *Struct
}

func (*TypeDef) Clone

func (n *TypeDef) Clone() Node

func (*TypeDef) Info

func (n *TypeDef) Info() (Pos, string, string)

Jump to

Keyboard shortcuts

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