parser

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeChecksum added in v0.2.7

func ComputeChecksum(idl *IDL) (string, error)

func ValidateIDL

func ValidateIDL(idl *IDL) error

ValidateIDL validates the parsed IDL and returns any validation errors

Types

type ArrayType

type ArrayType struct {
	Pos          lexer.Position
	ArrayMarkers string `parser:"'[' ']'"`
	// Capture the element type - can be nested
	ElementType *TypeExpr `parser:"@@"`
}

ArrayType represents []Type - uses TextUnmarshaler for recursive parsing

type Enum

type Enum struct {
	Pos       lexer.Position `json:"-"`
	Name      string         `json:"name"`
	Namespace string         `json:"namespace,omitempty"`
	Comment   string         `json:"comment,omitempty"`
	Values    []*EnumValue   `json:"values,omitempty"`
}

Enum represents an enum definition with values

type EnumDef

type EnumDef struct {
	Pos    lexer.Position
	Name   string   `parser:"@Ident '{'"`
	Values []string `parser:"@Ident* '}'"`
}

EnumDef represents an enum definition

type EnumValue

type EnumValue struct {
	Name    string `json:"name"`
	Comment string `json:"comment,omitempty"`
}

EnumValue represents a single enum value with optional comment

type ErrorDecl added in v0.2.1

type ErrorDecl struct {
	Pos     lexer.Position
	Code    int    `parser:"@IntLiteral"`
	Name    string `parser:"@Ident"`
	Message string `parser:"@StringLiteral"`
}

ErrorDecl represents a single error declaration: <code> <name> <message>

type ErrorDef added in v0.2.1

type ErrorDef struct {
	Pos       lexer.Position `json:"-"`
	Name      string         `json:"name"`                // e.g., "NotFound" or "errors.NotFound" if imported
	Namespace string         `json:"namespace,omitempty"` // Namespace where declared
	Code      int            `json:"code"`                // JSON-RPC error code
	Message   string         `json:"message"`             // Error message template
	Comment   string         `json:"comment,omitempty"`   // Documentation comment
}

ErrorDef represents a declared error with code, name, and message

type ErrorsDef added in v0.2.1

type ErrorsDef struct {
	Pos    lexer.Position
	Errors []*ErrorDecl `parser:"'{' @@* '}'"`
}

ErrorsDef represents an errors block

type Field

type Field struct {
	Pos      lexer.Position `json:"-"`
	Name     string         `json:"name"`
	Type     *Type          `json:"type"`
	Optional bool           `json:"optional,omitempty"`
	Comment  string         `json:"comment,omitempty"`
}

Field represents a struct field with type, optional flag, and comments

type FieldDef

type FieldDef struct {
	Pos      lexer.Position
	Name     string    `parser:"@Ident"`
	Type     *TypeExpr `parser:"@@"`
	Optional bool      `parser:"( @Optional )?"`
}

FieldDef represents a field definition

type IDL

type IDL struct {
	RootNamespace string       `json:"rootNamespace,omitempty"` // Namespace of the root file being parsed
	Checksum      string       `json:"checksum,omitempty"`      // SHA-256 checksum of structural elements
	Interfaces    []*Interface `json:"interfaces,omitempty"`
	Structs       []*Struct    `json:"structs,omitempty"`
	Enums         []*Enum      `json:"enums,omitempty"`
	Errors        []*ErrorDef  `json:"errors,omitempty"`
}

IDL represents the root structure containing all parsed IDL elements

func ParseIDL

func ParseIDL(filename string, input string) (*IDL, error)

ParseIDL parses an IDL file string and returns the parsed IDL structure filename is used for resolving relative imports

type IDLElement

type IDLElement struct {
	Pos       lexer.Position
	Namespace *NamespaceDef `parser:"  'namespace' @@"`
	Interface *InterfaceDef `parser:"| 'interface' @@"`
	Struct    *StructDef    `parser:"| 'struct' @@"`
	Enum      *EnumDef      `parser:"| 'enum' @@"`
	Errors    *ErrorsDef    `parser:"| 'errors' @@"`
}

IDLElement represents any top-level IDL element

type IDLFile

type IDLFile struct {
	Pos      lexer.Position
	Elements []*IDLElement `parser:"@@*"`
}

IDLFile is the root grammar structure

type ImportDef

type ImportDef struct {
	Pos  lexer.Position
	Path string
}

ImportDef represents an import statement (for backwards compatibility)

type ImportString

type ImportString string

ImportString is a custom type for parsing import paths

func (ImportString) String

func (i ImportString) String() string

String returns the string value

type Interface

type Interface struct {
	Pos       lexer.Position `json:"-"`
	Name      string         `json:"name"`
	Namespace string         `json:"namespace,omitempty"`
	Comment   string         `json:"comment,omitempty"`
	Methods   []*Method      `json:"methods,omitempty"`
}

Interface represents a service interface with methods

type InterfaceDef

type InterfaceDef struct {
	Pos     lexer.Position
	Name    string       `parser:"@Ident '{'"`
	Methods []*MethodDef `parser:"@@* '}'"`
}

InterfaceDef represents an interface definition

type MapTypeExpr

