Module: TreeSitter

Defined in:
lib/tree-sitter.rb,
lib/tree-sitter/version.rb,
ext/tree-sitter/tree-sitter.c

Defined Under Namespace

Classes: Document, DocumentError, GrammarError

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.compile(rb_name, rb_grammar) ⇒ Object

Public: Compiles a new grammar.

rb_name - A String identifying the grammar name. rb_grammar - A String containing the language grammar in JSON.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'ext/tree-sitter/tree-sitter.c', line 28

VALUE rb_compile(VALUE self, VALUE rb_name, VALUE rb_grammar) {
  Check_Type(rb_name, T_STRING);
  Check_Type(rb_grammar, T_STRING);

  const char *name = StringValueCStr(rb_name);

  const char *grammar = StringValueCStr(rb_grammar);
  TSCompileResult result = ts_compile_grammar(grammar);

  if (result.error_type != TSCompileErrorTypeNone) {
    rb_raise(rb_eGrammarError, "Failed to compile %s grammar: %s\n", name, result.error_message);
  }

  // struct languages *map;
  // map = ALLOC(struct languages);
  // map->name = name;
  // map->result = result;
  //
  // Data_Wrap_Struct(self, NULL, free_languages, map);
  //
  // free(name);
  // free(grammar);

  return Qtrue;
}

.languagesObject

Public: Lists the known languages.



58
59
60
61
62
63
# File 'ext/tree-sitter/tree-sitter.c', line 58

VALUE rb_languages(VALUE self) {
  struct languages *map;
  Data_Get_Struct(self, struct languages, map);

  return rb_iv_get(self, "@languages");
}