1 17 18 package org.sablecc.sablecc.launcher; 19 20 import java.util.List ; 21 22 import org.sablecc.sablecc.exception.InternalException; 23 import org.sablecc.sablecc.exception.InvalidArgumentException; 24 import org.sablecc.sablecc.exception.InvalidArgumentRuntimeException; 25 import org.sablecc.sablecc.launcher.syntax3.analysis.DepthFirstAdapter; 26 import org.sablecc.sablecc.launcher.syntax3.node.ALongOption; 27 import org.sablecc.sablecc.launcher.syntax3.node.AOperand; 28 import org.sablecc.sablecc.launcher.syntax3.node.APlainArgument; 29 import org.sablecc.sablecc.launcher.syntax3.node.AShortOption; 30 import org.sablecc.sablecc.launcher.syntax3.node.Start; 31 32 class ArgumentExtractor 33 extends DepthFirstAdapter { 34 35 private final List <OptionArgument> optionArguments; 36 37 private final List <TextArgument> textArguments; 38 39 private Option incompleteOption; 40 41 private ArgumentExtractor( 42 final List <OptionArgument> optionArguments, 43 final List <TextArgument> textArguments) { 44 45 if (optionArguments == null) { 46 throw new InternalException("optionArguments may not be null"); 47 } 48 49 if (textArguments == null) { 50 throw new InternalException("textArguments may not be null"); 51 } 52 53 this.optionArguments = optionArguments; 54 this.textArguments = textArguments; 55 } 56 57 @Override 58 public void caseAPlainArgument( 59 APlainArgument node) { 60 61 this.textArguments.add(new TextArgument(node.getText().getText())); 62 } 63 64 @Override 65 public void caseALongOption( 66 ALongOption node) 67 throws InvalidArgumentRuntimeException { 68 69 String longName = node.getLongName().getText(); 70 71 Option option = Option.getLongOption(longName); 73 74 if (option == null) { 75 throw new InvalidArgumentRuntimeException("invalid option: --" 76 + longName); 77 } 78 79 if (option.hasOperand()) { 81 83 AOperand operand = (AOperand) node.getOperand(); 84 85 if (operand != null) { 87 89 if (operand.getOperandText() != null) { 90 this.optionArguments.add(new OptionArgument(option, operand 91 .getOperandText().getText())); 92 } 93 else { 94 this.optionArguments.add(new OptionArgument(option, "")); 95 } 96 } 97 else { 98 this.incompleteOption = option; 100 } 101 } 102 else { 103 105 if (node.getOperand() != null) { 106 throw new InvalidArgumentRuntimeException("option --" 107 + longName + " does not expect an operand"); 108 } 109 110 this.optionArguments.add(new OptionArgument(option, null)); 111 } 112 } 113 114 @Override 115 public void caseAShortOption( 116 AShortOption node) { 117 118 if (this.incompleteOption != null) { 119 throw new InvalidArgumentRuntimeException("option -" 120 + this.incompleteOption.getShortName() + " is missing a " 121 + this.incompleteOption.getOperandName() + " operand"); 122 } 123 124 String shortName = node.getShortName().getText(); 125 126 Option option = Option.getShortOption(shortName); 128 129 if (option == null) { 130 throw new InvalidArgumentRuntimeException("invalid option: -" 131 + shortName); 132 } 133 134 if (option.hasOperand()) { 136 138 AOperand operand = (AOperand) node.getOperand(); 139 140 if (operand != null) { 142 144 if (operand.getOperandText() != null) { 145 this.optionArguments.add(new OptionArgument(option, operand 146 .getOperandText().getText())); 147 } 148 else { 149 this.optionArguments.add(new OptionArgument(option, "")); 150 } 151 } 152 else { 153 this.incompleteOption = option; 155 } 156 } 157 else { 158 160 if (node.getOperand() != null) { 161 throw new InvalidArgumentRuntimeException("option -" 162 + shortName + " does not expect an operand"); 163 } 164 165 this.optionArguments.add(new OptionArgument(option, null)); 166 } 167 168 } 169 170 public static Option extractArguments( 171 Start ast, 172 List <OptionArgument> optionArguments, 173 List <TextArgument> textArguments) 174 throws InvalidArgumentException { 175 176 ArgumentExtractor extractor = new ArgumentExtractor(optionArguments, 177 textArguments); 178 179 try { 180 ast.apply(extractor); 181 } 182 catch (InvalidArgumentRuntimeException e) { 183 throw new InvalidArgumentException(e.getMessage(), e); 184 } 185 186 return extractor.incompleteOption; 187 } 188 } 189 | Popular Tags |