parser

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package parser provides a protocol-independent intermediate representation (IR) for parsed YAML/JSON documents. It defines SemanticNode (a tree of typed nodes with source ranges), PointerIndex (O(1) JSON pointer to range lookup), and VirtualDocument (embedded content such as YAML literal blocks with position mapping back to the parent document). This package does not import any LSP or gossip types; it depends only on core/types for Range and Position.

Index

Constants

View Source
const (
	NodeMapping  = navigator.NodeMapping
	NodeSequence = navigator.NodeSequence
	NodeScalar   = navigator.NodeScalar
	NodeNull     = navigator.NodeNull
)

NodeKind constants.

Variables

View Source
var BuildPointerIndex = navigator.BuildPointerIndex

BuildPointerIndex delegates to navigator.BuildPointerIndex.

View Source
var NewPointerIndex = navigator.NewPointerIndex

NewPointerIndex delegates to navigator.NewPointerIndex.

Functions

This section is empty.

Types

type ASTBuilder

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

ASTBuilder converts a tree-sitter CST into a SemanticNode IR tree.

type CodeSampleProvider

type CodeSampleProvider struct{}

CodeSampleProvider extracts x-codeSamples source content for syntax validation.

func (*CodeSampleProvider) Complete

func (*CodeSampleProvider) Diagnostics

func (p *CodeSampleProvider) Diagnostics(_ VirtualDocument) ([]ctypes.Diagnostic, error)

func (*CodeSampleProvider) Extract

func (p *CodeSampleProvider) Extract(root *SemanticNode, parentURI string) []VirtualDocument

func (*CodeSampleProvider) Hover

func (*CodeSampleProvider) LanguageID

func (p *CodeSampleProvider) LanguageID() string

type CompletionItem

type CompletionItem struct {
	Label      string
	Detail     string
	InsertText string
	Kind       int // maps to LSP CompletionItemKind
}

CompletionItem is a completion suggestion from an embedded language provider.

type EmbeddedLanguageProvider

type EmbeddedLanguageProvider interface {
	// LanguageID returns the embedded language identifier (e.g. "markdown").
	LanguageID() string

	// Extract finds all embedded content regions in the given node tree.
	Extract(root *SemanticNode, parentURI string) []VirtualDocument

	// Hover returns hover information for a position within a virtual document.
	Hover(vdoc VirtualDocument, pos ctypes.Position) (*HoverResult, error)

	// Complete returns completion items for a position within a virtual document.
	Complete(vdoc VirtualDocument, pos ctypes.Position) ([]CompletionItem, error)

	// Diagnostics validates the virtual document content and returns diagnostics.
	Diagnostics(vdoc VirtualDocument) ([]ctypes.Diagnostic, error)
}

EmbeddedLanguageProvider detects and extracts embedded content from a semantic node.

type ExampleProvider

type ExampleProvider struct{}

ExampleProvider validates example values embedded in OpenAPI specs against their surrounding schema context.

func (*ExampleProvider) Complete

func (*ExampleProvider) Diagnostics

func (p *ExampleProvider) Diagnostics(_ VirtualDocument) ([]ctypes.Diagnostic, error)

func (*ExampleProvider) Extract

func (p *ExampleProvider) Extract(root *SemanticNode, parentURI string) []VirtualDocument

func (*ExampleProvider) Hover

func (*ExampleProvider) LanguageID

func (p *ExampleProvider) LanguageID() string

type FoldedBlockMapper

type FoldedBlockMapper struct {
	StartLine  uint32 // source line where content begins (after > indicator)
	IndentCols uint32 // columns of indentation stripped
}

FoldedBlockMapper handles YAML folded block scalars (>). Lines are folded (newlines become spaces except for blank-line-separated paragraphs).

func (*FoldedBlockMapper) ToSource

func (m *FoldedBlockMapper) ToSource(virtual ctypes.Position) ctypes.Position

func (*FoldedBlockMapper) ToVirtual

func (m *FoldedBlockMapper) ToVirtual(source ctypes.Position) ctypes.Position

type HoverResult

type HoverResult struct {
	Contents string // Markdown contents
	Range    ctypes.Range
}

HoverResult is a hover response from an embedded language provider.

type IdentityMapper

type IdentityMapper struct {
	Offset ctypes.Position // source position of the content start
}

IdentityMapper passes positions through unchanged (for quoted/flow scalars).

func (*IdentityMapper) ToSource

func (m *IdentityMapper) ToSource(virtual ctypes.Position) ctypes.Position

func (*IdentityMapper) ToVirtual

func (m *IdentityMapper) ToVirtual(source ctypes.Position) ctypes.Position

type LiteralBlockMapper

type LiteralBlockMapper struct {
	StartLine  uint32 // source line where content begins (after | indicator)
	IndentCols uint32 // columns of indentation stripped
}

LiteralBlockMapper handles YAML literal block scalars (|).

func (*LiteralBlockMapper) ToSource

func (m *LiteralBlockMapper) ToSource(virtual ctypes.Position) ctypes.Position

