Skip to content

Commit 503aaa5

Browse files
fix(parser): throw on unsupported Language version
1 parent b8e0e0a commit 503aaa5

File tree

1 file changed

+15
-7
lines changed
  • src/main/java/io/github/treesitter/jtreesitter

1 file changed

+15
-7
lines changed

src/main/java/io/github/treesitter/jtreesitter/Parser.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,29 @@ public Parser() {
3131
self = ts_parser_new().reinterpret(arena, TreeSitter::ts_parser_delete);
3232
}
3333

34-
/** Creates a new instance with the given language. */
35-
public Parser(Language language) {
34+
/** Creates a new instance with the given language.
35+
*
36+
* @throws IllegalArgumentException If the language version is not supported.
37+
*/
38+
public Parser(Language language) throws IllegalArgumentException {
3639
this();
37-
ts_parser_set_language(self, language.segment());
38-
this.language = language;
40+
this.setLanguage(language);
3941
}
4042

4143
/** Get the language that the parser will use for parsing. */
4244
public @Nullable Language getLanguage() {
4345
return language;
4446
}
4547

46-
/** Set the language that the parser will use for parsing. */
47-
public Parser setLanguage(Language language) {
48-
ts_parser_set_language(self, language.segment());
48+
/**
49+
* Set the language that the parser will use for parsing.
50+
*
51+
* @throws IllegalArgumentException If the language version is not supported.
52+
*/
53+
public Parser setLanguage(Language language) throws IllegalArgumentException {
54+
if (!ts_parser_set_language(self, language.segment())) {
55+
throw new IllegalArgumentException("Unsupported Language version (%d)".formatted(language.getVersion()));
56+
}
4957
this.language = language;
5058
return this;
5159
}

0 commit comments

Comments
 (0)