use of nl.ramsolutions.sw.magik.parser.NewDocParser 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.parser.NewDocParser in project magik-tools by StevenLooman.
the class NewDocTypeExistsTypeCheck method checkDefinitionLoops.
private void checkDefinitionLoops(final NewDocParser newDocParser, final String pakkage) {
// Test @loop types.
final ITypeKeeper typeKeeper = this.getTypeKeeper();
final TypeParser typeParser = new TypeParser(typeKeeper);
newDocParser.getLoopTypeNodes().entrySet().stream().filter(entry -> typeParser.parseTypeString(entry.getValue(), pakkage) == UndefinedType.INSTANCE).forEach(entry -> {
final AstNode typeNode = entry.getKey();
final String typeName = entry.getValue();
final String message = String.format(MESSAGE, typeName);
this.addIssue(typeNode, message);
});
}
use of nl.ramsolutions.sw.magik.parser.NewDocParser in project magik-tools by StevenLooman.
the class NewDocTypeExistsTypeCheck method walkPostProcedureInvocation.
@Override
protected void walkPostProcedureInvocation(final AstNode node) {
if (!DefSlottedExemplarParser.isDefSlottedExemplar(node)) {
return;
}
final ITypeKeeper typeKeeper = this.getTypeKeeper();
final TypeParser typeParser = new TypeParser(typeKeeper);
// Get slot defintions.
final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
final NewDocParser newDocParser = new NewDocParser(statementNode);
final Map<AstNode, String> slotTypeNodes = newDocParser.getSlotTypeNodes();
final PackageNodeHelper helper = new PackageNodeHelper(node);
final String currentPakkage = helper.getCurrentPackage();
// Test slot types.
slotTypeNodes.entrySet().stream().filter(entry -> {
final AbstractType type = typeParser.parseTypeString(entry.getValue(), currentPakkage);
return type == UndefinedType.INSTANCE;
}).forEach(entry -> {
final AstNode typeNode = entry.getKey();
final String typeName = entry.getValue();
final String message = String.format(MESSAGE, typeName);
this.addIssue(typeNode, message);
});
}
use of nl.ramsolutions.sw.magik.parser.NewDocParser in project magik-tools by StevenLooman.
the class NewDocCheck method walkPostProcedureInvocation.
@Override
protected void walkPostProcedureInvocation(AstNode node) {
if (!DefSlottedExemplarParser.isDefSlottedExemplar(node)) {
return;
}
// Get slot defintions.
final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
final NewDocParser newDocParser = new NewDocParser(statementNode);
final Map<String, AstNode> docSlotNameNodes = newDocParser.getSlotNameNodes();
final DefSlottedExemplarParser parser = new DefSlottedExemplarParser(node);
final List<Definition> definitions = parser.parseDefinitions();
final SlottedExemplarDefinition exemplarDefinition = (SlottedExemplarDefinition) definitions.get(0);
final List<Slot> slots = exemplarDefinition.getSlots();
final Map<String, Slot> slotNames = slots.stream().collect(Collectors.toMap(Slot::getName, slot -> slot));
// Compare parameters.
docSlotNameNodes.entrySet().stream().filter(entry -> !slotNames.containsKey(entry.getKey())).forEach(entry -> {
final String docName = entry.getKey();
final AstNode docNode = entry.getValue();
final String message = String.format(MESSAGE_SLOT_UNKNOWN, docName);
this.addIssue(docNode, message);
});
slotNames.entrySet().stream().filter(entry -> !docSlotNameNodes.containsKey(entry.getKey())).forEach(entry -> {
final String docName = entry.getKey();
final AstNode docNode = entry.getValue().getNode();
final String message = String.format(MESSAGE_SLOT_MISSING, docName);
this.addIssue(docNode, message);
});
}
use of nl.ramsolutions.sw.magik.parser.NewDocParser in project magik-tools by StevenLooman.
the class SemanticTokenWalker method walkCommentToken.
@SuppressWarnings("checkstyle:NestedIfDepth")
private void walkCommentToken(final Token token) {
final String value = token.getOriginalValue();
if (value.startsWith("##")) {
final Set<SemanticToken.Modifier> docModifier = Set.of(SemanticToken.Modifier.DOCUMENTATION);
final List<Token> docTokens = List.of(token);
final NewDocParser newDocParser = new NewDocParser(docTokens);
final AstNode newDocNode = newDocParser.getNewDocNode();
// It is either a FUNCTION node or element nodes.
final AstNode functionNode = newDocNode.getFirstChild(NewDocGrammar.FUNCTION);
if (functionNode != null) {
this.addSemanticToken(token, SemanticToken.Type.COMMENT, docModifier);
}
final List<AstNode> elementNodes = newDocNode.getChildren(NewDocGrammar.PARAM, NewDocGrammar.RETURN, NewDocGrammar.LOOP, NewDocGrammar.SLOT);
elementNodes.forEach(elementNode -> {
// DOC_START
final AstNode docStartNode = elementNode.getFirstChild(NewDocGrammar.DOC_START);
this.addSemanticToken(docStartNode, SemanticToken.Type.COMMENT, docModifier);
// Element
final AstNode keywordNode = elementNode.getChildren().get(1);
this.addSemanticToken(keywordNode, SemanticToken.Type.KEYWORD, docModifier);
// TYPE
final AstNode typeNode = elementNode.getFirstChild(NewDocGrammar.TYPE);
if (typeNode != null) {
final List<AstNode> typeNodes = typeNode.getDescendants(NewDocGrammar.TYPE_NAME, NewDocGrammar.TYPE_CLONE, NewDocGrammar.TYPE_SELF);
typeNodes.forEach(typeTypeNode -> {
final String identifier = typeTypeNode.getTokenValue();
final GlobalReference globalRef = identifier.indexOf(':') != -1 ? GlobalReference.of(identifier) : GlobalReference.of(this.currentPakkage, identifier);
if (typeTypeNode.is(NewDocGrammar.TYPE_CLONE, NewDocGrammar.TYPE_SELF)) {
final Set<SemanticToken.Modifier> constModifier = Set.of(SemanticToken.Modifier.DOCUMENTATION, SemanticToken.Modifier.READONLY);
this.addSemanticToken(typeTypeNode, SemanticToken.Type.CLASS, constModifier);
} else if (this.isKnownType(globalRef)) {
this.addSemanticToken(typeTypeNode, SemanticToken.Type.CLASS, docModifier);
}
});
}
// NAME
final AstNode nameNode = elementNode.getFirstChild(NewDocGrammar.NAME);
if (nameNode != null && nameNode.getToken() != null) {
final SemanticToken.Type type = NEW_DOC_ELEMENT_TYPE_MAPPING.get(elementNode.getType());
this.addSemanticToken(nameNode, type, docModifier);
}
// DESCRIPTION
final List<AstNode> descriptionNodes = elementNode.getChildren(NewDocGrammar.DESCRIPTION);
descriptionNodes.forEach(descriptionNode -> this.addSemanticToken(descriptionNode, SemanticToken.Type.COMMENT, docModifier));
});
} else {
this.addSemanticToken(token, SemanticToken.Type.COMMENT);
}
}
Aggregations