unnest traverses down the left-hand side of the parse graph until it encounters the first compound parse node or the first leaf in the parse graph.
(tree antlr.ParseTree)
| 1004 | // unnest traverses down the left-hand side of the parse graph until it encounters the first compound |
| 1005 | // parse node or the first leaf in the parse graph. |
| 1006 | func unnest(tree antlr.ParseTree) antlr.ParseTree { |
| 1007 | for tree != nil { |
| 1008 | switch t := tree.(type) { |
| 1009 | case *gen.ExprContext: |
| 1010 | // conditionalOr op='?' conditionalOr : expr |
| 1011 | if t.GetOp() != nil { |
| 1012 | return t |
| 1013 | } |
| 1014 | // conditionalOr |
| 1015 | tree = t.GetE() |
| 1016 | case *gen.ConditionalOrContext: |
| 1017 | // conditionalAnd (ops=|| conditionalAnd)* |
| 1018 | if t.GetOps() != nil && len(t.GetOps()) > 0 { |
| 1019 | return t |
| 1020 | } |
| 1021 | // conditionalAnd |
| 1022 | tree = t.GetE() |
| 1023 | case *gen.ConditionalAndContext: |
| 1024 | // relation (ops=&& relation)* |
| 1025 | if t.GetOps() != nil && len(t.GetOps()) > 0 { |
| 1026 | return t |
| 1027 | } |
| 1028 | // relation |
| 1029 | tree = t.GetE() |
| 1030 | case *gen.RelationContext: |
| 1031 | // relation op relation |
| 1032 | if t.GetOp() != nil { |
| 1033 | return t |
| 1034 | } |
| 1035 | // calc |
| 1036 | tree = t.Calc() |
| 1037 | case *gen.CalcContext: |
| 1038 | // calc op calc |
| 1039 | if t.GetOp() != nil { |
| 1040 | return t |
| 1041 | } |
| 1042 | // unary |
| 1043 | tree = t.Unary() |
| 1044 | case *gen.MemberExprContext: |
| 1045 | // member expands to one of: primary, select, index, or create message |
| 1046 | tree = t.Member() |
| 1047 | case *gen.PrimaryExprContext: |
| 1048 | // primary expands to one of identifier, nested, create list, create struct, literal |
| 1049 | tree = t.Primary() |
| 1050 | case *gen.NestedContext: |
| 1051 | // contains a nested 'expr' |
| 1052 | tree = t.GetE() |
| 1053 | case *gen.ConstantLiteralContext: |
| 1054 | // expands to a primitive literal |
| 1055 | tree = t.Literal() |
| 1056 | default: |
| 1057 | return t |
| 1058 | } |
| 1059 | } |
| 1060 | return tree |
| 1061 | } |
| 1062 | |
| 1063 | var ( |