parser

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: CC0-1.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Evaluate

func Evaluate(
	expr ConstExpr,
) (any, error)

Evaluate evaluates a constant expression to a concrete value. Returns int64 for integer expressions, float64 for float, string for string, bool for bool.

Types

type Annotation

type Annotation struct {
	Pos    Position
	Name   string
	Params map[string]ConstExpr
}

Annotation represents an AIDL annotation like @nullable or @Backing(type="int").

type BinaryExpr

type BinaryExpr struct {
	TokenPos Position
	Op       TokenKind
	Left     ConstExpr
	Right    ConstExpr
}

BinaryExpr represents a binary operator expression.

func (*BinaryExpr) ExprPos

func (e *BinaryExpr) ExprPos() Position

ExprPos returns the position of this expression.

type BoolLiteral

type BoolLiteral struct {
	TokenPos Position
	Value    bool
}

BoolLiteral represents a boolean constant.

func (*BoolLiteral) ExprPos

func (e *BoolLiteral) ExprPos() Position

ExprPos returns the position of this expression.

type CharLiteralExpr

type CharLiteralExpr struct {
	TokenPos Position
	Value    string
}

CharLiteralExpr represents a character constant.

func (*CharLiteralExpr) ExprPos

func (e *CharLiteralExpr) ExprPos() Position

ExprPos returns the position of this expression.

type ConstExpr

type ConstExpr interface {
	ExprPos() Position
	// contains filtered or unexported methods
}

ConstExpr is implemented by all constant expression AST nodes.

type ConstantDecl

type ConstantDecl struct {
	Pos       Position
	Type      *TypeSpecifier
	ConstName string
	Value     ConstExpr
}

ConstantDecl represents a constant declaration inside an interface, parcelable, or union.

type Definition

type Definition interface {
	GetName() string
	GetAnnotations() []*Annotation
	// contains filtered or unexported methods
}

Definition is implemented by all top-level declarations.

type Direction

type Direction int

Direction indicates the data flow direction for a method parameter.

const (
	DirectionNone  Direction = iota
	DirectionIn              // in
	DirectionOut             // out
	DirectionInOut           // inout
)

func (Direction) String

func (d Direction) String() string

String returns the AIDL keyword for the direction.

type Document

type Document struct {
	Package     *PackageDecl
	Imports     []*ImportDecl
	Definitions []Definition
}

Document is the root AST node for an AIDL file.

func Parse

func Parse(
	filename string,
	src []byte,
) (*Document, error)

Parse parses AIDL source code and returns the AST document.

func ParseFile

func ParseFile(
	filename string,
) (*Document, error)

ParseFile reads a file and parses it as AIDL.

type EnumDecl

type EnumDecl struct {
	Pos         Position
	Annots      []*Annotation
	EnumName    string
	BackingType *TypeSpecifier
	Enumerators []*Enumerator
}

EnumDecl represents an AIDL enum declaration.

func (*EnumDecl) GetAnnotations

func (d *EnumDecl) GetAnnotations() []*Annotation

GetAnnotations returns the annotations on this enum.

func (*EnumDecl) GetName

func (d *EnumDecl) GetName() string

GetName returns the enum name.

type Enumerator

type Enumerator struct {
	Pos   Position
	Name  string
	Value ConstExpr
}

Enumerator represents a single enumerator within an enum declaration.

type FieldDecl

type FieldDecl struct {
	Pos          Position
	Annots       []*Annotation
	Type         *TypeSpecifier
	FieldName    string
	DefaultValue ConstExpr
}

FieldDecl represents a field in a parcelable or union.

type FloatLiteral

type FloatLiteral struct {
	TokenPos Position
	Value    string
}

FloatLiteral represents a floating-point constant.

func (*FloatLiteral) ExprPos

func (e *FloatLiteral) ExprPos() Position

ExprPos returns the position of this expression.

type IdentExpr

type IdentExpr struct {
	TokenPos Position
	Name     string
}

IdentExpr represents a reference to a constant or enum value.

func (*IdentExpr) ExprPos

func (e *IdentExpr) ExprPos() Position

ExprPos returns the position of this expression.

type ImportDecl

type ImportDecl struct {
	Pos  Position
	Name string
}

