|
| 1 | +// Copyright (c) 2015 K Team. All Rights Reserved. |
| 2 | +package org.kframework; |
| 3 | + |
| 4 | +import org.kframework.attributes.Source; |
| 5 | +import org.kframework.definition.Module; |
| 6 | +import org.kframework.kore.K; |
| 7 | +import org.kframework.kore.Sort; |
| 8 | +import org.kframework.parser.concrete2kore.ParseInModule; |
| 9 | +import org.kframework.utils.errorsystem.ParseFailedException; |
| 10 | +import scala.Option; |
| 11 | +import scala.Tuple2; |
| 12 | +import scala.util.Either; |
| 13 | +import scala.util.control.Exception; |
| 14 | + |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.Set; |
| 17 | + |
| 18 | +/** |
| 19 | + * Parses a string with a particular start symbol/sort. |
| 20 | + * Constructed via the static {@link #from methods} |
| 21 | + */ |
| 22 | +@API |
| 23 | +public class Parser { |
| 24 | + private final ParseInModule parseInModule; |
| 25 | + |
| 26 | + private Parser(Module module) { |
| 27 | + this.parseInModule = new ParseInModule(module); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Parses a string with a particular start symbol/sort. |
| 32 | + * |
| 33 | + * @param startSymbol the start symbol/sort |
| 34 | + * @param toParse the String to parse |
| 35 | + * @param fromSource the Source of the String toParse |
| 36 | + * @return a pair: the left projection is a parsed string as a K, if successful; |
| 37 | + * the right projection is the set of issues encountered while parsing |
| 38 | + */ |
| 39 | + @SuppressWarnings("unchecked") |
| 40 | + public Tuple2<Option<K>, Set<Warning>> apply(Sort startSymbol, String toParse, Source fromSource) { |
| 41 | + Tuple2<Either<Set<ParseFailedException>, K>, Set<ParseFailedException>> res = parseInModule.parseString(toParse, startSymbol, fromSource); |
| 42 | + |
| 43 | + Set<Warning> problemSet = new HashSet<>(); |
| 44 | + problemSet.addAll((Set<Warning>) (Object) res._2()); |
| 45 | + if (res._1().isLeft()) |
| 46 | + problemSet.addAll((Set<Warning>) (Object) res._1().left().get()); |
| 47 | + return Tuple2.apply(res._1().right().toOption(), problemSet); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Parses a string with a particular start symbol/sort. |
| 52 | + * |
| 53 | + * @param startSymbol the start symbol/sort |
| 54 | + * @param toParse the String to parse |
| 55 | + * @return a pair: the left projection is a parsed string as a K, if successful; |
| 56 | + * the right projection is the set of issues encountered while parsing |
| 57 | + */ |
| 58 | + public Tuple2<Option<K>, Set<Warning>> apply(Sort startSymbol, String toParse) { |
| 59 | + return apply(startSymbol, toParse, Source.apply("generated")); |
| 60 | + } |
| 61 | + |
| 62 | + public static Parser from(Module module) { |
| 63 | + return new Parser(module); |
| 64 | + } |
| 65 | +} |
0 commit comments