patternjavaMinor
Simple REPL command parser in Java
Viewed 0 times
simpleparserjavareplcommand
Problem
I have this tiny library for implementing simple command line languages. It is not flexible enough for handling actual programming languages, but hopefully it may help implementing simpler REPL's faster/cleaner.
CommandParser.java:
```
package net.coderodde.commandparser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
/**
* This class implements a command parser.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Sep 30, 2015)
*/
public class CommandParser {
private final List commandDescriptorList =
new ArrayList<>();
private boolean isSorted = true;
public void add(CommandDescriptor commandDescriptor) {
Objects.requireNonNull(commandDescriptor,
"The input command descriptor is null.");
if (commandDescriptor.size() == 0) {
return;
}
commandDescriptorList.add(commandDescriptor);
isSorted = false;
}
public void process(String command) {
if (!isSorted) {
Collections.sort(commandDescriptorList, comparator);
isSorted = true;
}
for (CommandDescriptor descriptor : commandDescriptorList) {
if (descriptor.parse(command)) {
return;
}
}
}
private static final class CommandDescriptorComparator
implements Comparator {
@Override
public int compare(CommandDescriptor o1, CommandDescriptor o2) {
CommandToken token1 = o1.getToken(0);
CommandToken token2 = o2.getToken(0);
if (token1.getTokenType() == CommandToken.TokenType.IDENTIFIER) {
return 1;
} else if (token2.getTokenType()
== CommandToken.TokenType.IDENTIFIER) {
return -1;
} else {
return 0;
}
}
}
private static final CommandDescr
CommandParser.java:
```
package net.coderodde.commandparser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
/**
* This class implements a command parser.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Sep 30, 2015)
*/
public class CommandParser {
private final List commandDescriptorList =
new ArrayList<>();
private boolean isSorted = true;
public void add(CommandDescriptor commandDescriptor) {
Objects.requireNonNull(commandDescriptor,
"The input command descriptor is null.");
if (commandDescriptor.size() == 0) {
return;
}
commandDescriptorList.add(commandDescriptor);
isSorted = false;
}
public void process(String command) {
if (!isSorted) {
Collections.sort(commandDescriptorList, comparator);
isSorted = true;
}
for (CommandDescriptor descriptor : commandDescriptorList) {
if (descriptor.parse(command)) {
return;
}
}
}
private static final class CommandDescriptorComparator
implements Comparator {
@Override
public int compare(CommandDescriptor o1, CommandDescriptor o2) {
CommandToken token1 = o1.getToken(0);
CommandToken token2 = o2.getToken(0);
if (token1.getTokenType() == CommandToken.TokenType.IDENTIFIER) {
return 1;
} else if (token2.getTokenType()
== CommandToken.TokenType.IDENTIFIER) {
return -1;
} else {
return 0;
}
}
}
private static final CommandDescr
Solution
I can't help but think you could easily use ANTLR to back your command line API. It could require a couple of days to grasp first, but then you have something fast, robust, and dev-friendly.
The Command Token/Descriptor/Parser approach could be a hell to maintain or evolve.
By the way, something is missing in Demo.java, one should read:
The Demo doesn't work because of the missing
The Command Token/Descriptor/Parser approach could be a hell to maintain or evolve.
By the way, something is missing in Demo.java, one should read:
// 'show' command.
CommandDescriptor descriptorShow = new CommandDescriptor(showAction);
descriptorShow.addCommandToken(new CommandToken(TokenType.KEYWORD,
"show",
null));
descriptorShow.addCommandToken(new CommandToken(TokenType.IDENTIFIER,
null,
myIdentifierValidator));The Demo doesn't work because of the missing
TokenType.KEYWORD "show"Code Snippets
// 'show' command.
CommandDescriptor descriptorShow = new CommandDescriptor(showAction);
descriptorShow.addCommandToken(new CommandToken(TokenType.KEYWORD,
"show",
null));
descriptorShow.addCommandToken(new CommandToken(TokenType.IDENTIFIER,
null,
myIdentifierValidator));Context
StackExchange Code Review Q#106112, answer score: 2
Revisions (0)
No revisions yet.