Skip to content

Commit

Permalink
chore: Remove fixer_v2 Lexer Files (#118)
Browse files Browse the repository at this point in the history
# Description

Remove unnecessary files
  • Loading branch information
notJoon authored Jan 23, 2025
1 parent 52a25ff commit 323382b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 511 deletions.
73 changes: 3 additions & 70 deletions fixer_v2/query/hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,80 +132,13 @@ func ParseHolePattern(pattern string) (*HoleConfig, error) {
return config, nil
}

func (l *Lexer) matchHole() bool {
if l.position+1 >= len(l.input) {
return false
}
startPos := l.position

if l.input[l.position+1] == '[' {
isLongForm := (l.position+2 < len(l.input) && l.input[l.position+2] == '[')
end := l.findHoleEnd(isLongForm)
if end > 0 {
// Check for quantifier
if end < len(l.input) && isQuantifier(l.input[end]) {
end++
}

value := l.input[l.position:end]
config, err := ParseHolePattern(value)
if err != nil {
// If parsing fails, try to extract at least the name and create a basic config
basicName := extractHoleName(value)
basicConfig := HoleConfig{
Name: basicName,
Type: HoleAny,
Quantifier: QuantNone,
}
l.addTokenWithHoleConfig(TokenHole, value, startPos, basicConfig)
} else {
// Create a token with the parsed configuration
l.addTokenWithHoleConfig(TokenHole, value, startPos, *config)
}
l.position = end
return true
}
}
return false
}

func (l *Lexer) addTokenWithHoleConfig(tokenType TokenType, value string, pos int, config HoleConfig) {
l.tokens = append(l.tokens, Token{
Type: tokenType,
Value: value,
Position: pos,
HoleConfig: &config,
})
var quantifiers = map[byte]bool{
'*': true, '+': true, '?': true,
}

// isQuantifier checks if a character is a valid quantifier
func isQuantifier(c byte) bool {
return c == '*' || c == '+' || c == '?'
}

func (l *Lexer) findHoleEnd(isLongForm bool) int {
if isLongForm {
for i := l.position + 3; i < len(l.input)-1; i++ {
if l.input[i] == ']' && l.input[i+1] == ']' {
// Check if there's a quantifier after the closing brackets
if i+2 < len(l.input) && isQuantifier(l.input[i+2]) {
return i + 3
}
return i + 2
}
}
} else {
for i := l.position + 2; i < len(l.input); i++ {
if l.input[i] == ']' {
// Check if there's a quantifier after the closing bracket
if i+1 < len(l.input) && isQuantifier(l.input[i+1]) {
return i + 2
}
return i + 1
}
}
}
return -1
return quantifiers[c]
}

// extractHoleName extracts the hole name from a string like ":[name]" or ":[[name]]".
Expand Down
189 changes: 2 additions & 187 deletions fixer_v2/query/hole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,194 +71,9 @@ func TestParseHolePattern(t *testing.T) {
return
}
if err == nil {
if got.Name != tt.wantConfig.Name {
t.Errorf("Name = %v, want %v", got.Name, tt.wantConfig.Name)
if !got.Equal(*tt.wantConfig) {
t.Errorf("HoleConfig = %v, want %v", got, tt.wantConfig)
}
if got.Type != tt.wantConfig.Type {
t.Errorf("Type = %v, want %v", got.Type, tt.wantConfig.Type)
}
if got.Quantifier != tt.wantConfig.Quantifier {
t.Errorf("Quantifier = %v, want %v", got.Quantifier, tt.wantConfig.Quantifier)
}
}
})
}
}

