forked from yangchenxing/go-nginx-conf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
139 lines (129 loc) · 3.09 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
package ncparser
import (
"container/list"
"fmt"
"sync"
)
// NginxConfigureBlock represent a block in nginx configure file.
// The content of a nginx configure file should be a block.
type NginxConfigureBlock []NginxConfigureCommand
// NginxConfigureCommand represenct a command in nginx configure file.
type NginxConfigureCommand struct {
// Words compose the command
Words []string
// Block follow the command
Block NginxConfigureBlock
}
type parser struct {
sync.Mutex
*scanner
}
var (
emptyBlock = NginxConfigureBlock(nil)
emptyCommand = NginxConfigureCommand{}
)
// Parse the content of nginx configure file into NginxConfigureBlock
func Parse(content []byte) (blk NginxConfigureBlock, err error) {
var p parser
return p.parse(content)
}
func (p *parser) parse(content []byte) (blk NginxConfigureBlock, err error) {
p.Lock()
defer p.Unlock()
defer func() {
if e := recover(); e != nil {
err = e.(error)
}
}()
p.scanner = newScanner(content)
cmds := list.New()
ForLoop:
for {
token := p.scan()
switch token.typ {
case eof:
break ForLoop
case word:
cmd, err := p.scanCommand(token.lit)
if err != nil {
return nil, err
}
cmds.PushBack(cmd)
case comment:
continue
default:
return nil, fmt.Errorf("unexpected global token %s at line %d", token.typ, p.line)
}
}
cfg := make([]NginxConfigureCommand, cmds.Len())
for i, cmd := 0, cmds.Front(); cmd != nil; i, cmd = i+1, cmd.Next() {
cfg[i] = cmd.Value.(NginxConfigureCommand)
}
return cfg, nil
}
func (p *parser) scanCommand(startWord string) (NginxConfigureCommand, error) {
words := list.New()
if startWord != "" {
words.PushBack(startWord)
}
var err error
var block NginxConfigureBlock
ForLoop:
for {
token := p.scan()
switch token.typ {
case eof:
return emptyCommand, fmt.Errorf("missing terminating token at line %d", p.line)
case braceOpen:
block, err = p.scanBlock()
if err != nil {
return emptyCommand, err
}
break ForLoop
case semicolon:
break ForLoop
case comment:
continue
case word:
words.PushBack(token.lit)
default:
return emptyCommand, fmt.Errorf("unexpected command token %s at line %d", token.typ, p.line)
}
}
cmd := NginxConfigureCommand{
Words: make([]string, words.Len()),
Block: block,
}
for i, word := 0, words.Front(); word != nil; i, word = i+1, word.Next() {
cmd.Words[i] = word.Value.(string)
}
return cmd, nil
}
func (p *parser) scanBlock() (NginxConfigureBlock, error) {
cmds := list.New()
ForLoop:
for {
token := p.scan()
switch token.typ {
case eof:
return emptyBlock, fmt.Errorf("missing terminating token at line %d", p.line)
case braceClose:
break ForLoop
case comment:
continue
case word:
cmd, err := p.scanCommand(token.lit)
if err != nil {
return emptyBlock, err
}
cmds.PushBack(cmd)
default:
return emptyBlock, fmt.Errorf("unexpected block token %s at line %d", token.typ, p.line)
}
}
block := make([]NginxConfigureCommand, cmds.Len())
for i, cmd := 0, cmds.Front(); cmd != nil; i, cmd = i+1, cmd.Next() {
block[i] = cmd.Value.(NginxConfigureCommand)
}
return block, nil
}