hare

package
v0.0.0-...-8cdda25 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TypeInt     = &Type{Storage: StorageInt}
	TypeUint    = &Type{Storage: StorageUint}
	TypeSize    = &Type{Storage: StorageSize}
	TypeUintptr = &Type{Storage: StorageUintptr}
	TypeChar    = &Type{Storage: StorageChar}
	TypeI8      = &Type{Storage: StorageI8}
	TypeI16     = &Type{Storage: StorageI16}
	TypeI32     = &Type{Storage: StorageI32}
	TypeI64     = &Type{Storage: StorageI64}
	TypeU8      = &Type{Storage: StorageU8}
	TypeU16     = &Type{Storage: StorageU16}
	TypeU32     = &Type{Storage: StorageU32}
	TypeU64     = &Type{Storage: StorageU64}
	TypeF32     = &Type{Storage: StorageF32}
	TypeF64     = &Type{Storage: StorageF64}
	TypeRune    = &Type{Storage: StorageRune}
	TypeBool    = &Type{Storage: StorageBool}
	TypeVoid    = &Type{Storage: StorageVoid}
	TypeStr     = &Type{Storage: StorageStr}
	TypeNull    = &Type{Storage: StorageNull}

	TypeFConst = &Type{Storage: StorageFConst}
	TypeRConst = &Type{Storage: StorageRConst}
	TypeValist = &Type{Storage: StorageValist}
)

Functions

func Lookup

func Lookup(id *Identifier) (*Declaration, *EnumReference, *Error)

Lookup searches for a currently visible declaration for the identifier. If the identifier references an enum value, the first return value will be the enum's type declaration, and the second return value will describe the value. Otherwise the second return value is nil.

func ParseExpression

func ParseExpression(s string) (Expression, *Error)

func ParseType

func ParseType(s string) (*Type, *Error)

Types

type AllocExpression

type AllocExpression struct {
	Token  Token
	Init   Expression
	Cap    Expression
	Expand bool
	Free   bool
}

func (*AllocExpression) Range

func (a *AllocExpression) Range() Range

type AppendExpression

type AppendExpression struct {
	Token  Token
	Target Expression
	Value  Expression
	Len    Expression
	Expand bool
	Static bool
}

func (*AppendExpression) Range

func (a *AppendExpression) Range() Range

type ArrayLiteral

type ArrayLiteral struct {
	Start, End Token
	Members    []Expression
	Expand     bool
}

func (*ArrayLiteral) Range

func (a *ArrayLiteral) Range() Range

type ArrayType

type ArrayType struct {
	Elem           *Type
	Size           Expression
	Unbounded      bool
	ContextDefined bool
}

type AssertExpression

type AssertExpression struct {
	Token   Token
	Assert  Expression
	Message string
	Static  bool
}

func (*AssertExpression) Range

func (a *AssertExpression) Range() Range

type AssignmentExpression

type AssignmentExpression struct {
	Target   Expression
	Operator Token
	Value    Expression
}

func (*AssignmentExpression) Range

func (a *AssignmentExpression) Range() Range

type BinaryExpression

type BinaryExpression struct {
	L, R     Expression
	Operator Token
}

func (*BinaryExpression) Range

func (b *BinaryExpression) Range() Range

type Binding

type Binding struct {
	Names []Token
	Type  *Type
	Value Expression
	Attr  *DeclAttr
}

type BindingExpression

type BindingExpression struct {
	Token
	Static   bool
	Const    bool
	Bindings []Binding
}

func (*BindingExpression) Range

func (b *BindingExpression) Range() Range

type BoolLiteral

type BoolLiteral struct{ Token }

type CallExpression

type CallExpression struct {
	Func   Expression
	Args   []Expression
	Unwrap bool
}

func (*CallExpression) Range

func (c *CallExpression) Range() Range

type CastExpression

type CastExpression struct {
	Value Expression
	Cast  Token
	Type  *Type
}

func (*CastExpression) Range

func (c *CastExpression) Range() Range

type CompoundExpression

type CompoundExpression struct {
	Start, End  Token
	Label       Token
	Expressions []Expression
}

func (*CompoundExpression) Range

func (c *CompoundExpression) Range() Range

type ConstantDeclaration

type ConstantDeclaration struct {
	Type  *Type
	Value Expression
}

type ControlExpression

type ControlExpression struct {
	Control    Token
	Label      Token
	Expression Expression
}

func (*ControlExpression) Range

func (c *ControlExpression) Range() Range

type DeclAttr

type DeclAttr struct {
	Attr string
	Arg  string
}

type Declaration

type Declaration struct {
	Comment []string
	Export  bool
	Name    *Identifier
	Ignore  bool
	Data    interface{}
}

type DeferExpression

type DeferExpression struct {
	Token
	Expression
}

func (*DeferExpression) Range

func (d *DeferExpression) Range() Range

type DeleteExpression

type DeleteExpression struct {
	Token  Token
	Target Expression
	Static bool
}

func (*DeleteExpression) Range

func (d *DeleteExpression) Range() Range

type EnumReference

type EnumReference struct {
	Type  *EnumType
	Index int
}

An EnumReference describes a member of an enum.

type EnumType

type EnumType struct {
	Storage TypeStorage
	Members []EnumValue
}

type EnumValue

type EnumValue struct {
	Name  Token
	Value Expression
}

type Error

type Error struct {
	Message string
	Range
}

func ResolveType

func ResolveType(t *Type) *Error

func (*Error) Error

func (e *Error) Error() string

type ErrorPropExpression

type ErrorPropExpression struct {
	Value    Expression
	Operator Token
}

func (*ErrorPropExpression) Range

func (e *ErrorPropExpression) Range() Range

type Expression

type Expression interface {
	Range() Range
}

type FieldAccessExpression

type FieldAccessExpression struct {
	Prefix Expression
	Name   Token
	Index  int
}

func (*FieldAccessExpression) Range

func (f *FieldAccessExpression) Range() Range

type File

type File struct {
	Name    string
	Version int
}

type FloatLiteral

type FloatLiteral struct {
	Token
	Value float64
	Type  *Type
}

type ForExpression

type ForExpression struct {
	Token
	Binding      *BindingExpression
	Condition    Expression
	Afterthought Expression
	Body         Expression
}

type FunctionAttrType

type FunctionAttrType int
const (
	FunctionNormal FunctionAttrType = iota
	FunctionInit
	FunctionFini
	FunctionTest
)

type FunctionDeclaration

type FunctionDeclaration struct {
	Prototype FunctionType
	Body      Expression
	Symbol    string
	Attr      FunctionAttrType
}

type FunctionType

type FunctionType struct {
	NoReturn   bool
	Parameters []Parameter
	Varargs    bool
	CVarargs   bool
	Result     *Type
}

func (*FunctionType) Print

func (f *FunctionType) Print(w io.StringWriter, name string)

func (*FunctionType) String

func (f *FunctionType) String() string

type GlobalDeclaration

type GlobalDeclaration struct {
	Const  bool
	Type   *Type
	Value  Expression
	Symbol string
}

type IConstType

type IConstType struct {
	Min, Max int64
	Uint     bool
}

type Identifier

type Identifier struct {
	Names []Token
	Unit  *Unit
}

func (*Identifier) Last

func (i *Identifier) Last() Token

func (*Identifier) Print

func (i *Identifier) Print(w io.StringWriter)

func (*Identifier) Range

func (i *Identifier) Range() Range

func (*Identifier) String

func (i *Identifier) String() string

type IfExpression

type IfExpression struct {
	Token
	Condition Expression
	Then      Expression
	Else      Expression
}

type Import

type Import struct {
	Alias   string
	Module  Identifier
	All     bool
	Members []ImportMember
}

type ImportMember

type ImportMember struct {
	Alias string
	Name  Token
}

type IndexingExpression

type IndexingExpression struct {
	Prefix Expression
	Index  Expression
}

func (*IndexingExpression) Range

func (i *IndexingExpression) Range() Range

type InsertExpression

type InsertExpression struct {
	Token  Token
	Target Expression
	Value  Expression
	Expand bool
	Static bool
}

func (*InsertExpression) Range

func (i *InsertExpression) Range() Range

type IntLiteral

type IntLiteral struct {
	Token
	Value int64
	Type  *Type
}

type LengthExpression

type LengthExpression struct {
	Token      Token
	Expression Expression
}

func (*LengthExpression) Range

func (l *LengthExpression) Range() Range

type MatchCase

type MatchCase struct {
	Names  []Token
	Type   *Type
	Result []Expression
}

type MatchExpression

type MatchExpression struct {
	Token
	Value Expression
	Cases []MatchCase
}

type Module

type Module struct {
	// fully-qualified name, e.g. "encoding::utf8"
	Name string
	// file system path, e.g. "/usr/src/hare/stdlib/encoding/utf8"
	// or empty if the module was not found
	Path         string
	Declarations map[string]*Declaration
	Exports      map[string]*Declaration

	// The last I/O error encountered while loading the module. This hints at a
	// misconfigured hare installation or HAREPATH.
	// The module can still be used even if this error is set, but it is likely
	// missing declarations.
	LoadError *ModuleError
	// The last error caused by ambiguos tags. This may be resolved after adding
	// or removing files.
	// The module can still be used even if this error is set, but it is likely
	// missing declarations.
	TagError *ModuleConflictError
	// all files currently included in the module
	CurrentSources map[string]*Unit
	Cache          *ModuleCache
	// contains filtered or unexported fields
}

