ts_languages.py

clew/guard/ts_languages.py

Tree-sitter grammar loading and language-specific node type maps.

_language_from_config

str | None clew.guard.ts_languages._language_from_config(str ext, dict[str, Any] config)

Look up language name from user config extensions.

clew/guard/ts_languages.py:175

Source · 8 lines
def _language_from_config(ext: str, config: dict[str, Any]) -> str | None:
    languages = config.get("validate", {}).get("languages", {})
    for lang_name, lang_config in languages.items():
        if ext in lang_config.get("extensions", []) and lang_name in LANGUAGE_SPECS:
            return lang_name
    return None

Activity · 5 steps
lines 176–176languages = config.get("validate", {}).get("languages", {})lines 177–177◇ for lang_name, lang_config in languages.items()lines 178–178◇ if ext in lang_config.get("extensions", []) and lang_name in LANGUAGE_SPECSlines 179–179return lang_namelines 180–180return None

Flows

_looks_like_cpp_header

bool clew.guard.ts_languages._looks_like_cpp_header(str file_path)

Detect C++ constructs in a .h file to route to cpp grammar.

clew/guard/ts_languages.py:188

Source · 15 lines
def _looks_like_cpp_header(file_path: str) -> bool:
    try:
        with open(file_path, encoding="utf-8", errors="replace") as f:
            content = f.read(8192)
    except OSError:
        return False
    import re

    patterns = (
        r"^\s*namespace\s+\w",
        r"^\s*template\s*<",
        r"^\s*class\s+\w",
        r"^\s*(?:public|private|protected)\s*:",
    )
    return any(re.search(p, content, re.MULTILINE) for p in patterns)

1 more lines, not shown

Activity · 8 steps
lines 189–189trylines 190–190with open(file_path, encoding="utf-8", errors="replace") as flines 191–191content = f.read(8192)lines 192–192except OSErrorlines 193–193return Falselines 194–194import relines 196–201patterns = ( r"^\s*namespace\s+\w", r"^\s*template\s*<", r"^\s*class\s+\w", r"^\s*(?:public|private|protected)\s*:", )lines 202–202return any(re.search(p, content, re.MULTILINE) for p in patterns)

Flows

_substrate_extension_table

dict[str, str] clew.guard.ts_languages._substrate_extension_table()

Build the extension -> language table from the parsing substrate's registry.

clew/guard/ts_languages.py:104

Show on the map →

Called by none

Calls none

Source · 12 lines
def _substrate_extension_table() -> dict[str, str]:
    import lang_parsing_substrate

    table: dict[str, str] = {}
    for info in lang_parsing_substrate.languages():
        if info.key not in LANGUAGE_SPECS:
            continue
        for ext in (*info.extensions, *info.explicit_only):
            table.setdefault(f".{ext}", info.key)
    return table

Activity · 8 steps
lines 105–105import lang_parsing_substratelines 107–107table: dict[str, str] = {}lines 108–108◇ for info in lang_parsing_substrate.languages()lines 109–109◇ if info.key not in LANGUAGE_SPECSlines 110–110continuelines 111–111◇ for ext in (*info.extensions, *info.explicit_only)lines 112–112table.setdefault(f".{ext}", info.key)lines 113–113return table

Requirements

REQ-DDB-GUARD-020

get_language_spec

LanguageSpec | None clew.guard.ts_languages.get_language_spec(str lang_name)

Get the LanguageSpec for a named language.

clew/guard/ts_languages.py:140

Source · 4 lines
def get_language_spec(lang_name: str) -> LanguageSpec | None:
    return LANGUAGE_SPECS.get(lang_name)

Activity · 1 steps
lines 141–141return LANGUAGE_SPECS.get(lang_name)

Requirements

REQ-DDB-GUARD-020

Flows

get_parser_for_language

Any clew.guard.ts_languages.get_parser_for_language(str lang_name)

Get a tree-sitter Parser for a named language.

clew/guard/ts_languages.py:126

Source · 10 lines
def get_parser_for_language(lang_name: str) -> Any:
    spec = LANGUAGE_SPECS.get(lang_name)
    if spec is None:
        logger.warning("No tree-sitter spec for language: %s", lang_name)
        return None
    from ..tsnode import Parser as SubstrateParser

    return SubstrateParser(spec.grammar_module.removeprefix("substrate:"))

Activity · 6 steps
lines 127–127spec = LANGUAGE_SPECS.get(lang_name)lines 128–128◇ if spec is Nonelines 129–129logger.warning("No tree-sitter spec for language: %s", lang_name)lines 130–130return Nonelines 131–131from ..tsnode import Parser as SubstrateParserlines 133–133return SubstrateParser(spec.grammar_module.removeprefix("substrate:"))

Requirements

REQ-DDB-GUARD-020

Flows

language_for_extension

str | None clew.guard.ts_languages.language_for_extension(str ext)

Resolve a file extension to a language name.

clew/guard/ts_languages.py:148

Source · 4 lines
def language_for_extension(ext: str) -> str | None:
    return EXTENSION_TO_LANGUAGE.get(ext)

Activity · 1 steps
lines 149–149return EXTENSION_TO_LANGUAGE.get(ext)

Requirements

REQ-DDB-GUARD-020

Flows

language_for_file

str | None clew.guard.ts_languages.language_for_file(str file_path, dict[str, Any] config)

Resolve a file path to a language name using config extensions.

clew/guard/ts_languages.py:158

Source · 14 lines
def language_for_file(file_path: str, config: dict[str, Any]) -> str | None:
    from pathlib import Path

    ext = Path(file_path).suffix
    lang = language_for_extension(ext)
    if lang == "c" and ext == ".h" and _looks_like_cpp_header(file_path):
        lang = "cpp"
    if lang == "typescript" and ext == ".tsx":
        lang = "tsx"
    if lang is None:
        lang = _language_from_config(ext, config)
    return lang

Activity · 10 steps
lines 159–159from pathlib import Pathlines 161–161ext = Path(file_path).suffixlines 162–162lang = language_for_extension(ext)lines 163–163◇ if lang == "c" and ext == ".h" and _looks_like_cpp_header(file_path)lines 164–164lang = "cpp"lines 165–165◇ if lang == "typescript" and ext == ".tsx"lines 166–166lang = "tsx"lines 167–167◇ if lang is Nonelines 168–168lang = _language_from_config(ext, config)lines 169–169return lang

Requirements

REQ-DDB-GUARD-020

Flows