parser

package
Version: v0.0.0-...-725082c Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: BSD-2-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeExpression = "!e"
	TypePath       = "!p"
	TypeVariable   = "!v"
	TypeSelector   = "!s"
	TypeIndex      = "!i"
	TypeGroup      = "!g"
	TypeArguments  = "!a"
	TypeTemplate   = "!t"
	TypeString     = "!string"

	TypeIf    = "!if"
	TypeEnd   = "!end"
	TypeElse  = "!else"
	TypeFor   = "!for"
	TypeBreak = "!break"
)

Nodes containing these strings are special

Variables

View Source
var (
	// ErrInvalidUnread reports an unsuccessful UnreadByte or UnreadRune
	ErrInvalidUnread = errors.New("invalid use of UnreadByte or UnreadRune")

	// ErrEOS indicates the end of the stream
	ErrEOS = errors.New("EOS")

	// ErrSpaceNotUniform indicates mixed use of spaces and tabs for indentation
	ErrSpaceNotUniform = errors.New("space has both tabs and spaces")

	// ErrUnterminatedQuotedString is obvious.
	ErrUnterminatedQuotedString = errors.New("quoted string not terminated")

	ErrNotANumber       = errors.New("not a number")
	ErrNotFound         = errors.New("not found")
	ErrIncompatibleType = errors.New("incompatible type")
	ErrNilReceiver      = errors.New("nil function receiver")
	ErrInvalidIndex     = errors.New("invalid index")
	ErrFunctionNoGraph  = errors.New("functions doesn't return *Graph")
	ErrInvalidArgs      = errors.New("invalid arguments or nil receiver")
)

Functions

func Ast

func Ast(g *ogdl.Graph)

func Precedence

func Precedence(s string) int

Precedence is same as in Go, except for the missing operators (| << >> & ^ &^)

Assignment operators are given the lowest precedence.

Types

type Parser

type Parser struct {
	Ix  int
	Buf []byte
	// contains filtered or unexported fields
}

Parser exposes Ix and Buf making it easier to use it outside of this package.

func New

func New(buf []byte, eh eventHandler) *Parser

New creates a Parser. An event handler needs to be supplied if productions that emit events are used.

func (*Parser) ArgList

func (p *Parser) ArgList() bool

ArgList ::= space? expression space? [, space? expression]* space?

arglist < stream > events

arglist can be empty, then returning false (this fact is not represented in the BNF definition).

func (*Parser) Args

func (p *Parser) Args(dot bool) (bool, error)

Args ::= '(' space? sequence? space? ')'

func (*Parser) Break

func (p *Parser) Break() bool

Break (= newline) is NL, CR or CR+NL or EoS

func (*Parser) Byte

func (p *Parser) Byte() (byte, bool)

Byte reads and returns a single byte. If no byte is available, returns 0 and an error.

func (*Parser) Dec

func (p *Parser) Dec()

Dec decreses the event handler level by one

func (*Parser) Emit

func (p *Parser) Emit(s string)

Emit outputs a string event at the current level. This will show up in the graph

func (*Parser) End

func (p *Parser) End() bool

End returns true if the end of stream has been reached.

func (*Parser) Expression

func (p *Parser) Expression() bool

Expression := expr1 (op2 expr1)*

expression := expr1 (op2 expr1)*
expr1 := path | constant | op1 path | op1 constant | '(' expr ')' | op1 '(' expr ')'
constant ::= quoted | number

func (*Parser) Inc

func (p *Parser) Inc()

Inc increases the event handler level by one

func (*Parser) Index

func (p *Parser) Index() bool

Index ::= '[' expression ']'

func (*Parser) Level

func (p *Parser) Level() int

func (*Parser) Line

func (p *Parser) Line() string

func (*Parser) Number

func (p *Parser) Number() (string, bool)

Number returns true if it finds a number at the current parser position. It returns also the number found. TODO recognize exp notation ?

func (*Parser) Operator

func (p *Parser) Operator() string

Operator returns the operator at the current parser position, if any; an empty string if not.

func (*Parser) Path

func (p *Parser) Path() bool

Path parses an OGDL path, or an extended path as used in templates.

path ::= element ('.' element)*

element ::= token | integer | quoted | group | index | selector

(Dot optional before Group, Index, Selector)

group := '(' Expression [[,] Expression]* ')'
index := '[' Expression ']'
selector := '{' Expression '}'

The OGDL parser doesn't need to know about Unicode. The character classification relies on values < 127, thus in the ASCII range, which is also part of Unicode.

Note: On the other hand it would be stupid not to recognize for example Unicode quotation marks if we know that we have UTF-8. But when do we know for sure?

func (*Parser) PeekByte

func (p *Parser) PeekByte() byte

PeekByte returns the next byte witohut consuming it

func (*Parser) PeekRune

func (p *Parser) PeekRune() (rune, bool)

PeekRune returns the next rune witohut consuming it

func (*Parser) QToken

func (p *Parser) QToken(isTokenChar func(rune) bool) string

func (*Parser) Quoted

func (p *Parser) Quoted(ind int) string

Quoted string. Can have newlines in it. It returns the string and a possible error.

func (*Parser) Reset

func (p *Parser) Reset()

func (*Parser) Rune

func (p *Parser) Rune() (rune, bool)

Rune reads a single UTF-8 encoded Unicode character and returns the rune. If the encoded rune is invalid, it consumes one byte and returns unicode.ReplacementChar (U+FFFD) with a size of 1.

func (*Parser) Selector

func (p *Parser) Selector() bool

Selector ::= '{' expression? '}'

func (*Parser) SetLevel

func (p *Parser) SetLevel(i int)

func (*Parser) Space

func (p *Parser) Space() (int, byte)

Space is (0x20|0x09)+. It return the number of spaces found (whether tabs or spaces), and a byte than can have the values 0, ' ' and '\t' indicating mixed, all spaces or all tabs

func (*Parser) String

func (p *Parser) String() string

String is a concatenation of characters that are > 0x20

func (*Parser) Token

func (p *Parser) Token(isTokenChar func(rune) bool) string

Token reads from the Parser input stream and returns a token, if any.

func (*Parser) TokenList

func (p *Parser) TokenList(isTokenChar func(rune) bool) []string

func (*Parser) TokenListEv

func (p *Parser) TokenListEv(isTokenChar func(rune) bool) bool

func (*Parser) UnaryExpression

func (p *Parser) UnaryExpression() bool

UnaryExpression := cpath | constant | op1 cpath | op1 constant | '(' expr ')' | op1 '(' expr ')'

func (*Parser) UnreadByte

func (p *Parser) UnreadByte() bool

UnreadByte unreads the last byte. It can unread all buffered bytes.

func (*Parser) UnreadRune

func (p *Parser) UnreadRune(r rune) bool

UnreadRune unreads the last rune (should be supplied)

func (*Parser) WhiteSpace

func (p *Parser) WhiteSpace() bool

WhiteSpace is equivalent to Space | Break. It consumes all white space, whether spaces, tabs or newlines

Jump to

Keyboard shortcuts

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