/** * Creates a parser for a module. * Will probably want to move the method out of this class here eventually. * * @return a function taking a String to be parsed, a Source, and returning the parsed string as K. */ public BiFunction<String, Source, K> getParser(Module module, Sort programStartSymbol, KExceptionManager kem) { ParseInModule parseInModule = RuleGrammarGenerator.getCombinedGrammar(module, kompileOptions.strict()); return (BiFunction<String, Source, K> & Serializable) (s, source) -> { Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> res = parseInModule.parseString(s, programStartSymbol, source); kem.addAllKException(res._2().stream().map(e -> e.getKException()).collect(Collectors.toSet())); if (res._1().isLeft()) { throw res._1().left().get().iterator().next(); } return new TreeNodesToKORE(Outer::parseSort, kompileOptions.strict()).down(res._1().right().get()); }; }
public Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> parseString(String input, Sort startSymbol, Scanner scanner, Source source, int startLine, int startColumn, boolean inferSortChecks) { final Tuple2<Either<Set<ParseFailedException>, Term>, Set<ParseFailedException>> result = parseStringTerm(input, startSymbol, scanner, source, startLine, startColumn, inferSortChecks); Either<Set<ParseFailedException>, K> parseInfo; if (result._1().isLeft()) { parseInfo = Left.apply(result._1().left().get()); } else { parseInfo = Right.apply(new TreeNodesToKORE(Outer::parseSort, inferSortChecks && strict).apply(result._1().right().get())); } return new Tuple2<>(parseInfo, result._2()); }
private Stream<? extends K> performParse(Map<String, ParsedSentence> cache, ParseInModule parser, Scanner scanner, Bubble b) { int startLine = b.att().get("contentStartLine", Integer.class); int startColumn = b.att().get("contentStartColumn", Integer.class); Source source = b.att().get(Source.class); Tuple2<Either<java.util.Set<ParseFailedException>, K>, java.util.Set<ParseFailedException>> result; if (cache.containsKey(b.contents())) { ParsedSentence parse = cache.get(b.contents()); Optional<Source> cacheSource = parse.getParse().source(); //Cache might contain content from an identical file but another source path. //The content will have wrong Source attribute and must be invalidated. if (cacheSource.isPresent() && cacheSource.get().equals(source)) { cachedBubbles.getAndIncrement(); kem.addAllKException(parse.getWarnings().stream().map(e -> e.getKException()).collect(Collectors.toList())); return Stream.of(parse.getParse()); } } result = parser.parseString(b.contents(), START_SYMBOL, scanner, source, startLine, startColumn, !b.att().contains("macro") && !b.att().contains("alias")); parsedBubbles.getAndIncrement(); kem.addAllKException(result._2().stream().map(e -> e.getKException()).collect(Collectors.toList())); if (result._1().isRight()) { KApply k = (KApply) new TreeNodesToKORE(Outer::parseSort, isStrict).down(result._1().right().get()); k = KApply(k.klabel(), k.klist(), k.att().addAll(b.att().remove("contentStartLine").remove("contentStartColumn").remove(Source.class).remove(Location.class))); cache.put(b.contents(), new ParsedSentence(k, new HashSet<>(result._2()))); return Stream.of(k); } else { errors.addAll(result._1().left().get()); return Stream.empty(); } } }
for (Term t : amb.items()) { candidate = this.apply(t); K next = new TreeNodesToKORE(Outer::parseSort, strict).apply(new RemoveBracketVisitor().apply(candidate._1().right().get())); if (last != null) { if (!last.equals(next)) {
private K parseTerm(String pgm, ParseInModule parser) { Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> result = parser.parseString(pgm, Sorts.K(), Source.apply("generated by AddBracketsTest")); assertEquals(0, result._2().size()); return new TreeNodesToKORE(Outer::parseSort, false).down(result._1().right().get()); }
private void parseTerm(String term, String sort, K expected, int expectWarnings) { String source = "AddEmpytListsTest." + testName.getMethodName(); final Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> parseResult = parser.parseString(term, Sort(sort), new Source(source)); if (parseResult._1().isLeft()) { Assert.assertTrue("Unexpected parse errors" + parseResult._1().left().get(), false); } K actual = new TreeNodesToKORE(Outer::parseSort, false).down(parseResult._1().right().get()); Assert.assertEquals(expected, actual); if (parseResult._2().size() != expectWarnings) { Assert.assertTrue("Unexpected parse warnings" + parseResult._2(), false); } }