Closed yusup-abdullaev closed 2 years ago
So what I did - I created an private function which does the same as Regex
but also validates the match:
func regExt(pattern string, validate func(token string) error) Parser {
re := regexp.MustCompile("^" + pattern)
return NewParser(pattern, func(ps *State, node *Result) {
ps.WS(ps)
match := re.FindString(ps.Get())
if match == "" {
ps.ErrorHere(pattern)
return
}
if err := validate(match); err != nil {
ps.ErrorHere(pattern)
return
}
ps.Advance(len(match))
node.Token = match
})
}
This does the work for me and the issue is closed.
First, I want to say that this library is really really helpful for the small user command parser which I am writing. Thank you so much.
The question which I have - is there an ability to validate the token which I parsed? Example: I have a parser which handles the date in format
YYYY-MM-DD
:Is there an ability to add extra code which will inspect the extracted token and validate it?