Skip to content

Commit

Permalink
refactor Kast to lazy initliaze the parser generator
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhang90 committed Feb 6, 2017
1 parent c9de3ca commit a16f4b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
27 changes: 18 additions & 9 deletions kernel/src/main/java/org/kframework/kast/Kast.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@
public class Kast {

private transient Map<String, UserParser> cachedParsers = new HashMap<>();
private ParserGenerator generator;
private ParserGenerator generator = null;
private FileUtil files;

public Kast(ParserGenerator generator){
this.generator = generator;
public Kast(FileUtil files){
this.files = files;
}

public static K parseWithUserParser(String toParse, Source source, Sort startSymbol, String moduleName,
FileUtil files, KExceptionManager kem) {
return parseWithUserParser(toParse, source.source(), startSymbol.name(), moduleName, files, kem);
public static K parseWithModuleParser(String toParse, Source source, Sort startSymbol, String moduleName,
FileUtil files, KExceptionManager kem) {
return parseWithModuleParser(toParse, source.source(), startSymbol.name(), moduleName, files, kem);
}

// This function is used for one-time parsing. If the corresponding parser object exists in the extra directory, the
// function deserialize the object and use it to parse the text. If the parser object does not exist, the function
// will load the parser generator to generate one and then parse the text.
public static K parseWithUserParser(String toParse, String source, String startSymbol, String moduleName,
FileUtil files, KExceptionManager kem) {
public static K parseWithModuleParser(String toParse, String source, String startSymbol, String moduleName,
FileUtil files, KExceptionManager kem) {
String modulePath = files.moduleDerivedParserPath(moduleName);
BinaryLoader loader = new BinaryLoader(kem);
UserParser parser;
Expand Down Expand Up @@ -71,7 +72,15 @@ public K parseWithUserParserAndCache(String toParse, String source, String start
UserParser parser;
parser = cachedParsers.get(moduleName);
if(parser == null) {
parser = generator.getParser(moduleName, kem);
if(this.generator == null) {
try {
BinaryLoader loader = new BinaryLoader(kem);
this.generator = loader.loadOrDie(ParserGenerator.class, files.resolveKompiled(FileUtil.PARSER_GENERATOR_BIN));
}catch (KEMException e) {
throw KEMException.innerParserError("Parser Generator can not be deserialized.");
}
}
parser = this.generator.getParser(moduleName, kem);
cachedParsers.put(moduleName, parser);
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/main/java/org/kframework/kast/KastFrontEnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public int run() {
}
}
String moduleName = options.module == null ? kompileMetaInfo.mainSyntaxModuleName : options.module;
K parsed = Kast.parseWithUserParser(FileUtil.read(stringToParse),source, sort, moduleName, files ,kem);
K parsed = Kast.parseWithModuleParser(FileUtil.read(stringToParse),source, sort, moduleName, files ,kem);
System.out.println(ToKast.apply(parsed));
sw.printTotal("Total");
return 0;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/main/java/org/kframework/krun/KRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public K parse(String parser, String value, Sort startSymbol, Source source, Str
}*/
if(parser == null) {
String toParse = FileUtil.read(files.readFromWorkingDirectory(value));
return Kast.parseWithUserParser(toParse, source, startSymbol, mainSyntaxModuleName, files, this.kem);
return Kast.parseWithModuleParser(toParse, source, startSymbol, mainSyntaxModuleName, files, this.kem);
} else {
// ToDo(Yi): Update this branch when kast interface is nailed down.
List<String> tokens = new ArrayList<>(Arrays.asList(parser.split(" ")));
Expand Down

0 comments on commit a16f4b4

Please sign in to comment.