summaryrefslogtreecommitdiff
path: root/sources/scalac/util/OptionParser.java
blob: 9521ca388e459138a644ec83e8c81d38a8ffa4e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.util;

import scala.tools.util.Position;
import scala.tools.util.Reporter;

import java.text.Format;
import java.text.MessageFormat;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

import scalac.PhaseDescriptor;
//import scalac.optimizer.OptimizePhase;

public class CommandParser {

    private final String product;
    private final String version;
    private final String syntax;
    private final Reporter reporter;
    private final List/*<ArgumentParser>*/ parsers;

    public CommandParser(String product, String version, String syntax,
        Reporter reporter)
    {
        this.product = product;
        this.version = version;
        this.syntax = syntax;
        this.reporter = reporter;
        this.parsers = new ArrayList();
    }

    public String product() {
        return product;
    }

    public String version() {
        return version;
    }

    public String syntax() {
        return syntax;
    }

    public Reporter reporter() {
        return reporter;
    }

    public boolean add(ArgumentParser parser) {
        return parsers.add(parser);
    }

    public void add(int index, ArgumentParser parser) {
        parsers.add(index, parser);
    }

    public boolean remove(ArgumentParser parser) {
        return parsers.remove(parser);
    }

    public List parsers() {
        return parsers;
    }

    public boolean parse(String[] args) {
        int errors = reporter.errors();
        for (int i = 0; i < args.length; ) {
            for (int j = 0; j < parsers.size(); j++) {
                ArgumentParser parser = (ArgumentParser)parsers.get(j);
                if (parser.matches(args, i)) {
                    i = parser.consume(args, i);
                    break;
                }
	    }
	}
        return reporter.errors() == errors;
    }

    public String getHelpMessage() {
        Format format = new MessageFormat("  {0}\t  {1}");
        List options = new ArrayList(parsers.size());
        for (int i = 0; i < parsers.size(); i++) {
            if (!(parsers.get(i) instanceof OptionParser)) continue;
            OptionParser parser = (OptionParser)parsers.get(i);
            String option = parser.getHelpMessage(format);
            if (option != null) options.add(option);
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append("usage: ").append(product());
        if (options.size() > 0) buffer.append(" <options>");
        if (syntax != null) buffer.append(' ').append(syntax);
        buffer.append(Strings.EOL);
        if (options.size() > 0) {
            buffer.append("where possible options include:");
            buffer.append(Strings.EOL);
            buffer.append(Strings.format(options));
        }
        return buffer.toString();
    }

    public void error(String message) {
        reporter.error(new Position(product), message);
    }

    public void warning(String message) {
        reporter.warning(new Position(product), message);
    }
}

public abstract class ArgumentParser {

    public final CommandParser command;

    public ArgumentParser(CommandParser command) {
        this.command = command;
    }

    public abstract boolean matches(String[] args, int index);
    public abstract int consume(String[] args, int index);

}

public class UnknownArgumentParser extends ArgumentParser {

    public UnknownArgumentParser(CommandParser command) {
        super(command);
    }

    public boolean matches(String[] args, int index) {
        return true;
    }

    public int consume(String[] args, int index) {
        command.error("don't known what to do with '" + args[index] + "'");
        return index + 1;
    }
}

public class ScalaFileArgumentParser extends ArgumentParser {

    public final List list;

    public ScalaFileArgumentParser(CommandParser command) {
        super(command);
        this.list = new ArrayList();
    }

    public boolean matches(String[] args, int index) {
        return args[index].endsWith(".scala");
    }

    public int consume(String[] args, int index) {
        list.add(args[index]);
        return index + 1;
    }

    public String[] toArray() {
        return (String[])list.toArray(new String[list.size()]);
    }
}

public class ScalaProgramArgumentParser extends ArgumentParser {

    public String main;
    public String[] args;

    public ScalaProgramArgumentParser(CommandParser command) {
        super(command);
    }

    public boolean matches(String[] args, int index) {
        return args[index].equals("--");
    }

