rsnarsna
feat(lambdas): integrate ALLI-script engines and create 4 separate modular Lambda functions
ea3ef49 Download language_core/version_matrix.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 7.6 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/language_core/version_matrix.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/language_core/version_matrix.py
-
curl -L -o version_matrix.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/language_core/version_matrix.py
7.6 kB
| """ | |
| Language Core: Language Version Matrix and Capability Registry. | |
| Maps programming language versions (Python, JavaScript/TypeScript) to supported | |
| syntax features, grammar rules, deprecations, and breaking changes. | |
| """ | |
| from enum import Enum | |
| from typing import Dict, List, Tuple, Optional, Any, Set | |
| class Language(Enum): | |
| PYTHON = "python" | |
| JAVASCRIPT = "javascript" | |
| TYPESCRIPT = "typescript" | |
| class LanguageFeature(Enum): | |
| # Python Features | |
| PY_F_STRINGS = "py_f_strings" # 3.6+ | |
| PY_ASYNC_AWAIT = "py_async_await" # 3.5+ | |
| PY_TYPE_ANNOTATIONS = "py_type_annotations" # 3.6+ | |
| PY_WALRUS_OPERATOR = "py_walrus_operator" # 3.8+ (:=) | |
| PY_POSITIONAL_ONLY_ARGS = "py_positional_only_args" # 3.8+ (/) | |
| PY_PATTERN_MATCHING = "py_pattern_matching" # 3.10+ (match/case) | |
| PY_UNION_TYPE_PIPE = "py_union_type_pipe" # 3.10+ (int | str) | |
| PY_EXCEPTION_GROUPS = "py_exception_groups" # 3.11+ (except*) | |
| PY_TYPE_PARAM_SYNTAX = "py_type_param_syntax" # 3.12+ (type X[T] = ...) | |
| PY_FREE_THREADED = "py_free_threaded" # 3.13+ (no-GIL) | |
| # JavaScript / TypeScript Features | |
| JS_ARROW_FUNCTIONS = "js_arrow_functions" # ES6 / 2015 | |
| JS_ASYNC_AWAIT = "js_async_await" # ES2017 | |
| JS_OPTIONAL_CHAINING = "js_optional_chaining" # ES2020 (?.) | |
| JS_NULLISH_COALESCING = "js_nullish_coalescing" # ES2020 (??) | |
| JS_TOP_LEVEL_AWAIT = "js_top_level_await" # ES2022 | |
| TS_STRICT_TYPING = "ts_strict_typing" # TS 2.0+ | |
| TS_TEMPLATE_LITERAL_TYPES = "ts_template_types" # TS 4.1+ | |
| def parse_version_tuple(version_str: str) -> Tuple[int, int, int]: | |
| """Parses version strings like '3.10.4', '3.12', '2022' into (major, minor, patch).""" | |
| clean = version_str.strip().lstrip("vV") | |
| parts = clean.split(".") | |
| nums = [] | |
| for p in parts[:3]: | |
| # Extract leading numeric digits | |
| num_str = "" | |
| for ch in p: | |
| if ch.isdigit(): | |
| num_str += ch | |
| else: | |
| break | |
| nums.append(int(num_str) if num_str else 0) | |
| while len(nums) < 3: | |
| nums.append(0) | |
| return (nums[0], nums[1], nums[2]) | |
| class FeatureRule: | |
| def __init__( | |
| self, | |
| feature: LanguageFeature, | |
| language: Language, | |
| min_version: Tuple[int, int, int], | |
| deprecated_in: Optional[Tuple[int, int, int]] = None, | |
| removed_in: Optional[Tuple[int, int, int]] = None, | |
| description: str = "" | |
| ): | |
| self.feature = feature | |
| self.language = language | |
| self.min_version = min_version | |
| self.deprecated_in = deprecated_in | |
| self.removed_in = removed_in | |
| self.description = description | |
| class VersionMatrix: | |
| """ | |
| Global knowledge base of programming language versions and syntax capabilities. | |
| Validates syntax compatibility before code is generated or executed. | |
| """ | |
| _REGISTRY: Dict[LanguageFeature, FeatureRule] = { | |
| # Python Feature Rules | |
| LanguageFeature.PY_ASYNC_AWAIT: FeatureRule( | |
| LanguageFeature.PY_ASYNC_AWAIT, Language.PYTHON, (3, 5, 0), | |
| description="Native async/await syntax" | |
| ), | |
| LanguageFeature.PY_F_STRINGS: FeatureRule( | |
| LanguageFeature.PY_F_STRINGS, Language.PYTHON, (3, 6, 0), | |
| description="Formatted string literals f'...' " | |
| ), | |
| LanguageFeature.PY_TYPE_ANNOTATIONS: FeatureRule( | |
| LanguageFeature.PY_TYPE_ANNOTATIONS, Language.PYTHON, (3, 6, 0), | |
| description="Variable type annotations PEP 526" | |
| ), | |
| LanguageFeature.PY_WALRUS_OPERATOR: FeatureRule( | |
| LanguageFeature.PY_WALRUS_OPERATOR, Language.PYTHON, (3, 8, 0), | |
| description="Assignment expressions (:=)" | |
| ), | |
| LanguageFeature.PY_POSITIONAL_ONLY_ARGS: FeatureRule( | |
| LanguageFeature.PY_POSITIONAL_ONLY_ARGS, Language.PYTHON, (3, 8, 0), | |
| description="Positional-only parameter syntax (/)" | |
| ), | |
| LanguageFeature.PY_PATTERN_MATCHING: FeatureRule( | |
| LanguageFeature.PY_PATTERN_MATCHING, Language.PYTHON, (3, 10, 0), | |
| description="Structural pattern matching match/case" | |
| ), | |
| LanguageFeature.PY_UNION_TYPE_PIPE: FeatureRule( | |
| LanguageFeature.PY_UNION_TYPE_PIPE, Language.PYTHON, (3, 10, 0), | |
| description="Union types using pipe operator (int | str)" | |
| ), | |
| LanguageFeature.PY_EXCEPTION_GROUPS: FeatureRule( | |
| LanguageFeature.PY_EXCEPTION_GROUPS, Language.PYTHON, (3, 11, 0), | |
| description="Exception groups and except* syntax" | |
| ), | |
| LanguageFeature.PY_TYPE_PARAM_SYNTAX: FeatureRule( | |
| LanguageFeature.PY_TYPE_PARAM_SYNTAX, Language.PYTHON, (3, 12, 0), | |
| description="Generic type parameter syntax PEP 695" | |
| ), | |
| # JavaScript Feature Rules | |
| LanguageFeature.JS_ARROW_FUNCTIONS: FeatureRule( | |
| LanguageFeature.JS_ARROW_FUNCTIONS, Language.JAVASCRIPT, (6, 0, 0), | |
| description="ES6 Arrow functions () => {}" | |
| ), | |
| LanguageFeature.JS_ASYNC_AWAIT: FeatureRule( | |
| LanguageFeature.JS_ASYNC_AWAIT, Language.JAVASCRIPT, (8, 0, 0), | |
| description="ES2017 async/await" | |
| ), | |
| LanguageFeature.JS_OPTIONAL_CHAINING: FeatureRule( | |
| LanguageFeature.JS_OPTIONAL_CHAINING, Language.JAVASCRIPT, (11, 0, 0), | |
| description="ES2020 Optional chaining (?.)" | |
| ), | |
| LanguageFeature.JS_NULLISH_COALESCING: FeatureRule( | |
| LanguageFeature.JS_NULLISH_COALESCING, Language.JAVASCRIPT, (11, 0, 0), | |
| description="ES2020 Nullish coalescing (??)" | |
| ), | |
| } | |
| def is_feature_supported(cls, language: Language, version_str: str, feature: LanguageFeature) -> Tuple[bool, str]: | |
| """ | |
| Validates if a language version supports a given syntax feature. | |
| Returns (is_supported: bool, reason_message: str). | |
| """ | |
| rule = cls._REGISTRY.get(feature) | |
| if not rule: | |
| return True, f"Feature '{feature.value}' has no registered constraints." | |
| if rule.language != language: | |
| return False, f"Feature '{feature.value}' belongs to {rule.language.value}, not {language.value}." | |
| target_ver = parse_version_tuple(version_str) | |
| # Check minimum version requirement | |
| if target_ver < rule.min_version: | |
| min_str = ".".join(str(n) for n in rule.min_version[:2]) | |
| return False, ( | |
| f"Feature '{feature.value}' requires {language.value} >= {min_str}, " | |
| f"but target is {version_str}." | |
| ) | |
| # Check if removed in this version | |
| if rule.removed_in and target_ver >= rule.removed_in: | |
| rem_str = ".".join(str(n) for n in rule.removed_in[:2]) | |
| return False, ( | |
| f"Feature '{feature.value}' was removed in {language.value} {rem_str}." | |
| ) | |
| return True, "Supported" | |
| def get_supported_features(cls, language: Language, version_str: str) -> List[LanguageFeature]: | |
| """Returns all syntax features supported by a specific language version.""" | |
| supported = [] | |
| for feat, rule in cls._REGISTRY.items(): | |
| if rule.language == language: | |
| is_sup, _ = cls.is_feature_supported(language, version_str, feat) | |
| if is_sup: | |
| supported.append(feat) | |
| return supported | |