pkg

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 55 Imported by: 0

Documentation

Index

Constants

View Source
const OakSlug = "oak"

Variables

This section is empty.

Functions

func FileNameToSitterLanguage

func FileNameToSitterLanguage(filename string) (*sitter.Language, error)

func GetLanguageGlobs added in v0.1.1

func GetLanguageGlobs(lang string) ([]string, error)

func LanguageNameToSitterLanguage

func LanguageNameToSitterLanguage(name string) (*sitter.Language, error)

Types

type Capture

type Capture struct {
	// Name if the capture name from the query
	Name string
	// Text is the actual text that was captured
	Text string
	// Type is the Treesitter type of the captured node
	Type string

	// TODO(manuel, 2023-04-23): Add more information about the capture
	// for example: offset, line number, filename, query name, ...
	StartByte  uint32
	EndByte    uint32
	StartPoint sitter.Point
	EndPoint   sitter.Point
}

type Match

type Match map[string]Capture

type OakCommand

type OakCommand struct {
	Language string        `yaml:"language,omitempty"`
	Queries  []SitterQuery `yaml:"queries"`
	Template string        `yaml:"template"`

	SitterLanguage *sitter.Language
	*cmds.CommandDescription
}

func (*OakCommand) DumpTree

func (oc *OakCommand) DumpTree(tree *sitter.Tree)

DumpTree prints the tree out to the console. This is lifted straight from example_test in the smacker/sitter repo.

The output format is:

source_file [0-29]

