forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDefinition.java
More file actions
85 lines (75 loc) · 3.49 KB
/
Definition.java
File metadata and controls
85 lines (75 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.inject.util.Providers;
import org.kframework.attributes.Source;
import org.kframework.kompile.Kompile;
import org.kframework.main.GlobalOptions;
import org.kframework.parser.concrete2kore.ParserUtils;
import org.kframework.utils.errorsystem.KExceptionManager;
import org.kframework.utils.file.FileUtil;
import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@API
public class Definition {
/**
* Parses the text to create a {@link Definition} object.
* The main module of the definition will be last module defined in the text file.
*/
public static org.kframework.definition.Definition from(String definitionText) {
Pattern pattern = Pattern.compile("(?:^|\\s)module ([A-Z][A-Z\\-]*)");
Matcher m = pattern.matcher(definitionText);
if(!m.find()) {
throw new RuntimeException("Could not find any module in the definition");
}
String nameOfLastModule = m.group(m.groupCount());
return from(definitionText, nameOfLastModule, Source.apply("generated"));
}
/**
* Parses the text to create a {@link Definition} object.
*/
public static org.kframework.definition.Definition from(String definitionText, String mainModuleName) {
return from(definitionText, mainModuleName, Source.apply("generated"));
}
/**
* Parses the text to create a {@link Definition} object.
*/
public static org.kframework.definition.Definition from(String definitionText, String mainModuleName, Source source) {
return from(definitionText, mainModuleName, source, Lists.newArrayList(Kompile.BUILTIN_DIRECTORY));
}
/**
* Parses the text to create a {@link Definition} object.
*/
public static org.kframework.definition.Definition from(String definitionText, String mainModuleName, Source source, List<File> lookupDirectories) {
File tempDir = Files.createTempDir();
File theFileUtilTempDir = new File(tempDir.getAbsolutePath() + File.pathSeparator + "tempDir");
File definitionDir = new File(tempDir.getAbsolutePath() + File.pathSeparator + "definitionDir");
File workingDir = new File(tempDir.getAbsolutePath() + File.pathSeparator + "workingDir");
File kompiledDir = new File(tempDir.getAbsolutePath() + File.pathSeparator + "kompiledDir");
if(!theFileUtilTempDir.mkdir() || !definitionDir.mkdir() || !workingDir.mkdir() || !kompiledDir.mkdir()) {
throw new AssertionError("Could not create one of the temporary directories");
}
GlobalOptions globalOptions = new GlobalOptions();
KExceptionManager kem = new KExceptionManager(globalOptions);
FileUtil fileUtil = new FileUtil(theFileUtilTempDir,
Providers.of(definitionDir),
workingDir,
Providers.of(kompiledDir),
globalOptions,
System.getenv());
ParserUtils parserUtils = new ParserUtils(fileUtil::resolveWorkingDirectory, kem, globalOptions);
org.kframework.definition.Definition definition = parserUtils.loadDefinition(
mainModuleName,
mainModuleName,
definitionText,
source,
workingDir,
lookupDirectories,
false
);
return definition;
}
}