-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.go
62 lines (47 loc) · 1.62 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package rfc5322
import (
"io"
"net/mail"
"time"
"github.com/ProtonMail/go-rfc5322/parser"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
// CharsetReader, if non-nil, defines a function to generate charset-conversion readers,
// converting from the provided charset into UTF-8, for use when decoding RFC2047 encoded words.
// Charsets are always lower-case. utf-8, iso-8859-1 and us-ascii charsets are handled by default.
// One of the CharsetReader's result values must be non-nil.
var CharsetReader func(charset string, input io.Reader) (io.Reader, error)
// ParseAddressList parses one or more valid RFC5322 (with RFC2047) addresses.
func ParseAddressList(input string) ([]*mail.Address, error) {
if len(input) == 0 {
return []*mail.Address{}, nil
}
l := parser.NewRFC5322Lexer(antlr.NewInputStream(input))
p := parser.NewRFC5322Parser(antlr.NewCommonTokenStream(l, antlr.TokenDefaultChannel))
w := &walker{}
p.RemoveErrorListeners()
p.AddErrorListener(w)
tree := p.AddressList()
if w.err != nil {
return nil, w.err
}
antlr.ParseTreeWalkerDefault.Walk(w, tree)
return w.res.([]*mail.Address), w.err
}
// ParseDateTime parses a valid RFC5322 date-time.
func ParseDateTime(input string) (time.Time, error) {
if len(input) == 0 {
return time.Time{}, nil
}
l := parser.NewRFC5322Lexer(antlr.NewInputStream(input))
p := parser.NewRFC5322Parser(antlr.NewCommonTokenStream(l, antlr.TokenDefaultChannel))
w := &walker{}
p.RemoveErrorListeners()
p.AddErrorListener(w)
tree := p.DateTime()
if w.err != nil {
return time.Time{}, w.err
}
antlr.ParseTreeWalkerDefault.Walk(w, tree)
return w.res.(time.Time), w.err
}