Class: TreeStand::Parser
- Inherits:
-
Object
- Object
- TreeStand::Parser
- Extended by:
- T::Sig, TreeSitter::Mixins::Language
- Defined in:
- lib/tree_stand/parser.rb
Overview
Wrapper around the TreeSitter parser. It looks up the parser by filename in the configured parsers directory. If no Config#parser_path is setup, TreeStand will lookup in a set of default paths. You can always override any configuration by passing the environment variable ‘TREE_SITTER_PARSERS` (colon-separated).
Instance Attribute Summary collapse
- #ts_language ⇒ TreeSitter::Language readonly
- #ts_parser ⇒ TreeSitter::Parser readonly
Class Method Summary collapse
-
.lib_dirs ⇒ Array<Pathname>
The library directories we need to look into.
Instance Method Summary collapse
- #initialize(language) ⇒ void constructor
-
#parse_string(document, tree: nil) ⇒ TreeStand::Tree
Parse the provided document with the TreeSitter parser.
- #parse_string!(document, tree: nil) ⇒ TreeStand::Tree
Methods included from TreeSitter::Mixins::Language
Constructor Details
#initialize(language) ⇒ void
39 40 41 42 43 44 |
# File 'lib/tree_stand/parser.rb', line 39 def initialize(language) @ts_language = Parser.language(language) @ts_parser = TreeSitter::Parser.new.tap do |parser| parser.language = @ts_language end end |
Instance Attribute Details
#ts_language ⇒ TreeSitter::Language (readonly)
32 33 34 |
# File 'lib/tree_stand/parser.rb', line 32 def ts_language @ts_language end |
#ts_parser ⇒ TreeSitter::Parser (readonly)
35 36 37 |
# File 'lib/tree_stand/parser.rb', line 35 def ts_parser @ts_parser end |
Class Method Details
.lib_dirs ⇒ Array<Pathname>
The library directories we need to look into.
52 53 54 55 56 57 |
# File 'lib/tree_stand/parser.rb', line 52 def self.lib_dirs [ *TreeSitter::ENV_PARSERS, *(TreeStand.config.parser_path ? [TreeStand.config.parser_path] : TreeSitter::LIBDIRS), ] end |
Instance Method Details
#parse_string(document, tree: nil) ⇒ TreeStand::Tree
Parse the provided document with the TreeSitter parser.
64 65 66 67 68 |
# File 'lib/tree_stand/parser.rb', line 64 def parse_string(document, tree: nil) # @todo There's a bug with passing a non-nil tree ts_tree = @ts_parser.parse_string(nil, document) TreeStand::Tree.new(self, ts_tree, document) end |
#parse_string!(document, tree: nil) ⇒ TreeStand::Tree
Note:
Like #parse_string, except that if the tree contains any parse errors, raises an InvalidDocument error.
77 78 79 80 81 82 83 84 85 |
# File 'lib/tree_stand/parser.rb', line 77 def parse_string!(document, tree: nil) tree = parse_string(document, tree: tree) return tree unless tree.any?(&:error?) raise(InvalidDocument, <<~ERROR) Encountered errors in the document. Check the tree for more details. #{tree} ERROR end |