A quick and dirty parser/combinator library in Go.
Go to file
2021-11-29 06:32:32 +01:00
.gitlab-ci.yml Initial commit 2020-03-07 07:17:14 +01:00
go.mod Initial commit 2020-03-07 07:17:14 +01:00
LICENSE Add LICENSE 2020-03-07 06:10:39 +00:00
parser.go Use state passing and token emit 2020-04-30 12:53:18 +02:00
parser_test.go Use state passing and token emit 2020-04-30 12:53:18 +02:00
README.md Updated README 2021-11-29 06:29:11 +01:00
token.go Use state passing and token emit 2020-04-30 12:53:18 +02:00

parser

A quick and dirty parser/combinator library in Go.

I wrote this library because I wanted a strict way of parsing SMTP commands coming into a mailserver. In order to keep the parser code maintainable, I wanted something that would closely match the specification given in the SMTP RFC.

For instance:

// helo = "HELO" SP Domain CRLF
func helo() parser.Fn {
	return parser.Seq(            // Sequence
		parser.Emit(
			parser.Lit("HELO"),   // Line starts with HELO
			heloCmd,              // Emit a token ID
		),
		sp(),                     // Function that parses a space
		parser.Emit(
			domain(),             // Function that parses a domain
			argToken,             // Emit a token ID
		),
	)
}

Running the parser results in a slice of tokens. A token, contains an ID and the bytes that were captured:

type Token struct {
	ID      int
	Capture []byte
}

For instance, {ID: heloCmd, Capture: []byte("HELO")} or {ID: argToken, Capture: []byte("")}.

The slice of tokens can be can be used to build a datastructure. For complex data structures, recursive descent can be used.