introduce PsycSyntaxError and error messages

This commit is contained in:
lurchi 2018-08-22 19:49:17 +02:00
parent 87674ac5af
commit eaeed9a16d
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 24 additions and 11 deletions

View File

@ -27,6 +27,8 @@ type
cursor: int
remainingPartLen: int
PsycSyntaxError = object of Exception
Validator = proc(c: char): bool
proc validateTrue(c: char): bool = true
@ -41,10 +43,13 @@ proc getUntil(input: string, delimiters: openArray[char], packet: var PsycPacket
result.complete = true
if i > packet.cursor:
result.value = input[packet.cursor .. i - 1]
else:
result.value = ""
packet.cursor = i
break
if not validate(input[i]):
raise new(ValueError)
raise newException(PsycSyntaxError,
"invalid character '" & input[i] & "' in " & $packet.state)
proc parseKeyword(input: string,
packet: var PsycPacket): tuple[complete: bool, value: string] =
@ -53,8 +58,8 @@ proc parseKeyword(input: string,
result = input.getUntil([' ', '\t', '\n'], packet, validate)
if result.complete:
packet.cursor.inc()
if result.value.isNil() or result.value.len() == 0 or result.value[0] != '_':
raise new(ValueError) # name does not start with '_'
if result.value.len() == 0 or result.value[0] != '_':
raise newException(PsycSyntaxError, $packet.state & " does not start with '_'")
proc parseModifierValueLength(input: string,
packet: var PsycPacket): tuple[complete: bool,
@ -64,10 +69,13 @@ proc parseModifierValueLength(input: string,
return (false, -1)
packet.cursor.inc()
if value.len() == 0:
raise new(ValueError) # no value length
result = (true, value.parseInt()) # may throw ValueError if invalid number
raise newException(PsycSyntaxError, "missing length after " & $packet.state)
try:
result = (true, value.parseInt()) # may throw ValueError if invalid number
except ValueError:
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
if result.value < 0:
raise new(ValueError) # negative value length
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
proc parseModifierName(input: string,
packet: var PsycPacket): tuple[complete: bool,
@ -84,7 +92,8 @@ proc parseModifierName(input: string,
packet.cursor.inc()
if result.op == '?':
if input[packet.cursor] != '\n':
raise new(ValueError) # '?' must be on a line by itself
raise newException(PsycSyntaxError,
$packet.state & " has invalid operator '?'")
result.complete = true
return
(result.complete, result.name) = parseKeyword(input, packet)
@ -96,7 +105,7 @@ proc parseModifierName(input: string,
(result.complete, packet.remainingPartLen) = parseModifierValueLength(input,
packet)
elif input[packet.cursor - 1] != '\t':
raise new(ValueError) # invalid separator
raise newException(PsycSyntaxError, $packet.state & " has invalid separator")
proc parseMethod(input: string, packet: var PsycPacket): tuple[complete: bool,
value: string] =
@ -126,7 +135,8 @@ proc parseModifierValue(input: string,
if result.complete:
result.value = input[packet.cursor .. packet.cursor + packet.remainingPartLen - 1]
if input[packet.cursor + packet.remainingPartLen] != '\n':
raise new(ValueError) # missing '\n' after binary-arg
raise newException(PsycSyntaxError,
$packet.state & " is missing '\n' after binary-arg")
packet.cursor += result.value.len() + 1
packet.remainingPartLen = -1
else:
@ -144,9 +154,12 @@ proc parseContentLength(input: string,
if value.len() == 0:
result.value = -1
else:
result.value = value.parseInt() # parseInt may throw ValueError if invalid number
try:
result.value = value.parseInt() # parseInt may throw ValueError if invalid number
except ValueError:
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
if result.value < 0:
raise new(ValueError) # negative content length
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
proc parseData(input: string,
packet: var PsycPacket): tuple[complete: bool, value: string] =