Debian Codegraph @

parser.go

raw symbols SCIP
1 package ssh_config
2
3 import (
4 "fmt"
5 "strings"
6 "unicode"
7 )
8
9 type sshParser struct {
10 ignoreMatchDirective bool
11 flow chan token
12 config *Config
13 tokensBuffer []token
14 currentTable []string
15 seenTableKeys []string
16 // /etc/ssh parser or local parser - used to find the default for relative
17 // filepaths in the Include directive
18 system bool
19 depth uint8
20 }
21
22 type sshParserStateFn func() sshParserStateFn
23
24 // Formats and panics an error message based on a token
25 func (p *sshParser) raiseErrorf(tok *token, msg string, args ...interface{}) {
26 // TODO this format is ugly
27 panic(tok.Position.String() + ": " + fmt.Sprintf(msg, args...))
28 }
29
30 func (p *sshParser) raiseError(tok *token, err error) {
31 if err == ErrDepthExceeded {
32 panic(err)
33 }
34 // TODO this format is ugly
35 panic(tok.Position.String() + ": " + err.Error())
36 }
37
38 func (p *sshParser) run() {
39 for state := p.parseStart; state != nil; {
40 state = state()
41 }
42 }
43
44 func (p *sshParser) peek() *token {
45 if len(p.tokensBuffer) != 0 {
46 return &(p.tokensBuffer[0])
47 }
48
49 tok, ok := <-p.flow
50 if !ok {
51 return nil
52 }
53 p.tokensBuffer = append(p.tokensBuffer, tok)
54 return &tok
55 }
56
57 func (p *sshParser) getToken() *token {
58 if len(p.tokensBuffer) != 0 {
59 tok := p.tokensBuffer[0]
60 p.tokensBuffer = p.tokensBuffer[1:]
61 return &tok
62 }
63 tok, ok := <-p.flow
64 if !ok {
65 return nil
66 }
67 return &tok
68 }
69
70 func (p *sshParser) parseStart() sshParserStateFn {
71 tok := p.peek()
72
73 // end of stream, parsing is finished
74 if tok == nil {
75 return nil
76 }
77
78 switch tok.typ {
79 case tokenComment, tokenEmptyLine:
80 return p.parseComment
81 case tokenKey:
82 return p.parseKV
83 case tokenEOF:
84 return nil
85 default:
86 p.raiseErrorf(tok, fmt.Sprintf("unexpected token %q\n", tok))
87 }
88 return nil
89 }
90
91 func (p *sshParser) parseKV() sshParserStateFn {
92 key := p.getToken()
93 hasEquals := false
94 val := p.getToken()
95 if val.typ == tokenEquals {
96 hasEquals = true
97 val = p.getToken()
98 }
99 comment := ""
100 tok := p.peek()
101 if tok == nil {
102 tok = &token{typ: tokenEOF}
103 }
104 if tok.typ == tokenComment && tok.Position.Line == val.Position.Line {
105 tok = p.getToken()
106 comment = tok.val
107 }
108 if strings.ToLower(key.val) == "match" && !p.ignoreMatchDirective {
109 // https://github.com/kevinburke/ssh_config/issues/6
110 p.raiseErrorf(val, "ssh_config: Match directive parsing is unsupported")
111 return nil
112 }
113 if strings.ToLower(key.val) == "host" {
114 strPatterns := strings.Split(val.val, " ")
115 patterns := make([]*Pattern, 0)
116 for i := range strPatterns {
117 if strPatterns[i] == "" {
118 continue
119 }
120 pat, err := NewPattern(strPatterns[i])
121 if err != nil {
122 p.raiseErrorf(val, "Invalid host pattern: %v", err)
123 return nil
124 }
125 patterns = append(patterns, pat)
126 }
127 // val.val at this point could be e.g. "example.com "
128 hostval := strings.TrimRightFunc(val.val, unicode.IsSpace)
129 spaceBeforeComment := val.val[len(hostval):]
130 val.val = hostval
131 p.config.ignoreMatchDirective = p.ignoreMatchDirective
132 p.config.Hosts = append(p.config.Hosts, &Host{
133 Patterns: patterns,
134 Nodes: make([]Node, 0),
135 EOLComment: comment,
136 spaceBeforeComment: spaceBeforeComment,
137 hasEquals: hasEquals,
138 },
139 )
140 return p.parseStart
141 }
142 lastHost := p.config.Hosts[len(p.config.Hosts)-1]
143 if strings.ToLower(key.val) == "include" {
144 inc, err := NewInclude(strings.Split(val.val, " "), hasEquals, key.Position, comment, p.system, p.ignoreMatchDirective, p.depth+1)
145 if err == ErrDepthExceeded {
146 p.raiseError(val, err)
147 return nil
148 }
149 if err != nil {
150 p.raiseErrorf(val, "Error parsing Include directive: %v", err)
151 return nil
152 }
153 lastHost.Nodes = append(lastHost.Nodes, inc)
154 return p.parseStart
155 }
156 shortval := strings.TrimRightFunc(val.val, unicode.IsSpace)
157 spaceAfterValue := val.val[len(shortval):]
158 kv := &KV{
159 Key: key.val,
160 Value: shortval,
161 spaceAfterValue: spaceAfterValue,
162 Comment: comment,
163 hasEquals: hasEquals,
164 leadingSpace: key.Position.Col - 1,
165 position: key.Position,
166 }
167 lastHost.Nodes = append(lastHost.Nodes, kv)
168 return p.parseStart
169 }
170
171 func (p *sshParser) parseComment() sshParserStateFn {
172 comment := p.getToken()
173 lastHost := p.config.Hosts[len(p.config.Hosts)-1]
174 lastHost.Nodes = append(lastHost.Nodes, &Empty{
175 Comment: comment.val,
176 // account for the "#" as well
177 leadingSpace: comment.Position.Col - 2,
178 position: comment.Position,
179 },
180 )
181 return p.parseStart
182 }
183
184 func parseSSH(flow chan token, system, ignoreMatchDirective bool, depth uint8) *Config {
185 // Ensure we consume tokens to completion even if parser exits early
186 defer func() {
187 for range flow {
188 }
189 }()
190
191 result := newConfig()
192 result.position = Position{1, 1}
193 parser := &sshParser{
194 ignoreMatchDirective: ignoreMatchDirective,
195 flow: flow,
196 config: result,
197 tokensBuffer: make([]token, 0),
198 currentTable: make([]string, 0),
199 seenTableKeys: make([]string, 0),
200 system: system,
201 depth: depth,
202 }
203 parser.run()
204 return result
205 }