2018-11-12 07:41:00 +00:00
|
|
|
package nginx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-11-12 18:45:30 +00:00
|
|
|
// Decoder implements parser.Decoder
|
|
|
|
type Decoder struct{ reader io.Reader }
|
2018-11-12 07:41:00 +00:00
|
|
|
|
2018-11-12 18:45:30 +00:00
|
|
|
// Decode is the main function of the parser.Decoder interface
|
|
|
|
func (d *Decoder) Decode(v interface{}) error {
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
// check 'v'
|
|
|
|
if v == nil {
|
|
|
|
return ErrNullReceiver
|
|
|
|
}
|
2018-11-12 18:22:24 +00:00
|
|
|
vcast, ok := v.(*Line)
|
2018-11-12 07:41:00 +00:00
|
|
|
if !ok {
|
|
|
|
return ErrInvalidReceiver
|
|
|
|
}
|
2018-11-12 11:00:41 +00:00
|
|
|
vcast.Lines = make([]*Line, 0)
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
r := bufio.NewReader(d.reader)
|
|
|
|
n := -1 // line number
|
|
|
|
|
|
|
|
// regexes
|
2018-11-12 16:07:30 +00:00
|
|
|
reSection := regexp.MustCompile(`(?m)^([a-z0-9_-]+)\s*([^\{]*)\s*\{$`)
|
2018-11-12 07:41:00 +00:00
|
|
|
reAssign := regexp.MustCompile(`(?m)^([a-z0-9_-]+)\s+([^;#]+);$`)
|
|
|
|
reInclude := regexp.MustCompile(`(?m)^include\s+([^;#]+);$`)
|
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
// actual section stack
|
|
|
|
stack := make([]*Line, 0)
|
2018-11-12 18:38:24 +00:00
|
|
|
eof := false
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
n++
|
2018-11-12 18:38:24 +00:00
|
|
|
if eof {
|
|
|
|
break
|
|
|
|
}
|
2018-11-12 07:41:00 +00:00
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
l := &Line{Type: NONE}
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
// 1. read line
|
|
|
|
notrim, err := r.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
2018-11-12 18:38:24 +00:00
|
|
|
if len(notrim) > 0 {
|
|
|
|
eof = true
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2018-11-12 07:41:00 +00:00
|
|
|
} else if err != nil {
|
2018-11-12 11:00:41 +00:00
|
|
|
return &LineError{n, err}
|
2018-11-12 07:41:00 +00:00
|
|
|
}
|
2018-11-12 11:00:41 +00:00
|
|
|
notrim = strings.Trim(notrim, "\r\n")
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
// 2. ignore empty
|
2018-11-12 18:38:24 +00:00
|
|
|
line := strings.Trim(notrim, " \t\r\n")
|
2018-11-12 07:41:00 +00:00
|
|
|
if len(line) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
// 2. comment
|
2018-11-12 07:41:00 +00:00
|
|
|
if line[0] == '#' {
|
|
|
|
l.Type = COMMENT
|
2018-11-12 14:38:43 +00:00
|
|
|
l.Components = []string{line[1:]}
|
2018-11-12 07:41:00 +00:00
|
|
|
} else if line[0] == ';' {
|
|
|
|
l.Type = COLONCOMMENT
|
2018-11-12 14:38:43 +00:00
|
|
|
l.Components = []string{line[1:]}
|
2018-11-12 11:00:41 +00:00
|
|
|
} else if line[0] == '}' {
|
|
|
|
l.Type = SECTIONEND
|
|
|
|
if len(stack) < 1 {
|
|
|
|
return &LineError{n, ErrUnexpectedSectionClose}
|
|
|
|
}
|
2018-11-12 07:41:00 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
// 3. section
|
2018-11-12 07:41:00 +00:00
|
|
|
if l.Type == NONE {
|
|
|
|
match := reSection.FindStringSubmatch(line)
|
|
|
|
if match != nil {
|
|
|
|
l.Type = SECTION
|
2018-11-12 16:07:30 +00:00
|
|
|
l.Components = make([]string, 0)
|
|
|
|
l.Components = append(l.Components, match[1])
|
|
|
|
|
|
|
|
if len(match[2]) > 0 {
|
|
|
|
l.Components = append(l.Components, strings.Trim(match[2], " \t"))
|
|
|
|
}
|
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
l.Lines = make([]*Line, 0)
|
|
|
|
stack = append(stack, l)
|
2018-11-12 07:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
// 4. include
|
2018-11-12 07:41:00 +00:00
|
|
|
if l.Type == NONE {
|
|
|
|
match := reInclude.FindStringSubmatch(line)
|
|
|
|
if match != nil {
|
|
|
|
l.Type = INCLUDE
|
|
|
|
l.Components = []string{match[1]}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
// 5. assignment
|
2018-11-12 07:41:00 +00:00
|
|
|
if l.Type == NONE {
|
|
|
|
match := reAssign.FindStringSubmatch(line)
|
|
|
|
if match != nil {
|
|
|
|
l.Type = ASSIGNMENT
|
|
|
|
l.Components = match[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
// 6. invalid type
|
2018-11-12 11:00:41 +00:00
|
|
|
if l.Type == NONE {
|
|
|
|
return &LineError{n, ErrInvalidSyntax}
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
// 7. pop section
|
2018-11-12 11:00:41 +00:00
|
|
|
if l.Type == SECTIONEND {
|
|
|
|
cur := stack[len(stack)-1] // pop
|
|
|
|
stack = stack[0 : len(stack)-1] // pop
|
|
|
|
|
|
|
|
var last []*Line
|
|
|
|
if len(stack) > 0 {
|
|
|
|
last = stack[len(stack)-1].Lines
|
|
|
|
} else {
|
|
|
|
last = vcast.Lines
|
|
|
|
}
|
|
|
|
last = append(last, cur)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:35:45 +00:00
|
|
|
// 8. add to section / or receiver
|
2018-11-12 11:00:41 +00:00
|
|
|
stacklen := len(stack)
|
|
|
|
if stacklen > 0 && l.Type == SECTION {
|
|
|
|
stacklen--
|
|
|
|
}
|
|
|
|
|
|
|
|
if stacklen > 0 {
|
|
|
|
tail := stack[stacklen-1]
|
|
|
|
tail.Lines = append(tail.Lines, l)
|
|
|
|
} else {
|
|
|
|
vcast.Lines = append(vcast.Lines, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-12 07:41:00 +00:00
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
// fail on unclosed section
|
|
|
|
if len(stack) > 0 {
|
|
|
|
return ErrUnclosedSection
|
2018-11-12 07:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-12 11:08:54 +00:00
|
|
|
func inArray(haystack string, needle byte) bool {
|
2018-11-12 07:41:00 +00:00
|
|
|
for i, l := 0, len(haystack); i < l; i++ {
|
|
|
|
if haystack[i] == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|