Skip to content

Commit 503513d

Browse files
committed
parser updates
1 parent bb25ca7 commit 503513d

1 file changed

Lines changed: 41 additions & 17 deletions

File tree

wdl/wdlParser.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ParserError(Exception):
7575
setLevel = 0
7676
constList = []
7777
constNames = []
78+
constMap = {}
7879
paramList = []
7980
paramNames = []
8081
subroutines = []
@@ -259,8 +260,9 @@ def lvds(slot_number):
259260

260261
lvds_chan = None
261262

262-
if found(IDENTIFIER) and token.cargo in Constants:
263-
lvds_chan = str(int(float(Constants[token.cargo])))
263+
# accept IDENTIFIER const or NUMBER
264+
if found(IDENTIFIER) and token.cargo in constMap:
265+
lvds_chan = str(int(float(constMap[token.cargo])))
264266
consume(IDENTIFIER)
265267
elif found(NUMBER):
266268
lvds_chan = token.cargo
@@ -1921,14 +1923,14 @@ def to():
19211923
consume("TO")
19221924
# optional leading minus sign
19231925
if found("-"):
1924-
consume("-")
1926+
consume("-");
19251927
sign = -1.0
19261928
else:
19271929
sign = 1.0
19281930

1929-
# level can be a NUMBER or a defined constant
1930-
if found(IDENTIFIER) and token.cargo in Constants:
1931-
setLevel = str(sign * float(Constants[token.cargo]))
1931+
# NUMBER or const IDENTIFIER
1932+
if found(IDENTIFIER) and token.cargo in constMap:
1933+
setLevel = str(sign * float(constMap[token.cargo]))
19321934
consume(IDENTIFIER)
19331935
elif found(NUMBER):
19341936
setLevel = str(sign * float(token.cargo))
@@ -1953,16 +1955,34 @@ def slew():
19531955
setSlew = __SLEW_NONE
19541956
return
19551957

1956-
# otherwise continue, flag as fast (1) or slow (0)
19571958
consume(",")
1959+
19581960
if found("SLOW"):
1959-
consume("SLOW")
1961+
consume("SLOW");
19601962
setSlew = __SLEW_SLOW
19611963
elif found("FAST"):
1962-
consume("FAST")
1964+
consume("FAST");
19631965
setSlew = __SLEW_FAST
1966+
elif found(IDENTIFIER) and token.cargo in constMap:
1967+
_sv = float(constMap[token.cargo])
1968+
if abs(_sv - 0.0) < 0.5:
1969+
setSlew = __SLEW_SLOW
1970+
elif abs(_sv - 1.0) < 0.5:
1971+
setSlew = __SLEW_FAST
1972+
else:
1973+
error("SLEW constant must be 0 (SLOW) or 1 (FAST)")
1974+
consume(IDENTIFIER)
1975+
elif found(NUMBER):
1976+
_sv = float(token.cargo)
1977+
if abs(_sv - 0.0) < 0.5:
1978+
setSlew = __SLEW_SLOW
1979+
elif abs(_sv - 1.0) < 0.5:
1980+
setSlew = __SLEW_FAST
1981+
else:
1982+
error("SLEW number must be 0 (SLOW) or 1 (FAST)")
1983+
consume(NUMBER)
19641984
else:
1965-
error("SLEW expected SLOW | FAST but got: " + dq(token.cargo))
1985+
error("SLEW expected SLOW|FAST or 0|1 or defined constant")
19661986

19671987

19681988
# -----------------------------------------------------------------------------
@@ -2206,7 +2226,6 @@ def generic_sequence(*sequenceName):
22062226
(found(IDENTIFIER))
22072227
and (token.cargo not in subroutines)
22082228
and (token.cargo not in paramNames)
2209-
and (token.cargo not in Constants)
22102229
):
22112230
error(
22122231
"(wdlParser.py::generic_sequence) undefined symbol "
@@ -2238,18 +2257,17 @@ def generic_sequence(*sequenceName):
22382257
consume(IDENTIFIER)
22392258
consume("(")
22402259
# if next token isn't a closing paren then assume
2241-
# it's a number, param, or constant
2260+
# it's a number or param
22422261
if not found(")"):
22432262
sequence_line += "(" + token.cargo # keep the name; const is emitted earlier
22442263
# validate: allow NUMBER, defined param, or defined constant
22452264
if (
22462265
not found(NUMBER)
22472266
and token.cargo not in paramNames
2248-
and token.cargo not in constNames
22492267
):
22502268
error(
22512269
"(wdlParser.py::generic_sequence) undefined "
2252-
"param or constant " + token.show(align=False)
2270+
"param " + token.show(align=False)
22532271
)
22542272
elif found("RETURN"):
22552273
consume("RETURN")
@@ -2365,15 +2383,21 @@ def const():
23652383

23662384
line = "constant "
23672385
consume("const")
2368-
constNames.append(token.cargo)
2369-
line += token.cargo
2386+
name = token.cargo
2387+
constNames.append(name)
2388+
line += name
23702389
consume(IDENTIFIER)
23712390
line += token.cargo
23722391
consume("=")
2373-
line += token.cargo
2392+
val_str = token.cargo
2393+
line += val_str
23742394
consume(NUMBER)
23752395
constList.append(line)
23762396

2397+
try:
2398+
constMap[name] = float(val_str)
2399+
except ValueError:
2400+
error(f"(wdlParser.py::const) invalid const value for {name}: {val_str}")
23772401

23782402
# -----------------------------------------------------------------------------
23792403
# @fn print

0 commit comments

Comments
 (0)