From 8a92b5348f8cf6769f30237df906ee9ce71ad237 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Thu, 10 Aug 2017 22:06:08 +1000 Subject: Unicode safe by default --- parser.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'parser.go') diff --git a/parser.go b/parser.go index 7c3f866..12bb858 100644 --- a/parser.go +++ b/parser.go @@ -76,26 +76,14 @@ func ParsifyAll(parsers ...Parserish) []Parser { return ret } -// WS will consume whitespace, it should only be needed when AutoWS is turned off -func WS() Parser { - return NewParser("AutoWS", func(ps *State) Result { - ps.WS(ps) - return Result{} - }) -} - -// Cut prevents backtracking beyond this point. Usually used after keywords when you -// are sure this is the correct path. Improves performance and error reporting. -func Cut(ps *State) Result { - ps.Cut = ps.Pos - return Result{} -} - // Run applies some input to a parser and returns the result, failing if the input isnt fully consumed. // It is a convenience method for the most common way to invoke a parser. -func Run(parser Parserish, input string) (result interface{}, err error) { +func Run(parser Parserish, input string, ws ...VoidParser) (result interface{}, err error) { p := Parsify(parser) ps := NewState(input) + if len(ws) > 0 { + ps.WS = ws[0] + } ret := p(ps) ps.AutoWS() @@ -111,6 +99,21 @@ func Run(parser Parserish, input string) (result interface{}, err error) { return ret.Result, nil } +// WS will consume whitespace, it should only be needed when AutoWS is turned off +func WS() Parser { + return NewParser("AutoWS", func(ps *State) Result { + ps.WS(ps) + return Result{} + }) +} + +// Cut prevents backtracking beyond this point. Usually used after keywords when you +// are sure this is the correct path. Improves performance and error reporting. +func Cut(ps *State) Result { + ps.Cut = ps.Pos + return Result{} +} + // Regex returns a match if the regex successfully matches func Regex(pattern string) Parser { re := regexp.MustCompile("^" + pattern) -- cgit v1.2.3