glr

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package glr implements a small-scale GLR(1)-parser. It is mainly intended for Markdown parsing, but may be of use for other purposes, too.

Clients have to use the tools of package lr to prepare the necessary parse tables. The GLR parser utilizes these tables to create a right derivation for a given input, provided through a scanner interface.

This parser is intended for small to moderate grammars, e.g. for configuration input or small domain-specific languages. It is *not* intended for full-fledged programming languages (there are superb other tools around for these kinds of usages, usually creating LL(k)- or LALR(1)-parsers, or in the case of Bison, even a GLR(1)-parser).

The main focus for this implementation is adaptability and on-the-fly usage. Clients are able to construct the parse tables from a grammar and use the parser directly, without a code-generation or compile step. If you want, you can create a grammar from user input and use a parser for it in a couple of lines of code.

Package glr can handle ambiguous grammars, i.e. grammars which will have shift/reduce- or reduce/reduce-conflicts in their parse tables. For simpler parsing of deterministic SLR grammars, see package slr.

Warning

The API is still very much in flux! Currently it is something like:

scanner := glr.NewStdScanner(strings.NewReader("some input text"))
p := glr.NewParser(grammar, gotoTable, actionTable)
p.Parse(startState, scanner)

BSD License

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of this software or the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func T

func T() tracing.Trace

T traces to the SyntaxTracer

Types

type Parser

type Parser struct {
	G *lr.Grammar // grammar to use; do not alter after initialization
	// contains filtered or unexported fields
}

A Parser type for GLR parsing. Create and initialize one with glr.NewParser(...)

func NewParser

func NewParser(g *lr.Grammar, gotoTable *sparse.IntMatrix, actionTable *sparse.IntMatrix) *Parser

NewParser creates and initializes a parser object, given information from an lr.LRTableGenerator. Clients have to provide a link to the grammar and the parser tables.

func (*Parser) Parse

func (p *Parser) Parse(S *lr.CFSMState, scan Scanner) (bool, error)

Parse startes a new parse, given a start state and a scanner tokenizing the input. The parser must have been initialized.

Parse returns true, if the input was successfully recognized by the parse, false otherwise.

type Scanner

type Scanner interface {
	MoveTo(position uint64)
	NextToken(expected []int) (tokval int, token interface{})
}

Scanner is an interface the parser relies on.

type StdScanner

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

StdScanner provides a default scanner implementation, but clients are free (and even encouraged) to provide their own. This implementation is based on stdlib's text/scanner.

func NewStdScanner

func NewStdScanner(r io.Reader) *StdScanner

NewStdScanner creates a new default scanner from a Reader.

func (*StdScanner) MoveTo

func (s *StdScanner) MoveTo(position uint64)

MoveTo is not functional for default scanners. Default scanners allow sequential processing only.

func (*StdScanner) NextToken

func (s *StdScanner) NextToken(expected []int) (int, interface{})

NextToken gets the next token scanned from the input source. Returns the token value and a user-defined token type.

Clients may provide an array of token values, one of which is expected at the current parse position. For the default scanner, as of now this is unused. In the future it will help with error-repair.

type Token

type Token struct {
	Value  int
	Lexeme []byte
}

A Token type, if you want to use it. Tokens of this type are returned by StdScanner.

Clients may provide their own token data type.

func (*Token) String

func (token *Token) String() string

Jump to

Keyboard shortcuts

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