Implemented invalid character checks
This commit is contained in:
parent
6852cbb24e
commit
3664e74074
@ -22,6 +22,9 @@ func TestErrors(t *testing.T) {
|
|||||||
{"InvalidInstructionNumber.q", &errors.InvalidInstruction{Instruction: "123"}},
|
{"InvalidInstructionNumber.q", &errors.InvalidInstruction{Instruction: "123"}},
|
||||||
{"InvalidExpression.q", errors.InvalidExpression},
|
{"InvalidExpression.q", errors.InvalidExpression},
|
||||||
{"InvalidOperator.q", &errors.InvalidOperator{Operator: "+++"}},
|
{"InvalidOperator.q", &errors.InvalidOperator{Operator: "+++"}},
|
||||||
|
{"InvalidCharacter.q", &errors.InvalidCharacter{Character: "@"}},
|
||||||
|
{"InvalidCharacter2.q", &errors.InvalidCharacter{Character: "@"}},
|
||||||
|
{"InvalidCharacter3.q", &errors.InvalidCharacter{Character: "@"}},
|
||||||
{"MissingAssignValue.q", errors.MissingAssignValue},
|
{"MissingAssignValue.q", errors.MissingAssignValue},
|
||||||
{"MissingBlockEnd.q", errors.MissingBlockEnd},
|
{"MissingBlockEnd.q", errors.MissingBlockEnd},
|
||||||
{"MissingBlockStart.q", errors.MissingBlockStart},
|
{"MissingBlockStart.q", errors.MissingBlockStart},
|
||||||
|
@ -119,6 +119,10 @@ func scanFile(path string, functions chan<- *Function) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tokens[i].Kind == token.Invalid {
|
||||||
|
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||||
|
}
|
||||||
|
|
||||||
if tokens[i].Kind == token.EOF {
|
if tokens[i].Kind == token.EOF {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -156,6 +160,10 @@ func scanFile(path string, functions chan<- *Function) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tokens[i].Kind == token.Invalid {
|
||||||
|
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||||
|
}
|
||||||
|
|
||||||
if tokens[i].Kind == token.EOF {
|
if tokens[i].Kind == token.EOF {
|
||||||
if groupLevel > 0 {
|
if groupLevel > 0 {
|
||||||
return errors.New(errors.MissingGroupEnd, file, tokens[i].Position)
|
return errors.New(errors.MissingGroupEnd, file, tokens[i].Position)
|
||||||
@ -204,6 +212,10 @@ func scanFile(path string, functions chan<- *Function) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tokens[i].Kind == token.Invalid {
|
||||||
|
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||||
|
}
|
||||||
|
|
||||||
if tokens[i].Kind == token.EOF {
|
if tokens[i].Kind == token.EOF {
|
||||||
if blockLevel > 0 {
|
if blockLevel > 0 {
|
||||||
return errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
|
return errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
|
||||||
|
@ -22,6 +22,8 @@ func Tokenize(buffer []byte) List {
|
|||||||
|
|
||||||
for i < len(buffer) {
|
for i < len(buffer) {
|
||||||
switch buffer[i] {
|
switch buffer[i] {
|
||||||
|
// Whitespace
|
||||||
|
case ' ', '\t':
|
||||||
// Texts
|
// Texts
|
||||||
case '"':
|
case '"':
|
||||||
start := i
|
start := i
|
||||||
@ -118,6 +120,9 @@ func Tokenize(buffer []byte) List {
|
|||||||
tokens = append(tokens, Token{Operator, position, buffer[position:i]})
|
tokens = append(tokens, Token{Operator, position, buffer[position:i]})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalid characters
|
||||||
|
tokens = append(tokens, Token{Invalid, i, buffer[i : i+1]})
|
||||||
}
|
}
|
||||||
|
|
||||||
i++
|
i++
|
||||||
|
13
src/errors/InvalidCharacter.go
Normal file
13
src/errors/InvalidCharacter.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// InvalidCharacter error is created when an invalid character appears.
|
||||||
|
type InvalidCharacter struct {
|
||||||
|
Character string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error generates the string representation.
|
||||||
|
func (err *InvalidCharacter) Error() string {
|
||||||
|
return fmt.Sprintf("Invalid character '%s'", err.Character)
|
||||||
|
}
|
1
tests/errors/InvalidCharacter.q
Normal file
1
tests/errors/InvalidCharacter.q
Normal file
@ -0,0 +1 @@
|
|||||||
|
@
|
1
tests/errors/InvalidCharacter2.q
Normal file
1
tests/errors/InvalidCharacter2.q
Normal file
@ -0,0 +1 @@
|
|||||||
|
main@
|
1
tests/errors/InvalidCharacter3.q
Normal file
1
tests/errors/InvalidCharacter3.q
Normal file
@ -0,0 +1 @@
|
|||||||
|
main()@
|
Loading…
Reference in New Issue
Block a user