use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class MagikIndexer method handleDefinition.
private void handleDefinition(final MagikFile magikFile, final IndexedExemplarDefinition definition) {
final AstNode node = definition.getNode();
final GlobalReference globalRef = definition.getGlobalReference();
final MagikType type = this.typeKeeper.getType(globalRef) != UndefinedType.INSTANCE ? (MagikType) this.typeKeeper.getType(globalRef) : new IndexedType(globalRef);
this.typeKeeper.addType(type);
final Map<String, String> slots = Collections.emptyMap();
final List<String> parents = definition.getParents();
final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:indexed_format_mixin"));
this.fillType(type, magikFile, node, globalRef.getPakkage(), slots, parents, defaultParentType);
final Path path = Paths.get(magikFile.getUri());
this.indexedTypes.get(path).add(type);
LOGGER.debug("Indexed type: {}", type);
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class MagikIndexer method handleDefinition.
private void handleDefinition(final MagikFile magikFile, final SlottedExemplarDefinition definition) {
final AstNode node = definition.getNode();
final GlobalReference globalRef = definition.getGlobalReference();
final MagikType type = this.typeKeeper.getType(globalRef) instanceof SlottedType ? (MagikType) this.typeKeeper.getType(globalRef) : new SlottedType(globalRef);
this.typeKeeper.addType(type);
final NewDocParser docParser = new NewDocParser(node);
final Map<String, String> slotTypes = docParser.getSlotTypes();
// This needs a default value ("") due to https://bugs.openjdk.java.net/browse/JDK-8148463
final Map<String, String> slots = definition.getSlots().stream().map(SlottedExemplarDefinition.Slot::getName).collect(Collectors.toMap(slotName -> slotName, slotName -> slotTypes.getOrDefault(slotName, "")));
final List<String> parents = definition.getParents();
final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:slotted_format_mixin"));
this.fillType(type, magikFile, node, globalRef.getPakkage(), slots, parents, defaultParentType);
final Path path = Paths.get(magikFile.getUri());
this.indexedTypes.get(path).add(type);
LOGGER.debug("Indexed type: {}", type);
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class ClassInfoTypeKeeperReader method readMethod.
private void readMethod(final String line, final BufferedReader reader) throws IOException {
// - 1 : "method" <class name> <method name> <parameters> <-- possibly no assignment parameter
// 2 : n ["private"/"classconst"/"classvar"/"iter"]*
// ["basic"/"restricted"/"internal"/pragma]* source_file
// 3+: <n lines of comments>
// Line 1
// ignore <global> and <condition> for now
final MagikType type;
final String methodName;
try (Scanner scanner = new Scanner(line)) {
// "method"
scanner.next();
final String className = scanner.next();
if (!"<global>".equals(className) && !"<condition>".equals(className)) {
final GlobalReference globalRef = GlobalReference.of(className);
methodName = scanner.next();
if (!this.typeKeeper.hasType(globalRef)) {
// LOGGER.debug("Type not found: {}, for method: {}", pakkageName, methodName);
type = null;
} else {
type = (MagikType) this.typeKeeper.getType(globalRef);
}
// <parameters>
scanner.nextLine();
} else {
// Skip line
scanner.nextLine();
type = null;
methodName = null;
}
}
// Line 2
int commentLineCount = 0;
try (Scanner scanner = new Scanner(reader.readLine())) {
commentLineCount = scanner.nextInt();
final List<String> pragmas = new ArrayList<>();
final List<String> skipList = List.of("private", "classconst", "classvar", "iter");
while (scanner.hasNext("[^/]+")) {
final String pragma = scanner.next();
if (skipList.contains(pragma)) {
continue;
}
pragmas.add(pragma);
}
// Skip path
scanner.nextLine();
}
// Line 3+
final StringBuilder commentBuilder = new StringBuilder();
for (int i = 0; i < commentLineCount; ++i) {
final String commentLine = reader.readLine();
commentBuilder.append(commentLine);
commentBuilder.append('\n');
}
final String comment = commentBuilder.toString();
if (type != null) {
for (final Method method : type.getMethods(methodName)) {
method.setDoc(comment);
}
}
reader.readLine();
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testReasonMethodReturnProc.
@Test
void testReasonMethodReturnProc() {
final String code = "" + "_method object.test\n" + " _return _proc@test()\n" + " >> 10\n" + " _endproc\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final ProcedureInstance resultType = (ProcedureInstance) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getProcedureName()).isEqualTo("test");
final Collection<Method> procMethods = resultType.getMethods("invoke()");
assertThat(procMethods).isNotEmpty();
for (final Method procMethod : procMethods) {
final ExpressionResult procResult = procMethod.getCallResult();
assertThat(procResult.size()).isEqualTo(1);
final MagikType procResultType = (MagikType) procResult.get(0, null);
assertThat(procResultType).isNotNull();
assertThat(procResultType.getFullName()).isEqualTo("sw:integer");
}
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testEmptyLocalDefinition.
@Test
void testEmptyLocalDefinition() {
final String code = "" + "_method object.test\n" + " _local a\n" + " _return a\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:unset");
}
Aggregations