dsl

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatError

func FormatError(query string, err error) string

FormatError formats a parsing or compilation error with context

func IsValidField

func IsValidField(field string) bool

IsValidField checks if a field name is valid

func IsValidType

func IsValidType(typeValue string) bool

IsValidType checks if a type value is valid

func ParseQuery

func ParseQuery(query string) (store.Predicate, error)

ParseQuery parses a query string and returns a predicate

Example

ExampleParseQuery demonstrates basic usage of the DSL parser

package main

import (
	"fmt"
	"log"

	"github.com/go-go-golems/glazed/pkg/help/dsl"
)

func main() {
	// Simple field query
	predicate, err := dsl.ParseQuery("type:example")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Parsed: type:example\n")
	_ = predicate

	// Boolean operations
	predicate, err = dsl.ParseQuery("examples AND topic:database")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Parsed: examples AND topic:database\n")
	_ = predicate

	// Complex query with grouping
	predicate, err = dsl.ParseQuery("(examples OR tutorials) AND topic:database")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Parsed: (examples OR tutorials) AND topic:database\n")
	_ = predicate

	// Text search
	predicate, err = dsl.ParseQuery("\"full text search\"")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Parsed: \"full text search\"\n")
	_ = predicate

}
Output:

Parsed: type:example
Parsed: examples AND topic:database
Parsed: (examples OR tutorials) AND topic:database
Parsed: "full text search"

func SuggestCorrection

func SuggestCorrection(query string, err error) string

SuggestCorrection suggests corrections for common mistakes

func ValidateQuery

func ValidateQuery(query string) error

ValidateQuery validates a query string without executing it

Types

type BinaryExpression

type BinaryExpression struct {
	Left     Expression
	Operator string
	Right    Expression
}

BinaryExpression represents a binary operation (AND, OR)

func (*BinaryExpression) String

func (be *BinaryExpression) String() string

type Compiler

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

Compiler compiles AST expressions to predicates

func NewCompiler

func NewCompiler() *Compiler

NewCompiler creates a new compiler

func (*Compiler) Compile

func (c *Compiler) Compile(expr Expression) (store.Predicate, error)

Compile compiles an AST expression to a predicate

func (*Compiler) GetValidFields

func (c *Compiler) GetValidFields() []string

GetValidFields returns all valid field names

func (*Compiler) GetValidTypeValues

func (c *Compiler) GetValidTypeValues() []string

GetValidTypeValues returns valid values for the type field

func (*Compiler) ValidateField

func (c *Compiler) ValidateField(field string) bool

ValidateField validates if a field name is supported

type Expression

type Expression interface {
	Node
	// contains filtered or unexported methods
}

Expression represents an expression node

func Parse

func Parse(input string) (Expression, error)

Parse parses the input and returns the AST

type FieldExpression

type FieldExpression struct {
	Field string
	Value string
}

FieldExpression represents a field:value expression

func (*FieldExpression) String

func (fe *FieldExpression) String() string

type Lexer

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

Lexer tokenizes input text

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer

func (*Lexer) CurrentPosition

func (l *Lexer) CurrentPosition() (int, int, int)

CurrentPosition returns current position information

func (*Lexer) Error

func (l *Lexer) Error(msg string) Token

Error creates an error token

func (*Lexer) GetAllTokens

func (l *Lexer) GetAllTokens() []Token

GetAllTokens returns all tokens from the input (useful for testing)

func (*Lexer) IsAtEnd

func (l *Lexer) IsAtEnd() bool

IsAtEnd checks if lexer is at end of input

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token

type Node

type Node interface {
	String() string
}

Node represents a node in the AST

type Parser

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

Parser parses tokens into an AST

func NewParser

func NewParser(lexer *Lexer) *Parser

NewParser creates a new parser

func (*Parser) Errors

func (p *Parser) Errors() []string

Errors returns all parser errors

func (*Parser) HasErrors

func (p *Parser) HasErrors() bool

HasErrors returns true if there are any errors

func (*Parser) ParseExpression

func (p *Parser) ParseExpression() Expression

ParseExpression parses the entire expression

type QueryInfo

type QueryInfo struct {
	ValidFields []string
	ValidTypes  []string
	Examples    []string
}

QueryInfo provides information about query syntax

func GetQueryInfo

func GetQueryInfo() *QueryInfo

GetQueryInfo returns information about the query DSL

Example

ExampleGetQueryInfo demonstrates getting information about the DSL

package main

import (
	"fmt"

	"github.com/go-go-golems/glazed/pkg/help/dsl"
)

func main() {
	info := dsl.GetQueryInfo()

	fmt.Printf("Valid fields count: %d\n", len(info.ValidFields))
	fmt.Printf("Valid types: %v\n", info.ValidTypes)
	fmt.Printf("Example queries: %v\n", info.Examples[:3]) // Show first 3 examples

}
Output:

Valid fields count: 8
Valid types: [example tutorial topic application]
Example queries: [type:example topic:database flag:--output]

type TextExpression

type TextExpression struct {
	Text string
}

TextExpression represents a quoted text search

func (*TextExpression) String

func (te *TextExpression) String() string

type Token

type Token struct {
	Type     TokenType
	Value    string
	Position int
	Line     int
	Column   int
}

Token represents a single token

func (Token) String

func (t Token) String() string

String returns a string representation of the token

type TokenType

type TokenType int

TokenType represents the type of a token

const (
	// Special tokens
	TokenIllegal TokenType = iota
	TokenEOF
	TokenError

	// Literals
	TokenIdent     // identifiers like field names, values
	TokenString    // quoted strings like "hello world"
	TokenFieldName // field names before colon

	// Operators
	TokenColon      // :
	TokenAnd        // AND
	TokenOr         // OR
	TokenNot        // NOT
	TokenLeftParen  // (
	TokenRightParen // )
)

func (TokenType) String

func (tt TokenType) String() string

String returns a string representation of the token type

type UnaryExpression

type UnaryExpression struct {
	Operator string
	Right    Expression
}

UnaryExpression represents a unary operation (NOT)

func (*UnaryExpression) String

func (ue *UnaryExpression) String() string

Jump to

Keyboard shortcuts

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