forked from rainliu/sip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressParser.go
130 lines (113 loc) · 3.07 KB
/
AddressParser.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package parser
import (
"github.com/tutuvss/sip/core"
"github.com/tutuvss/sip/address"
"strings"
)
/** SIPParser for addresses.
*/
type AddressParser struct {
SIPParser
}
func NewAddressParser(addr string) *AddressParser {
this := &AddressParser{}
this.SIPParser.super(addr)
this.SIPParser.GetLexer().SetLexerName("charLexer")
return this
}
func NewAddressParserFromLexer(lexer core.Lexer) *AddressParser {
this := &AddressParser{}
this.SetLexer(lexer)
this.GetLexer().SelectLexer("charLexer")
return this
}
func (this *AddressParser) super(addr string) {
this.SIPParser.super(addr)
this.SIPParser.GetLexer().SetLexerName("charLexer")
}
func (this *AddressParser) superFromLexer(lexer core.Lexer) {
this.SetLexer(lexer)
this.GetLexer().SelectLexer("charLexer")
}
func (this *AddressParser) NameAddr() (addr *address.AddressImpl, ParseException error) {
var ch byte
var uri address.URI
lexer := this.GetLexer()
if ch, _ = lexer.LookAheadK(0); ch == '<' {
lexer.Match('<')
lexer.SelectLexer("sip_urlLexer")
lexer.SPorHT()
uriParser := NewURLParserFromLexer(lexer)
if uri, ParseException = uriParser.UriReference(); ParseException != nil {
return nil, ParseException
}
addr = address.NewAddressImpl()
addr.SetAddressType(address.NAME_ADDR)
addr.SetURI(uri)
lexer.SPorHT()
lexer.Match('>')
return addr, nil
} else {
addr = address.NewAddressImpl()
addr.SetAddressType(address.NAME_ADDR)
var name string
if ch, _ = lexer.LookAheadK(0); ch == '"' {
if name, ParseException = lexer.QuotedString(); ParseException != nil {
return nil, ParseException
}
lexer.SPorHT()
} else {
if name, ParseException = lexer.GetNextTokenByDelim('<'); ParseException != nil {
return nil, ParseException
}
}
addr.SetDisplayName(strings.TrimSpace(name))
lexer.Match('<')
lexer.SPorHT()
uriParser := NewURLParserFromLexer(lexer)
if uri, ParseException = uriParser.UriReference(); ParseException != nil {
return nil, ParseException
}
addr.SetAddressType(address.NAME_ADDR)
addr.SetURI(uri)
lexer.SPorHT()
lexer.Match('>')
return addr, nil
}
}
func (this *AddressParser) Address() (retval *address.AddressImpl, ParseException error) {
var ch byte
lexer := this.GetLexer()
k := 0
for lexer.HasMoreChars() {
if ch, ParseException = lexer.LookAheadK(k); ch == '<' ||
ch == '"' ||
ch == ':' ||
ch == '/' {
break
} else if ParseException != nil /*ch == 0*/ { //'\0'
return nil, this.CreateParseException("unexpected EOL")
} else {
k++
}
}
if ch, _ = lexer.LookAheadK(k); ch == '<' ||
ch == '"' {
if retval, ParseException = this.NameAddr(); ParseException != nil {
return nil, ParseException
}
} else if ch == ':' ||
ch == '/' {
retval = address.NewAddressImpl()
uriParser := NewURLParserFromLexer(lexer)
var uri address.URI
if uri, ParseException = uriParser.UriReference(); ParseException != nil {
return nil, ParseException
}
retval.SetAddressType(address.ADDRESS_SPEC)
retval.SetURI(uri)
} else {
return nil, this.CreateParseException("Bad address spec")
}
return retval, nil
}