-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathparser.go
389 lines (342 loc) · 9.92 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package parser
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/parser/token"
)
// Option parsing option
type Option func(*Parser)
type options struct {
parseInclude bool
skipIncludeParsingErr bool
skipComments bool
customDirectives map[string]string
skipValidSubDirectiveBlock map[string]struct{}
skipValidDirectivesErr bool
}
func defaultOptions() options {
return options{
parseInclude: false,
skipIncludeParsingErr: false,
skipComments: false,
customDirectives: map[string]string{},
skipValidSubDirectiveBlock: map[string]struct{}{},
skipValidDirectivesErr: false,
}
}
// Parser is an nginx config parser
type Parser struct {
opts options
configRoot string // TODO: confirmation needed (whether this is the parent of nginx.conf)
lexer *lexer
currentToken token.Token
followingToken token.Token
parsedIncludes map[*config.Include]*config.Config
statementParsers map[string]func() (config.IDirective, error)
blockWrappers map[string]func(*config.Directive) (config.IDirective, error)
directiveWrappers map[string]func(*config.Directive) (config.IDirective, error)
includeWrappers map[string]func(*config.Directive) (config.IDirective, error)
commentBuffer []string
file *os.File
}
// WithSameOptions copy options from another parser
func WithSameOptions(p *Parser) Option {
return func(curr *Parser) {
curr.opts = p.opts
}
}
func withParsedIncludes(parsedIncludes map[*config.Include]*config.Config) Option {
return func(p *Parser) {
p.parsedIncludes = parsedIncludes
}
}
func withConfigRoot(configRoot string) Option {
return func(p *Parser) {
p.configRoot = configRoot
}
}
// WithSkipIncludeParsingErr ignores include parsing errors
func WithSkipIncludeParsingErr() Option {
return func(p *Parser) {
p.opts.skipIncludeParsingErr = true
}
}
// WithDefaultOptions default options
func WithDefaultOptions() Option {
return func(p *Parser) {
p.opts = defaultOptions()
}
}
// WithSkipComments default options
func WithSkipComments() Option {
return func(p *Parser) {
p.opts.skipComments = true
}
}
// WithIncludeParsing enable parsing included files
func WithIncludeParsing() Option {
return func(p *Parser) {
p.opts.parseInclude = true
}
}
// WithCustomDirectives add your custom directives as valid directives
func WithCustomDirectives(directives ...string) Option {
return func(p *Parser) {
for _, directive := range directives {
p.opts.customDirectives[directive] = directive
}
}
}
// WithSkipValidBlocks add your custom block as valid
func WithSkipValidBlocks(directives ...string) Option {
return func(p *Parser) {
for _, directive := range directives {
p.opts.skipValidSubDirectiveBlock[directive] = struct{}{}
}
}
}
// WithSkipValidDirectivesErr ignores unknown directive errors
func WithSkipValidDirectivesErr() Option {
return func(p *Parser) {
p.opts.skipValidDirectivesErr = true
}
}
// NewStringParser parses nginx conf from string
func NewStringParser(str string, opts ...Option) *Parser {
return NewParserFromLexer(lex(str), opts...)
}
// NewParser create new parser
func NewParser(filePath string, opts ...Option) (*Parser, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
l := newLexer(bufio.NewReader(f))
l.file = filePath
p := NewParserFromLexer(l, opts...)
p.file = f
return p, nil
}
// NewParserFromLexer initilizes a new Parser
func NewParserFromLexer(lexer *lexer, opts ...Option) *Parser {
configRoot, _ := filepath.Split(lexer.file)
parser := &Parser{
lexer: lexer,
opts: defaultOptions(),
parsedIncludes: make(map[*config.Include]*config.Config),
configRoot: configRoot,
}
for _, o := range opts {
o(parser)
}
parser.nextToken()
parser.nextToken()
parser.blockWrappers = config.BlockWrappers
parser.directiveWrappers = config.DirectiveWrappers
parser.includeWrappers = config.IncludeWrappers
return parser
}
func (p *Parser) nextToken() {
p.currentToken = p.followingToken
p.followingToken = p.lexer.scan()
}
func (p *Parser) curTokenIs(t token.Type) bool {
return p.currentToken.Type == t
}
func (p *Parser) followingTokenIs(t token.Type) bool {
return p.followingToken.Type == t
}
// Parse the gonginx.
func (p *Parser) Parse() (*config.Config, error) {
parsedBlock, err := p.parseBlock(false, false)
if err != nil {
return nil, err
}
c := &config.Config{
FilePath: p.lexer.file, //TODO: set filepath here,
Block: parsedBlock,
}
err = p.Close()
return c, err
}
// ParseBlock parse a block statement
func (p *Parser) parseBlock(inBlock bool, isSkipValidDirective bool) (*config.Block, error) {
context := &config.Block{
Directives: make([]config.IDirective, 0),
}
var s config.IDirective
var err error
var line int
parsingLoop:
for {
switch {
case p.curTokenIs(token.EOF):
if inBlock {
return nil, errors.New("unexpected eof in block")
}
break parsingLoop
case p.curTokenIs(token.LuaCode):
context.IsLuaBlock = true
context.LiteralCode = p.currentToken.Literal
case p.curTokenIs(token.BlockEnd):
break parsingLoop
case p.curTokenIs(token.Keyword) || p.curTokenIs(token.QuotedString):
s, err = p.parseStatement(isSkipValidDirective)
if err != nil {
return nil, err
}
if s.GetBlock() == nil {
s.SetParent(s)
} else {
// each directive should have a parent directive, not a block
// find each directive in the block and set the parent directive
b := s.GetBlock()
for _, dir := range b.GetDirectives() {
dir.SetParent(s)
}
}
line = p.currentToken.Line
s.SetLine(line)
context.Directives = append(context.Directives, s)
case p.curTokenIs(token.Comment):
if p.opts.skipComments {
break
}
// inline comment
if line == p.currentToken.Line {
if s == nil && len(context.Directives) > 0 {
s = context.Directives[len(context.Directives)-1]
}
s.SetInlineComment(p.currentToken.Literal)
s.SetLine(line)
p.commentBuffer = nil
} else {
p.commentBuffer = append(p.commentBuffer, p.currentToken.Literal)
}
}
p.nextToken()
}
return context, nil
}
func (p *Parser) parseStatement(isSkipValidDirective bool) (config.IDirective, error) {
d := &config.Directive{
Name: p.currentToken.Literal,
}
if !p.opts.skipValidDirectivesErr && !isSkipValidDirective {
_, ok := ValidDirectives[d.Name]
_, ok2 := p.opts.customDirectives[d.Name]
if !ok && !ok2 {
return nil, fmt.Errorf("unknown directive '%s' on line %d, column %d", d.Name, p.currentToken.Line, p.currentToken.Column)
}
}
//if we have a special parser for the directive, we use it.
if sp, ok := p.statementParsers[d.Name]; ok {
return sp()
}
if len(p.commentBuffer) > 0 {
d.Comment = p.commentBuffer
p.commentBuffer = nil
}
//parse parameters until the end.
// keep track of the line index of the directive
directiveLineIndex := p.currentToken.Line
for p.nextToken(); p.currentToken.IsParameterEligible(); p.nextToken() {
d.Parameters = append(d.Parameters, config.Parameter{
Value: p.currentToken.Literal,
RelativeLineIndex: p.currentToken.Line - directiveLineIndex}) // save the relative line index of the parameter
if p.currentToken.Is(token.BlockEnd) {
return d, nil
}
}
//if we find a semicolon it is a directive, we will check directive converters
if p.curTokenIs(token.Semicolon) {
if iw, ok := p.includeWrappers[d.Name]; ok {
include, err := iw(d)
if err != nil {
return nil, err
}
return p.ParseInclude(include.(*config.Include))
} else if dw, ok := p.directiveWrappers[d.Name]; ok {
return dw(d)
}
return d, nil
}
for {
if p.curTokenIs(token.Comment) {
p.commentBuffer = append(p.commentBuffer, p.currentToken.Literal)
p.nextToken()
} else {
break
}
}
//ok, it does not end with a semicolon but a block starts, we will convert that block if we have a converter
if p.curTokenIs(token.BlockStart) {
_, blockSkip1 := SkipValidBlocks[d.Name]
_, blockSkip2 := p.opts.skipValidSubDirectiveBlock[d.Name]
isSkipBlockSubDirective := blockSkip1 || blockSkip2 || isSkipValidDirective
b, err := p.parseBlock(true, isSkipBlockSubDirective)
if err != nil {
return nil, err
}
d.Block = b
if strings.HasSuffix(d.Name, "_by_lua_block") {
return p.blockWrappers["_by_lua_block"](d)
}
if bw, ok := p.blockWrappers[d.Name]; ok {
return bw(d)
}
return d, nil
}
return nil, fmt.Errorf("unexpected token %s (%s) on line %d, column %d", p.currentToken.Type.String(), p.currentToken.Literal, p.currentToken.Line, p.currentToken.Column)
}
// ParseInclude just parse include confs
func (p *Parser) ParseInclude(include *config.Include) (config.IDirective, error) {
if p.opts.parseInclude {
includePath := include.IncludePath
if !filepath.IsAbs(includePath) {
includePath = filepath.Join(p.configRoot, include.IncludePath)
}
includePaths, err := filepath.Glob(includePath)
if err != nil && !p.opts.skipIncludeParsingErr {
return nil, err
}
for _, includePath := range includePaths {
if conf, ok := p.parsedIncludes[include]; ok {
// same file includes itself? don't blow up the parser
if conf == nil {
continue
}
} else {
p.parsedIncludes[include] = nil
}
parser, err := NewParser(includePath,
WithSameOptions(p),
withParsedIncludes(p.parsedIncludes),
withConfigRoot(p.configRoot),
)
if err != nil && !p.opts.skipIncludeParsingErr {
panic(err)
}
config, err := parser.Parse()
if err != nil {
return nil, err
}
//TODO: link parent config or include direcitve?
p.parsedIncludes[include] = config
include.Configs = append(include.Configs, config)
}
}
return include, nil
}
// Close closes the file handler and releases the resources
func (p *Parser) Close() (err error) {
if p.file != nil {
err = p.file.Close()
}
return err
}