tree_sitter_stack_graphs_python/
lib.rs

1// -*- coding: utf-8 -*-
2// ------------------------------------------------------------------------------------------------
3// Copyright © 2023, stack-graphs authors.
4// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6// ------------------------------------------------------------------------------------------------
7
8use tree_sitter_stack_graphs::loader::LanguageConfiguration;
9use tree_sitter_stack_graphs::loader::LoadError;
10use tree_sitter_stack_graphs::CancellationFlag;
11
12/// The stack graphs tsg source for this language.
13pub const STACK_GRAPHS_TSG_PATH: &str = "src/stack-graphs.tsg";
14/// The stack graphs tsg source for this language.
15pub const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
16
17/// The stack graphs builtins configuration for this language.
18pub const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
19/// The stack graphs builtins path for this language
20pub const STACK_GRAPHS_BUILTINS_PATH: &str = "src/builtins.py";
21/// The stack graphs builtins source for this language.
22pub const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.py");
23
24pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
25    try_language_configuration(cancellation_flag).unwrap_or_else(|err| panic!("{}", err))
26}
27
28pub fn try_language_configuration(
29    cancellation_flag: &dyn CancellationFlag,
30) -> Result<LanguageConfiguration, LoadError> {
31    LanguageConfiguration::from_sources(
32        tree_sitter_python::LANGUAGE.into(),
33        Some(String::from("source.py")),
34        None,
35        vec![String::from("py")],
36        STACK_GRAPHS_TSG_PATH.into(),
37        STACK_GRAPHS_TSG_SOURCE,
38        Some((
39            STACK_GRAPHS_BUILTINS_PATH.into(),
40            STACK_GRAPHS_BUILTINS_SOURCE,
41        )),
42        Some(STACK_GRAPHS_BUILTINS_CONFIG),
43        cancellation_flag,
44    )
45}