Module represents a hare module. Fields should be treated as read-only.

func (*Module) HasErrors

func (m *Module) HasErrors() bool

HasErrors returns true if any errors were encountered while loading the module. The module can still be used even in the presence of errors, but it is likely missing declarations.

func (*Module) ParseErrors

func (m *Module) ParseErrors() []*Error

ParseError returns all parse errors encountered in this module. The module can still be used even in the presence of errors, but it is likely missing declarations.

func (*Module) Reload

func (m *Module) Reload()

Reload discards all cached files and loads the module from the file system.

func (*Module) RemoveFile

func (m *Module) RemoveFile(name string)

RemoveFile removes a file from the module. This should be called e.g. after a file was deleted.

func (*Module) StripInternals

func (m *Module) StripInternals()

StripInternals removes all unexported declarations and function bodies from the module.

This reduces memory usage for modules that are only needed to resolve identifiers in other modules. Module internals will be restored if files are added or removed.

type ModuleCache

type ModuleCache struct {
	// The current working directory. If set, it is examined first while
	// searching for modules.
	WorkingDir string
	// The current tags, without the '+'; e.g. ["linux", "x86_64"]
	Tags []string
	// The current HAREPATH.
	Harepath []string

	OnLoad func(*Module)
	// contains filtered or unexported fields
}

A ModuleCache can store and lookup modules.

The ModuleCache contains information neccessary for finding modules and files, such as HAREPATH and tags; these must not be changed after modules are loaded.

func (*ModuleCache) Get

func (c *ModuleCache) Get(name string) *Module

Get looks up a module from its fully-qualified name. Returns nil if no module was found.

func (*ModuleCache) GetUnit

func (c *ModuleCache) GetUnit(name string) *Unit

GetUnit looks up a unit from a file name. If the file does not exist, it returns an empty unit.

func (*ModuleCache) GuessWorkingDir

func (c *ModuleCache) GuessWorkingDir(file string)

GuessWorkingDir tries to find a likely working dir based on a path inside it.

func (*ModuleCache) ModuleForFile

func (c *ModuleCache) ModuleForFile(name string) *Module

ModuleForFile takes a file path and returns the module that this file belongs to.

Returns nil if the file does not belong to any visible module (either because it is not on the HAREPATH or because it is shadowed by another module with higher priority).

func (*ModuleCache) SetDefaultHarepath

func (c *ModuleCache) SetDefaultHarepath()

SetDefaultHarepath sets the harepath according to environment variables.

func (*ModuleCache) SetDefaultTags

func (c *ModuleCache) SetDefaultTags()

SetDefaultTags initializes the tags with default values.

type ModuleConflictError

type ModuleConflictError struct {
	Module string
	Path   string
	Names  []string
}

func (*ModuleConflictError) Error

func (e *ModuleConflictError) Error() string

type ModuleError

type ModuleError struct {
	Module string
	Path   string
	Cause  error
}

func (*ModuleError) Error

func (e *ModuleError) Error() string

func (*ModuleError) Unwrap

func (e *ModuleError) Unwrap() error

type NullLiteral

type NullLiteral struct{ Token }

type OffsetExpression

type OffsetExpression struct {
	Token Token
	FieldAccessExpression
}

func (*OffsetExpression) Range

func (o *OffsetExpression) Range() Range

type Parameter

type Parameter struct {
	Name Token
	Type *Type
}

type Position

type Position struct {
	Line, Index int
}

func (Position) Less

func (p Position) Less(q Position) bool

type Range

type Range struct {
	Start, End Position
	File       *File
}

func (Range) Contains

func (r Range) Contains(p Position) bool

func (Range) Union

func (r Range) Union(q Range) Range

type RuneLiteral

type RuneLiteral struct {
	Token
	Value rune
}

type SizeExpression

type SizeExpression struct {
	Token Token
	Type  *Type
}

func (*SizeExpression) Range

func (s *SizeExpression) Range() Range

type SlicingExpression

type SlicingExpression struct {
	Prefix     Expression
	Start, End Expression
}

func (*SlicingExpression) Range

func (s *SlicingExpression) Range() Range

type StringLiteral

type StringLiteral struct {
	Token
	Value string
}

type StructField

