本文整理汇总了Java中org.antlr.v4.runtime.Parser类的典型用法代码示例。如果您正苦于以下问题:Java Parser类的具体用法?Java Parser怎么用?Java Parser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Parser类属于org.antlr.v4.runtime包,在下文中一共展示了Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: reportNoViableAlternative
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
@Override
protected void reportNoViableAlternative(Parser recognizer, NoViableAltException e) {
// change error message from default implementation
TokenStream tokens = recognizer.getInputStream();
String input;
if (tokens != null) {
if (e.getStartToken().getType() == Token.EOF) {
input = "the end";
} else {
input = escapeWSAndQuote(tokens.getText(e.getStartToken(), e.getOffendingToken()));
}
} else {
input = escapeWSAndQuote("<unknown input>");
}
String msg = "inadmissible input at " + input;
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
示例2: recover
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
@Override
public void recover(final Parser recognizer, final RecognitionException re) {
final Token token = re.getOffendingToken();
String message;
if (token == null) {
message = "no parse token found.";
} else if (re instanceof InputMismatchException) {
message = "unexpected token [" + getTokenErrorDisplay(token) + "]" +
" was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
} else if (re instanceof NoViableAltException) {
if (token.getType() == PainlessParser.EOF) {
message = "unexpected end of script.";
} else {
message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
}
} else {
message = "unexpected token near [" + getTokenErrorDisplay(token) + "].";
}
Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex());
throw location.createError(new IllegalArgumentException(message, re));
}
示例3: recoverInline
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
/**
* Make sure we don't attempt to recover inline; if the parser successfully
* recovers, it won't throw an exception.
*/
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
InputMismatchException e = new InputMismatchException(recognizer);
String policies = recognizer.getInputStream().getText();
StringTokenizer tk = new StringTokenizer(policies, ";");
String policy = "";
int idx = 0;
while (tk.hasMoreElements()) {
policy = (String) tk.nextElement();
idx += policy.length();
if (idx >= e.getOffendingToken().getStartIndex()) {
break;
}
}
String message = Messages.get(Messages.DEFAULT_LOCALE,
"error_invalid_firewallconfig", new Object[] {
e.getOffendingToken().getText(), policy });
throw new RuntimeException(message);
}
示例4: makeBasicParser
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
private static <T extends Parser> T makeBasicParser(Class<T> parserClass,
ANTLRInputStream stream, String parsedObjectName, List<AntlrError> errors) {
Lexer lexer;
Parser parser;
if (parserClass.isAssignableFrom(SQLParser.class)) {
lexer = new SQLLexer(stream);
parser = new SQLParser(new CommonTokenStream(lexer));
} else if (parserClass.isAssignableFrom(IgnoreListParser.class)) {
lexer = new IgnoreListLexer(stream);
parser = new IgnoreListParser(new CommonTokenStream(lexer));
} else {
throw new IllegalArgumentException("Unknown parser class: " + parserClass);
}
CustomAntlrErrorListener err = new CustomAntlrErrorListener(parsedObjectName, errors);
lexer.removeErrorListeners();
lexer.addErrorListener(err);
parser.removeErrorListeners();
parser.addErrorListener(err);
return parserClass.cast(parser);
}
示例5: newParser
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
public static <L extends Lexer, P extends Parser> P newParser(
Function<CharStream, L> lexerFactory,
Function<TokenStream, P> parserFactory,
String input,
boolean useBailErrorStrategy,
boolean removeErrorListeners) {
CharStream charStream = new ANTLRInputStream(input);
L lexer = lexerFactory.apply(charStream);
if (removeErrorListeners) {
lexer.removeErrorListeners();
}
TokenStream tokenStream = new CommonTokenStream(lexer);
P parser = parserFactory.apply(tokenStream);
if (useBailErrorStrategy) {
parser.setErrorHandler(new BailErrorStrategy());
}
if (removeErrorListeners) {
parser.removeErrorListeners();
}
return parser;
}
示例6: syntaxError
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, RecognitionException e)
{
List<String> stack = ((Parser)recognizer).getRuleInvocationStack(); Collections.reverse(stack);
System.err.println("rule stack: "+stack);
System.err.println("linea "+line+":"+charPositionInLine+" at "+
offendingSymbol+": "+msg);
String rule = "rule stack: "+stack;
String mensaje = "linea "+line+":"+charPositionInLine+" at "+
offendingSymbol+": "+msg + "\n\r";
agregarLog("Un error inesperado ha ocurrido " +"\n" + mensaje, line, charPositionInLine,true);
}
示例7: createAstBuilder
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
public static AstBuilder createAstBuilder(CompilationUnit source,TokenStream tokens){
KalangParser p = new KalangParser(tokens);
AstBuilder sp = new AstBuilder(source, p);
p.setErrorHandler(new DefaultErrorStrategy() {
@Override
public void reportError(Parser recognizer, RecognitionException e) {
String msg = AntlrErrorString.exceptionString(recognizer, e);
Token end = e.getOffendingToken();
Token start;
RuleContext ctx = e.getCtx();
if(ctx instanceof ParserRuleContext){
start = ((ParserRuleContext) ctx).getStart();
}else{
start = end;
}
sp.getDiagnosisReporter().report(Diagnosis.Kind.ERROR, msg,start,end);
}
});
return sp;
}
示例8: createParser
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
@Override
public KalangParser createParser(CompilationUnit compilationUnit, CommonTokenStream tokenStream) {
KalangParser parser = new KalangParser(tokenStream);
parser.setErrorHandler(new DefaultErrorStrategy(){
@Override
public void reportError(Parser recognizer, RecognitionException e) {
String msg = AntlrErrorString.exceptionString(recognizer, e);
Diagnosis diagnosis = new Diagnosis(
compilationUnit.getCompileContext()
, Diagnosis.Kind.ERROR
, OffsetRangeHelper.getOffsetRange(e.getOffendingToken())
, msg
, compilationUnit.getSource()
);
diagnosisHandler.handleDiagnosis(diagnosis);
}
});
return parser;
}
示例9: LangDescriptor
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
public LangDescriptor(String name,
String corpusDir,
String fileRegex,
Class<? extends Lexer> lexerClass,
Class<? extends Parser> parserClass,
String startRuleName,
int indentSize,
int singleLineCommentType)
{
this.name = name;
this.corpusDir = corpusDir;
this.fileRegex = fileRegex;
this.lexerClass = lexerClass;
this.parserClass = parserClass;
this.startRuleName = startRuleName;
this.indentSize = indentSize;
this.singleLineCommentType = singleLineCommentType;
}
示例10: parsePHP
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
public static Pair<Parser, Lexer> parsePHP(String filePath) {
AntlrCaseInsensitiveFileStream input;
try {
input = new AntlrCaseInsensitiveFileStream(filePath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
PHPLexer lexer = new PHPLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PHPParser parser = new InterruptablePHPParser(tokens, filePath);
/* turn on prediction mode to speed up parsing */
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
Pair<Parser, Lexer> retval = new Pair<Parser, Lexer>(parser, lexer);
return retval;
}
示例11: processFile
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
public static Document processFile(String filePath) {
Pair<Parser, Lexer> pl = parsePHP(filePath);
PHPParser parser = (PHPParser) pl.a;
parser.setBuildParseTree(true);
/*
* htmlDocument is the start rule (the top-level rule)
* for the PHP grammar
*/
ParserRuleContext tree = parser.htmlDocument();
List<String> ruleNames = Arrays.asList(parser.getRuleNames());
Map<Integer, String> invTokenMap = getInvTokenMap(parser);
TokenStream tokenStream = parser.getTokenStream();
ParseTreeDOMSerializer ptSerializer = new ParseTreeDOMSerializer(ruleNames, invTokenMap, tokenStream);
ParseTreeWalker.DEFAULT.walk(ptSerializer, tree);
Document result= ptSerializer.getDOMDocument();
return result;
}
示例12: reportError
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
public void reportError(Parser recognizer, RecognitionException e) {
if (!this.inErrorRecoveryMode(recognizer)) {
this.beginErrorCondition(recognizer);
if (e instanceof NoViableAltException) {
this.reportNoViableAlternative(recognizer, (NoViableAltException) e);
} else if (e instanceof InputMismatchException) {
this.reportInputMismatch(recognizer, (InputMismatchException) e);
} else if (e instanceof FailedPredicateException) {
this.reportFailedPredicate(recognizer, (FailedPredicateException) e);
} else {
System.err.println("unknown recognition error type: " + e.getClass().getName());
recognizer.notifyErrorListeners(e.getOffendingToken(), e.getMessage(), e);
}
}
}
示例13: reportNoViableAlternative
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
protected void reportNoViableAlternative(Parser recognizer, NoViableAltException e) {
TokenStream tokens = recognizer.getInputStream();
String input;
if (tokens != null) {
if (e.getStartToken().getType() == -1) {
input = "<EOF>";
} else {
input = tokens.getText(e.getStartToken(), e.getOffendingToken());
}
} else {
input = "<unknown input>";
}
String msg = "no viable alternative at input " + this.escapeWSAndQuote(input);
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
示例14: regStr
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
private String regStr(ParseTree tree, Parser p){
if(tree instanceof VariableContext){
return preProcessor.getVarName(tree);
} else if(tree instanceof PredicateContext){
String functor = WAMTokenizer.predicateFunctor((PredicateContext)tree);
if(tree.getChildCount()>1){
functor += "(";
ParseTree parenthesized = tree.getChild(1);
ParseTree argseq = parenthesized.getChild(1);
int nrArgs = ((argseq.getChildCount()-1)/2) + 1;
for(int j = 0; j < nrArgs; j++){
Integer child = getPrimeRegister(argseq.getChild(j*2).getChild(0));
String childReg = child<0?("Y"+(child*-1)):"X"+(child+1);
functor += childReg+(j!=(nrArgs-1)?",":"");
}
functor += ")";
}
return functor;
}
return " error ";
}
示例15: reportNoViableAlternative
import org.antlr.v4.runtime.Parser; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e)
{
TokenStream tokens = recognizer.getInputStream();
String input;
if (tokens instanceof TokenStream)
{
if (e.getStartToken().getType() == Token.EOF)
input = "<EOF>";
else
input = getText(tokens, e.getStartToken(), e.getOffendingToken());
}
else
{
input = "<unknown input>";
}
String msg = "no viable alternative at input " + escapeWSAndQuote(input);
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}