Package org.antlr.v4.runtime

Examples of org.antlr.v4.runtime.ParserInterpreter


//    lexer.setTokenFactory(factory);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
   
    MsonParser parser = new MsonParser(tokens);
//    parser.setTokenFactory(factory);
    parser.addErrorListener(new DiagnosticErrorListener());
    parser.getInterpreter().setPredictionMode(
        PredictionMode.LL_EXACT_AMBIG_DETECTION);
    parser.setBuildParseTree(true);
    ParseTree tree = parser.mson();
    // show tree in text form
View Full Code Here


    ClassUtils cu = new ClassUtils();
    setup(cu);
    registry.clearForType(JavaCompletionTypes.CUSTOM_TYPE);
    registry.clearForType(JavaCompletionTypes.FIELD);
    registry.clearForType(JavaCompletionTypes.NAME);
    Lexer lexer = new JavaLexer(new ANTLRInputStream(txt));
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Create a parser that reads from the scanner
    JavaParser parser = new JavaParser(tokens);
    parser.removeErrorListeners();
View Full Code Here

          String startRule, String input,
          String parseTree)
  {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    ParserInterpreter parser = g.createParserInterpreter(tokens);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    System.out.println("parse tree: "+t.toStringTree(parser));
    assertEquals(parseTree, t.toStringTree(parser));
  }
View Full Code Here

      LexerGrammar lg, Grammar g,
      String startRule, String... input)
  {

    LexerInterpreter lexEngine = lg.createLexerInterpreter(null);
    ParserInterpreter parser = g.createParserInterpreter(null);
    parser.setProfile(true);
    for (String s : input) {
      lexEngine.reset();
      parser.reset();
      lexEngine.setInputStream(new ANTLRInputStream(s));
      CommonTokenStream tokens = new CommonTokenStream(lexEngine);
      parser.setInputStream(tokens);
      Rule r = g.rules.get(startRule);
      if ( r==null ) {
        return parser.getParseInfo().getDecisionInfo();
      }
      ParserRuleContext t = parser.parse(r.index);
//      try {
//        Utils.waitForClose(t.inspect(parser).get());
//      }
//      catch (Exception e) {
//        e.printStackTrace();
//      }
//
//      System.out.println(t.toStringTree(parser));
    }
    return parser.getParseInfo().getDecisionInfo();
  }
View Full Code Here

                        } else {
              Parser previousParser = parser;

              if (USE_PARSER_INTERPRETER) {
                Parser referenceParser = parserCtor.newInstance(tokens);
                parser = new ParserInterpreter(referenceParser.getGrammarFileName(), referenceParser.getVocabulary(), Arrays.asList(referenceParser.getRuleNames()), referenceParser.getATN(), tokens);
              }
              else {
                parser = parserCtor.newInstance(tokens);
              }

              DFA[] decisionToDFA = (FILE_GRANULARITY || previousParser == null ? parser : previousParser).getInterpreter().decisionToDFA;
              if (!REUSE_PARSER_DFA || (!FILE_GRANULARITY && previousParser == null)) {
                decisionToDFA = new DFA[decisionToDFA.length];
              }

              if (COMPUTE_TRANSITION_STATS) {
                parser.setInterpreter(new StatisticsParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache()));
              } else if (!REUSE_PARSER_DFA) {
                parser.setInterpreter(new ParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache()));
              }

              sharedParsers[thread] = parser;
                        }

            parser.removeParseListeners();
            parser.removeErrorListeners();
            if (!TWO_STAGE_PARSING) {
              parser.addErrorListener(DescriptiveErrorListener.INSTANCE);
              parser.addErrorListener(new SummarizingDiagnosticErrorListener());
            }

            if (parser.getInterpreter().decisionToDFA[0] == null) {
              ATN atn = parser.getATN();
              for (int i = 0; i < parser.getInterpreter().decisionToDFA.length; i++) {
                parser.getInterpreter().decisionToDFA[i] = new DFA(atn.getDecisionState(i), i);
              }
            }

            parser.getInterpreter().setPredictionMode(TWO_STAGE_PARSING ? PredictionMode.SLL : PREDICTION_MODE);
            parser.setBuildParseTree(BUILD_PARSE_TREES);
            if (!BUILD_PARSE_TREES && BLANK_LISTENER) {
              parser.addParseListener(listener);
            }
            if (BAIL_ON_ERROR || TWO_STAGE_PARSING) {
              parser.setErrorHandler(new BailErrorStrategy());
            }

                        Method parseMethod = parserClass.getMethod(entryPoint);
                        Object parseResult;

            try {
              if (COMPUTE_CHECKSUM && !BUILD_PARSE_TREES) {
                parser.addParseListener(new ChecksumParseTreeListener(checksum));
              }

              if (USE_PARSER_INTERPRETER) {
                ParserInterpreter parserInterpreter = (ParserInterpreter)parser;
                parseResult = parserInterpreter.parse(Collections.lastIndexOfSubList(Arrays.asList(parser.getRuleNames()), Collections.singletonList(entryPoint)));
              }
              else {
                parseResult = parseMethod.invoke(parser);
              }
            } catch (InvocationTargetException ex) {
              if (!TWO_STAGE_PARSING) {
                throw ex;
              }

              String sourceName = tokens.getSourceName();
              sourceName = sourceName != null && !sourceName.isEmpty() ? sourceName+": " : "";
              if (REPORT_SECOND_STAGE_RETRY) {
                System.err.println(sourceName+"Forced to retry with full context.");
              }

              if (!(ex.getCause() instanceof ParseCancellationException)) {
                throw ex;
              }

              tokens.reset();
              if (REUSE_PARSER && parser != null) {
                parser.setInputStream(tokens);
              } else {
                Parser previousParser = parser;

                if (USE_PARSER_INTERPRETER) {
                  Parser referenceParser = parserCtor.newInstance(tokens);
                  parser = new ParserInterpreter(referenceParser.getGrammarFileName(), referenceParser.getVocabulary(), Arrays.asList(referenceParser.getRuleNames()), referenceParser.getATN(), tokens);
                }
                else {
                  parser = parserCtor.newInstance(tokens);
                }
View Full Code Here

      throw new IllegalStateException("A parser interpreter can only be created for a parser or combined grammar.");
    }

    char[] serializedAtn = ATNSerializer.getSerializedAsChars(atn);
    ATN deserialized = new ATNDeserializer().deserialize(serializedAtn);
    return new ParserInterpreter(fileName, getVocabulary(), Arrays.asList(getRuleNames()), deserialized, tokenStream);
  }
