|
| 1 | +import logging |
| 2 | +import re |
| 3 | +from types import NoneType |
| 4 | +from typing import ClassVar |
| 5 | +from aegis_core.ast.helpers import offset_location |
| 6 | +from aegis_core.ast.metadata import VariableMetadata, retrieve_metadata |
| 7 | +from aegis_core.semantics import TokenModifier, TokenType |
| 8 | +from bolt import ( |
| 9 | + AstClassName, |
| 10 | + AstFormatString, |
| 11 | + AstFunctionSignature, |
| 12 | + AstFunctionSignatureArgument, |
| 13 | + AstValue, |
| 14 | +) |
| 15 | +from mecha import AstNode |
| 16 | +from mecha.ast import AstItemSlot |
| 17 | +from tokenstream import SourceLocation |
| 18 | +from .provider import BaseFeatureProvider |
| 19 | + |
| 20 | + |
| 21 | +__all__ = [ |
| 22 | + "ItemSlotProvider", |
| 23 | + "ClassNameProvider", |
| 24 | + "FunctionSignatureProvider", |
| 25 | + "FunctionSignatureArgProvider", |
| 26 | + "ValueProvider", |
| 27 | + "FormatStringProvider", |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +# --- Mecha --- |
| 32 | +class ItemSlotProvider(BaseFeatureProvider[AstItemSlot]): |
| 33 | + @classmethod |
| 34 | + def semantics(cls, params): |
| 35 | + return [(params.node, "variable", ["readonly"])] |
| 36 | + |
| 37 | + |
| 38 | +# --- Bolt --- |
| 39 | +class ClassNameProvider(BaseFeatureProvider[AstClassName]): |
| 40 | + @classmethod |
| 41 | + def semantics(cls, params): |
| 42 | + return [(params.node, "class", [])] |
| 43 | + |
| 44 | + |
| 45 | +class FunctionSignatureProvider(BaseFeatureProvider[AstFunctionSignature]): |
| 46 | + @classmethod |
| 47 | + def semantics(cls, params): |
| 48 | + signature = params.node |
| 49 | + location = signature.location |
| 50 | + node = AstNode( |
| 51 | + location=location, |
| 52 | + end_location=offset_location(signature.location, len(signature.name)), |
| 53 | + ) |
| 54 | + |
| 55 | + tokens = [(node, "function", [])] |
| 56 | + |
| 57 | + if len(signature.arguments) >= 1: |
| 58 | + first = signature.arguments[0] |
| 59 | + |
| 60 | + if isinstance(first, AstFunctionSignatureArgument) and first.name == "self": |
| 61 | + tokens.append((first, "macro", [])) |
| 62 | + |
| 63 | + return tokens |
| 64 | + |
| 65 | + |
| 66 | +class FunctionSignatureArgProvider(BaseFeatureProvider[AstFunctionSignatureArgument]): |
| 67 | + @classmethod |
| 68 | + def semantics(cls, params): |
| 69 | + if params.node.type_annotation: |
| 70 | + return [(params.node.type_annotation, "class", [])] |
| 71 | + |
| 72 | + return None |
| 73 | + |
| 74 | + |
| 75 | +class ValueProvider(BaseFeatureProvider[AstValue]): |
| 76 | + @classmethod |
| 77 | + def semantics(cls, params): |
| 78 | + value = params.node |
| 79 | + |
| 80 | + metadata = retrieve_metadata(value, VariableMetadata) |
| 81 | + |
| 82 | + if not metadata or not metadata.type_annotation: |
| 83 | + return None |
| 84 | + |
| 85 | + annotation = metadata.type_annotation |
| 86 | + |
| 87 | + if annotation is NoneType: |
| 88 | + |
| 89 | + return [(value, "variable", ["readonly"])] |
| 90 | + |
| 91 | + elif annotation is bool: |
| 92 | + return [(value, "macro", [])] |
| 93 | + |
| 94 | + return None |
| 95 | + |
| 96 | + |
| 97 | +class FormatStringProvider(BaseFeatureProvider[AstFormatString]): |
| 98 | + FORMAT_REGEX: ClassVar[re.Pattern] = re.compile(r"\{(:.+)?\}") |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def semantics(cls, params): |
| 102 | + format_string = params.node |
| 103 | + |
| 104 | + formats = cls.FORMAT_REGEX.findall(format_string.fmt) |
| 105 | + |
| 106 | + nodes: list[tuple[AstNode, TokenType, list[TokenModifier]]] = [] |
| 107 | + |
| 108 | + nodes.append( |
| 109 | + ( |
| 110 | + AstNode( |
| 111 | + format_string.location, offset_location(format_string.location, 1) |
| 112 | + ), |
| 113 | + "macro", |
| 114 | + [], |
| 115 | + ) |
| 116 | + ) |
| 117 | + |
| 118 | + for format, value in zip(formats, format_string.values): |
| 119 | + nodes.append( |
| 120 | + ( |
| 121 | + AstNode(offset_location(value.location, -1), value.location), |
| 122 | + "macro", |
| 123 | + [], |
| 124 | + ) |
| 125 | + ) |
| 126 | + nodes.append( |
| 127 | + ( |
| 128 | + AstNode( |
| 129 | + value.end_location, |
| 130 | + offset_location(value.end_location, 1 + len(format)), |
| 131 | + ), |
| 132 | + "macro", |
| 133 | + [], |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + return nodes |
0 commit comments