func (*LiteralBlockMapper) ToVirtual

func (m *LiteralBlockMapper) ToVirtual(source ctypes.Position) ctypes.Position

type MarkdownProvider

type MarkdownProvider struct{}

MarkdownProvider extracts Markdown content from description fields.

func (*MarkdownProvider) Complete

func (*MarkdownProvider) Diagnostics

func (p *MarkdownProvider) Diagnostics(vdoc VirtualDocument) ([]ctypes.Diagnostic, error)

func (*MarkdownProvider) Extract

func (p *MarkdownProvider) Extract(root *SemanticNode, parentURI string) []VirtualDocument

func (*MarkdownProvider) Hover

func (*MarkdownProvider) LanguageID

func (p *MarkdownProvider) LanguageID() string

type NodeKind

type NodeKind = navigator.NodeKind

NodeKind is an alias for navigator.NodeKind.

type OffsetMapper

type OffsetMapper interface {
	ToSource(virtual ctypes.Position) ctypes.Position
	ToVirtual(source ctypes.Position) ctypes.Position
}

OffsetMapper translates positions between a virtual document and its source.

type Parser

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

Parser wraps tree-sitter YAML and JSON grammars for incremental parsing.

func NewParser

func NewParser(yamlLang *sitter.Language, jsonLang *sitter.Language) *Parser

NewParser creates a new parser with the given YAML and JSON languages.

func (*Parser) Close

func (p *Parser) Close()

Close releases parser resources.

func (*Parser) IncrementalParse

func (p *Parser) IncrementalParse(oldTree *sitter.Tree, content []byte, format string) (*sitter.Tree, error)

IncrementalParse performs an incremental reparse after an edit.

func (*Parser) Parse

func (p *Parser) Parse(content []byte, format string) (*sitter.Tree, error)

Parse performs a full parse of the content.

type PointerIndex

type PointerIndex = navigator.PointerIndex

PointerIndex is an alias for navigator.PointerIndex.

type QuotedStringMapper

type QuotedStringMapper struct {
	StartLine uint32 // source line of the opening quote
	StartCol  uint32 // source column after the opening quote character
}

QuotedStringMapper handles quoted YAML strings with escape sequences.

func (*QuotedStringMapper) ToSource

func (m *QuotedStringMapper) ToSource(virtual ctypes.Position) ctypes.Position

func (*QuotedStringMapper) ToVirtual

func (m *QuotedStringMapper) ToVirtual(source ctypes.Position) ctypes.Position

type SemanticNode

type SemanticNode = navigator.SemanticNode

SemanticNode is an alias for navigator.SemanticNode.

func BuildFromCST

func BuildFromCST(root *sitter.Node, source []byte) (*SemanticNode, error)

BuildFromCST creates a SemanticNode tree from a tree-sitter root node.

func BuildFromRaw

func BuildFromRaw(content []byte, format string) *SemanticNode

BuildFromRaw creates a SemanticNode from raw YAML/JSON bytes without tree-sitter. This fallback uses gopkg.in/yaml.v3 to produce a basic semantic tree.

type VirtualDocument

type VirtualDocument struct {
	URI         string // synthetic URI (e.g., "vdoc://file.yaml#/paths/~1users/get/description")
	LanguageID  string // e.g. "markdown"
	Content     string
	SourceURI   string       // parent document URI
	SourceRange ctypes.Range // range in parent document
	Mapper      OffsetMapper
}

func ExtractVirtualDocuments

func ExtractVirtualDocuments(node *SemanticNode, uri string, providers []EmbeddedLanguageProvider) []VirtualDocument

ExtractVirtualDocuments aggregates results from all providers into a single sorted slice.

type VirtualDocumentManager

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

VirtualDocumentManager maintains the set of virtual documents for all source documents.

func NewVirtualDocumentManager

func NewVirtualDocumentManager(providers ...EmbeddedLanguageProvider) *VirtualDocumentManager

NewVirtualDocumentManager creates a manager with the given providers.

func (*VirtualDocumentManager) FindAtPosition

func (m *VirtualDocumentManager) FindAtPosition(parentURI string, pos ctypes.Position) *VirtualDocument

FindAtPosition finds the virtual document containing the given position in the parent document.

func (*VirtualDocumentManager) ForParent

func (m *VirtualDocumentManager) ForParent(parentURI string) []VirtualDocument

ForParent returns all virtual documents for a parent URI.

func (*VirtualDocumentManager) Get

Get returns the virtual document for a URI.

func (*VirtualDocumentManager) Providers

Providers returns the registered embedded language providers.

func (*VirtualDocumentManager) Remove

func (m *VirtualDocumentManager) Remove(parentURI string)

Remove removes all virtual documents for a parent URI.

func (*VirtualDocumentManager) Update

func (m *VirtualDocumentManager) Update(parentURI string, root *SemanticNode)

Update regenerates virtual documents for a parent document.

Jump to

Keyboard shortcuts

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