Search in sources :

Example 1 with IndexedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType 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 IndexedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.

the class TypeKeeper method registerRequiredTypes.

private void registerRequiredTypes() {
    final Package swPakkage = this.getPackage("sw");
    swPakkage.put("object", new ObjectType(GlobalReference.of("sw:object")));
    swPakkage.put("unset", new IntrinsicType(GlobalReference.of("sw:unset")));
    swPakkage.put("false", new IntrinsicType(GlobalReference.of("sw:false")));
    swPakkage.put("maybe", new IntrinsicType(GlobalReference.of("sw:maybe")));
    swPakkage.put("integer", new IntrinsicType(GlobalReference.of("sw:integer")));
    swPakkage.put("bignum", new IntrinsicType(GlobalReference.of("sw:bignum")));
    swPakkage.put("float", new IntrinsicType(GlobalReference.of("sw:float")));
    swPakkage.put("symbol", new IndexedType(GlobalReference.of("sw:symbol")));
    swPakkage.put("character", new IntrinsicType(GlobalReference.of("sw:character")));
    swPakkage.put("sw_regexp", new IntrinsicType(GlobalReference.of("sw:sw_regexp")));
    swPakkage.put("char16_vector", new IndexedType(GlobalReference.of("sw:char16_vector")));
    swPakkage.put("simple_vector", new IndexedType(GlobalReference.of("sw:simple_vector")));
    swPakkage.put("heavy_thread", new IntrinsicType(GlobalReference.of("sw:heavy_thread")));
    swPakkage.put("light_thread", new IntrinsicType(GlobalReference.of("sw:light_thread")));
    swPakkage.put("condition", new SlottedType(GlobalReference.of("sw:condition")));
    swPakkage.put("enumeration_value", new SlottedType(GlobalReference.of("sw:enumeration_value")));
    swPakkage.put("indexed_format_mixin", new IntrinsicType(GlobalReference.of("sw:indexed_format_mixin")));
    swPakkage.put("slotted_format_mixin", new IntrinsicType(GlobalReference.of("sw:slotted_format_mixin")));
}
Also used : ObjectType(nl.ramsolutions.sw.magik.analysis.typing.types.ObjectType) IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) Package(nl.ramsolutions.sw.magik.analysis.typing.types.Package) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType)

Example 3 with IndexedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.

the class LocalTypeReasonerTest method testAssignmentMethod.

@Test
void testAssignmentMethod() {
    final String code = "" + "_method object.test\n" + "    _local a << property_list.new()\n" + "    a[:1] << 10\n" + "    _return a\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType propertyListType = new IndexedType(GlobalReference.of("sw:property_list"));
    propertyListType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(SelfType.INSTANCE));
    typeKeeper.addType(propertyListType);
    // 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:property_list");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 4 with IndexedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.

the class JsonTypeKeeperReader method handleType.

private void handleType(final JSONObject instruction) {
    final String typeFormat = instruction.getString("type_format");
    final String name = instruction.getString("type_name");
    final GlobalReference globalRef = GlobalReference.of(name);
    final MagikType type;
    if (this.typeKeeper.getType(globalRef) != UndefinedType.INSTANCE) {
        type = (MagikType) this.typeKeeper.getType(globalRef);
    } else if ("intrinsic".equals(typeFormat)) {
        type = new IntrinsicType(globalRef);
    } else if ("slotted".equals(typeFormat)) {
        type = new SlottedType(globalRef);
    } else if ("indexed".equals(typeFormat)) {
        type = new IndexedType(globalRef);
    } else {
        throw new InvalidParameterException("Unknown type: " + typeFormat);
    }
    if (instruction.has("doc")) {
        final String doc = instruction.getString("doc");
        type.setDoc(doc);
    }
    instruction.getJSONArray("slots").forEach(slotObj -> {
        final JSONObject slot = (JSONObject) slotObj;
        final String slotName = slot.getString("name");
        final Slot typeSlot = type.addSlot(null, slotName);
        final String slotTypeName = slot.getString("type_name");
        this.slotTypeNames.put(typeSlot, slotTypeName);
    });
    // Save inheritance for later...
    final JSONArray parents = instruction.getJSONArray("parents");
    this.typeParents.put(type, parents);
    this.typeKeeper.addType(type);
}
Also used : IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) InvalidParameterException(java.security.InvalidParameterException) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) JSONObject(org.json.JSONObject) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) JSONArray(org.json.JSONArray) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)

Example 5 with IndexedType

use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.

the class MagikIndexer method fillType.

private void fillType(final MagikType magikType, final MagikFile magikFile, final AstNode node, final String packageName, final Map<String, String> slots, final List<String> parents, @Nullable final AbstractType defaultParentType) {
    // Set location.
    final URI uri = magikFile.getUri();
    final Location location = new Location(uri, node);
    magikType.setLocation(location);
    // Set slots.
    magikType.clearSlots();
    slots.entrySet().forEach(entry -> {
        final String slotName = entry.getKey();
        final String slotTypeName = entry.getValue();
        final AbstractType slotType = this.typeParser.parseTypeString(slotTypeName, packageName);
        final Slot slot = magikType.addSlot(null, slotName);
        slot.setType(slotType);
    });
    // Parents.
    magikType.clearParents();
    final PackageNodeHelper packageHelper = new PackageNodeHelper(node);
    final String pakkageName = packageHelper.getCurrentPackage();
    parents.stream().forEach(parent -> {
        final GlobalReference parentGlobalRef = parent.indexOf(':') != -1 ? GlobalReference.of(parent) : GlobalReference.of(pakkageName, parent);
        final AbstractType parentType = this.typeKeeper.getType(parentGlobalRef);
        if (parentType == UndefinedType.INSTANCE) {
            LOGGER.warn("Parent type not found: {} from package: {}", parent, pakkageName);
        } else {
            magikType.addParent(parentType);
        }
    });
    // Default parent types ()
    boolean parentNonIntrinsicType = magikType.getParents().stream().anyMatch(parentType -> parentType instanceof SlottedType || parentType instanceof IndexedType);
    if (defaultParentType != null && !parentNonIntrinsicType) {
        magikType.addParent(defaultParentType);
    }
    // Type doc.
    final String typeDoc = MagikCommentExtractor.extractDocComments(node).map(Token::getValue).map(// Strip '##'
    line -> line.substring(2)).collect(Collectors.joining("\n"));
    magikType.setDoc(typeDoc);
}
Also used : 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) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Token(com.sonar.sslr.api.Token) URI(java.net.URI) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) Location(nl.ramsolutions.sw.magik.analysis.Location)

Aggregations

IndexedType (nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType)6 MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)5 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)4 AstNode (com.sonar.sslr.api.AstNode)3 IntrinsicType (nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType)3 SlottedType (nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType)3 Path (java.nio.file.Path)2 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)2 Package (nl.ramsolutions.sw.magik.analysis.typing.types.Package)2 Slot (nl.ramsolutions.sw.magik.analysis.typing.types.Slot)2 Token (com.sonar.sslr.api.Token)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Paths (java.nio.file.Paths)1 InvalidParameterException (java.security.InvalidParameterException)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1