1 17 18 package org.sablecc.sablecc.launcher; 19 20 import java.io.PushbackReader ; 21 import java.io.StringReader ; 22 import java.util.Collections ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.sablecc.sablecc.exception.InvalidArgumentException; 27 import org.sablecc.sablecc.launcher.syntax3.lexer.Lexer; 28 import org.sablecc.sablecc.launcher.syntax3.node.Start; 29 import org.sablecc.sablecc.launcher.syntax3.parser.Parser; 30 31 public class Arguments { 32 33 private final List <OptionArgument> optionArguments; 34 35 private final List <TextArgument> textArguments; 36 37 public Arguments( 38 String [] args) 39 throws InvalidArgumentException { 40 41 List <OptionArgument> optionArguments = new LinkedList <OptionArgument>(); 42 43 List <TextArgument> textArguments = new LinkedList <TextArgument>(); 44 45 int currentArgIndex = 0; 46 47 while (currentArgIndex < args.length && textArguments.size() == 0) { 49 50 try { 51 Start ast = new Parser(new Lexer(new PushbackReader ( 52 new StringReader (args[currentArgIndex]), 1024))) 53 .parse(); 54 55 Option incompleteOption = ArgumentExtractor.extractArguments( 56 ast, optionArguments, textArguments); 57 58 if (incompleteOption != null) { 59 60 if (currentArgIndex + 1 >= args.length) { 61 62 if (args[currentArgIndex].startsWith("--")) { 63 throw new InvalidArgumentException("option --" 64 + incompleteOption.getLongName() 65 + " is missing a " 66 + incompleteOption.getOperandName() 67 + " operand"); 68 } 69 else { 70 throw new InvalidArgumentException("option -" 71 + incompleteOption.getShortName() 72 + " is missing a " 73 + incompleteOption.getOperandName() 74 + " operand"); 75 } 76 } 77 78 optionArguments.add(new OptionArgument(incompleteOption, 79 args[++currentArgIndex])); 80 } 81 } 82 catch (InvalidArgumentException e) { 83 throw new InvalidArgumentException("invalid argument \"" 84 + args[currentArgIndex] + "\": " + e.getMessage(), e); 85 } 86 catch (Exception e) { 87 throw new InvalidArgumentException("invalid argument \"" 88 + args[currentArgIndex] + "\"", e); 89 } 90 91 currentArgIndex++; 92 } 93 94 while (currentArgIndex < args.length) { 96 97 textArguments.add(new TextArgument(args[currentArgIndex])); 98 99 currentArgIndex++; 100 } 101 102 this.optionArguments = Collections.unmodifiableList(optionArguments); 103 this.textArguments = Collections.unmodifiableList(textArguments); 104 } 105 106 public List <OptionArgument> getOptionArguments() { 107 108 return this.optionArguments; 109 } 110 111 public List <TextArgument> getTextArguments() { 112 113 return this.textArguments; 114 } 115 116 } 117 | Popular Tags |