View Full Code Here

  public ParseTreePattern compile(String pattern, int patternRuleIndex) {
    List<? extends Token> tokenList = tokenize(pattern);
    ListTokenSource tokenSrc = new ListTokenSource(tokenList);
    CommonTokenStream tokens = new CommonTokenStream(tokenSrc);

    ParserInterpreter parserInterp = new ParserInterpreter(parser.getGrammarFileName(),
                                 parser.getVocabulary(),
                                 Arrays.asList(parser.getRuleNames()),
                                 parser.getATNWithBypassAlts(),
                                 tokens);

    ParseTree tree = null;
    try {
      parserInterp.setErrorHandler(new BailErrorStrategy());
      tree = parserInterp.parse(patternRuleIndex);
//      System.out.println("pattern tree = "+tree.toStringTree(parserInterp));
    }
    catch (ParseCancellationException e) {
      throw (RecognitionException)e.getCause();
    }
View Full Code Here

    lexEngine.removeErrorListeners();
    lexEngine.addErrorListener(printError);

    CommonTokenStream tokens = new CommonTokenStream(lexEngine);

    ParserInterpreter parser = g.createParserInterpreter(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(printError);
    Rule start = g.getRule(startRule);
    ParseTree tree = parser.parse(start.index);

    // this loop works around a bug in ANTLR 4.2
    // https://github.com/antlr/antlr4/issues/461
    // https://github.com/antlr/intellij-plugin-v4/issues/23
    while (tree.getParent() != null) {
      tree = tree.getParent();
    }
    String sexpression = toStringTree(tree, Arrays.asList(parser.getRuleNames())).trim();

    return sexpression;
  }
View Full Code Here

    // Create a parser that reads from the scanner
    JavaParser parser = new JavaParser(tokens);
    parser.removeErrorListeners();
   
    // start parsing at the compilationUnit rule
    ParserRuleContext t = parser.compilationUnit();
    ParseTreeWalker walker = new ParseTreeWalker();
    List<AutocompleteCandidate> q = new ArrayList<AutocompleteCandidate>();
         
    ImportDeclarationCompletion extractor = new ImportDeclarationCompletion(txt,cur,registry,cps,cu);
    NameBuilder extractor2 = new NameBuilder(registry,cu );
View Full Code Here

    isKeywords = false;
  }

  @Override
  public void enterPackage(@NotNull PackageContext ctx) {
    ParserRuleContext prc = ctx.getParent();
    if (!(prc instanceof DomainArrayContext)) {
      return;
    }
    DomainArrayContext dac = (DomainArrayContext) prc;
    DomainHeaderContext dhctx = (DomainHeaderContext) dac.getChild(0);
View Full Code Here

TOP

Related Classes of org.antlr.v4.runtime.ParserInterpreter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.