enhanced ending operator detection

This commit is contained in:
Celestino Amoroso 2026-05-02 09:54:24 +02:00
parent 6c5e9db34b
commit 3ac8cab275
2 changed files with 90 additions and 4 deletions

View File

@ -182,21 +182,49 @@ func StringEndsWithOperator(s string) bool {
return endingOperator(s) != SymNone return endingOperator(s) != SymNone
} }
// func endingOperator(s string) (sym Symbol) {
// var matchLength = 0
// sym = SymNone
// lower := strings.TrimRight(strings.ToLower(s), " \t")
// for symbol, spec := range symbolMap {
// if strings.HasSuffix(lower, spec.repr) {
// if len(spec.repr) > matchLength {
// matchLength = len(spec.repr)
// if spec.kind == symClassOperator && (spec.opType == posInfix || spec.opType == posPrefix) {
// sym = symbol
// } else {
// sym = SymNone
// }
// }
// }
// }
// return
// }
func endingOperator(s string) (sym Symbol) { func endingOperator(s string) (sym Symbol) {
var matchLength = 0 var matchLength = 0
var repr string
sym = SymNone sym = SymNone
lower := strings.TrimRight(strings.ToLower(s), " \t") lower := strings.TrimRight(strings.ToLower(s), " \t")
for symbol, spec := range symbolMap { for symbol, spec := range symbolMap {
if len(spec.repr) > matchLength || repr == spec.repr {
if strings.HasSuffix(lower, spec.repr) { if strings.HasSuffix(lower, spec.repr) {
if len(spec.repr) > matchLength { if isNotEndingSymbol(spec) && repr != spec.repr {
repr = spec.repr
matchLength = len(spec.repr) matchLength = len(spec.repr)
if spec.kind == symClassOperator && (spec.opType == posInfix || spec.opType == posPrefix) {
sym = symbol sym = symbol
} else { } else {
sym = SymNone sym = SymNone
break
// matchLength = 0
} }
} }
} }
} }
return return
} }
func isNotEndingSymbol(spec symbolSpec) bool {
return (spec.kind == symClassOperator && (spec.opType == posInfix || spec.opType == posPrefix)) ||
(spec.kind == symClassParenthesis && spec.opType == posPrefix)
}

58
t_symbol_test.go Normal file
View File

@ -0,0 +1,58 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// t_symbol_test.go
package expr
import (
"testing"
"git.portale-stac.it/go-pkg/expr/kern"
)
func TestOperatorEnding(t *testing.T) {
section := "Symbol"
inputs := []inputType{
/* 1 */ {`a++`, false, nil},
/* 2 */ {`a;`, true, nil},
/* 3 */ {`a +`, true, nil},
/* 4 */ {`a + 1`, false, nil},
/* 5 */ {`a - `, true, nil},
/* 6 */ {`a( `, true, nil},
/* 7 */ {`a) `, false, nil},
/* 8 */ {`++a `, false, nil},
}
// testStringArrayEndingWithOperatorSpec(t, section, inputs, 6)
testStringArrayEndingWithOperator(t, section, inputs)
}
func testStringEndingWithOperator(source string) bool {
return StringEndsWithOperator(source)
}
func testStringArrayEndingWithOperatorSpec(t *testing.T, section string, inputs []inputType, spec ...int) {
for i := range spec {
if spec[i] < 1 || spec[i] > len(inputs) {
t.Errorf("Invalid test spec index: %d (must be between 1 and %d)", spec[i], len(inputs))
continue
}
doEndingTest(t, section, inputs[spec[i]-1], spec[i]-1)
}
}
func testStringArrayEndingWithOperator(t *testing.T, section string, inputs []inputType) {
for i, input := range inputs {
doEndingTest(t, section, input, i)
}
}
func doEndingTest(t *testing.T, section string, input inputType, i int) {
wantErr := getWantedError(&input)
logTest(t, i+1, section, input.source, input.wantResult, wantErr)
gotResult := testStringEndingWithOperator(input.source)
t.Logf("Is %s op ending? %v", input.source, gotResult)
if gotResult != input.wantResult.(bool) {
t.Errorf("%d: `%s` -> result = %v [%s], want = %v [%s]", i+1, input.source, gotResult, kern.TypeName(gotResult), input.wantResult, kern.TypeName(input.wantResult))
}
}