我的 java 应用程序需要一个解析器,所以我发现了 JParsec 库,并在他们的网站上使用简单的计算器语法浏览了一个教程。似乎我应该能够使用该库执行我计划的操作,因此开始制作原型,将示例计算器中的十进制文字解析更改为将非常简单的标识符解析为字符串。我现在唯一想做的操作是“+”,用于连接字符串,但我不断收到错误消息。这是我的代码:
import org.codehaus.jparsec.OperatorTable;
import org.codehaus.jparsec.Parser;
import org.codehaus.jparsec.Parsers;
import org.codehaus.jparsec.Scanners;
import org.codehaus.jparsec.Terminals;
import org.codehaus.jparsec.Tokens.Fragment;
import org.codehaus.jparsec.functors.Binary;
import org.codehaus.jparsec.functors.Map;
public class Test
{
enum BinaryOperator implements Binary<String> {
PLUS {
public String map(String a, String b) {
return a + b;
}
}
}
static final Parser<Void> IGNORED = Scanners.WHITESPACES.skipMany();
static final Parser<String> TEXT = Terminals.Identifier.TOKENIZER.map(new Map<Fragment, String>() {
public String map(Fragment arg0) {
return arg0.text()+"NEWTOSEEIFITSWORKING";
}
});
private static final Terminals OPERATORS = Terminals.operators("+");
static final Parser<?> TOKENIZER = Parsers.or(Terminals.Identifier.TOKENIZER, OPERATORS.tokenizer());
static Parser<?> term(String... names) {
return OPERATORS.token(names);
}
static <T> Parser<T> op(String name, T value) {
return term(name).retn(value);
}
static Parser<String> textparser(Parser<String> atom) {
Parser<String> parser = new OperatorTable<String>().infixl(op("+", BinaryOperator.PLUS), 10).build(atom);
return parser;
}
public static final Parser<String> TEXTPARSER = textparser(TEXT).from(TOKENIZER, IGNORED);
public static void main(String[] args)
{
System.out.println(TEXTPARSER.parse("hello+world"));
}}
以下是我遇到的错误:
Exception in thread "main" org.codehaus.jparsec.error.ParserException: Cannot scan characters on tokens.
line 1, column 1
at org.codehaus.jparsec.Parser.asParserException(Parser.java:591)
at org.codehaus.jparsec.Parser.run(Parser.java:600)
at org.codehaus.jparsec.MapParser.apply(MapParser.java:30)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.MapParser.apply(MapParser.java:30)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.BindNextParser.apply(BindNextParser.java:37)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.Sequence2Parser.apply(Sequence2Parser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.ParserInternals.runNestedParser(ParserInternals.java:116)
at org.codehaus.jparsec.NestedParser.apply(NestedParser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.Sequence2Parser.apply(Sequence2Parser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.Parsers.parse(Parsers.java:97)
at org.codehaus.jparsec.Parser.parse(Parser.java:535)
at org.codehaus.jparsec.Parser.parse(Parser.java:546)
at org.codehaus.jparsec.Parser.parse(Parser.java:551)
at Test.main(Test.java:52)
Caused by: java.lang.IllegalStateException: Cannot scan characters on tokens.
at org.codehaus.jparsec.ParserState.characters(ParserState.java:56)
at org.codehaus.jparsec.PatternScanner.apply(PatternScanner.java:36)
at org.codehaus.jparsec.ReturnSourceParser.apply(ReturnSourceParser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
... 18 more
知道我做错了什么吗?任何帮助是极大的赞赏!