function_declaration [0-29]
    func [0-4]
    name: identifier [5-6]
    parameters: parameter_list [6-29]
        ( [6-7]
        parameter_declaration [7-18]
            name: identifier [7-8]
            , [8-9]
            name: identifier [10-11]
            , [11-12]
            name: identifier [13-14]
            type: type_identifier [15-18]

The recursive cursor walker from the documentation didn't seem to work, at least on the hcl file.

func (*OakCommand) GetLanguage

func (oc *OakCommand) GetLanguage() (*sitter.Language, error)

func (*OakCommand) Parse

func (oc *OakCommand) Parse(ctx context.Context, oldTree *sitter.Tree, code []byte) (*sitter.Tree, error)

Parse parses the given code using the language set in the command and returns the resulting tree.

func (*OakCommand) PrintQueries added in v0.1.1

func (oc *OakCommand) PrintQueries(w io.Writer) error

func (*OakCommand) Render

func (oc *OakCommand) Render(results QueryResults) (string, error)

func (*OakCommand) RenderQueries added in v0.1.1

func (oc *OakCommand) RenderQueries(layers *layers.ParsedLayers) error

RenderQueries replaces all the queries in the command with their "rendered" (using go templates) version.

WARNING: This is destructive and should only be called once. NOTE(manuel, 2023-06-19) This is not a great API, but it will do for now.

func (*OakCommand) RenderWithTemplate

func (oc *OakCommand) RenderWithTemplate(results QueryResults, tmpl *template.Template) (string, error)

func (*OakCommand) RenderWithTemplateFile

func (oc *OakCommand) RenderWithTemplateFile(results QueryResults, file string) (string, error)

func (*OakCommand) ResultsToJSON

func (oc *OakCommand) ResultsToJSON(results QueryResults, f io.Writer) error

func (*OakCommand) ResultsToYAML

func (oc *OakCommand) ResultsToYAML(results QueryResults, f io.Writer) error

type OakCommandDescription

type OakCommandDescription struct {
	Language string        `yaml:"language,omitempty"`
	Queries  []SitterQuery `yaml:"queries"`
	Template string        `yaml:"template,omitempty"`

	Name   string                            `yaml:"name"`
	Short  string                            `yaml:"short"`
	Long   string                            `yaml:"long,omitempty"`
	Layout []*layout.Section                 `yaml:"layout,omitempty"`
	Flags  []*parameters.ParameterDefinition `yaml:"flags,omitempty"`
	Layers []layers.ParameterLayer           `yaml:"layers,omitempty"`

	Parents []string `yaml:",omitempty"`
	Source  string   `yaml:",omitempty"`
}

type OakCommandLoader

type OakCommandLoader struct {
}

func (*OakCommandLoader) IsFileSupported added in v0.1.8

func (o *OakCommandLoader) IsFileSupported(f fs.FS, fileName string) bool

func (*OakCommandLoader) LoadCommandAliasFromYAML

func (o *OakCommandLoader) LoadCommandAliasFromYAML(
	s io.Reader,
	options ...alias.Option,
) ([]*alias.CommandAlias, error)

func (*OakCommandLoader) LoadCommands added in v0.1.8

func (o *OakCommandLoader) LoadCommands(
	f fs.FS, entryName string,
	options []cmds.CommandDescriptionOption,
	aliasOptions []alias.Option,
) ([]cmds.Command, error)

type OakCommandOption

type OakCommandOption func(*OakCommand)

func WithLanguage

func WithLanguage(lang string) OakCommandOption

func WithQueries

func WithQueries(queries ...SitterQuery) OakCommandOption

func WithSitterLanguage

func WithSitterLanguage(lang *sitter.Language) OakCommandOption

func WithTemplate

func WithTemplate(template string) OakCommandOption

type OakGlazeCommand added in v0.2.0

type OakGlazeCommand struct {
	*OakCommand
}

func NewOakGlazedCommand added in v0.1.1

func NewOakGlazedCommand(d *cmds.CommandDescription, options ...OakCommandOption) *OakGlazeCommand

func (*OakGlazeCommand) RunIntoGlazeProcessor added in v0.2.0

func (oc *OakGlazeCommand) RunIntoGlazeProcessor(
	ctx context.Context,
	parsedLayers *layers.ParsedLayers,
	gp middlewares.Processor,
) error

type OakGlazedCommandLoader added in v0.1.1

type OakGlazedCommandLoader struct{}

func (*OakGlazedCommandLoader) IsFileSupported added in v0.1.8

func (o *OakGlazedCommandLoader) IsFileSupported(f fs.FS, fileName string) bool

func (*OakGlazedCommandLoader) LoadCommandAliasFromYAML added in v0.1.1

func (o *OakGlazedCommandLoader) LoadCommandAliasFromYAML(
	s io.Reader,
	options ...alias.Option,
) ([]*alias.CommandAlias, error)

func (*OakGlazedCommandLoader) LoadCommands added in v0.1.8

func (o *OakGlazedCommandLoader) LoadCommands(
	f fs.FS, entryName string,
	options []cmds.CommandDescriptionOption,
	aliasOptions []alias.Option,
) ([]cmds.Command, error)

type OakParameterLayer

type OakParameterLayer struct {
	layers.ParameterLayerImpl `yaml:",inline"`
}

func NewOakParameterLayer

func NewOakParameterLayer(
	options ...layers.ParameterLayerOptions,
) (*OakParameterLayer, error)

type OakSettings added in v0.2.0

type OakSettings struct {
	Recurse      bool     `glazed.parameter:"recurse"`
	PrintQueries bool     `glazed.parameter:"print-queries"`
	Glob         []string `glazed.parameter:"glob"`
}

type OakWriterCommand added in v0.1.1

type OakWriterCommand struct {
	*OakCommand
}

func NewOakWriterCommand added in v0.1.1

func NewOakWriterCommand(d *cmds.CommandDescription, options ...OakCommandOption) *OakWriterCommand

func (*OakWriterCommand) RunIntoWriter added in v0.1.1

func (oc *OakWriterCommand) RunIntoWriter(
	ctx context.Context,
	parsedLayers *layers.ParsedLayers,
	w io.Writer,
) error

type QueryResults

type QueryResults map[string]*Result

func ExecuteQueries

func ExecuteQueries(
	lang *sitter.Language,
	tree *sitter.Node,
	queries []SitterQuery,
	sourceCode []byte,
) (QueryResults, error)

ExecuteQueries runs the given queries on the given tree and returns the results. Individual names are resolved using the sourceCode string, so as to provide full identifier names when matched.

TODO(manuel, 2023-06-19) We only need the language from oc here, right?

type Result

type Result struct {
	QueryName string
	Matches   []Match
}

func (*Result) Clone added in v0.1.2

func (r *Result) Clone() *Result

type RunSettings added in v0.2.0

type RunSettings struct {
	Sources []string `glazed.parameter:"sources"`
}

type SitterQuery

type SitterQuery struct {
	// Name of the resulting variable after parsing
	Name string `yaml:"name"`
	// Query contains the tree-sitter query that will be applied to the source code
	Query string `yaml:"query"`
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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