Skip to content

Question: parsing line-based tokens (multiple character lookahead) #403

Answered by viktomas
viktomas asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks @alecthomas you helped me heaps! That was almost the solution 🙇. Since I care about the new lines in the Content field, I had to make the new line an explicit symbol like this

type TestFile struct {
	Path    string `FileStart NewLine @Content NewLine`
	Content string `ContentStart NewLine @(Content NewLine?)+`
}
var (
	exampleLexer = lexer.MustSimple([]lexer.SimpleRule{
		{`FileStart`, `===`},
		{`ContentStart`, `---`},
		{"NewLine", `[\n]`},
		{"Content", `[^\n]+`},
	})
	exampleParser = participle.MustBuild[Example](
		participle.Lexer(exampleLexer),
	)
)

Explanation for other people who might look at this:

Since the lexer tries the rules in order, each line will try the FileStart

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@viktomas
Comment options

Answer selected by viktomas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants