github.com/team-ide/go-dialect@v1.9.20/dialect/statement.go (about)

     1  package dialect
     2  
     3  type Statement interface {
     4  	GetTemplate() (template string)
     5  	GetContent() (content *string)
     6  	GetParent() (parent Statement)
     7  	GetChildren() (children *[]Statement)
     8  	Format(statementContext *StatementContext) (text string, err error)
     9  }
    10  
    11  type AbstractStatement struct {
    12  	Content  string      `json:"content,omitempty"`
    13  	Children []Statement `json:"children,omitempty"`
    14  	Parent   Statement   `json:"-"`
    15  }
    16  
    17  func (this_ *AbstractStatement) GetContent() (content *string) {
    18  	content = &this_.Content
    19  	return
    20  }
    21  
    22  func (this_ *AbstractStatement) GetParent() (parent Statement) {
    23  	parent = this_.Parent
    24  	return
    25  }
    26  func (this_ *AbstractStatement) GetChildren() (children *[]Statement) {
    27  	children = &this_.Children
    28  	return
    29  }
    30  func (this_ *AbstractStatement) GetTemplate() (template string) {
    31  	template += this_.Content
    32  	for _, one := range this_.Children {
    33  		template += one.GetTemplate()
    34  	}
    35  	return
    36  }
    37  
    38  type RootStatement struct {
    39  	*AbstractStatement
    40  }
    41  
    42  type IgnorableStatement struct {
    43  	*AbstractStatement
    44  }
    45  
    46  func (this_ *IgnorableStatement) GetTemplate() (template string) {
    47  	template += "["
    48  
    49  	template += this_.Content
    50  
    51  	for _, one := range this_.Children {
    52  		template += one.GetTemplate()
    53  	}
    54  
    55  	template += "]"
    56  	return
    57  }
    58  
    59  type TextStatement struct {
    60  	*AbstractStatement
    61  }
    62  
    63  type IfStatement struct {
    64  	*AbstractStatement
    65  	Condition           string               `json:"condition"`
    66  	ConditionExpression *ExpressionStatement `json:"conditionExpression"`
    67  	ElseIfs             []*ElseIfStatement   `json:"elseIfs"`
    68  	Else                *ElseStatement       `json:"else"`
    69  }
    70  
    71  func (this_ *IfStatement) GetTemplate() (template string) {
    72  	template += "if " + this_.Condition + " {\n"
    73  
    74  	//template += this_.Content
    75  	for _, one := range this_.Children {
    76  		template += one.GetTemplate()
    77  	}
    78  
    79  	if len(this_.ElseIfs) > 0 || this_.Else != nil {
    80  		template += "\n}"
    81  	} else {
    82  		template += "\n}\n"
    83  	}
    84  	for _, one := range this_.ElseIfs {
    85  		template += one.GetTemplate()
    86  	}
    87  
    88  	if this_.Else != nil {
    89  		template += this_.Else.GetTemplate()
    90  	}
    91  
    92  	return
    93  }
    94  
    95  type ElseIfStatement struct {
    96  	*AbstractStatement
    97  	Condition           string               `json:"condition"`
    98  	ConditionExpression *ExpressionStatement `json:"conditionExpression"`
    99  	If                  *IfStatement         `json:"-"`
   100  	Index               int                  `json:"index"`
   101  }
   102  
   103  func (this_ *ElseIfStatement) GetTemplate() (template string) {
   104  	template += " else if " + this_.Condition + " {\n"
   105  
   106  	//template += this_.Content
   107  
   108  	for _, one := range this_.Children {
   109  		template += one.GetTemplate()
   110  	}
   111  
   112  	if this_.IsEndElseIf() {
   113  		if this_.If.Else != nil {
   114  			template += "\n}"
   115  		} else {
   116  			template += "\n}\n"
   117  		}
   118  
   119  	} else {
   120  		template += "\n}"
   121  	}
   122  	return
   123  }
   124  func (this_ *ElseIfStatement) IsEndElseIf() (isEnd bool) {
   125  	if this_.Index == len(this_.If.ElseIfs)-1 {
   126  		isEnd = true
   127  	}
   128  	return
   129  }
   130  
   131  type ElseStatement struct {
   132  	*AbstractStatement
   133  	If *IfStatement `json:"-"`
   134  }
   135  
   136  func (this_ *ElseStatement) GetTemplate() (template string) {
   137  	template += " else {\n"
   138  
   139  	//template += this_.Content
   140  
   141  	for _, one := range this_.Children {
   142  		template += one.GetTemplate()
   143  	}
   144  
   145  	template += "\n}\n"
   146  	return
   147  }
   148  
   149  type ForStatement struct {
   150  	*AbstractStatement
   151  }
   152  
   153  func (this_ *ForStatement) GetTemplate() (template string) {
   154  	template += "for " + this_.Content + " {\n"
   155  
   156  	//template += this_.Content
   157  
   158  	for _, one := range this_.Children {
   159  		template += one.GetTemplate()
   160  	}
   161  
   162  	template += "\n}\n"
   163  	return
   164  }
   165  
   166  type ExpressionStatement struct {
   167  	*AbstractStatement
   168  }
   169  
   170  type ExpressionStringStatement struct {
   171  	*AbstractStatement
   172  	Value string `json:"value"`
   173  }
   174  
   175  type ExpressionNumberStatement struct {
   176  	*AbstractStatement
   177  	Value float64 `json:"value"`
   178  }
   179  
   180  type ExpressionIdentifierStatement struct {
   181  	*AbstractStatement
   182  	Identifier string `json:"identifier"`
   183  }
   184  
   185  type ExpressionFuncStatement struct {
   186  	*AbstractStatement
   187  	Func string      `json:"func"`
   188  	Args []Statement `json:"args"`
   189  }
   190  
   191  type ExpressionOperatorStatement struct {
   192  	*AbstractStatement
   193  	Operator string `json:"operator"`
   194  }
   195  
   196  // ExpressionBracketsStatement 括号
   197  type ExpressionBracketsStatement struct {
   198  	*AbstractStatement
   199  }