Changed tokenizer state into a queue so that it can more easily step back up the tree

This commit is contained in:
Christopher Ramey 2020-08-15 17:53:54 -08:00
parent e10de3188a
commit 5de5349fde

View File

@ -45,8 +45,7 @@ const (
TK_NONE = iota TK_NONE = iota
TK_MONITOR TK_MONITOR
TK_GROUP TK_GROUP
TK_PLAIN_HOST TK_HOST
TK_GROUP_HOST
) )
func stateName(s int) string { func stateName(s int) string {
@ -57,10 +56,8 @@ func stateName(s int) string {
return "TK_MONTIOR" return "TK_MONTIOR"
case TK_GROUP: case TK_GROUP:
return "TK_GROUP" return "TK_GROUP"
case TK_PLAIN_HOST: case TK_HOST:
return "TK_PLAIN_HOST" return "TK_HOST"
case TK_GROUP_HOST:
return "TK_GROUP_HOST"
default: default:
return "UNKNOWN" return "UNKNOWN"
} }
@ -82,7 +79,7 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
tk := scan.Text() tk := scan.Text()
stateswitch: stateswitch:
switch parser.State { switch parser.GetState() {
case TK_NONE: case TK_NONE:
switch strings.ToLower(tk) { switch strings.ToLower(tk) {
case "monitor": case "monitor":
@ -96,14 +93,14 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
switch strings.ToLower(tk) { switch strings.ToLower(tk) {
case "host": case "host":
config.NewGroup().NewHost() config.NewGroup().NewHost()
parser.SetState(TK_PLAIN_HOST) parser.SetState(TK_HOST)
case "group": case "group":
config.NewGroup() config.NewGroup()
parser.SetState(TK_GROUP) parser.SetState(TK_GROUP)
default: default:
parser.SetState(TK_NONE) parser.PrevState()
goto stateswitch goto stateswitch
} }
@ -113,7 +110,7 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
switch strings.ToLower(tk) { switch strings.ToLower(tk) {
case "host": case "host":
group.NewHost() group.NewHost()
parser.SetState(TK_GROUP_HOST) parser.SetState(TK_HOST)
continue continue
default: default:
@ -122,13 +119,11 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
continue continue
} }
parser.SetState(TK_MONITOR) parser.PrevState()
goto stateswitch goto stateswitch
} }
case TK_PLAIN_HOST: case TK_HOST:
fallthrough
case TK_GROUP_HOST:
host := config.LastGroup().LastHost() host := config.LastGroup().LastHost()
if host.Name == "" { if host.Name == "" {
host.Name = tk host.Name = tk
@ -143,17 +138,12 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
continue continue
default: default:
if parser.State == TK_GROUP_HOST { parser.PrevState()
parser.SetState(TK_GROUP)
} else if parser.State == TK_PLAIN_HOST {
parser.SetState(TK_MONITOR)
}
goto stateswitch goto stateswitch
} }
default: default:
return nil, fmt.Errorf("Unknown parser state: %d", parser.State) return nil, fmt.Errorf("Unknown parser state: %d", parser.GetState())
} }
} }
if err := scan.Err(); err != nil { if err := scan.Err(); err != nil {
@ -164,12 +154,26 @@ func ReadConfig(fn string) (*AlrmConfig, error) {
type Parser struct { type Parser struct {
Line int Line int
State int states []int
}
func (pr *Parser) GetState() int {
if len(pr.states) < 1 {
return TK_NONE
}
return pr.states[len(pr.states) - 1]
} }
func (pr *Parser) SetState(state int) { func (pr *Parser) SetState(state int) {
//fmt.Printf("%s -> %s\n", stateName(pr.State), stateName(state)) //fmt.Printf("%s -> %s\n", stateName(pr.GetState()), stateName(state))
pr.State = state pr.states = append(pr.states, state)
}
func (pr *Parser) PrevState() int {
if len(pr.states) > 0 {
pr.states = pr.states[:len(pr.states) - 1]
}
return pr.GetState()
} }
func (pr *Parser) Split(data []byte, atEOF bool) (int, []byte, error) { func (pr *Parser) Split(data []byte, atEOF bool) (int, []byte, error) {