parser

package
v0.0.0-...-a689315 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildLexer

func BuildLexer() *lexer.StatefulDefinition

func BuildParser

func BuildParser[T any]() *participle.Parser[T]

Types

type AccessorExpr

type AccessorExpr struct {
	Head *IndexExpr `@@`
	Tail []struct {
		Op    string `@("-" ">" | ".")`
		Field string `@Ident`

		Pos lexer.Position
	} `@@*`

	Pos lexer.Position
}

func (*AccessorExpr) Transform

func (ae *AccessorExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type AddExpr

type AddExpr struct {
	Head *MulExpr `@@`
	Tail []struct {
		Op   string   `@("+" | "-")`
		Expr *MulExpr `@@`

		Pos lexer.Position
	} `@@*`

	Pos lexer.Position
}

removing left recursion, source: https://github.com/alecthomas/participle/blob/master/_examples/expr3/main.go#L33

func (*AddExpr) Transform

func (ae *AddExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type AndExpr

type AndExpr struct {
	Left  *EqualityExpr `@@`
	Op    string        `[ @("&")`
	Right *AndExpr      `@@ ]`

	Pos lexer.Position
}

func (*AndExpr) Transform

func (ae *AndExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type AssignStmt

type AssignStmt struct {
	Left  *Expr `@@`
	Right *Expr `"=" @@ ";"`

	Pos lexer.Position
}

func (*AssignStmt) Transform

func (a *AssignStmt) Transform(scope ast.ScopeLike) ast.StatementLike

type CastingExpr

type CastingExpr struct {
	Type *Type       `[ "(" @@ ")" ]`
	Expr *SizeOfExpr `@@`

	Pos lexer.Position
}

func (*CastingExpr) Transform

func (ce *CastingExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type ComparisonExpr

type ComparisonExpr struct {
	Left  *ShiftExpr      `@@`
	Op    string          `[ @("<" "=" | ">" "=" | "<" | ">")`
	Right *ComparisonExpr `@@ ]`

	Pos lexer.Position
}

func (*ComparisonExpr) Transform

func (ce *ComparisonExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type CompoundStmt

type CompoundStmt struct {
	Stmts []*Stmt `"{" @@* "}"`

	Pos lexer.Position
}

func (*CompoundStmt) Transform

func (c *CompoundStmt) Transform(scope ast.ScopeLike) ast.StatementLike

type DeclStmt

type DeclStmt struct {
	Declarator *Declarator `@@`
	Expr       *Expr       `[ "=" @@ ] ";"`

	Pos lexer.Position
}

func (*DeclStmt) Transform

func (a *DeclStmt) Transform(scope ast.ScopeLike) ast.StatementLike

type Declarator

type Declarator struct {
	Type  *Type  `@@`
	Ident string `@Ident`

	Pos lexer.Position
}

type EqualityExpr

type EqualityExpr struct {
	Left  *ComparisonExpr `@@`
	Op    string          `[ @("=" "=" | "!" "=")`
	Right *EqualityExpr   `@@ ]`

	Pos lexer.Position
}

func (*EqualityExpr) Transform

func (ee *EqualityExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type ExclusiveOrExpr

type ExclusiveOrExpr struct {
	Left  *AndExpr         `@@`
	Op    string           `[ @("^")`
	Right *ExclusiveOrExpr `@@ ]`

	Pos lexer.Position
}

func (*ExclusiveOrExpr) Transform

func (ea *ExclusiveOrExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type Expr

type Expr struct {
	LogicalExpr *LogicalExpr `@@`

	Pos lexer.Position
}

func (*Expr) Transform

func (e *Expr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type ExprStmt

type ExprStmt struct {
	Expr *Expr `@@ ";"`

	Pos lexer.Position
}

func (*ExprStmt) Transform

func (a *ExprStmt) Transform(scope ast.ScopeLike) ast.StatementLike

type FnCallExpr

type FnCallExpr struct {
	Ident string  `@Ident`
	Args  []*Expr `"(" (@@ ("," @@)*)? ")"`

	Pos lexer.Position
}

func (*FnCallExpr) Transform

func (fce *FnCallExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type Function

type Function struct {
	Declarator  *Declarator   `@@ "("`
	Params      []*Declarator `( @@ ( "," @@ )* )?`
	Variadic    bool          `@( "," "." "." "." )? ")"`
	Body        *CompoundStmt `( @@`
	OnlyDeclare bool          `| @";" )`

	Pos lexer.Position
}

func (*Function) Transform

func (f *Function) Transform(scope ast.ScopeLike) *ast.Function

type IfStmt

type IfStmt struct {
	Condition *Expr `"if" "(" @@ ")"`
	Then      *Stmt `@@`
	Else      *Stmt `( "else" @@ )?`

	Pos lexer.Position
}

func (*IfStmt) Transform

func (i *IfStmt) Transform(scope ast.ScopeLike) ast.StatementLike

type InclusiveOrExpr

type InclusiveOrExpr struct {
	Left  *ExclusiveOrExpr `@@`
	Op    string           `[ @("|")`
	Right *InclusiveOrExpr `@@ ]`

	Pos lexer.Position
}

func (*InclusiveOrExpr) Transform

func (io *InclusiveOrExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type IndexExpr

type IndexExpr struct {
	Head *UnaryExpr `@@`
	Tail []struct {
		Index *Expr `'[' @@ ']'`

		Pos lexer.Position
	} `@@*`

	Pos lexer.Position
}

func (*IndexExpr) Transform

func (ie *IndexExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type InternalString

type InternalString struct {
	Parts []string `( @Escaped | @Chars )*`
}

type LogicalExpr

type LogicalExpr struct {
	Left  *InclusiveOrExpr `@@`
	Op    string           `[ @("&" "&" | "|" "|")`
	Right *LogicalExpr     `@@ ]`

	Pos lexer.Position
}

func (*LogicalExpr) Transform

func (le *LogicalExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type Module

type Module struct {
	TypeDefs  []*TypeDef  `@@*`
	Functions []*Function `@@*`

	Pos lexer.Position
}

func (*Module) Transform

func (m *Module) Transform(scope *ast.Scope) *ast.Module

type MulExpr

type MulExpr struct {
	Head *CastingExpr `@@`
	Tail []struct {
		Op   string       `@("*" | "/" | "%")`
		Expr *CastingExpr `@@`

		Pos lexer.Position
	} `@@*`

	Pos lexer.Position
}

func (*MulExpr) Transform

func (me *MulExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type Parser

type Parser struct {
}

func NewParser

func NewParser() *Parser

func (*Parser) ParseFile

func (p *Parser) ParseFile(file *utils.File) (*Module, error)

func (*Parser) ParseString

func (p *Parser) ParseString(s string) (*Module, error)

func (*Parser) Parser

func (p *Parser) Parser(lexer *lexer.StatefulDefinition) *participle.Parser[Module]

func (*Parser) String

func (p *Parser) String() string

type PostfixExpr

type PostfixExpr struct {
	Next    *AccessorExpr    `@@`
	Postfix *PostfixExprTick `@@`

	Pos lexer.Position
}

PO -> PO o | AE removing left recursion because participle doesn't support it: https://cyberzhg.github.io/toolbox/left_rec PO -> AE PO' PO' -> o PO' | ε

func (*PostfixExpr) Transform

func (pe *PostfixExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type PostfixExprTick

type PostfixExprTick struct {
	Op   string           `[ @("+" "+" | "-" "-")`
	Expr *PostfixExprTick `@@ ]`

	Pos lexer.Position
}

func (*PostfixExprTick) Transform

func (pet *PostfixExprTick) Transform(scope ast.ScopeLike, ae ast.ExpressionLike) ast.ExpressionLike

type PrefixExpr

type PrefixExpr struct {
	Op   string       `( @( "+" "+" | "-" "-" | "!" | "-" | "*" | "&" )`
	Expr *PrefixExpr  `@@`
	Next *PostfixExpr `| @@ )`

	Pos lexer.Position
}

func (*PrefixExpr) Transform

func (pe *PrefixExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type PrimaryExpr

type PrimaryExpr struct {
	// StructExpr must be before Ident, because StructExpr starts with Ident
	Struct   *StructExpr `@@`
	Variable string      `| @Ident`
	Sign     string      `| @("+" | "-")?`
	Number   string      `@Number`
	Char     string      `| ( CharStart @SingleChar CharEnd )`
	// we need to use a struct here, because we want to allow empty strings
	String *InternalString `| ( StringStart @@ StringEnd )`
	Expr   *Expr           `| "(" @@ ")"`

	Pos lexer.Position
}

func (*PrimaryExpr) Transform

func (pe *PrimaryExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type ReturnStmt

type ReturnStmt struct {
	Expr *Expr `"return" @@? ";"`

	Pos lexer.Position
}

func (*ReturnStmt) Transform

func (r *ReturnStmt) Transform(scope ast.ScopeLike) ast.StatementLike

type ShiftExpr

type ShiftExpr struct {
	Head *AddExpr `@@`
	Tail []struct {
		Op   string   `@("<" "<" | ">" ">")`
		Expr *AddExpr `@@`

		Pos lexer.Position
	} `@@*`

	Pos lexer.Position
}

func (*ShiftExpr) Transform

func (ae *ShiftExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type SizeOfExpr

type SizeOfExpr struct {
	Head *PrefixExpr `@@`

	Type *Type       `| "sizeof" "(" @@ ")"`
	Expr *PrefixExpr `| "sizeof" @@`

	Pos lexer.Position
}

func (*SizeOfExpr) Transform

func (soe *SizeOfExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type Stmt

type Stmt struct {
	DeclStmt     *DeclStmt     `@@`
	AssignStmt   *AssignStmt   `| @@`
	ExprStmt     *ExprStmt     `| @@`
	ReturnStmt   *ReturnStmt   `| @@`
	CompoundStmt *CompoundStmt `| @@`
	IfStmt       *IfStmt       `| @@`
	WhileStmt    *WhileStmt    `| @@`

	Pos lexer.Position
}

func (*Stmt) Transform

func (s *Stmt) Transform(scope ast.ScopeLike) []ast.StatementLike

type Struct

type Struct struct {
	Fields []*Declarator `"struct" "{" @@ ( "," @@ )* "," "}"`
}

type StructExpr

type StructExpr struct {
	Alias        string         `@Ident`
	StructFields []*StructField `"{" @@ ( "," @@ )* "," "}"`
}

type StructField

type StructField struct {
	Field string `"." @Ident`
	Expr  *Expr  `"=" @@`
}

type Type

type Type struct {
	Lengths []int `( "[" @Number "]" )*`

	Struct *Struct `( @@`
	// must match lexer.go BasicType AND ast.Type
	Basic string `| @("bool" | "void" | "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64")`
	Alias string `| @Ident )`

	Pointers string `@"*"*`

	Pos lexer.Position
}

func (*Type) Transform

func (t *Type) Transform(scope ast.ScopeLike) *ast.Type

type TypeDef

type TypeDef struct {
	Type  *Type  `"type" @@ `
	Ident string `@Ident ";"`

	Pos lexer.Position
}

func (*TypeDef) Transform

func (t *TypeDef) Transform(scope ast.ScopeLike) *ast.TypeDef

type UnaryExpr

type UnaryExpr struct {
	// SizeOfExpr  *SizeOfExpr  `@@`
	FnCallExpr  *FnCallExpr  `@@`
	PrimaryExpr *PrimaryExpr `| @@`

	Pos lexer.Position
}

func (*UnaryExpr) Transform

func (ue *UnaryExpr) Transform(scope ast.ScopeLike) ast.ExpressionLike

type WhileStmt

type WhileStmt struct {
	Condition *Expr `"while" "(" @@ ")"`
	Body      *Stmt `@@`

	Pos lexer.Position
}

func (*WhileStmt) Transform

func (w *WhileStmt) Transform(scope ast.ScopeLike) ast.StatementLike

Jump to

Keyboard shortcuts

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