Ragex. LanguageSupport
(Ragex v0.38.0)
View Source
Shared language detection, adapter resolution, and parsing utilities.
Consolidates duplicated detect_language/1, get_adapter/1, parse_document/3,
and find_source_files/2 helpers that were previously copy-pasted across
Security, Smells, BusinessLogic, MetastaticBridge, Semantic, and other modules.
Supported Languages
:elixir--.ex,.exs:erlang--.erl,.hrl:python--.py:ruby--.rb:haskell--.hs:javascript--.js,.jsx,.mjs,.cjs:typescript--.ts,.tsx:cure--.cure:agda--.agda,.lagda
Usage
alias Ragex.LanguageSupport
language = LanguageSupport.detect_language("lib/my_module.ex")
# => :elixir
{:ok, adapter} = LanguageSupport.get_adapter(:elixir)
# => {:ok, Metastatic.Adapters.Elixir}
{:ok, doc} = LanguageSupport.parse_document(source, :elixir)
Summary
Functions
Returns default directory exclusion patterns.
Detects language from a file path extension.
Ensures Metastatic's semantic operation patterns are registered.
Finds supported source files in a directory.
Returns the Metastatic adapter module for a language.
Returns whether a language has a Metastatic adapter.
Returns the list of file extensions with Metastatic adapter support.
Parses source code into a Metastatic.Document via the appropriate adapter.
Parses a file into a Metastatic.Document.
Clears the ensure_semantic_patterns/0 guard so the next call re-registers.
Checks if a file path should be excluded based on relative path components from the root directory and configured exclusion patterns.
Returns the list of all supported file extensions.
Types
Functions
@spec default_exclude_patterns() :: [String.t()]
Returns default directory exclusion patterns.
Detects language from a file path extension.
Examples
iex> Ragex.LanguageSupport.detect_language("lib/my_module.ex")
:elixir
iex> Ragex.LanguageSupport.detect_language("script.py")
:python
iex> Ragex.LanguageSupport.detect_language("unknown.xyz")
:unknown
@spec ensure_semantic_patterns() :: :ok
Ensures Metastatic's semantic operation patterns are registered.
Metastatic registers its OpKind pattern tables from module-level
register_all/0 calls at the bottom of each Metastatic.Semantic.Domains.*
module. Elixir only evaluates that kind of top-level side effect while the
module is being compiled, so in any build where the beams are loaded from
disk (a release, a mix compile from a clean _build, a dependency compiled
ahead of time, ...) the tables stay empty and Enricher.enrich_tree/2
silently attaches no op_kind metadata at all.
Empty pattern tables mean semantic analysis degrades to "no operations
detected" with no error anywhere, so Ragex calls this once during
application start (see Ragex.Application) and again from every code path
that reads op_kind.
The call is idempotent -- Metastatic.Semantic.Patterns.register/3 appends
to the existing list -- so it is guarded by a :persistent_term flag to keep
repeated calls cheap and to avoid growing the tables on every invocation.
Returns :ok even when the domain modules are unavailable, so a Metastatic
version that reorganises them cannot break startup.
Finds supported source files in a directory.
Options
:recursive-- recursively search subdirectories (default:true):metastatic_only-- only include languages with Metastatic adapters (default:false):exclude_patterns-- patterns/directories to exclude (default: common build/deps dirs)
Examples
{:ok, files} = LanguageSupport.find_source_files("lib/")
{:ok, files} = LanguageSupport.find_source_files("lib/", metastatic_only: true)
Returns the Metastatic adapter module for a language.
Examples
iex> Ragex.LanguageSupport.get_adapter(:elixir)
{:ok, Metastatic.Adapters.Elixir}
iex> Ragex.LanguageSupport.get_adapter(:javascript)
{:ok, Metastatic.Adapters.JavaScript}
Returns whether a language has a Metastatic adapter.
@spec metastatic_extensions() :: [String.t()]
Returns the list of file extensions with Metastatic adapter support.
@spec parse_document(String.t(), language(), keyword()) :: {:ok, Metastatic.Document.t()} | {:error, term()}
Parses source code into a Metastatic.Document via the appropriate adapter.
Parameters
content-- source code stringlanguage-- language atom (or auto-detect via:pathoption)opts-- keyword options:path-- file path for auto-detection (used only iflanguageis not provided)
Examples
{:ok, doc} = LanguageSupport.parse_document(source, :elixir)
@spec parse_file(String.t(), keyword()) :: {:ok, Metastatic.Document.t()} | {:error, term()}
Parses a file into a Metastatic.Document.
Reads the file, detects the language, and parses.
Options
:language-- override language detection (default: auto-detect from extension)
Examples
{:ok, doc} = LanguageSupport.parse_file("lib/my_module.ex")
@spec reset_semantic_patterns() :: :ok
Clears the ensure_semantic_patterns/0 guard so the next call re-registers.
Intended for tests that clear Metastatic's pattern tables.
Checks if a file path should be excluded based on relative path components from the root directory and configured exclusion patterns.
@spec supported_extensions() :: [String.t()]
Returns the list of all supported file extensions.