func TestMatchHoleWithConfig(t *testing.T) {
tests := []struct {
name string
input string
startPos int
wantMatch bool
wantToken Token
wantPos int
}{
{
name: "basic hole",
input: ":[var] rest",
startPos: 0,
wantMatch: true,
wantToken: Token{
Type: TokenHole,
Value: ":[var]",
Position: 0,
HoleConfig: &HoleConfig{
Name: "var",
Type: HoleAny,
Quantifier: QuantNone,
},
},
wantPos: 6,
},
{
name: "typed hole",
input: ":[[expr:expression]] rest",
startPos: 0,
wantMatch: true,
wantToken: Token{
Type: TokenHole,
Value: ":[[expr:expression]]",
Position: 0,
HoleConfig: &HoleConfig{
Name: "expr",
Type: HoleExpression,
Quantifier: QuantNone,
},
},
wantPos: 20,
},
{
name: "hole with quantifier",
input: ":[[stmts:block]]* {",
startPos: 0,
wantMatch: true,
wantToken: Token{
Type: TokenHole,
Value: ":[[stmts:block]]*",
Position: 0,
HoleConfig: &HoleConfig{
Name: "stmts",
Type: HoleBlock,
Quantifier: QuantZeroOrMore,
},
},
wantPos: 17,
},
{
name: "whitespace hole with optional",
input: ":[[ws:whitespace]]? rest",
startPos: 0,
wantMatch: true,
wantToken: Token{
Type: TokenHole,
Value: ":[[ws:whitespace]]?",
Position: 0,
HoleConfig: &HoleConfig{
Name: "ws",
Type: HoleWhitespace,
Quantifier: QuantZeroOrOne,
},
},
wantPos: 19,
},
{
name: "identifier with one or more",
input: ":[[ids:identifier]]+ rest",
startPos: 0,
wantMatch: true,
wantToken: Token{
Type: TokenHole,
Value: ":[[ids:identifier]]+",
Position: 0,
HoleConfig: &HoleConfig{
Name: "ids",
Type: HoleIdentifier,
Quantifier: QuantOneOrMore,
},
},
wantPos: 20,
},
{
name: "invalid hole format",
input: ":[invalid rest",
startPos: 0,
wantMatch: false,
wantPos: 0,
},
{
name: "invalid type",
input: ":[[x:invalid]] rest",
startPos: 0,
wantMatch: true, // should match but create basic hole
wantToken: Token{
Type: TokenHole,
Value: ":[[x:invalid]]",
Position: 0,
HoleConfig: &HoleConfig{
Name: "x:invalid",
Type: HoleAny,
Quantifier: QuantNone,
},
},
wantPos: 14,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := NewLexer(tt.input)
l.position = tt.startPos

got := l.matchHole()
if got != tt.wantMatch {
t.Errorf("matchHole() match = %v, want %v", got, tt.wantMatch)
return
}

if !tt.wantMatch {
return
}

if len(l.tokens) != 1 {
t.Errorf("matchHole() produced %d tokens, want 1", len(l.tokens))
return
}

gotToken := l.tokens[0]

if gotToken.Type != tt.wantToken.Type {
t.Errorf("Token.Type = %v, want %v", gotToken.Type, tt.wantToken.Type)
}
if gotToken.Value != tt.wantToken.Value {
t.Errorf("Token.Value = %v, want %v", gotToken.Value, tt.wantToken.Value)
}
if gotToken.Position != tt.wantToken.Position {
t.Errorf("Token.Position = %v, want %v", gotToken.Position, tt.wantToken.Position)
}

// Compare HoleConfig if present
if tt.wantToken.HoleConfig != nil {
if gotToken.HoleConfig == nil {
t.Errorf("Token.HoleConfig is nil, want %+v", tt.wantToken.HoleConfig)
} else {
if gotToken.HoleConfig.Name != tt.wantToken.HoleConfig.Name {
t.Errorf("HoleConfig.Name = %v, want %v",
gotToken.HoleConfig.Name, tt.wantToken.HoleConfig.Name)
}
if gotToken.HoleConfig.Type != tt.wantToken.HoleConfig.Type {
t.Errorf("HoleConfig.Type = %v, want %v",
gotToken.HoleConfig.Type, tt.wantToken.HoleConfig.Type)
}
if gotToken.HoleConfig.Quantifier != tt.wantToken.HoleConfig.Quantifier {
t.Errorf("HoleConfig.Quantifier = %v, want %v",
gotToken.HoleConfig.Quantifier, tt.wantToken.HoleConfig.Quantifier)
}
}
}

if l.position != tt.wantPos {
t.Errorf("Lexer position = %v, want %v", l.position, tt.wantPos)
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions fixer_v2/query/internal.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package query

import "unicode"

/*
State Transition Machine Design Rationale
Expand Down Expand Up @@ -229,3 +231,8 @@ var identCharTable = [256]bool{
func isIdentChar(c byte) bool {
return identCharTable[c]
}

// isWhitespace checks if the given byte is a space, tab, newline, etc. using unicode.IsSpace.
func isWhitespace(c byte) bool {
return unicode.IsSpace(rune(c))
}
Loading

0 comments on commit 323382b

Please sign in to comment.