當前位置: 首頁>>代碼示例>>Java>>正文


Java Parser類代碼示例

本文整理匯總了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);
}
 
開發者ID:transwarpio,項目名稱:rapidminer,代碼行數:18,代碼來源:CapitulatingErrorStrategy.java

示例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));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:24,代碼來源:ParserErrorStrategy.java

示例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);
}
 
開發者ID:servicecatalog,項目名稱:oscm-app,代碼行數:26,代碼來源:FWPolicyErrorStrategy.java

示例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);
}
 
開發者ID:pgcodekeeper,項目名稱:pgcodekeeper,代碼行數:23,代碼來源:AntlrParser.java

示例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;
}
 
開發者ID:nkouevda,項目名稱:antlr-examples,代碼行數:24,代碼來源:ParserUtil.java

示例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);
    
    
}
 
開發者ID:fcpauldiaz,項目名稱:Compilador,代碼行數:21,代碼來源:DescriptiveErrorListener.java

示例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;
}
 
開發者ID:kasonyang,項目名稱:kalang,代碼行數:22,代碼來源:AstBuilderFactory.java

示例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;
}
 
開發者ID:kasonyang,項目名稱:kalang,代碼行數:21,代碼來源:KalangCompiler.java

示例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;
}
 
開發者ID:antlr,項目名稱:codebuff,代碼行數:19,代碼來源:LangDescriptor.java

示例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;
  }
 
開發者ID:wsdookadr,項目名稱:mdetect,代碼行數:17,代碼來源:ParseUtils.java

示例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;
}
 
開發者ID:wsdookadr,項目名稱:mdetect,代碼行數:18,代碼來源:ParseUtils.java

示例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);
    }

  }
}
 
開發者ID:confluentinc,項目名稱:ksql,代碼行數:17,代碼來源:KsqlParserErrorStrategy.java

示例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);
}
 
開發者ID:confluentinc,項目名稱:ksql,代碼行數:17,代碼來源:KsqlParserErrorStrategy.java

示例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 ";
}
 
開發者ID:BasTesterink,項目名稱:WAM,代碼行數:22,代碼來源:RegisterAssigner.java

示例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);
}
 
開發者ID:HuaweiBigData,項目名稱:StreamCQL,代碼行數:23,代碼來源:CQLErrorStrategy.java


注:本文中的org.antlr.v4.runtime.Parser類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。