Better error messages in scanf.

This commit is contained in:
data-man 2018-05-30 23:49:56 +03:00
commit 664b949d54

View file

@ -316,6 +316,9 @@ macro scanf*(input: string; pattern: static[string]; results: varargs[typed]): b
conds.add resLen conds.add resLen
template at(s: string; i: int): char = (if i < s.len: s[i] else: '\0') template at(s: string; i: int): char = (if i < s.len: s[i] else: '\0')
template matchError() =
error("type mismatch between pattern '$" & pattern[p] & "' (position: " & $p & ") and " & $getType(results[i]) &
" var '" & repr(results[i]) & "'")
var i = 0 var i = 0
var p = 0 var p = 0
@ -338,37 +341,37 @@ macro scanf*(input: string; pattern: static[string]; results: varargs[typed]): b
if i < results.len and getType(results[i]).typeKind == ntyString: if i < results.len and getType(results[i]).typeKind == ntyString:
matchBind "parseIdent" matchBind "parseIdent"
else: else:
error("no string var given for $w") matchError
inc i inc i
of 'b': of 'b':
if i < results.len and getType(results[i]).typeKind == ntyInt: if i < results.len and getType(results[i]).typeKind == ntyInt:
matchBind "parseBin" matchBind "parseBin"
else: else:
error("no int var given for $b") matchError
inc i inc i
of 'o': of 'o':
if i < results.len and getType(results[i]).typeKind == ntyInt: if i < results.len and getType(results[i]).typeKind == ntyInt:
matchBind "parseOct" matchBind "parseOct"
else: else:
error("no int var given for $o") matchError
inc i inc i
of 'i': of 'i':
if i < results.len and getType(results[i]).typeKind == ntyInt: if i < results.len and getType(results[i]).typeKind == ntyInt:
matchBind "parseInt" matchBind "parseInt"
else: else:
error("no int var given for $i") matchError
inc i inc i
of 'h': of 'h':
if i < results.len and getType(results[i]).typeKind == ntyInt: if i < results.len and getType(results[i]).typeKind == ntyInt:
matchBind "parseHex" matchBind "parseHex"
else: else:
error("no int var given for $h") matchError
inc i inc i
of 'f': of 'f':
if i < results.len and getType(results[i]).typeKind == ntyFloat: if i < results.len and getType(results[i]).typeKind == ntyFloat:
matchBind "parseFloat" matchBind "parseFloat"
else: else:
error("no float var given for $f") matchError
inc i inc i
of 's': of 's':
conds.add newCall(bindSym"inc", idx, newCall(bindSym"skipWhitespace", inp, idx)) conds.add newCall(bindSym"inc", idx, newCall(bindSym"skipWhitespace", inp, idx))
@ -392,7 +395,7 @@ macro scanf*(input: string; pattern: static[string]; results: varargs[typed]): b
conds.add newCall(bindSym"!=", resLen, newLit min) conds.add newCall(bindSym"!=", resLen, newLit min)
conds.add resLen conds.add resLen
else: else:
error("no string var given for $" & pattern[p]) matchError
inc i inc i
of '{': of '{':
inc p inc p
@ -414,7 +417,7 @@ macro scanf*(input: string; pattern: static[string]; results: varargs[typed]): b
conds.add newCall(bindSym"!=", resLen, newLit 0) conds.add newCall(bindSym"!=", resLen, newLit 0)
conds.add resLen conds.add resLen
else: else:
error("no var given for $" & expr) error("no var given for $" & expr & " (position: " & $p & ")")
inc i inc i
of '[': of '[':
inc p inc p