    public int consume(String[] args, int index) {
        if (index + 1 < args.length) {
            this.main = args[index + 1];
            this.args = new String[args.length - index - 2];
            System.arraycopy(args, index + 2, this.args, 0, this.args.length);
            return args.length;
        } else {
            command.error("option --: missing module name");
            return args.length;
        }
    }
}

public abstract class OptionParser extends ArgumentParser {

    public final String option;
    public final String description;

    public OptionParser(CommandParser command, String option,
        String description)
    {
        super(command);
        this.option = option;
        this.description = description;
    }

    public String getHelpSyntax() {
        return "-" + option;
    }

    public String getHelpDescription() {
        return description;
    }

    public void getHelpMessageArgs(List args) {
        args.add(getHelpSyntax());
        args.add(getHelpDescription());
    }

    public String getHelpMessage(Format format) {
        if (description == null) return null;
        List args = new ArrayList();
        getHelpMessageArgs(args);
        return format.format(args.toArray());
    }

    public void error(String message) {
        command.error("option -" + option + ": " + message);
    }

    public void warning(String message) {
        command.warning("option -" + option + ": " + message);
    }
}
/*
public class OptimizeOptionParser extends OptionParser {

    private final OptimizePhase optimizer;
    public boolean optimize;

    public OptimizeOptionParser(CommandParser command,
        String option, String description, OptimizePhase optimizer)
    {
        super(command, option, description);
        this.optimizer = optimizer;
        this.optimize = false;
    }

    public boolean matches(String[] args, int index) {
        return args[index].equals("-" + option);
    }

    public int consume(String[] args, int index) {
        optimizer.setOptions(args[index].substring(1 + option.length()));
        optimize = true;
        return index + 1;
    }

    public String getHelpSyntax() {
        return super.getHelpSyntax() + "[:<options>]";
    }
}
*/
public class VersionOptionParser extends OptionParser {

    private final String version;

    public VersionOptionParser(CommandParser command,
        String option, String description, String version)
    {
        super(command, option, description);
        this.version = version;
    }

    public boolean matches(String[] args, int index) {
        return args[index].equals("-" + option);
    }

    public int consume(String[] args, int index) {
        System.out.println(version);
        System.exit(0);
        return index + 1;
    }
}

public class HelpOptionParser extends OptionParser {

    public HelpOptionParser(CommandParser command,
        String option, String description)
    {
        super(command, option, description);
    }

    public boolean matches(String[] args, int index) {
        return args[index].equals("-?") ||
            args[index].equals("-" + option) ||
            args[index].equals("--" + option);
    }

    public int consume(String[] args, int index) {
        System.out.println(command.getHelpMessage());
        System.exit(0);
        return index + 1;
    }

    public String getHelpSyntax() {
        return "-? " + super.getHelpSyntax();
    }
}

public class UnknownOptionParser extends OptionParser {

    public UnknownOptionParser(CommandParser command) {
        super(command, "", null);
    }

    public boolean matches(String[] args, int index) {
        return args[index].startsWith("-");
    }

    public int consume(String[] args, int index) {
        command.error("unknown option " + args[index]);
        return index + 1;
    }
}

public class BooleanOptionParser extends OptionParser {

    public boolean value;

    public BooleanOptionParser(CommandParser command,
        String option, String description, boolean value)
    {
        super(command, option, description);
        this.value = value;
    }

    public boolean matches(String[] args, int index) {
        return args[index].equals("-" + option);
    }

    public int consume(String[] args, int index) {
        value = true;
        return index + 1;
    }
}

public class StringOptionParser extends OptionParser {

    public String value;
    public String argument;

    public StringOptionParser(CommandParser command,
        String option, String description, String argument, String value)
    {
        super(command, option, description);
        this.argument = argument;
        this.value = value;
    }

    public boolean matches(String[] args, int index) {
        return args[index].equals("-" + option);
    }

    public int consume(String[] args, int index) {
        if (index + 1 < args.length) {
            value = args[index + 1];
            return index + 2;
        } else {
            error("missing argument");
            return index + 1;
        }
    }

    public String getHelpSyntax() {
        String syntax = super.getHelpSyntax();
        if (argument != null) syntax = syntax + " <" + argument + ">";
        return syntax;
    }
}

public class PhaseSetOptionParser extends OptionParser {

