# HG changeset patch # User Adam Chlipala # Date 1432823023 14400 # Node ID a49c12ddf47d0b74429f541a7a8185a308d24960 # Parent 265c1aeed71bac6b9dcbd44227caa798ab6ac041 Handling overflow in integer literals (contributed by Gabriel Riba) diff -r 265c1aeed71b -r a49c12ddf47d src/urweb.lex --- a/src/urweb.lex Wed May 20 12:44:28 2015 -0400 +++ b/src/urweb.lex Thu May 28 10:23:43 2015 -0400 @@ -182,7 +182,7 @@ ws = [\ \t\012\r]; intconst = [0-9]+; realconst = [0-9]+\.[0-9]*; -hexconst = 0x[0-9A-F]{1,8}; +hexconst = 0x[0-9A-F]+; notags = ([^<{\n(]|(\([^\*<{\n]))+; xcom = ([^\-]|(-[^\-]))+; oint = [0-9][0-9][0-9]; @@ -542,17 +542,25 @@ {id} => (Tokens.SYMBOL (yytext, pos yypos, pos yypos + size yytext)); {cid} => (Tokens.CSYMBOL (yytext, pos yypos, pos yypos + size yytext)); - {hexconst} => (case StringCvt.scanString (Int64.scan StringCvt.HEX) (String.extract (yytext, 2, NONE)) of + {hexconst} => (let val digits = String.extract (yytext, 2, NONE) + val v = (StringCvt.scanString (Int64.scan StringCvt.HEX) digits) + handle Overflow => NONE + in + case v of SOME x => Tokens.INT (x, pos yypos, pos yypos + size yytext) | NONE => (ErrorMsg.errorAt' (pos yypos, pos yypos) ("Expected hexInt, received: " ^ yytext); - continue ())); + continue ()) + end); - {intconst} => (case Int64.fromString yytext of + {intconst} => (let val v = (Int64.fromString yytext) handle Overflow => NONE + in + case v of SOME x => Tokens.INT (x, pos yypos, pos yypos + size yytext) | NONE => (ErrorMsg.errorAt' (pos yypos, pos yypos) ("Expected int, received: " ^ yytext); - continue ())); + continue ()) + end); {realconst} => (case Real64.fromString yytext of SOME x => Tokens.FLOAT (x, pos yypos, pos yypos + size yytext) | NONE => (ErrorMsg.errorAt' (pos yypos, pos yypos)