parse

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package parse turns TypeScript source text into the canonical AST.

Frontend is the contract; two implementations exist:

Use the tsx frontend by default. Use TypeFrontend when the type grammar is all you need and the compiler dependency is not worth it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func File

func File(name string, src []byte) (*ast.SourceFile, error)

File parses a module with the default frontend.

func MustType

func MustType(src string) ast.Type

MustType parses a type expression and panics on error. It is intended for package-level variables and tests, where a malformed literal is a bug.

func Type

func Type(src string) (ast.Type, error)

Type parses a single type expression.

Types

type Capability

type Capability uint32

Capability reports what a frontend supports.

const (
	// CapTypes covers the type grammar.
	CapTypes Capability = 1 << iota
	// CapDeclarations covers interface, type alias, enum, class, and function
	// declarations.
	CapDeclarations
	// CapExpressions covers the full expression and statement grammar.
	CapExpressions
	// CapJSX covers .tsx syntax.
	CapJSX
	// CapComments preserves comments as attached documentation.
	CapComments
	// CapPositions records accurate source positions on every node.
	CapPositions
)

type Capable

type Capable interface {
	Capabilities() Capability
}

Capable is implemented by frontends that can describe their own coverage.

type Error

type Error struct {
	Pos token.Location
	Msg string
}

Error is a syntax error with a source location.

func (Error) Error

func (e Error) Error() string

type ErrorList

type ErrorList []Error

ErrorList is a collection of syntax errors.

func (ErrorList) Err

func (l ErrorList) Err() error

Err returns the list as an error, or nil when empty.

func (ErrorList) Error

func (l ErrorList) Error() string

type Frontend

type Frontend interface {
	// ParseFile parses a whole module.
	ParseFile(name string, src []byte) (*ast.SourceFile, error)

	// ParseType parses a single type expression, the operation a code
	// generator needs when accepting a type as configuration.
	ParseType(src []byte) (ast.Type, error)
}

Frontend converts TypeScript source into the canonical AST.

Implementations return the most complete tree they can alongside any errors, so that partial results remain useful.

var Default Frontend = TypeFrontend{}

Default is the frontend used by the package-level helpers.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner turns TypeScript source into tokens.

func NewScanner

func NewScanner(name string, src []byte, errs *ErrorList) *Scanner

NewScanner returns a Scanner over src.

func (*Scanner) File

func (s *Scanner) File() *token.File

File returns the position table for the scanned source.

func (*Scanner) Next

func (s *Scanner) Next() Token

Next returns the next token.

func (*Scanner) ScanTemplateContinue

func (s *Scanner) ScanTemplateContinue() Token

ScanTemplateContinue resumes scanning a template literal at a closing brace. The parser calls it after consuming a substitution expression.

type Tok

type Tok uint8

Tok is a token kind.

const (
	EOF Tok = iota
	Ident
	Str        // string literal
	Num        // numeric literal
	BigIntTok  // numeric literal with n suffix
	NoSubTmpl  // `text`
	TmplHead   // `text${
	TmplMiddle // }text${
	TmplTail   // }text`
	Punct      // any operator or delimiter; Text holds the exact spelling
)

type Token

type Token struct {
	Kind Tok
	Text string
	Span token.Span
	// Raw is the undecoded source text, retained for literals so that a
	// numeric type literal round-trips its original spelling.
	Raw string
	// NewlineBefore records whether a line terminator preceded this token,
	// which TypeScript's grammar is sensitive to in several places.
	NewlineBefore bool
	// Docs holds JSDoc comment blocks immediately preceding this token.
	Docs []string
}

Token is a lexed token. Text is the source spelling for identifiers and punctuation, and the decoded value for string and template literals.

type TypeFrontend

type TypeFrontend struct{}

TypeFrontend parses the TypeScript type grammar plus interface, type alias, enum, import, and export declarations.

It does not parse the expression and statement grammar; anything it does not recognize is preserved verbatim as an ast.RawStmt and reported.

func (TypeFrontend) Capabilities

func (TypeFrontend) Capabilities() Capability

Capabilities reports what this frontend covers.

func (TypeFrontend) ParseFile

func (TypeFrontend) ParseFile(name string, src []byte) (*ast.SourceFile, error)

ParseFile parses a module.

func (TypeFrontend) ParseType

func (TypeFrontend) ParseType(src []byte) (ast.Type, error)

ParseType parses a single type expression.

Jump to

Keyboard shortcuts

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