Skip to content

Commit

Permalink
Allow underscores in numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jun 3, 2012
1 parent e3785dd commit b3234a7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/lexer.l
Expand Up @@ -58,10 +58,11 @@ STRING \"([^\"]|\"\")*\"
BIT_STRING [BOXbox]\"[0-9a-fA-f_]+\"
CHAR '.'
COMMENT --.*\n
INT [0-9]+{EXPONENT}?
DIGITS [0-9][0-9_]*
INT {DIGITS}{EXPONENT}?
REAL {INT}\.{INT}
EXPONENT [Ee][+-]?[0-9]+
BASED_INT {INT}#[0-9a-fA-f]+#
EXPONENT [Ee][+-]?{DIGITS}
BASED_INT {INT}#[0-9a-fA-f][0-9a-fA-f_]*#
SPACE [ \t\r\n]+
TICK \'

Expand Down Expand Up @@ -347,9 +348,19 @@ static int parse_ex_id(const char *str)
TOKEN(tID);
}

static void strip_underscores(char *s)
{
char *p;
for (p = s; *s != '\0'; s++)
if (*s != '_')
*p++ = *s;
*p = '\0';
}

static int parse_int(const char *str)
{
char *tmp = strdup(str);
strip_underscores(tmp);

char *base = strtok(tmp, "eE");
char *exp = strtok(NULL, "eE");
Expand All @@ -373,6 +384,7 @@ static int parse_int(const char *str)
static int parse_based_int(const char *str)
{
char *tmp = strdup(str);
strip_underscores(tmp);

char *base = strtok(tmp, "#");
char *value = strtok(NULL, "#");
Expand All @@ -389,6 +401,7 @@ static int parse_based_int(const char *str)
static int parse_real(const char *str)
{
char *tmp = strdup(str);
strip_underscores(tmp);

char *base = strtok(tmp, "eE");
char *exp = strtok(NULL, "eE");
Expand Down
1 change: 1 addition & 0 deletions test/parse/based.vhd
Expand Up @@ -5,5 +5,6 @@ package p is
constant c : integer := 8#7#;
constant d : integer := 10#1234#;
constant e : integer := 16#beef01#;
constant f : integer := 2#1_0#;

end package;
2 changes: 2 additions & 0 deletions test/parse/literal.vhd
Expand Up @@ -9,6 +9,8 @@ architecture a of e is
constant f : real := 0.21712;
constant g : real := 1.4e6;
constant h : real := 235.1e-2;
constant i : integer := 1_2_3_4;
constant j : real := 5_6_7.12_3;
begin

end architecture;
21 changes: 20 additions & 1 deletion test/test_parse.c
Expand Up @@ -662,7 +662,7 @@ START_TEST(test_literal)
a = parse();
fail_if(a == NULL);
fail_unless(tree_kind(a) == T_ARCH);
fail_unless(tree_decls(a) == 10);
fail_unless(tree_decls(a) == 12);

d = tree_decl(a, 0);
fail_unless(tree_ident(d) == ident_new("POS"));
Expand Down Expand Up @@ -743,6 +743,22 @@ START_TEST(test_literal)
fail_unless(l.kind == L_REAL);
fail_unless(l.r == 2.351);

d = tree_decl(a, 10);
fail_unless(tree_ident(d) == ident_new("I"));
v = tree_value(d);
fail_unless(tree_kind(v) == T_LITERAL);
l = tree_literal(v);
fail_unless(l.kind == L_INT);
fail_unless(l.i == 1234);

d = tree_decl(a, 11);
fail_unless(tree_ident(d) == ident_new("J"));
v = tree_value(d);
fail_unless(tree_kind(v) == T_LITERAL);
l = tree_literal(v);
fail_unless(l.kind == L_REAL);
fail_unless(l.r == 567.123);

a = parse();
fail_unless(a == NULL);

Expand Down Expand Up @@ -1384,6 +1400,9 @@ START_TEST(test_based)
d = tree_decl(p, 4);
fail_unless(tree_literal(tree_value(d)).i == 0xbeef01);

d = tree_decl(p, 5);
fail_unless(tree_literal(tree_value(d)).i == 2);

p = parse();
fail_unless(p == NULL);

Expand Down

0 comments on commit b3234a7

Please sign in to comment.