Class: TreeStand::Parser
- Inherits:
-
Object
- Object
- TreeStand::Parser
- Extended by:
- T::Sig
- 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.
Instance Attribute Summary collapse
- #ts_language ⇒ TreeSitter::Language readonly
- #ts_parser ⇒ TreeSitter::Parser readonly
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
Constructor Details
#initialize(language) ⇒ void
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tree_stand/parser.rb', line 27 def initialize(language) @language_string = language @ts_language = TreeSitter::Language.load( language, "#{TreeStand.config.parser_path}/#{language}.so" ) @ts_parser = TreeSitter::Parser.new.tap do |parser| parser.language = @ts_language end end |
Instance Attribute Details
#ts_language ⇒ TreeSitter::Language (readonly)
21 22 23 |
# File 'lib/tree_stand/parser.rb', line 21 def ts_language @ts_language end |
#ts_parser ⇒ TreeSitter::Parser (readonly)
23 24 25 |
# File 'lib/tree_stand/parser.rb', line 23 def ts_parser @ts_parser end |
Instance Method Details
#parse_string(document, tree: nil) ⇒ TreeStand::Tree
Parse the provided document with the TreeSitter parser.
43 44 45 46 47 |
# File 'lib/tree_stand/parser.rb', line 43 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.
56 57 58 59 60 61 62 63 64 |
# File 'lib/tree_stand/parser.rb', line 56 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 |