Skip to content

Commit

Permalink
Lexer: Support port specifications like [0-9].
Browse files Browse the repository at this point in the history
Same as [0,1,2,3,4,5,6,7,8,9].
  • Loading branch information
kohler committed Dec 6, 2016
1 parent 4092bd5 commit 8cbdf72
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 3 additions & 1 deletion doc/click.5
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ can more concisely be written like this, using the `=>' many-to-many connector:
.Rs
.IR c " [0], " c " [1], " c " [2] => Paint(0), Paint(1), Paint(2) -> " next ";"
.Re
or, even more concisely, either of the following:
or, even more concisely, any of the following:
.Rs
.IR c " [0,1,2] => Paint(0), Paint(1), Paint(2) -> " next ";"
.br
.IR c " [0-2] => Paint(0), Paint(1), Paint(2) -> " next ";"
.br
.IR c " => Paint(0), Paint(1), Paint(2) -> " next ";"
.Re
Each many-to-many connection must list the same number of output ports (on the left) as input ports (on the right).
Expand Down
18 changes: 15 additions & 3 deletions lib/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Copyright (c) 2004-2011 Regents of the University of California
* Copyright (c) 2008-2012 Meraki, Inc.
* Copyright (c) 2010 Intel Corporation
* Copyright (c) 2012 Eddie Kohler
* Copyright (c) 2012-2016 Eddie Kohler
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -1196,6 +1196,8 @@ Lexer::yport(bool isoutput)
{
if (!expect('[', true))
return;
int last_port = -1;
bool dash = false;

while (1) {
Lexeme t = lex();
Expand All @@ -1205,7 +1207,13 @@ Lexer::yport(bool isoutput)
lerror("syntax error: port number should be integer");
port = 0;
}
_ps->push_back_port(isoutput, port);
if (dash) {
for (++last_port; last_port <= port; ++last_port)
_ps->push_back_port(isoutput, last_port);
} else {
_ps->push_back_port(isoutput, port);
last_port = port;
}
} else if (t.is(']')) {
if (_ps->nports(isoutput) == 0)
_ps->push_back_port(isoutput, 0);
Expand All @@ -1220,7 +1228,11 @@ Lexer::yport(bool isoutput)
t = lex();
if (t.is(']'))
break;
else if (!t.is(',')) {
else if (t.is('-') && !dash)
dash = true;
else if (t.is(','))
dash = false;
else {
lerror("syntax error: expected %<,%>");
unlex(t);
}
Expand Down
16 changes: 14 additions & 2 deletions tools/lib/lexert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ LexerT::yport(Vector<int> &ports, const char *pos[2])
}

int nports = ports.size();
int last_port = -1;
bool dash = false;
while (1) {
Lexeme t = lex();
if (t.is(lexIdent)) {
Expand All @@ -610,7 +612,13 @@ LexerT::yport(Vector<int> &ports, const char *pos[2])
lerror(t, "syntax error: port number should be integer");
port = 0;
}
ports.push_back(port);
if (dash) {
for (++last_port; last_port <= port; ++last_port)
ports.push_back(last_port);
} else {
ports.push_back(port);
last_port = port;
}
} else if (t.is(']')) {
if (nports == ports.size())
ports.push_back(0);
Expand All @@ -627,7 +635,11 @@ LexerT::yport(Vector<int> &ports, const char *pos[2])
if (t.is(']')) {
pos[1] = t.pos2();
return true;
} else if (!t.is(',')) {
} else if (t.is('-') && !dash)
dash = true;
else if (t.is(','))
dash = false;
else {
lerror(t, "syntax error: expected ','");
unlex(t);
}
Expand Down

0 comments on commit 8cbdf72

Please sign in to comment.