lib.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // -*- coding: utf-8 -*-
  2. // ------------------------------------------------------------------------------------------------
  3. // Copyright © 2020, tree-sitter-python authors.
  4. // See the LICENSE file in this repo for license details.
  5. // ------------------------------------------------------------------------------------------------
  6. //! This crate provides a Python grammar for the [tree-sitter][] parsing library.
  7. //!
  8. //! Typically, you will use the [language][language func] function to add this grammar to a
  9. //! tree-sitter [Parser][], and then use the parser to parse some code:
  10. //!
  11. //! ```
  12. //! use tree_sitter::Parser;
  13. //!
  14. //! let code = r#"
  15. //! def double(x):
  16. //! return x * 2
  17. //! "#;
  18. //! let mut parser = Parser::new();
  19. //! parser.set_language(tree_sitter_python::language()).expect("Error loading Python grammar");
  20. //! let parsed = parser.parse(code, None);
  21. //! # let parsed = parsed.unwrap();
  22. //! # let root = parsed.root_node();
  23. //! # assert!(!root.has_error());
  24. //! ```
  25. //!
  26. //! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
  27. //! [language func]: fn.language.html
  28. //! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
  29. //! [tree-sitter]: https://tree-sitter.github.io/
  30. use tree_sitter::Language;
  31. extern "C" {
  32. fn tree_sitter_python() -> Language;
  33. }
  34. /// Returns the tree-sitter [Language][] for this grammar.
  35. ///
  36. /// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
  37. pub fn language() -> Language {
  38. unsafe { tree_sitter_python() }
  39. }
  40. /// The source of the Python tree-sitter grammar description.
  41. pub const GRAMMAR: &'static str = include_str!("../../grammar.js");
  42. /// The syntax highlighting query for this language.
  43. pub const HIGHLIGHT_QUERY: &'static str = include_str!("../../queries/highlights.scm");
  44. /// The content of the [`node-types.json`][] file for this grammar.
  45. ///
  46. /// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
  47. pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
  48. /// The symbol tagging query for this language.
  49. pub const TAGGING_QUERY: &'static str = include_str!("../../queries/tags.scm");
  50. #[cfg(test)]
  51. mod tests {
  52. #[test]
  53. fn can_load_grammar() {
  54. let mut parser = tree_sitter::Parser::new();
  55. parser
  56. .set_language(super::language())
  57. .expect("Error loading Python grammar");
  58. }
  59. }