diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-09 21:41:57 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-09 21:43:09 +1000 |
commit | dc3c5a8325eb2f425906b6419806862de84416ef (patch) | |
tree | 7fcb0d60419a731ce8c1d38772d3284b33f322aa /parser.go | |
parent | d4b58316bc8d7afa51f1ac66600ca84e4c0dc115 (diff) |
Add a regex parser
Diffstat (limited to 'parser.go')
-rw-r--r-- | parser.go | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -3,6 +3,7 @@ package goparsify import ( "errors" "fmt" + "regexp" "strings" "unicode/utf8" ) @@ -102,6 +103,20 @@ func Run(parser Parserish, input string) (result interface{}, err error) { return ret.Result, nil } +// Regex returns a match if the regex successfully matches +func Regex(pattern string) Parser { + re := regexp.MustCompile("^" + pattern) + return NewParser(pattern, func(ps *State) Result { + ps.AutoWS() + if match := re.FindString(ps.Get()); match != "" { + ps.Advance(len(match)) + return Result{Token: match} + } + ps.ErrorHere(pattern) + return Result{} + }) +} + // Exact will fully match the exact string supplied, or error. The match will be stored in .Token func Exact(match string) Parser { if len(match) == 1 { |