type MapTypeExpr struct {
	Pos        lexer.Position
	MapPattern string `parser:"@Map '[' @String ']'"`
	// Capture the value type - can be nested
	ValueType *TypeExpr `parser:"@@"`
}

MapTypeExpr represents map[string]ValueType - matches map pattern, value type parsed in post-processing

type Method

type Method struct {
	Pos            lexer.Position `json:"-"`
	Name           string         `json:"name"`
	Parameters     []*Parameter   `json:"parameters,omitempty"`
	ReturnType     *Type          `json:"returnType"`
	ReturnOptional bool           `json:"returnOptional,omitempty"`
	Raises         []string       `json:"raises,omitempty"`
}

Method represents an interface method with parameters and return type

type MethodDef

type MethodDef struct {
	Pos            lexer.Position
	Name           string              `parser:"@Ident '('"`
	Parameters     []*ParameterDef     `parser:"( @@ (',' @@)* )? ')' "`
	ReturnType     *TypeExpr           `parser:"@@"`
	ReturnOptional bool                `parser:"( @Optional )?"`
	Raises         []*RaisesIdentifier `parser:"( 'raises' '(' @@ ( ',' @@ )* ')' )?"`
}

MethodDef represents a method definition

type NamespaceDef

type NamespaceDef struct {
	Pos  lexer.Position
	Name string `parser:"@Ident"`
}

NamespaceDef represents a namespace declaration

type Parameter

type Parameter struct {
	Pos  lexer.Position `json:"-"`
	Name string         `json:"name"`
	Type *Type          `json:"type"`
}

Parameter represents a method parameter

type ParameterDef

type ParameterDef struct {
	Pos  lexer.Position
	Name string    `parser:"@Ident"`
	Type *TypeExpr `parser:"@@"`
}

ParameterDef represents a parameter definition

type ParseError

type ParseError struct {
	Line   int
	Column int
	Msg    string
}

ParseError represents a parsing error with position information

func (*ParseError) Error

func (e *ParseError) Error() string

type QualifiedName

type QualifiedName struct {
	Pos   lexer.Position
	Parts []string `parser:"@Ident ( '.' @Ident )*"`
}

QualifiedName represents a qualified type name (e.g., "inc.Response" or "Response")

func (*QualifiedName) String

func (q *QualifiedName) String() string

String returns the qualified name as a string

type RaisesIdentifier added in v0.2.1

type RaisesIdentifier struct {
	Name *QualifiedName `parser:"@@"`
}

RaisesIdentifier represents an error identifier in a raises clause

type Struct

type Struct struct {
	Pos       lexer.Position `json:"-"`
	Name      string         `json:"name"`
	Namespace string         `json:"namespace,omitempty"`
	Extends   string         `json:"extends,omitempty"` // Empty if no extends, can be qualified (e.g., "inc.Response")
	Comment   string         `json:"comment,omitempty"`
	Fields    []*Field       `json:"fields,omitempty"`
}

Struct represents a struct definition with fields and optional extends

type StructDef

type StructDef struct {
	Pos     lexer.Position
	Name    string         `parser:"@Ident"`
	Extends *QualifiedName `parser:"( 'extends' @@ )?"`
	Fields  []*FieldDef    `parser:"'{' @@* '}'"`
}

StructDef represents a struct definition

type Type

type Type struct {
	Pos lexer.Position `json:"-"`

	// For built-in types: string, int, float, bool
	BuiltIn string `json:"builtIn,omitempty"`

	// For arrays: []Type
	Array *Type `json:"array,omitempty"`

	// For maps: map[string]ValueType
	MapValue *Type `json:"mapValue,omitempty"`

	// For user-defined types (interfaces, structs, enums)
	UserDefined string `json:"userDefined,omitempty"`
}

Type represents a type (built-in, array, map, or user-defined)

func (*Type) IsArray

func (t *Type) IsArray() bool

IsArray returns true if this is an array type

func (*Type) IsBuiltIn

func (t *Type) IsBuiltIn() bool

IsBuiltIn returns true if this is a built-in type

func (*Type) IsMap

func (t *Type) IsMap() bool

IsMap returns true if this is a map type

func (*Type) IsUserDefined

func (t *Type) IsUserDefined() bool

IsUserDefined returns true if this is a user-defined type

func (*Type) String

func (t *Type) String() string

String returns a string representation of the type

type TypeExpr

type TypeExpr struct {
	Pos         lexer.Position
	BuiltIn     *string        `parser:"( @String | @Int | @Float | @Bool )"`
	Array       *ArrayType     `parser:"| @@"`
	MapType     *MapTypeExpr   `parser:"| @@"`
	UserDefined *QualifiedName `parser:"| @@"`
}

TypeExpr represents a type expression

type ValidationError

type ValidationError struct {
	Line   int
	Column int
	Msg    string
}

ValidationError represents a validation error with position information

func (*ValidationError) Error

func (e *ValidationError) Error() string

type ValidationErrors

type ValidationErrors struct {
	Errors []*ValidationError
}

ValidationErrors is a collection of validation errors

func (*ValidationErrors) Add

func (e *ValidationErrors) Add(err *ValidationError)

func (*ValidationErrors) Error

func (e *ValidationErrors) Error() string

func (*ValidationErrors) HasErrors

func (e *ValidationErrors) HasErrors() bool

Jump to

Keyboard shortcuts

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