Class: TreeStand::Parser

Inherits:
Object
  • Object
show all
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).

Examples:

TreeStand.configure do
  config.parser_path = "path/to/parser/folder/"
end

# Looks for a parser in `path/to/parser/folder/sql.{so,dylib}`
sql_parser = TreeStand::Parser.new("sql")

# Looks for a parser in `path/to/parser/folder/ruby.{so,dylib}`
ruby_parser = TreeStand::Parser.new("ruby")

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TreeSitter::Mixins::Language

ext, language

Constructor Details

#initialize(language) ⇒ void

Parameters:

  • language (String)


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_languageTreeSitter::Language (readonly)

Returns:

  • (TreeSitter::Language)


32
33
34
# File 'lib/tree_stand/parser.rb', line 32

def ts_language
  @ts_language
end

#ts_parserTreeSitter::Parser (readonly)

Returns:

  • (TreeSitter::Parser)


35
36
37
# File 'lib/tree_stand/parser.rb', line 35

def ts_parser
  @ts_parser
end

Class Method Details

.lib_dirsArray<Pathname>

The library directories we need to look into.

Returns:

  • (Array<Pathname>)

    the list of candidate places to use when searching for parsers.

See Also:

  • ENV_PARSERS
  • LIBDIRS


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.

Parameters:

  • document (String)
  • tree (TreeStand::Tree, nil) (defaults to: nil)

    providing the old tree will allow the parser to take advantage of incremental parsing and improve performance by re-useing nodes from the old tree.

Returns:



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.

Parameters:

Returns:

Raises:

See Also:



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