|
| 1 | +package org.kframework; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import com.google.common.io.Files; |
| 5 | +import com.google.inject.util.Providers; |
| 6 | +import org.kframework.attributes.Source; |
| 7 | +import org.kframework.kompile.Kompile; |
| 8 | +import org.kframework.main.GlobalOptions; |
| 9 | +import org.kframework.parser.concrete2kore.ParserUtils; |
| 10 | +import org.kframework.utils.errorsystem.KExceptionManager; |
| 11 | +import org.kframework.utils.file.FileUtil; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.util.regex.Matcher; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +@API |
| 18 | +public class Definition { |
| 19 | + |
| 20 | + /** |
| 21 | + * Parses the text to create a {@link Definition} object. |
| 22 | + * The main module of the definition will be last module defined in the text file. |
| 23 | + */ |
| 24 | + public static org.kframework.definition.Definition from(String definitionText) { |
| 25 | + Pattern pattern = Pattern.compile("module ([A-Z_]*)"); |
| 26 | + Matcher m = pattern.matcher(definitionText); |
| 27 | + if(!m.find()) { |
| 28 | + throw new RuntimeException("Could not find any module in the definition"); |
| 29 | + } |
| 30 | + String nameOfLastModule = m.group(m.groupCount()); |
| 31 | + return from(definitionText, nameOfLastModule, Source.apply("generated")); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Parses the text to create a {@link Definition} object. |
| 36 | + */ |
| 37 | + public static org.kframework.definition.Definition from(String definitionText, String mainModuleName) { |
| 38 | + return from(definitionText, mainModuleName, Source.apply("generated")); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Parses the text to create a {@link Definition} object. |
| 43 | + */ |
| 44 | + public static org.kframework.definition.Definition from(String definitionText, String mainModuleName, Source source) { |
| 45 | + File tmpDir = Files.createTempDir(); |
| 46 | + File tempDir = new File(tmpDir.getAbsolutePath() + File.pathSeparator + "tempDir"); |
| 47 | + File definitionDir = new File(tmpDir.getAbsolutePath() + File.pathSeparator + "definitionDir"); |
| 48 | + File workingDir = new File(tmpDir.getAbsolutePath() + File.pathSeparator + "workingDir"); |
| 49 | + File kompiledDir = new File(tmpDir.getAbsolutePath() + File.pathSeparator + "kompiledDir"); |
| 50 | + if(!tempDir.mkdir() || !definitionDir.mkdir() || !workingDir.mkdir() || !kompiledDir.mkdir()) { |
| 51 | + throw new AssertionError("Could not create one of the temporary directories"); |
| 52 | + } |
| 53 | + GlobalOptions globalOptions = new GlobalOptions(); |
| 54 | + KExceptionManager kem = new KExceptionManager(globalOptions); |
| 55 | + |
| 56 | + FileUtil fileUtil = new FileUtil(tempDir, |
| 57 | + Providers.of(definitionDir), |
| 58 | + workingDir, |
| 59 | + Providers.of(kompiledDir), |
| 60 | + globalOptions, |
| 61 | + System.getenv()); |
| 62 | + ParserUtils parserUtils = new ParserUtils(fileUtil, kem, globalOptions); |
| 63 | + |
| 64 | + org.kframework.definition.Definition definition = parserUtils.loadDefinition( |
| 65 | + mainModuleName, |
| 66 | + mainModuleName, |
| 67 | + definitionText, |
| 68 | + source, |
| 69 | + workingDir, |
| 70 | + Lists.newArrayList(Kompile.BUILTIN_DIRECTORY), |
| 71 | + true |
| 72 | + ); |
| 73 | + |
| 74 | + return definition; |
| 75 | + } |
| 76 | +} |
0 commit comments