ImportDecl represents an import statement.

type IntegerLiteral

type IntegerLiteral struct {
	TokenPos Position
	Value    string
}

IntegerLiteral represents an integer constant (decimal, hex, octal, binary).

func (*IntegerLiteral) ExprPos

func (e *IntegerLiteral) ExprPos() Position

ExprPos returns the position of this expression.

type InterfaceDecl

type InterfaceDecl struct {
	Pos       Position
	Annots    []*Annotation
	IntfName  string
	Oneway    bool
	Methods   []*MethodDecl
	Constants []*ConstantDecl
	// Nested type definitions inside this interface.
	NestedTypes []Definition
}

InterfaceDecl represents an AIDL interface declaration.

func (*InterfaceDecl) GetAnnotations

func (d *InterfaceDecl) GetAnnotations() []*Annotation

GetAnnotations returns the annotations on this interface.

func (*InterfaceDecl) GetName

func (d *InterfaceDecl) GetName() string

GetName returns the interface name.

type JavaWireField

type JavaWireField struct {
	// Name is the PascalCase field name (matches the struct field name).
	Name string
	// WriteMethod is the spec type: bool, int32, int64, float32, float64,
	// string8, string16, typed_object, or opaque.
	WriteMethod string
	// Condition, if non-empty, is a bitmask expression like "FieldsMask & 256"
	// meaning the field is only serialized when that bit is set.
	Condition string
	// GoType, if non-empty, is the qualified Go type for a typed_object field
	// whose parcelable was found in the spec registry (e.g., "os.WorkSource").
	// The codegen uses this to generate a *GoType struct field with proper
	// nullable marshal/unmarshal instead of an opaque null marker.
	GoType string
}

JavaWireField describes one field's serialization in the Java writeToParcel() method. When present on a ParcelableDecl, the codegen uses this to produce marshal/unmarshal code that matches the Java wire format (including conditional fields).

type Lexer

type Lexer struct {
	Src      []byte
	Offset   int
	Line     int
	Column   int
	Filename string
}

Lexer tokenizes AIDL source code.

func NewLexer

func NewLexer(
	filename string,
	src []byte,
) *Lexer

NewLexer creates a new Lexer for the given source.

func (*Lexer) Next

func (l *Lexer) Next() Token

Next returns the next token from the source. Returns TokenEOF at end of input. Returns an error token with Value containing the error message on lexer errors.

type MethodDecl

type MethodDecl struct {
	Pos           Position
	Annots        []*Annotation
	Oneway        bool
	ReturnType    *TypeSpecifier
	MethodName    string
	Params        []*ParamDecl
	TransactionID int
}

MethodDecl represents a method declaration inside an interface.

type NullLiteral

type NullLiteral struct {
	TokenPos Position
}

NullLiteral represents the null constant.

func (*NullLiteral) ExprPos

func (e *NullLiteral) ExprPos() Position

ExprPos returns the position of this expression.

type PackageDecl

type PackageDecl struct {
	Pos  Position
	Name string
}

PackageDecl represents a package declaration.

type ParamDecl

type ParamDecl struct {
	Pos       Position
	Annots    []*Annotation
	Direction Direction
	Type      *TypeSpecifier
	ParamName string
}

ParamDecl represents a method parameter.

type ParcelableDecl

type ParcelableDecl struct {
	Pos       Position
	Annots    []*Annotation
	ParcName  string
	Fields    []*FieldDecl
	Constants []*ConstantDecl
	// Nested type definitions inside this parcelable.
	NestedTypes []Definition
	// CppHeader is set for forward-declared parcelables (cpp_header "...").
	CppHeader string
	// NdkHeader is set for forward-declared parcelables (ndk_header "...").
	NdkHeader string
	// RustType is set for forward-declared parcelables (rust_type "...").
	RustType string
	// JavaWireFormat, when non-nil, overrides the standard AIDL-field-based
	// marshal/unmarshal with code matching the Java writeToParcel() layout.
	// Fields are still populated for struct generation, but marshal/unmarshal
	// uses this instead of the generic field-walking approach.
	JavaWireFormat []JavaWireField
}

ParcelableDecl represents an AIDL parcelable declaration.

