glean

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 6 Imported by: 0

README

Glean is a parser generator for the Go programming language. It scans .go files to find the rules of a grammar, and writes a parser in Go.

The parser generator can be found in the glean subdirectory of this directory.

Documentation

Overview

Package glean contains useful utilities for parser generators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanDir

func ScanDir(rules RuleAdder, dirname string) (pkg string, warnings []error, err error)

ScanDir searches for grammar rules in the .go files in a directory

Files named *_test.go are ignored. For each rule found, rules.AddRule is called. All the files must belong to the same package; the name of that package is the first returned value.

func ScanFiles

func ScanFiles(rules RuleAdder, filenames ...string) (pkg string, warnings []error, err error)

ScanFiles searches one or more files for grammar rules.

For each rule found, rules.AddRule is called. All the files must belong to the same package; the name of that package is the first returned value.

Types

type Grammar

type Grammar interface {
	RuleAdder
	ParserWriter
}

A Grammar can accumulate rules and write a parser.

type ParserWriter

type ParserWriter interface {
	// ParserWriter writes a grammar parser in Go.
	//
	// Typically, the caller will write the result into a .go file,
	// with a comment marking the file as automatically generated.
	//
	// The goal argument is the goal symbol that will be the result
	// of the parse.  The parse function will have the signature
	//
	// func ([]interface{}) (goal, error)
	//
	// The packname argument is copied to the package statment in the
	// generated code. The prefix is prepended to the names of all
	// file-level identifiers; in particular the name of the main
	// parse function will be prefix + "Parse".
	WriteParser(goal, packname, prefix string) (string, error)
}

A ParserWriter can write a parser (in Go) for a grammar.

type RuleAdder

type RuleAdder interface {
	// AddRule adds one rule to the grammar.
	//
	// Callers should ensure the same name is never used in two calls
	// to AddRule.
	AddRule(name, target string, items []string) error
}

A RuleAdder can have grammar rules added to it.

Grammar symbols are represented by arbitrary strings. Each rule has a name, which should be unique.

If the intent is to write a parser for the grammar, then the rule names and symbol strings should be valid Go identifiers.

Directories

Path Synopsis
Package earley contains an implementation of glean.Grammar using a simple Earley-style parser.
Package earley contains an implementation of glean.Grammar using a simple Earley-style parser.
Glean generates parsers for context-free grammars.
Glean generates parsers for context-free grammars.
Package gleanerrors contains types used to report errors from parsers generated by glean.
Package gleanerrors contains types used to report errors from parsers generated by glean.

Jump to

Keyboard shortcuts

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