Improved scanner test coverage
All checks were successful
/ test (push) Successful in 14s

This commit is contained in:
Eduard Urbach 2025-06-22 14:35:49 +02:00
parent 320e023d74
commit ca91cd693c
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
18 changed files with 64 additions and 61 deletions

View file

@ -15,12 +15,14 @@ var errs = []struct {
ExpectedError error ExpectedError error
}{ }{
{"ExpectedFunctionDefinition.q", scanner.ExpectedFunctionDefinition}, {"ExpectedFunctionDefinition.q", scanner.ExpectedFunctionDefinition},
{"ExpectedFunctionDefinition2.q", scanner.ExpectedFunctionDefinition},
{"ExpectedPackageName.q", scanner.ExpectedPackageName}, {"ExpectedPackageName.q", scanner.ExpectedPackageName},
{"InvalidCharacter.q", &scanner.InvalidCharacter{Character: "@"}}, {"InvalidCharacter.q", &scanner.InvalidCharacter{Character: "@"}},
{"InvalidCharacter2.q", &scanner.InvalidCharacter{Character: "@"}}, {"InvalidCharacter2.q", &scanner.InvalidCharacter{Character: "@"}},
{"InvalidCharacter3.q", &scanner.InvalidCharacter{Character: "@"}}, {"InvalidCharacter3.q", &scanner.InvalidCharacter{Character: "@"}},
{"InvalidCharacter4.q", &scanner.InvalidCharacter{Character: "+++"}}, {"InvalidCharacter4.q", &scanner.InvalidCharacter{Character: "+++"}},
{"InvalidFunctionDefinition.q", scanner.InvalidFunctionDefinition}, {"InvalidFunctionDefinition.q", scanner.InvalidFunctionDefinition},
{"InvalidFunctionDefinition2.q", scanner.InvalidFunctionDefinition},
{"InvalidTopLevel.q", &scanner.InvalidTopLevel{Instruction: "123"}}, {"InvalidTopLevel.q", &scanner.InvalidTopLevel{Instruction: "123"}},
{"InvalidTopLevel2.q", &scanner.InvalidTopLevel{Instruction: "\"Hello\""}}, {"InvalidTopLevel2.q", &scanner.InvalidTopLevel{Instruction: "\"Hello\""}},
{"InvalidTopLevel3.q", &scanner.InvalidTopLevel{Instruction: "+"}}, {"InvalidTopLevel3.q", &scanner.InvalidTopLevel{Instruction: "+"}},
@ -28,6 +30,7 @@ var errs = []struct {
{"MissingBlockEnd2.q", scanner.MissingBlockEnd}, {"MissingBlockEnd2.q", scanner.MissingBlockEnd},
{"MissingBlockStart.q", scanner.MissingBlockStart}, {"MissingBlockStart.q", scanner.MissingBlockStart},
{"MissingGroupEnd.q", scanner.MissingGroupEnd}, {"MissingGroupEnd.q", scanner.MissingGroupEnd},
{"MissingGroupEnd2.q", scanner.MissingGroupEnd},
{"MissingGroupStart.q", scanner.MissingGroupStart}, {"MissingGroupStart.q", scanner.MissingGroupStart},
{"MissingParameter.q", scanner.MissingParameter}, {"MissingParameter.q", scanner.MissingParameter},
{"MissingParameter2.q", scanner.MissingParameter}, {"MissingParameter2.q", scanner.MissingParameter},

View file

@ -56,13 +56,9 @@ func (s *scanner) scanFunction(file *fs.File, tokens token.List, i int) (int, er
return i, errors.New(MissingBlockEnd, file, tokens[i].Position) return i, errors.New(MissingBlockEnd, file, tokens[i].Position)
} }
if bodyStart == -1 {
return i, errors.New(ExpectedFunctionDefinition, file, tokens[i].Position) return i, errors.New(ExpectedFunctionDefinition, file, tokens[i].Position)
} }
return i, nil
}
if blockLevel > 0 { if blockLevel > 0 {
i++ i++
continue continue

View file

@ -12,10 +12,10 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
var ( var (
groupLevel = 0 groupLevel = 0
nameStart = i nameStart = i
paramsStart = -1 inputStart = -1
paramsEnd = -1 inputEnd = -1
typeStart = -1 outputStart = -1
typeEnd = -1 outputEnd = -1
) )
i++ i++
@ -27,7 +27,7 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
i++ i++
if groupLevel == 1 { if groupLevel == 1 {
paramsStart = i inputStart = i
} }
continue continue
@ -41,7 +41,7 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
} }
if groupLevel == 0 { if groupLevel == 0 {
paramsEnd = i inputEnd = i
i++ i++
break break
} }
@ -59,10 +59,6 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
return nil, i, errors.New(MissingGroupEnd, file, tokens[i].Position) return nil, i, errors.New(MissingGroupEnd, file, tokens[i].Position)
} }
if paramsStart == -1 {
return nil, i, errors.New(InvalidFunctionDefinition, file, tokens[i].Position)
}
return nil, i, errors.New(InvalidFunctionDefinition, file, tokens[i].Position) return nil, i, errors.New(InvalidFunctionDefinition, file, tokens[i].Position)
} }
@ -76,18 +72,18 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
// Return type // Return type
if i < len(tokens) && tokens[i].Kind == token.ReturnType { if i < len(tokens) && tokens[i].Kind == token.ReturnType {
typeStart = i + 1 outputStart = i + 1
for i < len(tokens) && tokens[i].Kind != delimiter { for i < len(tokens) && tokens[i].Kind != delimiter {
i++ i++
} }
typeEnd = i outputEnd = i
} }
name := tokens[nameStart].String(file.Bytes) name := tokens[nameStart].String(file.Bytes)
function := core.NewFunction(name, file) function := core.NewFunction(name, file)
parameters := tokens[paramsStart:paramsEnd] parameters := tokens[inputStart:inputEnd]
for param := range parameters.Split { for param := range parameters.Split {
if len(param) == 0 { if len(param) == 0 {
@ -104,19 +100,26 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
}) })
} }
if typeStart != -1 { if outputStart == -1 {
if tokens[typeStart].Kind == token.GroupStart && tokens[typeEnd-1].Kind == token.GroupEnd { return function, i, nil
typeStart++
typeEnd--
} }
outputTokens := tokens[typeStart:typeEnd] if tokens[outputStart].Kind == token.GroupStart {
if tokens[outputEnd-1].Kind == token.GroupEnd {
outputStart++
outputEnd--
} else {
return nil, i, errors.New(MissingGroupEnd, file, tokens[outputEnd-1].Position)
}
}
outputTokens := tokens[outputStart:outputEnd]
if len(outputTokens) == 0 { if len(outputTokens) == 0 {
return nil, i, errors.New(MissingParameter, file, tokens[typeStart].Position) return nil, i, errors.New(MissingParameter, file, tokens[outputStart].Position)
} }
errorPos := token.Position(typeStart) errorPos := token.Position(outputStart)
for param := range outputTokens.Split { for param := range outputTokens.Split {
if len(param) == 0 { if len(param) == 0 {
@ -137,7 +140,5 @@ func scanSignature(file *fs.File, tokens token.List, i int, delimiter token.Kind
errorPos = param[len(param)-1].End() + 1 errorPos = param[len(param)-1].End() + 1
} }
}
return function, i, nil return function, i, nil
} }

View file

@ -1 +1 @@
main() f()

View file

@ -0,0 +1 @@
f() 123

View file

@ -1 +1 @@
main@ f@

View file

@ -1,4 +1,4 @@
main() { f() {
x := 123 +++ 456 x := 123 +++ 456
syscall(60, x) syscall(60, x)
} }

View file

@ -1,3 +1,3 @@
main() { f() {
if 42 {} if 42 {}
} }

View file

@ -1,3 +1,3 @@
main() { f() {
syscall(+, -, *, /) syscall(+, -, *, /)
} }

View file

@ -1 +1 @@
main f

View file

@ -0,0 +1 @@
f 123

View file

@ -1 +1 @@
main(){ f(){

View file

@ -1,3 +1,3 @@
main() { f() {
loop { loop {
} }

View file

@ -1 +1 @@
main()} f()}

View file

@ -1 +1 @@
main( f(

View file

@ -0,0 +1 @@
f() -> (

View file

@ -1 +1 @@
main) f)

View file

@ -1 +1 @@
f(,) {} f(a (int)->(int),) {}