func (*ParcelableDecl) GetAnnotations

func (d *ParcelableDecl) GetAnnotations() []*Annotation

GetAnnotations returns the annotations on this parcelable.

func (*ParcelableDecl) GetName

func (d *ParcelableDecl) GetName() string

GetName returns the parcelable name.

type Position

type Position struct {
	Filename string
	Line     int
	Column   int
}

Position represents a source location in an AIDL file.

func (Position) String

func (p Position) String() string

String returns a human-readable representation of the position.

type StringLiteralExpr

type StringLiteralExpr struct {
	TokenPos Position
	Value    string
}

StringLiteralExpr represents a string constant (unquoted value).

func (*StringLiteralExpr) ExprPos

func (e *StringLiteralExpr) ExprPos() Position

ExprPos returns the position of this expression.

type TernaryExpr

type TernaryExpr struct {
	TokenPos Position
	Cond     ConstExpr
	Then     ConstExpr
	Else     ConstExpr
}

TernaryExpr represents a ternary (conditional) expression.

func (*TernaryExpr) ExprPos

func (e *TernaryExpr) ExprPos() Position

ExprPos returns the position of this expression.

type Token

type Token struct {
	Kind  TokenKind
	Pos   Position
	Value string
}

Token represents a single lexical token from AIDL source.

type TokenKind

type TokenKind int

TokenKind identifies the type of a lexical token.

const (
	TokenEOF TokenKind = iota
	TokenError
	TokenIdent
	TokenIntLiteral
	TokenFloatLiteral
	TokenStringLiteral
	TokenCharLiteral

	// Keywords.
	TokenPackage
	TokenImport
	TokenInterface
	TokenParcelable
	TokenEnum
	TokenUnion
	TokenConst
	TokenOneway
	TokenIn
	TokenOut
	TokenInout
	TokenTrue
	TokenFalse
	TokenVoid
	TokenNull

	// Punctuation.
	TokenLBrace
	TokenRBrace
	TokenLParen
	TokenRParen
	TokenLBracket
	TokenRBracket
	TokenSemicolon
	TokenComma
	TokenDot
	TokenAssign
	TokenLAngle
	TokenRAngle

	// Operators.
	TokenPlus
	TokenMinus
	TokenStar
	TokenSlash
	TokenPercent
	TokenAmp
	TokenPipe
	TokenCaret
	TokenTilde
	TokenBang
	TokenLShift
	TokenRShift
	TokenAmpAmp
	TokenPipePipe
	TokenEqEq
	TokenBangEq
	TokenLessEq
	TokenGreaterEq
	TokenQuestion
	TokenColon

	// Annotations.
	TokenAnnotation
)

func (TokenKind) String

func (k TokenKind) String() string

String returns a human-readable name for the token kind.

type TypeSpecifier

type TypeSpecifier struct {
	Pos      Position
	Annots   []*Annotation
	Name     string
	TypeArgs []*TypeSpecifier
	IsArray  bool
	// FixedSize holds the fixed-size array dimension expression (e.g., "6"
	// or "CONST_NAME") when the type uses fixed-size array syntax like
	// byte[6]. Empty string means dynamic array or non-array.
	FixedSize string
}

TypeSpecifier represents a type reference in AIDL.

type UnaryExpr

type UnaryExpr struct {
	TokenPos Position
	Op       TokenKind
	Operand  ConstExpr
}

UnaryExpr represents a unary operator expression.

func (*UnaryExpr) ExprPos

func (e *UnaryExpr) ExprPos() Position

ExprPos returns the position of this expression.

type UnionDecl

type UnionDecl struct {
	Pos       Position
	Annots    []*Annotation
	UnionName string
	Fields    []*FieldDecl
	Constants []*ConstantDecl
	// Nested type definitions inside this union.
	NestedTypes []Definition
}

UnionDecl represents an AIDL union declaration.

func (*UnionDecl) GetAnnotations

func (d *UnionDecl) GetAnnotations() []*Annotation

GetAnnotations returns the annotations on this union.

func (*UnionDecl) GetName

func (d *UnionDecl) GetName() string

GetName returns the union name.

Jump to

Keyboard shortcuts

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