    private final PhaseDescriptor[] phases;
    private final int flag;
    private final PrefixMatcher matcher;

    public PhaseSetOptionParser(CommandParser command,
        String option, String description, PhaseDescriptor[] phases, int flag)
    {
        super(command, option, description);
        this.phases = phases;
        this.flag = flag;
        this.matcher = new PrefixMatcher();
        for (int i = 0; i < phases.length; i++) {
            PhaseDescriptor phase = phases[i];
            matcher.insert(phase.name(), phase, phase.description());
        }
    }

    public boolean matches(String[] args, int index) {
        return args[index].startsWith("-" + option + ":");
    }

    public int consume(String[] args, int index) {
        StringTokenizer tokens = new StringTokenizer(
            args[index].substring(option.length() + 2), ",");
        while (tokens.hasMoreTokens()) consumePhase(tokens.nextToken());
        return index + 1;
    }

    public void consumePhase(String token) {
        if (token.equals("all")) {
            for (int i = 0; i < phases.length; i++)
                phases[i].addFlag(flag, false);
            return;
        }
        PhaseDescriptor phase = lookup(getPhaseName(token));
        if (phase != null) {
            boolean before = getBeforeFlag(token);
            boolean after = getAfterFlag(token) || !before;
            if (before) phase.addFlag(flag, true);
            if (after) phase.addFlag(flag, false);
        }
    }

    public PhaseDescriptor lookup(String name) {
        if (name.length() == 0) {
            error("illegal zero-length phase name");
            return null;
        }
        PrefixMatcher.Entry[] entries = matcher.lookup(name);
        if (entries.length == 1) return (PhaseDescriptor)entries[0].value;
        error(matcher.getErrorMessage(name, entries, "phase name"));
        return null;
    }

    public boolean getBeforeFlag(String token) {
        for (int i = token.length(); 0 < i--; ) {
            switch (token.charAt(i)) {
            case '-': return true;
            case '+': continue;
            default : return false;
            }
        }
        return false;
    }

    public boolean getAfterFlag(String token) {
        for (int i = token.length(); 0 < i--; ) {
            switch (token.charAt(i)) {
            case '-': continue;
            case '+': return true;
            default : return false;
            }
        }
        return false;
    }

    public String getPhaseName(String token) {
        for (int i = token.length(); 0 < i--; ) {
            switch (token.charAt(i)) {
            case '-': continue;
            case '+': continue;
            default : return token.substring(0, i + 1);
            }
        }
        return "";
    }

    public String getHelpSyntax() {
        return super.getHelpSyntax() + ":<phases>";
    }
}

public class PrintOptionParser extends PhaseSetOptionParser {

    public boolean tokens;

    public PrintOptionParser(CommandParser command,
        String option, String description, PhaseDescriptor[] phases, int flag)
    {
        super(command, option, description, phases, flag);
        this.tokens = false;
    }

    public void consumePhase(String token) {
        if ("tokens".equals(token))
            tokens = true;
        else
            super.consumePhase(token);
    }

}

public class ChoiceOptionParser extends OptionParser {

    public final String argument;
    public final String[] choices;

    public String value;

    public ChoiceOptionParser(CommandParser command,
        String option, String description, String argument, String[] choices,
        String value)
    {
        super(command, option, description);
        this.argument = argument;
        this.choices = choices;
        this.value = value;
    }

    public boolean matches(String[] args, int index) {
        return args[index].startsWith("-" + option + ":");
    }

    public int consume(String[] args, int index) {
        String choice = args[index].substring(option.length() + 2);
        boolean found = false;
        for (int i = 0; i < choices.length; i++) {
            if (choices[i].equals(choice)) { found = true; break; }
        }
        if (found) {
            value = choice;
        } else if (choice.length() > 0) {
            error("unknown " + argument + " '" + choice + "'");
        } else {
            error("missing " + argument);
        }
        return index + 1;
    }

    public String getHelpSyntax() {
        String syntax = super.getHelpSyntax();
        if (argument != null) syntax = syntax + ":<" + argument + ">";
        return syntax;
    }
}