type StructField struct {
	Offset     Expression
	Name       Token
	Type       *Type
	Identifier *Identifier
}

type StructFieldValue

type StructFieldValue struct {
	Name  Token
	Type  *Type
	Value Expression
}

type StructLiteral

type StructLiteral struct {
	Type   *Identifier
	Fields []StructFieldValue
	Fill   bool
}

func (*StructLiteral) Range

func (s *StructLiteral) Range() Range

type StructType

type StructType struct {
	Union  bool
	Fields []StructField
}

func (*StructType) Print

func (s *StructType) Print(w io.StringWriter, lines bool, prefix string)

type SwitchCase

type SwitchCase struct {
	Options []Expression
	Result  []Expression
}

type SwitchExpression

type SwitchExpression struct {
	Token
	Value Expression
	Cases []SwitchCase
}

type TaggedUnionType

type TaggedUnionType struct {
	Types []*Type
}

type Token

type Token struct {
	File      *File
	Raw       string
	Line, Pos int
	Type      TokenType
}

func Tokenize

func Tokenize(f *File, lines []string) []Token

func (Token) Range

func (t Token) Range() Range

type TokenType

type TokenType int
const (
	TokComment TokenType = iota
	TokKeyword
	TokIdentifier
	TokInt
	TokFloat
	TokRune
	TokString
	TokRawString
	TokAttr
	TokSymbol
	TokInvalid TokenType = -1
)

type Tokens

type Tokens struct {
	Src []Token
	// contains filtered or unexported fields
}

func (*Tokens) DocComment

func (t *Tokens) DocComment() []string

func (*Tokens) EOF

func (t *Tokens) EOF() bool

func (*Tokens) Fail

func (t *Tokens) Fail(tok Token, msg string)

func (*Tokens) Get

func (t *Tokens) Get(typ TokenType) (Token, bool)

func (*Tokens) GetRaw

func (t *Tokens) GetRaw(raw string) bool

func (*Tokens) GetString

func (t *Tokens) GetString(first Token) string

func (*Tokens) Last

func (t *Tokens) Last() Token

func (*Tokens) Must

func (t *Tokens) Must(typ TokenType, msg string) Token

func (*Tokens) MustRaw

func (t *Tokens) MustRaw(raw string, msg string)

func (*Tokens) Next

func (t *Tokens) Next() Token

func (*Tokens) Ok

func (t *Tokens) Ok() bool

func (*Tokens) Peek

func (t *Tokens) Peek() Token

func (*Tokens) Sync

func (t *Tokens) Sync() bool

type Tuple

type Tuple struct {
	Start, End Token
	Content    []Expression
}

func (*Tuple) Range

func (t *Tuple) Range() Range

type TupleType

type TupleType struct {
	Types []*Type
}

type Type

type Type struct {
	Storage TypeStorage
	Const   bool
	Error   bool
	Alias   *Identifier
	Data    TypeData
	Range   Range

	Size  int
	Align int
	// contains filtered or unexported fields
}

func (*Type) Print

func (t *Type) Print(w io.StringWriter)

func (*Type) String

func (t *Type) String() string

type TypeData

type TypeData interface {
	// contains filtered or unexported methods
}

type TypeDeclaration

type TypeDeclaration struct {
	Type *Type
}

type TypeStorage

type TypeStorage int
const (
	StorageVoid TypeStorage = iota
	StorageNull
	StorageInt
	StorageI8
	StorageI16
	StorageI32
	StorageI64
	StorageUint
	StorageU8
	StorageU16
	StorageU32
	StorageU64
	StorageSize
	StorageUintptr
	StorageF32
	StorageF64
	StorageRune
	StorageChar
	StorageBool
	StorageStr
	StorageRConst
	StorageFConst
	StorageValist

	StorageIConst
	StoragePointer
	StorageNPointer
	StorageArray
	StorageSlice
	StorageStruct
	StorageTagged
	StorageTuple
	StorageEnum
	StorageFn
	StorageAlias
)

type UnaryExpression

type UnaryExpression struct {
	Value    Expression
	Operator Token
}

func (*UnaryExpression) Range

func (u *UnaryExpression) Range() Range

type Unit

type Unit struct {
	File *File
	// the module this unit is part of, may be nil
	Module *Module

	Imports      []Import
	Declarations []*Declaration
	Errors       []*Error
}

Unit represents a hare compilation unit.

func (*Unit) ParseLines

func (u *Unit) ParseLines(lines []string)

func (*Unit) ParseTokens

func (u *Unit) ParseTokens(tok []Token)

type VoidLiteral

type VoidLiteral struct{ Token }

Jump to

Keyboard shortcuts

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