diff options
Diffstat (limited to '')
| -rw-r--r-- | internal/ast/processor.go | 119 | ||||
| -rw-r--r-- | internal/ast/tree.go | 43 |
2 files changed, 0 insertions, 162 deletions
diff --git a/internal/ast/processor.go b/internal/ast/processor.go deleted file mode 100644 index 55dce59..0000000 --- a/internal/ast/processor.go +++ /dev/null @@ -1,119 +0,0 @@ -package ast - -import ( - "fmt" - "strconv" - - "go.neonxp.ru/conf/internal/parser" - "go.neonxp.ru/conf/model" -) - -func ToDoc(config *Node) (model.Body, error) { - if len(config.Children) < 1 { - return nil, fmt.Errorf("invalid ast tree") - } - - doc := config.Children[0] - - return processDoc(doc), nil -} - -func processDoc(docNode *Node) model.Body { - doc := make(model.Body, 0, len(docNode.Children)) - for _, stmt := range docNode.Children { - processStmt(&doc, stmt) - } - return doc -} - -func processStmt(doc *model.Body, stmt *Node) { - ident := extractIdent(stmt.Children[0]) - nodeBody := stmt.Children[1] - switch nodeBody.Symbol { - case parser.Command: - *doc = append(*doc, processDirective(ident, nodeBody)) - case parser.Assignment: - *doc = append(*doc, processSetting(ident, nodeBody)) - } -} - -func processDirective(ident string, directive *Node) *model.Directive { - result := &model.Directive{ - Name: ident, - } - - for _, child := range directive.Children { - // Can be arguments OR body OR both - switch child.Symbol { - case parser.Values: - result.Arguments = extractValues(child) - case parser.Body: - // Children[0] = '{', Children[1] = Body, Children[2] = '}' - result.Body = processDoc(child.Children[1]) - } - } - - return result -} - -func processSetting(ident string, setting *Node) *model.Setting { - result := &model.Setting{ - Key: ident, - Value: extractValues(setting.Children[1]), // Children[0] = '=', Children[1] = Values, Children[2] = ';' - } - - return result -} - -func extractIdent(word *Node) string { - return word.Children[0].Source -} - -func extractValues(args *Node) []model.Value { - result := make([]model.Value, len(args.Children)) - for i, child := range args.Children { - v, err := extractValue(child) - if err != nil { - result[i] = err - continue - } - result[i] = v - } - - return result -} - -func extractValue(value *Node) (any, error) { - v := value.Children[0] - s := v.Children[0].Source - switch v.Symbol { - case parser.Word: - return model.Word(s), nil - case parser.String: - return unquote(s), nil - case parser.Number: - d, err := strconv.Atoi(s) - if err == nil { - return d, nil - } - fl, err := strconv.ParseFloat(s, 32) - if err == nil { - return fl, nil - } - return nil, fmt.Errorf("invalid number: %s (%s)", v.Source, s) - case parser.Boolean: - return s == "true", nil - default: - return nil, fmt.Errorf("unknown value type: %s (%s)", v.Symbol, s) - } -} - -func unquote(str string) string { - if len(str) == 0 { - return "" - } - if str[0:1] == `"` || str[0:1] == "'" || str[0:1] == "`" { - return str[1 : len(str)-1] - } - return str -} diff --git a/internal/ast/tree.go b/internal/ast/tree.go deleted file mode 100644 index 72b7c78..0000000 --- a/internal/ast/tree.go +++ /dev/null @@ -1,43 +0,0 @@ -package ast - -import ( - "go.neonxp.ru/conf/internal/parser" - "modernc.org/scanner" -) - -func Parse(p *parser.Parser, data []int32) []*Node { - nodes := make([]*Node, 0, 2) - for len(data) != 0 { - next := int32(1) - var node *Node - switch n := data[0]; { - case n < 0: - next = 2 + data[1] - node = &Node{ - Symbol: parser.Symbol(-data[0]), - Children: Parse(p, data[2:next]), - } - default: - tok := p.Token(n) - node = &Node{ - Token: tok, - Symbol: parser.Symbol(tok.Ch), - Source: tok.Src(), - Col: tok.Position().Column, - Line: tok.Position().Line, - } - } - nodes = append(nodes, node) - data = data[next:] - } - return nodes -} - -type Node struct { - Symbol parser.Symbol - Token scanner.Token - Children []*Node - Source string - Col int - Line int -} |
