parser

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ASTBuilder

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

ASTBuilder builds our internal AST from tree-sitter CST

func NewASTBuilder

func NewASTBuilder(filename string, source []byte) *ASTBuilder

NewASTBuilder creates a new AST builder

func (*ASTBuilder) Build

func (b *ASTBuilder) Build(tsNode *sitter.Node) *Node

Build builds the AST from a tree-sitter node

type Location

type Location struct {
	File      string
	StartLine int
	StartCol  int
	EndLine   int
	EndCol    int
}

Location represents the position of a node in the source code

func (Location) String

func (l Location) String() string

String returns a string representation of the location

type Node

type Node struct {
	Type     NodeType
	Value    interface{} // Can hold various values depending on node type
	Children []*Node
	Location Location
	Parent   *Node

	// Common fields for various node types
	Name string // For function/class/variable names

	// Function-related fields
	Params    []*Node // Function parameters
	Body      []*Node // Function/block body
	Async     bool    // Async function
	Generator bool    // Generator function

	// Control flow fields
	Test       *Node   // Condition for if/while/for
	Consequent *Node   // Then branch for if
	Alternate  *Node   // Else branch for if
	Init       *Node   // For loop initializer
	Update     *Node   // For loop update
	Cases      []*Node // Switch cases

	// Try-catch fields
	Handler   *Node   // Catch clause
	Finalizer *Node   // Finally block
	Handlers  []*Node // Multiple catch handlers

	// Expression fields
	Left      *Node   // Left operand
	Right     *Node   // Right operand
	Operator  string  // Operator (+, -, *, etc.)
	Argument  *Node   // Unary expression argument
	Arguments []*Node // Function call arguments
	Callee    *Node   // Function being called
	Object    *Node   // Object in member expression
	Property  *Node   // Property in member expression

	// Variable declaration fields
	Kind         string  // var, let, const
	Declarations []*Node // Variable declarators

	// Import/Export fields
	Source      *Node   // Import source
	Specifiers  []*Node // Import/export specifiers
	Declaration *Node   // Export declaration
	Imported    *Node   // Imported name
	Local       *Node   // Local binding

	// TypeScript fields
	TypeAnnotation *Node   // Type annotation
	TypeParameters []*Node // Generic type parameters

	// Utility fields
	Computed bool   // Computed property
	Optional bool   // Optional chaining
	Raw      string // Raw literal value
}

Node represents an AST node

func NewNode

func NewNode(nodeType NodeType) *Node

NewNode creates a new AST node

func ParseForLanguage

func ParseForLanguage(filename string, source []byte) (*Node, error)

ParseForLanguage automatically selects JavaScript or TypeScript parser based on file extension

func (*Node) AddChild

func (n *Node) AddChild(child *Node)

AddChild adds a child node

func (*Node) IsExpression

func (n *Node) IsExpression() bool

IsExpression returns true if the node is an expression

func (*Node) IsFunction

func (n *Node) IsFunction() bool

IsFunction returns true if the node is a function

func (*Node) IsStatement

func (n *Node) IsStatement() bool

IsStatement returns true if the node is a statement

func (*Node) String

func (n *Node) String() string

String returns a string representation of the node

func (*Node) Walk

func (n *Node) Walk(visitor func(*Node) bool)

Walk traverses the AST depth-first and calls the visitor function for each node If the visitor returns false, traversal of that branch is stopped

type NodeType

type NodeType string

NodeType represents the type of AST node

