Skip to content

Commit 6593b4c

Browse files
clean parser
1 parent 3470379 commit 6593b4c

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

parser/parser.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ type (
2020
func parsePrefixGroup(reader *astutil.NodeReader, matcher astutil.NodeMatcher, fn prefixParseFn) ast.TokenList {
2121
var replaceNodes []ast.Node
2222
for reader.NextNode(false) {
23+
if list, ok := reader.CurNode.(ast.TokenList); ok {
24+
newReader := astutil.NewNodeReader(list)
25+
replaceNode := parsePrefixGroup(newReader, matcher, fn)
26+
reader.Replace(replaceNode, reader.Index-1)
27+
}
2328
if reader.CurNodeIs(matcher) {
2429
replaceNodes = append(replaceNodes, fn(reader))
25-
} else if list, ok := reader.CurNode.(ast.TokenList); ok {
26-
newReader := astutil.NewNodeReader(list)
27-
replaceNodes = append(replaceNodes, parsePrefixGroup(newReader, matcher, fn))
2830
} else {
2931
replaceNodes = append(replaceNodes, reader.CurNode)
3032
}
@@ -36,11 +38,13 @@ func parsePrefixGroup(reader *astutil.NodeReader, matcher astutil.NodeMatcher, f
3638
func parseInfixGroup(reader *astutil.NodeReader, matcher astutil.NodeMatcher, ignoreWhiteSpace bool, fn infixParseFn) ast.TokenList {
3739
var replaceNodes []ast.Node
3840
for reader.NextNode(false) {
41+
if list, ok := reader.CurNode.(ast.TokenList); ok {
42+
newReader := astutil.NewNodeReader(list)
43+
replaceNode := parseInfixGroup(newReader, matcher, ignoreWhiteSpace, fn)
44+
reader.Replace(replaceNode, reader.Index-1)
45+
}
3946
if reader.PeekNodeIs(ignoreWhiteSpace, matcher) {
4047
replaceNodes = append(replaceNodes, fn(reader))
41-
} else if list, ok := reader.CurNode.(ast.TokenList); ok {
42-
newReader := astutil.NewNodeReader(list)
43-
replaceNodes = append(replaceNodes, parseInfixGroup(newReader, matcher, ignoreWhiteSpace, fn))
4448
} else {
4549
replaceNodes = append(replaceNodes, reader.CurNode)
4650
}
@@ -480,15 +484,6 @@ var aliasRecursionMatcher = astutil.NodeMatcher{
480484
}
481485

482486
func parseAliasedWithoutAs(reader *astutil.NodeReader) ast.Node {
483-
if reader.CurNodeIs(aliasRecursionMatcher) {
484-
if list, ok := reader.CurNode.(ast.TokenList); ok {
485-
// FIXME: more simplity
486-
// For sub query
487-
parenthesis := parsePrefixGroup(astutil.NewNodeReader(list), aliasLeftMatcher, parseAliasedWithoutAs)
488-
reader.Replace(parenthesis, reader.Index-1)
489-
}
490-
}
491-
492487
if !reader.PeekNodeIs(true, aliasRightMatcher) {
493488
return reader.CurNode
494489
}
@@ -510,14 +505,6 @@ func parseAliased(reader *astutil.NodeReader) ast.Node {
510505
if !reader.CurNodeIs(aliasLeftMatcher) {
511506
return reader.CurNode
512507
}
513-
if reader.CurNodeIs(aliasRecursionMatcher) {
514-
if list, ok := reader.CurNode.(ast.TokenList); ok {
515-
// FIXME: more simplity
516-
// For sub query
517-
parenthesis := parseInfixGroup(astutil.NewNodeReader(list), aliasInfixMatcher, true, parseAliased)
518-
reader.Replace(parenthesis, reader.Index-1)
519-
}
520-
}
521508

522509
realName := reader.CurNode
523510
_, as := reader.PeekNode(true)
@@ -657,8 +644,6 @@ func parseExpressionInParenthesis(reader *astutil.NodeReader) ast.Node {
657644
if list, ok := reader.CurNode.(ast.TokenList); ok {
658645
list = parseInfixGroup(astutil.NewNodeReader(list), operatorInfixMatcher, true, parseOperator)
659646
list = parseInfixGroup(astutil.NewNodeReader(list), comparisonInfixMatcher, true, parseComparison)
660-
list = parseInfixGroup(astutil.NewNodeReader(list), aliasInfixMatcher, true, parseAliased)
661-
list = parseInfixGroup(astutil.NewNodeReader(list), identifierListInfixMatcher, true, parseIdentifierList)
662647
return list
663648
}
664649
return reader.CurNode

parser/parser_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,11 +1066,17 @@ func TestParseIdentifierList(t *testing.T) {
10661066
checkFn: func(t *testing.T, stmts []*ast.Statement, input string) {
10671067
testStatement(t, stmts[0], 4, input)
10681068
list := stmts[0].GetTokens()
1069-
testParenthesis(t, list[0], "(foo, bar, foobar)")
1070-
parenthesis := list[0].(*ast.Parenthesis)
1071-
tokens := parenthesis.Inner().GetTokens()
1072-
testIdentifierList(t, tokens[0], "foo, bar, foobar")
1073-
// il := testIdentifierList(t, tokens[0], "foo, bar, foobar")
1069+
1070+
parenthesis1 := testParenthesis(t, list[0], "(foo, bar, foobar)")
1071+
tokens1 := parenthesis1.Inner().GetTokens()
1072+
testIdentifierList(t, tokens1[0], "foo, bar, foobar")
1073+
1074+
testItem(t, list[1], ",")
1075+
testItem(t, list[2], " ")
1076+
1077+
parenthesis2 := testParenthesis(t, list[3], "(fooo, barr, fooobarr)")
1078+
tokens2 := parenthesis2.Inner().GetTokens()
1079+
testIdentifierList(t, tokens2[0], "fooo, barr, fooobarr")
10741080
},
10751081
},
10761082
{

0 commit comments

Comments
 (0)