dsl

package
v0.5.0 Latest Latest
Warning

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

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

Documentation

Overview

Package dsl is the rule-expression engine behind the validator: a lexer, a parser with precedence ! > && > ||, the AST the compiler consumes, and the token-level helpers (dive splitting, top-level leaf removal, top-level || detection) that let the validator edit expressions without re-parsing them as text. It is internal: its API tracks the validator's needs only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsDiveToken

func ContainsDiveToken(input string, isRawArg IsRawArgFunc) bool

ContainsDiveToken reports whether a standalone "dive" leaf appears at any nesting depth, which the validator uses to reject a second dive inside the element expression. Input that does not lex reports false.

func DefaultIsRawArg

func DefaultIsRawArg(name string) bool

DefaultIsRawArg is the IsRawArgFunc used when none is given: regex and not_regex take a raw pattern, every other rule gets comma-split arguments.

func HasTopLevelOr

func HasTopLevelOr(input string, isRawArg IsRawArgFunc) bool

HasTopLevelOr reports whether input has a || outside parentheses, which tells the validator to parenthesize it before &&-joining. Input that does not lex reports true, so a malformed expression is wrapped rather than spliced.

func RemoveTopLevelLeaves

func RemoveTopLevelLeaves(input string, names map[string]bool, isRawArg IsRawArgFunc) string

RemoveTopLevelLeaves drops the top-level && segments that consist of one bare leaf whose name, or whose trimmed source text ("min:3"), is in names, and re-joins the survivors with " && ". Segments under parentheses, || or !, and the reserved "dive" leaf, are never removed. It is lexer-driven so a separator inside a quoted or raw argument is not mistaken for structure; input that does not lex is returned unchanged.

func SplitDive

func SplitDive(input string, isRawArg IsRawArgFunc) (container, element string, hasDive bool, err error)

SplitDive splits an expression on a top-level standalone "dive" into the container expression (before it) and the element expression (after it). hasDive is false, with the input returned as container, when there is no dive. A dive that is parenthesized or carries arguments is an ordinary leaf and does not split. Malformed placement fails closed with a *ParseError: dive must be joined on both sides by && with operands, and a trailing dive is rejected because it would silently disable element rules.

Types

type And

type And struct {
	L, R Node
}

And is logical conjunction: L && R. Chains are left-associative.

type IsRawArgFunc

type IsRawArgFunc func(name string) bool

IsRawArgFunc reports whether the named rule reads its argument raw: one argument, commas literal, no splitting. The validator supplies one that consults its registry; nil means DefaultIsRawArg.

type Leaf

type Leaf struct {
	Name string
	Args []string
	Pos  int
}

Leaf is a single rule invocation such as in:a,b,c. Args are already unquoted and split (or, for raw-argument rules, verbatim); Pos is the byte offset of the name in the source, for error messages.

type Node

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

Node is a node in a parsed rule expression: a Leaf, Not, And or Or.

func Parse

func Parse(input string, isRawArg IsRawArgFunc) (Node, error)

Parse builds an AST from a rule expression. Precedence is ! > && > ||, both binary operators are left-associative, and parentheses group. Rule names are not resolved here, so an unknown rule parses; the compiler rejects it.

Returns a *ParseError for a lexing error, an empty expression, an unbalanced parenthesis, a dangling operator, an expression nested deeper than 10000 levels or larger than 50000 nodes.

type Not

type Not struct {
	X Node
}

Not is logical negation: !X.

type Or

type Or struct {
	L, R Node
}

Or is logical disjunction: L || R. Chains are left-associative.

type ParseError

type ParseError struct {
	Pos int
	Msg string
}

ParseError is a lexing or parsing failure. Pos is the byte offset in the expression where it was detected; Msg is human-readable.

func (*ParseError) Error

func (e *ParseError) Error() string

Error renders "dsl: <msg> at position <pos>".

Jump to

Keyboard shortcuts

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