rules

package
v0.0.0-...-86e9f11 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package rules defines a syntax for rule-based re-writing DSLs. The supported syntax is as follows:

comment = < go comment syntax >
string = < go double-quote syntax > | < go backtick syntax >
identifier = < go identifier >
rule = value {',' value} '->' term
term = (identifier ':' value) | identifier | value
value = list | string
list = '(' item {space+ item} ')'

For example:

// here is a line comment
(foo "quoted-string" x:(baz `backtick-string`)) -> x

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteTo

func WriteTo(dst io.Writer, lst []Rule) (int64, error)

WriteTo writes formatted rules to dst. Each rule is written on its own line.

Types

type Float

type Float float64

Float is a literal float

func (Float) String

func (f Float) String() string

type Int

type Int int64

Int is a literal integer

func (Int) String

func (i Int) String() string

type List

type List []Term

List is a list of terms.

func (List) String

func (l List) String() string

type Rule

type Rule struct {
	// From is the conjunction of
	// expressions to match against.
	From []Value
	// To is the value associated with
	// the right-hand-side of the rule.
	To Term
	// Location is the original textual position
	// at which the rule began
	Location scanner.Position
}

Rule represents one rule. The rule package does not assign any semantic meaning to the structure of a Rule; it only defines the syntax for Rules.

func Parse

func Parse(r io.Reader) ([]Rule, error)

Parse parses a list of rules from a file.

func (*Rule) Equal

func (r *Rule) Equal(o *Rule) bool

Equal returns true if two rules are equal, or false otherwise. Equal does not compare the Location field of terms (see also Term.Equal).

func (*Rule) String

func (r *Rule) String() string

String implements fmt.Stringer

type String

type String string

String is a literal Go string

func (String) String

func (s String) String() string

String returns the literal representation of s, *not* s itself. Cast s to a string if you want to use the raw string value.

type Term

type Term struct {
	// Name is the name (identifier) of this term.
	// If Value is non-nil, then Name
	// may be the empty string.
	Name string

	// Value is the value of the term.
	// Value may be nil if this term
	// is a bare identifier.
	Value Value

	// Input is scratch space that can
	// be used by external packages.
	Input string

	// Location is the location of the term
	// in the original text file. It can be
	// used to provide more helpful error messages.
	Location scanner.Position
}

Term is a term in a rule. Every term in a rule is optionally named (see Name) so that it can be referred to in other terms.

func (*Term) Equal

func (t *Term) Equal(o *Term) bool

Equal returns true if two terms are equivalent, or false otherwise. Equal does not compare the Location fields of each Term.

func (*Term) String

func (t *Term) String() string

String implements fmt.Stringer

String returns the canonical textual representation of the Term t.

type Value

type Value interface {
	String() string
}

Value is one of List, String, Int, or Float

Jump to

Keyboard shortcuts

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