Implemented regex validation for variables

This commit is contained in:
Martino Ferrari
2026-01-29 15:38:10 +01:00
parent cb79d490e7
commit 8be139ab27
3 changed files with 60 additions and 1 deletions

View File

@@ -137,7 +137,7 @@ func (l *Lexer) NextToken() Token {
return l.emit(TokenLBracket)
case ']':
return l.emit(TokenRBracket)
case '&', '?', '!', '<', '>', '*', '(', ')':
case '&', '?', '!', '<', '>', '*', '(', ')', '~', '%', '^':
return l.emit(TokenSymbol)
case '"':
return l.lexString()

View File

@@ -296,6 +296,12 @@ func (p *Parser) parseVariableDefinition(startTok Token) (Definition, bool) {
break
}
if t.Type == TokenEqual {
if p.peekN(1).Type == TokenSymbol && p.peekN(1).Value == "~" {
p.next()
p.next()
typeTokens = append(typeTokens, Token{Type: TokenSymbol, Value: "=~", Position: t.Position})
continue
}
break
}
typeTokens = append(typeTokens, p.next())

View File

@@ -0,0 +1,53 @@
package integration
import (
"strings"
"testing"
"github.com/marte-community/marte-dev-tools/internal/index"
"github.com/marte-community/marte-dev-tools/internal/parser"
"github.com/marte-community/marte-dev-tools/internal/validator"
)
func TestRegexVariable(t *testing.T) {
content := `
#var IP: string & =~"^[0-9.]+$" = "127.0.0.1"
#var BadIP: string & =~"^[0-9.]+$" = "abc"
+Obj = {
IP = $IP
}
`
// Test Validator
pt := index.NewProjectTree()
p := parser.NewParser(content)
cfg, err := p.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
pt.AddFile("regex.marte", cfg)
v := validator.NewValidator(pt, ".")
v.CheckVariables()
foundError := false
for _, d := range v.Diagnostics {
if strings.Contains(d.Message, "Variable 'BadIP' value mismatch") {
foundError = true
}
}
if !foundError {
t.Error("Expected error for BadIP")
for _, d := range v.Diagnostics {
t.Logf("Diag: %s", d.Message)
}
}
// Test valid variable
for _, d := range v.Diagnostics {
if strings.Contains(d.Message, "Variable 'IP' value mismatch") {
t.Error("Unexpected error for IP")
}
}
}