romshark / llparser

A universal LL top-down parser written in Go
BSD 3-Clause "New" or "Revised" License
45 stars 1 forks source link

[FTR] A pattern for matching an arbitrary series of a specified set of characters #2

Closed bubunyo closed 4 years ago

bubunyo commented 5 years ago

Proposal

Implement a new pattern type parse.Any to match a series of a specified set of characters.

Current Workaround

Using parse.Either resulting in ugly code and an inefficient parse-tree structure.

romshark commented 5 years ago

This could indeed make sense to improve the parser flexibility but I'd probably call it "Lexed".

// Lexed instructs the lexer to match an arbitrary sequence of characters
// until Fn returns 0 and skip as many runes as returned by Fn on each step
type Lexed struct {
  Kind        FragmentKind
  Designation string
  Fn          func(Cursor) uint
}
parser.Lexed{
  Kind: 100,
  Fn: func(crs Cursor) int {
    if isEscapedString(crs) {
      // Skip this rune and the next one too
      return 2
    }
    if n := allowedStringRune(crs); n > 0 {
      // Skip this rune
      return n
    }
    // Stop, end of string reached
    return 0
  }
}