Ragex.LanguageSupport
(Ragex v0.10.1)
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,.ts,.tsx,.mjs,.cjs
Note: JavaScript has no Metastatic adapter yet; get_adapter/1 returns an error for it.
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
Detects language from a file path extension.
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.
Returns the list of all supported file extensions.
Types
Functions
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
Finds supported source files in a directory.
Options
:recursive-- recursively search subdirectories (default:true):metastatic_only-- only include languages with Metastatic adapters (default:false)
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)
{:error, {:unsupported_language, :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 supported_extensions() :: [String.t()]
Returns the list of all supported file extensions.