github.com/orteth01/up@v0.2.0/internal/logs/parser/ast/ast.go (about)

     1  package ast
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // Op type.
     9  type Op string
    10  
    11  // Op types.
    12  const (
    13  	LNOT Op = "not"
    14  	NOT     = "!"
    15  	IN      = "in"
    16  	OR      = "||"
    17  	AND     = "&&"
    18  	NE      = "!="
    19  	EQ      = "="
    20  	GT      = ">"
    21  	LT      = "<"
    22  	GE      = ">="
    23  	LE      = "<="
    24  )
    25  
    26  // Node interface.
    27  type Node interface {
    28  	String() string
    29  }
    30  
    31  // Root node.
    32  type Root struct {
    33  	Node Node
    34  }
    35  
    36  // String implementation.
    37  func (n Root) String() string {
    38  	return fmt.Sprintf(`{ %s }`, n.Node)
    39  }
    40  
    41  // Expr node.
    42  type Expr struct {
    43  	Node Node
    44  }
    45  
    46  // String implementation.
    47  func (n Expr) String() string {
    48  	return fmt.Sprintf(`(%s)`, n.Node)
    49  }
    50  
    51  // Literal node.
    52  type Literal string
    53  
    54  // String implementation.
    55  func (n Literal) String() string {
    56  	return fmt.Sprintf(`%s`, string(n))
    57  }
    58  
    59  // Tuple node.
    60  type Tuple []Node
    61  
    62  // String implementation.
    63  func (n Tuple) String() string {
    64  	return fmt.Sprintf(`%#v`, n)
    65  }
    66  
    67  // Contains node.
    68  type Contains struct {
    69  	Node Node
    70  }
    71  
    72  // String implementation.
    73  func (n Contains) String() string {
    74  	switch v := n.Node.(type) {
    75  	case String:
    76  		return fmt.Sprintf(`"*%s*"`, string(v))
    77  	default:
    78  		return fmt.Sprintf(`%s`, n.Node)
    79  	}
    80  }
    81  
    82  // String node.
    83  type String string
    84  
    85  // String implementation.
    86  func (n String) String() string {
    87  	return fmt.Sprintf(`%q`, string(n))
    88  }
    89  
    90  // Property node.
    91  type Property string
    92  
    93  // String implementation.
    94  func (n Property) String() string {
    95  	return fmt.Sprintf(`$.%s`, string(n))
    96  }
    97  
    98  // Field node.
    99  type Field string
   100  
   101  // String implementation.
   102  func (n Field) String() string {
   103  	return fmt.Sprintf(`$.fields.%s`, string(n))
   104  }
   105  
   106  // Subscript node.
   107  type Subscript struct {
   108  	Left  Node
   109  	Right Node
   110  }
   111  
   112  // String implementation.
   113  func (n Subscript) String() string {
   114  	return fmt.Sprintf(`%s[%s]`, n.Left, n.Right)
   115  }
   116  
   117  // Member node.
   118  type Member struct {
   119  	Left  Node
   120  	Right Node
   121  }
   122  
   123  // String implementation.
   124  func (n Member) String() string {
   125  	return fmt.Sprintf(`%s.%s`, n.Left, n.Right)
   126  }
   127  
   128  // Number node.
   129  type Number string
   130  
   131  // String implementation.
   132  func (n Number) String() string {
   133  	return fmt.Sprintf(`%s`, string(n))
   134  }
   135  
   136  // Binary node.
   137  type Binary struct {
   138  	Op    Op
   139  	Left  Node
   140  	Right Node
   141  }
   142  
   143  // String implementation.
   144  func (n Binary) String() string {
   145  	switch n.Op {
   146  	case IN:
   147  		var s []string
   148  		for _, v := range n.Right.(Tuple) {
   149  			s = append(s, fmt.Sprintf(`%s %s %s`, n.Left, EQ, v))
   150  		}
   151  		return fmt.Sprintf(`(%s)`, strings.Join(s, " || "))
   152  	default:
   153  		return fmt.Sprintf(`%s %s %s`, n.Left, n.Op, n.Right)
   154  	}
   155  }
   156  
   157  // Unary node.
   158  type Unary struct {
   159  	Op    Op
   160  	Right Node
   161  }
   162  
   163  // String implementation.
   164  func (n Unary) String() string {
   165  	switch n.Op {
   166  	case LNOT:
   167  		return fmt.Sprintf(`!(%s)`, n.Right)
   168  	default:
   169  		return fmt.Sprintf(`%s%s`, n.Op, n.Right)
   170  	}
   171  }