prompto.utils.CmdLineParser Maven / Gradle / Ivy
The newest version!
package prompto.utils;
import java.util.HashMap;
import java.util.Map;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeProperty;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import prompto.utils.ArgsParser.ELEMENTContext;
import prompto.utils.ArgsParser.EntryContext;
import prompto.utils.ArgsParser.KeyContext;
import prompto.utils.ArgsParser.STRINGContext;
public class CmdLineParser {
public static Map read(String[] args) {
Map map = new HashMap();
for(int i=0, max=args.length; i parse(String input) throws Exception {
if(input==null)
input = "";
try {
CharStream stream = CharStreams.fromString(input);
ArgsLexer lexer = new ArgsLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ArgsParser parser = new ArgsParser(tokens);
ParseTree tree = parser.parse();
CmdLineBuilder builder = new CmdLineBuilder();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(builder, tree);
return builder.getCmdLineArgs();
} catch (RecognitionException e) {
e.printStackTrace(System.err);
throw new Exception(e);
}
}
static class CmdLineBuilder extends ArgsParserBaseListener {
Map args = new HashMap();
ParseTreeProperty values = new ParseTreeProperty();
public Map getCmdLineArgs() {
return args;
}
@Override
public void exitEntry(EntryContext ctx) {
String key = values.get(ctx.k);
String value = values.get(ctx.v);
args.put(key,value);
}
@Override
public void exitKey(KeyContext ctx) {
values.put(ctx,ctx.getText());
}
@Override
public void exitSTRING(STRINGContext ctx) {
String s = ctx.getText();
values.put(ctx,s.substring(1,s.length()-1));
}
@Override
public void exitELEMENT(ELEMENTContext ctx) {
values.put(ctx,ctx.getText());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy