Search in sources :

Example 1 with MagikType

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);
}
Also used : Path(java.nio.file.Path) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)

Example 2 with MagikType

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);
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) LoggerFactory(org.slf4j.LoggerFactory) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) Package(nl.ramsolutions.sw.magik.analysis.typing.types.Package) AliasType(nl.ramsolutions.sw.magik.analysis.typing.types.AliasType) IndexedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.IndexedExemplarDefinition) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) EnumerationDefinition(nl.ramsolutions.sw.magik.analysis.definitions.EnumerationDefinition) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) EnumSet(java.util.EnumSet) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) MethodDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition) TypeAnnotationHandler(nl.ramsolutions.sw.magik.analysis.typing.TypeAnnotationHandler) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ParameterDefinition(nl.ramsolutions.sw.magik.analysis.definitions.ParameterDefinition) GlobalDefinition(nl.ramsolutions.sw.magik.analysis.definitions.GlobalDefinition) HashMap(java.util.HashMap) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) HashSet(java.util.HashSet) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Location(nl.ramsolutions.sw.magik.analysis.Location) Nullable(javax.annotation.Nullable) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) MagikCommentExtractor(nl.ramsolutions.sw.magik.parser.MagikCommentExtractor) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Token(com.sonar.sslr.api.Token) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Logger(org.slf4j.Logger) MagikFile(nl.ramsolutions.sw.magik.MagikFile) IOException(java.io.IOException) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) BinaryOperatorDefinition(nl.ramsolutions.sw.magik.analysis.definitions.BinaryOperatorDefinition) MixinDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MixinDefinition) IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) PackageDefinition(nl.ramsolutions.sw.magik.analysis.definitions.PackageDefinition) Paths(java.nio.file.Paths) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) Collections(java.util.Collections) BinaryOperator(nl.ramsolutions.sw.magik.analysis.typing.BinaryOperator) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) Path(java.nio.file.Path) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition)

Example 3 with MagikType

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();
}
Also used : Scanner(java.util.Scanner) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) ArrayList(java.util.ArrayList) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)

Example 4 with MagikType

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");
    }
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) ProcedureInstance(nl.ramsolutions.sw.magik.analysis.typing.types.ProcedureInstance) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Example 5 with MagikType

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");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Aggregations

MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)64 Test (org.junit.jupiter.api.Test)47 AstNode (com.sonar.sslr.api.AstNode)33 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)32 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)28 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)22 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)20 TypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.TypeKeeper)18 SlottedType (nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType)17 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)16 Method (nl.ramsolutions.sw.magik.analysis.typing.types.Method)14 Location (nl.ramsolutions.sw.magik.analysis.Location)10 IntrinsicType (nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType)10 Parameter (nl.ramsolutions.sw.magik.analysis.typing.types.Parameter)9 Position (org.eclipse.lsp4j.Position)9 IndexedType (nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType)8 Path (java.nio.file.Path)7 URI (java.net.URI)6 Collections (java.util.Collections)5 EnumSet (java.util.EnumSet)5