Documentation ¶
Overview ¶
Package parsec implements a library of parser-combinators using basic recognizers like,
And OrdChoice Kleene Many Maybe
Parser combinators can be used to construct higher-order parsers using basic parser function. All parser functions are expected to follow the `Parser` type signature, accepting a `Scanner` interface and returning a `ParsecNode` and a new scanner. If a parser fails to match the input string according to its rules, then it must return nil for ParsecNode, and a new Scanner.
ParsecNode can either be a Terminal structure or NonTerminal structure or a list of Terminal/NonTerminal structure. The AST output is expected to be made up of ParsecNode.
Nodify is a callback function that every combinators use as a callback to construct a ParsecNode.
Index ¶
- type Nodify
- type NonTerminal
- type ParsecNode
- type Parser
- func And(callb Nodify, parsers ...interface{}) Parser
- func Char() Parser
- func End() Parser
- func Float() Parser
- func Hex() Parser
- func Ident() Parser
- func Int() Parser
- func Kleene(callb Nodify, parsers ...interface{}) Parser
- func Many(callb Nodify, parsers ...interface{}) Parser
- func ManyUntil(callb Nodify, parsers ...interface{}) Parser
- func Maybe(callb Nodify, parser interface{}) Parser
- func NoEnd() Parser
- func Oct() Parser
- func Optional(callb Nodify, parser interface{}, default_node ParsecNode) Parser
- func OrdChoice(callb Nodify, parsers ...interface{}) Parser
- func OrdTokens(patterns []string, names []string) Parser
- func OrdTokensCI(patterns []string, names []string) Parser
- func String() Parser
- func Token(pattern string, name string) Parser
- func TokenCI(pattern string, name string) Parser
- type Scanner
- type SimpleScanner
- func (s *SimpleScanner) Clone() Scanner
- func (s *SimpleScanner) Endof() bool
- func (s *SimpleScanner) GetCursor() int
- func (s *SimpleScanner) Match(pattern string) ([]byte, Scanner)
- func (s *SimpleScanner) SkipWS() ([]byte, Scanner)
- func (s *SimpleScanner) SubmatchAll(pattern string) (map[string][]byte, Scanner)
- type Terminal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Nodify ¶
type Nodify func([]ParsecNode) ParsecNode
Nodify callback function to construct custom ParsecNode.
type NonTerminal ¶
type NonTerminal struct { Name string // contains terminal's token type Value string // value of the terminal Children []ParsecNode // list of children to this node. }
NonTerminal structure can be used to construct a non-terminal ParsecNode.
type Parser ¶
type Parser func(Scanner) (ParsecNode, Scanner)
Parser function parses input text, higher order parsers are constructed using combinators.
func And ¶
And combinator accepts a list of `Parser`, or reference to a parser, that must match the input string, atleast until the last Parser argument. Returns a parser function that can be used to construct higher-level parsers.
If all parser matches, a list of ParsecNode, where each ParsecNode is constructed by matching parser, will be passed as argument to Nodify callback. Even if one of the input parser function fails then empty slice of ParsecNode will be supplied as argument to Nodify callback.
func Char ¶
func Char() Parser
Char returns a parser function to match a single character in the input stream.
func Float ¶
func Float() Parser
Float returns a parser function to match a float literal in the input stream.
func Hex ¶
func Hex() Parser
Hex returns a parser function to match a hexadecimal literal in the input stream.
func Ident ¶
func Ident() Parser
Ident returns a parser function to match an identifier token in the input stream, an identifier is matched with the following pattern,
`^[A-Za-z][0-9a-zA-Z_]*`
func Int ¶
func Int() Parser
Int returns a parser function to match an integer literal in the input stream.
func Kleene ¶
Kleene combinator accepts two parsers, or reference to parsers, namely opScan and sepScan, where opScan parser will be used to match input string and contruct ParsecNode and sepScan parser will be used to match input string and ignore the matched string. If sepScan parser is not supplied, then opScan parser will be applied on the input until it fails.
The process of matching opScan parser and sepScan parser will continue in a loop until either one of them fails on the input stream.
For every successful match of opScan, the returned ParsecNode from matching parser will be accumulated and passed as argument to Nodify callback. If there is not a single match for opScan, then an empty slice of ParsecNode will be passed as argument to Nodify callback.
func Many ¶
Many combinator accepts two parsers, or reference to parsers, namely opScan and sepScan, where opScan parser will be used to match input string and contruct ParsecNode and sepScan parser will be used to match input string and ignore the matched string. If sepScan parser is not supplied, then opScan parser will be applied on the input until it fails.
The process of matching opScan parser and sepScan parser will continue in a loop until either one of them fails on the input stream.
The difference between `Many` combinator and `Kleene` combinator is that there shall atleast be one match of opScan.
For every successful match of opScan, the returned ParsecNode from matching parser will be accumulated and passed as argument to Nodify callback. If there is not a single match for opScan, then `nil` will be returned for ParsecNode.
func ManyUntil ¶
ManyUntil combinator accepts three parsers, or references to parsers, namely opScan, sepScan and untilScan, where opScan parser will be used to match input string and contruct ParsecNode and sepScan parser will be used to match input string and ignore the matched string. If sepScan parser is not supplied, then opScan parser will be applied on the input until it fails.
The process of matching opScan parser and sepScan parser will continue in a loop until either one of them fails on the input stream or untilScan matches.
For every successful match of opScan, the returned ParsecNode from matching parser will be accumulated and passed as argument to Nodify callback. If there is not a single match for opScan, then `nil` will be returned for ParsecNode.
func Maybe ¶
Maybe combinator accepts a single parser, or reference to a parser, and tries to match the input stream with it.
func Oct ¶
func Oct() Parser
Oct returns a parser function to match an octal number literal in the input stream.
func Optional ¶
func Optional(callb Nodify, parser interface{}, default_node ParsecNode) Parser
Optional combinator accepts a single parser, or reference to a parser, and tries to match the input stream with it. If the input stream doesn't match, the default terminal node is returned.
func OrdChoice ¶
OrdChoice combinator accepts a list of `Parser`, or reference to a parser, where atleast one of the parser must match the input string. Returns a parser function that can be used to construct higher level parsers.
The first matching parser function's output is passed as argument to Nodify callback. If non of the parsers match the input, then `nil` is returned for ParsecNode
func OrdTokensCI ¶
Case-insensitive variation of OrdTokens - automatically prefixes patterns with (?i) flag.
func String ¶
func String() Parser
String returns a parser function to match a double quoted string in the input stream.
type Scanner ¶
type Scanner interface { // Clone will return new clone of the underlying scanner structure. // This will be used by combinators to _backtrack_. Clone() Scanner // GetCursor gets the current cursor position inside input text. GetCursor() int // Match the input stream with `pattern` and return // matching string after advancing the cursor. Match(pattern string) ([]byte, Scanner) // SubmatchAll the input stream with a choice of `patterns` // and return matching string and submatches, after // advancing the cursor. SubmatchAll(pattern string) (map[string][]byte, Scanner) // SkipWs skips white space characters in the input stream. // Return skipped whitespaces as byte-slice and advance the cursor. SkipWS() ([]byte, Scanner) // Endof detects whether end-of-file is reached in the input // stream and return a boolean indicating the same. Endof() bool }
Scanner interface supplies necessary methods to match the input stream.
func NewScanner ¶
NewScanner creates and returns a reference to new instance of SimpleScanner object.
type SimpleScanner ¶
type SimpleScanner struct {
// contains filtered or unexported fields
}
SimpleScanner implements Scanner interface based on golang's regexp module.
func (*SimpleScanner) Clone ¶
func (s *SimpleScanner) Clone() Scanner
Clone method receiver in Scanner{} interface.
func (*SimpleScanner) Endof ¶
func (s *SimpleScanner) Endof() bool
Endof method receiver in Scanner{} interface.
func (*SimpleScanner) GetCursor ¶
func (s *SimpleScanner) GetCursor() int
GetCursor method receiver in Scanner{} interface.
func (*SimpleScanner) Match ¶
func (s *SimpleScanner) Match(pattern string) ([]byte, Scanner)
Match method receiver in Scanner{} interface.
func (*SimpleScanner) SkipWS ¶
func (s *SimpleScanner) SkipWS() ([]byte, Scanner)
SkipWS method receiver in Scanner{} interface.
func (*SimpleScanner) SubmatchAll ¶
func (s *SimpleScanner) SubmatchAll( pattern string) (map[string][]byte, Scanner)
SubmatchAll method receiver in Scanner{} interface.