const (
	// Program and structure
	NodeProgram NodeType = "Program"
	NodeScript  NodeType = "Script"

	// Function declarations
	NodeFunction           NodeType = "FunctionDeclaration"
	NodeFunctionExpression NodeType = "FunctionExpression"
	NodeArrowFunction      NodeType = "ArrowFunctionExpression"
	NodeAsyncFunction      NodeType = "AsyncFunctionDeclaration"
	NodeGeneratorFunction  NodeType = "GeneratorFunctionDeclaration"
	NodeMethodDefinition   NodeType = "MethodDefinition"

	// Class declarations
	NodeClass           NodeType = "ClassDeclaration"
	NodeClassExpression NodeType = "ClassExpression"

	// Variable declarations
	NodeVariableDeclaration NodeType = "VariableDeclaration"
	NodeVariableDeclarator  NodeType = "VariableDeclarator"
	NodeIdentifier          NodeType = "Identifier"

	// Control flow statements
	NodeIfStatement       NodeType = "IfStatement"
	NodeSwitchStatement   NodeType = "SwitchStatement"
	NodeCaseClause        NodeType = "SwitchCase"
	NodeDefaultClause     NodeType = "SwitchDefault"
	NodeForStatement      NodeType = "ForStatement"
	NodeForInStatement    NodeType = "ForInStatement"
	NodeForOfStatement    NodeType = "ForOfStatement"
	NodeWhileStatement    NodeType = "WhileStatement"
	NodeDoWhileStatement  NodeType = "DoWhileStatement"
	NodeBreakStatement    NodeType = "BreakStatement"
	NodeContinueStatement NodeType = "ContinueStatement"
	NodeReturnStatement   NodeType = "ReturnStatement"
	NodeThrowStatement    NodeType = "ThrowStatement"

	// Exception handling
	NodeTryStatement  NodeType = "TryStatement"
	NodeCatchClause   NodeType = "CatchClause"
	NodeFinallyClause NodeType = "FinallyClause"

	// Expressions
	NodeCallExpression        NodeType = "CallExpression"
	NodeMemberExpression      NodeType = "MemberExpression"
	NodeBinaryExpression      NodeType = "BinaryExpression"
	NodeUnaryExpression       NodeType = "UnaryExpression"
	NodeLogicalExpression     NodeType = "LogicalExpression"
	NodeConditionalExpression NodeType = "ConditionalExpression"
	NodeAssignmentExpression  NodeType = "AssignmentExpression"
	NodeUpdateExpression      NodeType = "UpdateExpression"
	NodeNewExpression         NodeType = "NewExpression"
	NodeThisExpression        NodeType = "ThisExpression"
	NodeSequenceExpression    NodeType = "SequenceExpression"
	NodeAwaitExpression       NodeType = "AwaitExpression"
	NodeYieldExpression       NodeType = "YieldExpression"
	NodeSpreadElement         NodeType = "SpreadElement"
	NodeTemplateLiteral       NodeType = "TemplateLiteral"

	// Literals
	NodeLiteral          NodeType = "Literal"
	NodeStringLiteral    NodeType = "StringLiteral"
	NodeNumberLiteral    NodeType = "NumberLiteral"
	NodeBooleanLiteral   NodeType = "BooleanLiteral"
	NodeNullLiteral      NodeType = "NullLiteral"
	NodeRegExpLiteral    NodeType = "RegExpLiteral"
	NodeArrayExpression  NodeType = "ArrayExpression"
	NodeObjectExpression NodeType = "ObjectExpression"
	NodeProperty         NodeType = "Property"

	// Module system (ESM)
	NodeImportDeclaration        NodeType = "ImportDeclaration"
	NodeImportSpecifier          NodeType = "ImportSpecifier"
	NodeImportDefaultSpecifier   NodeType = "ImportDefaultSpecifier"
	NodeImportNamespaceSpecifier NodeType = "ImportNamespaceSpecifier"
	NodeExportNamedDeclaration   NodeType = "ExportNamedDeclaration"
	NodeExportDefaultDeclaration NodeType = "ExportDefaultDeclaration"
	NodeExportAllDeclaration     NodeType = "ExportAllDeclaration"
	NodeExportSpecifier          NodeType = "ExportSpecifier"

	// Module system (CommonJS)
	NodeRequireCall   NodeType = "RequireCall"
	NodeModuleExports NodeType = "ModuleExports"

	// Other statements
	NodeExpressionStatement NodeType = "ExpressionStatement"
	NodeBlockStatement      NodeType = "BlockStatement"
	NodeEmptyStatement      NodeType = "EmptyStatement"
	NodeLabeledStatement    NodeType = "LabeledStatement"
	NodeWithStatement       NodeType = "WithStatement"
	NodeDebuggerStatement   NodeType = "DebuggerStatement"

	// TypeScript-specific nodes
	NodeInterfaceDeclaration NodeType = "InterfaceDeclaration"
	NodeTypeAlias            NodeType = "TypeAliasDeclaration"
	NodeEnumDeclaration      NodeType = "EnumDeclaration"
	NodeTypeAnnotation       NodeType = "TypeAnnotation"
	NodeTypeParameter        NodeType = "TypeParameter"
	NodeImportType           NodeType = "ImportType"
	NodeAsExpression         NodeType = "AsExpression"
	NodeNonNullExpression    NodeType = "NonNullExpression"

	// JSX (if needed)
	NodeJSXElement   NodeType = "JSXElement"
	NodeJSXFragment  NodeType = "JSXFragment"
	NodeJSXAttribute NodeType = "JSXAttribute"

	// Tree-sitter specific structural nodes
	NodeStatementBlock NodeType = "StatementBlock"
	NodeElseClause     NodeType = "ElseClause"
)

JavaScript/TypeScript AST node types

type Parser

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

Parser wraps tree-sitter parser for JavaScript/TypeScript

func NewParser

func NewParser() *Parser

NewParser creates a new JavaScript parser

func NewTypeScriptParser

func NewTypeScriptParser() *Parser

NewTypeScriptParser creates a new TypeScript parser

func (*Parser) Close

func (p *Parser) Close()

Close closes the parser and frees resources

func (*Parser) IsTypeScript

func (p *Parser) IsTypeScript() bool

IsTypeScript returns true if this parser is configured for TypeScript

func (*Parser) Parse

func (p *Parser) Parse(source []byte) (*Node, error)

Parse parses JavaScript/TypeScript source code

func (*Parser) ParseFile

func (p *Parser) ParseFile(filename string, source []byte) (*Node, error)

ParseFile parses a JavaScript/TypeScript file

func (*Parser) ParseString

func (p *Parser) ParseString(source string) (*Node, error)

ParseString parses JavaScript/TypeScript source code from a string

Jump to

Keyboard shortcuts

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