diff options
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 { |