Skip to content

Commit 1541cf1

Browse files
committed
Parse a new style of parameter syntax
See: #265 (comment)
1 parent 347b484 commit 1541cf1

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

src/main/java/org/scijava/script/process/ParameterScriptProcessor.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,24 @@
6161
* supported:
6262
* </p>
6363
* <ul>
64-
* <li>{@code // @<type> <varName>}</li>
65-
* <li>{@code // @<type>(<attr1>=<value1>, ..., <attrN>=<valueN>) <varName>}
66-
* </li>
67-
* <li>{@code // @<IOType> <type> <varName>}</li>
68-
* <li>{@code // @<IOType>(<attr1>=<value1>, ..., <attrN>=<valueN>) <type>
64+
* <li>{@code #@<type> <varName>}</li>
65+
* <li>{@code #@<type>(<attr1>=<value1>, ..., <attrN>=<valueN>) <varName>}</li>
66+
* <li>{@code #@<IOType> <type> <varName>}</li>
67+
* <li>{@code #@<IOType>(<attr1>=<value1>, ..., <attrN>=<valueN>) <type>
6968
* <varName>}</li>
7069
* </ul>
7170
* <p>
7271
* Where:
7372
* </p>
7473
* <ul>
75-
* <li>{@code //} = the comment style of the scripting language, so that the
74+
* <li>{@code #@} - signals a special script processing instruction, so that the
7675
* parameter line is ignored by the script engine itself.</li>
77-
* <li>{@code <IOType>} = one of {@code INPUT}, {@code OUTPUT}, or {@code BOTH}.
76+
* <li>{@code <IOType>} - one of {@code INPUT}, {@code OUTPUT}, or {@code BOTH}.
7877
* </li>
79-
* <li>{@code <varName>} = the name of the input or output variable.</li>
80-
* <li>{@code <type>} = the Java {@link Class} of the variable.</li>
81-
* <li>{@code <attr*>} = an attribute key.</li>
82-
* <li>{@code <value*>} = an attribute value.</li>
78+
* <li>{@code <varName>} - the name of the input or output variable.</li>
79+
* <li>{@code <type>} - the Java {@link Class} of the variable.</li>
80+
* <li>{@code <attr*>} - an attribute key.</li>
81+
* <li>{@code <value*>} - an attribute value.</li>
8382
* </ul>
8483
* <p>
8584
* See the @{@link Parameter} annotation for a list of valid attributes.
@@ -88,10 +87,10 @@
8887
* Here are a few examples:
8988
* </p>
9089
* <ul>
91-
* <li>{@code // @Dataset dataset}</li>
92-
* <li>{@code // @double(type=OUTPUT) result}</li>
93-
* <li>{@code // @BOTH ImageDisplay display}</li>
94-
* <li>{@code // @INPUT(persist=false, visibility=INVISIBLE) boolean verbose}
90+
* <li>{@code #@Dataset dataset}</li>
91+
* <li>{@code #@double(type=OUTPUT) result}</li>
92+
* <li>{@code #@BOTH ImageDisplay display}</li>
93+
* <li>{@code #@INPUT(persist=false, visibility=INVISIBLE) boolean verbose}
9594
* </li>
9695
* </ul>
9796
* <p>
@@ -129,6 +128,14 @@ public void begin(final ScriptInfo scriptInfo) {
129128

130129
@Override
131130
public void process(final String line) {
131+
// parse new-style parameters starting with @# anywhere in the script.
132+
if (line.matches("^#@.*")) {
133+
final int at = line.indexOf('@');
134+
parseParam(line.substring(at + 1));
135+
return;
136+
}
137+
138+
// parse old-style parameters in the initial script header
132139
if (header) {
133140
// NB: Check if line contains an '@' with no prior alphameric
134141
// characters. This assumes that only non-alphanumeric characters can
@@ -170,10 +177,11 @@ private void parseParam(final String param, final Map<String, Object> attrs) {
170177
final String[] tokens = param.trim().split("[ \t\n]+");
171178
if (tokens.length < 1) { warnInvalid(param); return; }
172179
final String typeName, varName;
173-
if (isIOType(tokens[0])) {
180+
final String maybeIOType = tokens[0].toUpperCase();
181+
if (isIOType(maybeIOType)) {
174182
// assume syntax: <IOType> <type> <varName>
175183
if (tokens.length < 3) { warnInvalid(param); return; }
176-
attrs.put("type", tokens[0]);
184+
attrs.put("type", maybeIOType);
177185
typeName = tokens[1];
178186
varName = tokens[2];
179187
}
@@ -205,7 +213,7 @@ private Map<String, Object> parseAttrs(final String attrs) {
205213
}
206214

207215
private boolean isIOType(final String token) {
208-
return convertService.convert(token, ItemIO.class) != null;
216+
return convertService.convert(token.toUpperCase(), ItemIO.class) != null;
209217
}
210218

211219
private void warnInvalid(final String param) {

src/test/java/org/scijava/script/ScriptInfoTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,41 @@ public static void tearDown() {
8787

8888
// -- Tests --
8989

90+
/** Tests whether new-style parameter syntax are parsed correctly. */
91+
@Test
92+
public void testNewStyle() throws Exception {
93+
final String script = "" + //
94+
"##########\n" + //
95+
"# Inputs #\n" + //
96+
"##########\n" + //
97+
"#@input int stuff\n" + //
98+
"#@input int things\n" + //
99+
"\n" + //
100+
"###########\n" + //
101+
"# Credits #\n" + //
102+
"###########\n" + //
103+
"Brought to you by:\n" + //
104+
"person@example.com\n" + //
105+
"\n" + //
106+
"###########\n" + //
107+
"# Outputs #\n" + //
108+
"###########\n" + //
109+
"#@output String blackHoles\n" +
110+
"#@output String revelations\n" +
111+
"\n" + //
112+
"THE END!\n";
113+
final ScriptModule scriptModule =
114+
scriptService.run("newStyle.bsizes", script, true).get();
115+
116+
final Object output = scriptModule.getReturnValue();
117+
118+
if (output == null) fail("null result");
119+
else if (!(output instanceof Integer)) {
120+
fail("result is a " + output.getClass().getName());
121+
}
122+
else assertEquals(4, ((Integer) output).intValue());
123+
}
124+
90125
/**
91126
* Tests that the return value <em>is</em> appended as an extra output when no
92127
* explicit outputs were declared.
@@ -122,7 +157,6 @@ public void testReturnValueExcluded() throws Exception {
122157
assertFalse(outputs.containsKey(ScriptModule.RETURN_VALUE));
123158
}
124159

125-
126160
/**
127161
* Ensures parameters are parsed correctly from scripts, even in the presence
128162
* of noise like e-mail addresses.

0 commit comments

Comments
 (0)