Esempio n. 1
0
 private ParseSuccess getParseResult1(GrammarGraph registry) {
   ParseStatistics statistics =
       ParseStatistics.builder()
           .setDescriptorsCount(8)
           .setGSSNodesCount(2)
           .setGSSEdgesCount(5)
           .setNonterminalNodesCount(3)
           .setTerminalNodesCount(3)
           .setIntermediateNodesCount(2)
           .setPackedNodesCount(5)
           .setAmbiguousNodesCount(0)
           .build();
   return new ParseSuccess(expectedSPPF1(registry), statistics, input1);
 }
Esempio n. 2
0
  @Override
  public final ParseResult parse(
      Input input,
      GrammarGraph grammarGraph,
      Nonterminal nonterminal,
      Map<String, ? extends Object> map,
      boolean global) {
    this.grammarGraph = grammarGraph;
    this.input = input;

    grammarGraph.reset(input);
    resetParser();

    /** Data-dependent GLL parsing */
    this.ctx = new PersistentEvaluatorContext(input);

    if (global) map.forEach((k, v) -> ctx.declareGlobalVariable(k, v));

    NonterminalGrammarSlot startSymbol = getStartSymbol(nonterminal);

    if (startSymbol == null) {
      throw new RuntimeException("No nonterminal named " + nonterminal + " found");
    }

    NonterminalNode root;

    Environment env = null;

    if (!global && !map.isEmpty()) {
      Object[] arguments = new Object[map.size()];

      int i = 0;
      for (String parameter : nonterminal.getParameters()) arguments[i++] = map.get(parameter);

      startGSSNode = startSymbol.getGSSNode(0, new GSSNodeData<>(arguments));
      env = getEmptyEnvironment().declare(nonterminal.getParameters(), arguments);
    } else {
      startGSSNode = startSymbol.getGSSNode(0);
    }

    cu = startGSSNode;

    logger.log("Parsing %s:", input.getURI());

    long start = System.nanoTime();
    long startUserTime = BenchmarkUtil.getUserTime();
    long startSystemTime = BenchmarkUtil.getSystemTime();

    parse(startSymbol, env);

    root = startGSSNode.getNonterminalNode(input.length() - 1);

    ParseResult parseResult;

    long end = System.nanoTime();
    long endUserTime = BenchmarkUtil.getUserTime();
    long endSystemTime = BenchmarkUtil.getSystemTime();

    if (root == null) {
      parseResult = new ParseError(errorSlot, input, errorIndex, errorGSSNode);
      logger.log("Parse error:\n %s", parseResult);
    } else {
      ParseStatistics parseStatistics =
          ParseStatistics.builder()
              .setNanoTime(end - start)
              .setUserTime(endUserTime - startUserTime)
              .setSystemTime(endSystemTime - startSystemTime)
              .setMemoryUsed(BenchmarkUtil.getMemoryUsed())
              .setDescriptorsCount(descriptorsCount)
              .setGSSNodesCount(countGSSNodes + 1) // + start gss node
              .setGSSEdgesCount(countGSSEdges)
              .setNonterminalNodesCount(countNonterminalNodes)
              .setTerminalNodesCount(countTerminalNodes)
              .setIntermediateNodesCount(countIntemediateNodes)
              .setPackedNodesCount(countPackedNodes)
              .setAmbiguousNodesCount(countAmbiguousNodes)
              .build();

      parseResult = new ParseSuccess(root, parseStatistics, input);
      logger.log("Parsing finished successfully.");
      logger.log(parseStatistics.toString());
    }

    return parseResult;
  }