Package ch.usi.si.seart.treesitter
Class Parser
- java.lang.Object
-
- ch.usi.si.seart.treesitter.Parser
-
- All Implemented Interfaces:
AutoCloseable
public class Parser extends Object
Parsers are stateful objects used to produce aTreefrom some source code, based on the rules outlined by the usedLanguage.- Since:
- 1.0.0
- Author:
- Tommy MacWilliam, Ozren Dabić
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classParser.BuilderFacilitates the construction ofParserinstances.
-
Constructor Summary
Constructors Constructor Description Parser(@NotNull Language language)Deprecated, for removal: This API element is subject to removal in a future version.UsegetFor(Language)orbuilder()instead
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Parser.Builderbuilder()Obtain a newBuilderfor constructing a parser instance.voidclose()Delete the external resource, freeing all the memory that it used.booleanequals(Object obj)static ParsergetFor(@NotNull Language language)Static factory for obtaining new parser instances.static intgetLanguageVersion()The latest ABI version that is supported by the current version of the library.static intgetMinimumCompatibleLanguageVersion()The earliest ABI version that is supported by the current version of the library.longgetTimeout()Get the duration in microseconds that parsing is allowed to take.inthashCode()booleanisNull()Checks whether the memory address associated with this external resource isnullptr.Treeparse(@NotNull String source)Use the parser to parse some source code and create a syntaxTree.Treeparse(@NotNull String source, @NotNull Tree oldTree)Use the parser to incrementally reparse a changed source code string, re-using unchanged parts of theTreeto expedite the process.Treeparse(@NotNull Path path)Use the parser to parse source code from a file.Treeparse(@NotNull Path path, @NotNull Tree oldTree)Use the parser to reparse source code from a file, re-using unchanged parts of theTreeto expedite the process.voidsetLanguage(@NotNull Language language)Set the language that the parser should use for parsing.voidsetTimeout(long timeout)Set the maximum duration in microseconds that parsing should be allowed to take.voidsetTimeout(long timeout, @NotNull TimeUnit timeUnit)Set the maximum duration that parsing should be allowed to take.voidsetTimeout(@NotNull Duration duration)Set the maximum duration that parsing should be allowed to take.Parser.BuildertoBuilder()Obtain a newBuilderinitialized with the current parser settings.
-
-
-
Constructor Detail
-
Parser
@Deprecated(since="1.7.0", forRemoval=true) public Parser(@NotNull @NotNull Language language)
Deprecated, for removal: This API element is subject to removal in a future version.UsegetFor(Language)orbuilder()instead
-
-
Method Detail
-
getFor
public static Parser getFor(@NotNull @NotNull Language language)
Static factory for obtaining new parser instances.- Parameters:
language- The language used for parsing- Returns:
- A new parser instance
- Since:
- 1.7.0
-
builder
public static Parser.Builder builder()
Obtain a newBuilderfor constructing a parser instance.- Returns:
- a new parser builder
- Since:
- 1.7.0
-
toBuilder
public Parser.Builder toBuilder()
Obtain a newBuilderinitialized with the current parser settings.- Returns:
- a new parser builder
- Since:
- 1.8.0
-
getLanguageVersion
public static int getLanguageVersion()
The latest ABI version that is supported by the current version of the library. When languages are generated by the tree-sitter CLI, they are assigned an ABI version number that corresponds to the current CLI version.The tree-sitter library is generally backwards-compatible with languages generated using older CLI versions, but is not forwards-compatible.
- Returns:
- latest ABI version number
- Since:
- 1.6.0
-
getMinimumCompatibleLanguageVersion
public static int getMinimumCompatibleLanguageVersion()
The earliest ABI version that is supported by the current version of the library.- Returns:
- earliest supported ABI version number
- Since:
- 1.6.0
-
setLanguage
public void setLanguage(@NotNull @NotNull Language language)Set the language that the parser should use for parsing.- Parameters:
language- the language used for parsing- Throws:
NullPointerException- if the language is nullUnsatisfiedLinkError- if the language was not linked to native codeABIVersionError- if the language ABI version is outdatedIncompatibleLanguageException- if the language can not be set
-
getTimeout
public long getTimeout()
Get the duration in microseconds that parsing is allowed to take.- Returns:
- the timeout duration set for parsing, 0 if it was not set
- Since:
- 1.1.0
-
setTimeout
public void setTimeout(@NotNull @NotNull Duration duration)Set the maximum duration that parsing should be allowed to take. If parsing time exceeds this value, an exception is thrown. The duration is rounded down to zero if it does not exceed one microsecond. Timeouts are considered disabled when the value is zero.- Parameters:
duration- the timeout duration- Throws:
NullPointerException- if the duration isnull- Since:
- 1.1.0
-
setTimeout
public void setTimeout(long timeout, @NotNull @NotNull TimeUnit timeUnit)Set the maximum duration that parsing should be allowed to take. If parsing time exceeds this value, an exception is thrown. The duration is rounded down to zero if it does not exceed one microsecond. Timeouts are considered disabled when the value is zero.- Parameters:
timeout- the timeout duration amounttimeUnit- the duration time unit- Throws:
NullPointerException- if the time unit isnullIllegalArgumentException- if the timeout value is negative- Since:
- 1.1.0
-
setTimeout
public void setTimeout(long timeout)
Set the maximum duration in microseconds that parsing should be allowed to take. If parsing time exceeds this value, an exception is thrown. Timeouts are considered disabled when the value is zero.- Parameters:
timeout- the timeout in microseconds- Throws:
IllegalArgumentException- if the timeout value is negative- Since:
- 1.1.0
-
parse
public Tree parse(@NotNull @NotNull String source) throws ParsingException
Use the parser to parse some source code and create a syntaxTree.- Parameters:
source- the source code string to be parsed- Returns:
- a syntax tree matching the provided source
- Throws:
ParsingException- if a parsing failure occurs- Since:
- 1.3.0
-
parse
public Tree parse(@NotNull @NotNull String source, @NotNull @NotNull Tree oldTree) throws ParsingException
Use the parser to incrementally reparse a changed source code string, re-using unchanged parts of theTreeto expedite the process.- Parameters:
source- the source code string to be parsedoldTree- the syntax tree before changes were made- Returns:
- a syntax tree matching the provided source
- Throws:
ParsingException- if a parsing failure occurs- Since:
- 1.3.0
-
parse
public Tree parse(@NotNull @NotNull Path path) throws ParsingException
Use the parser to parse source code from a file.- Parameters:
path- the path of the file to be parsed- Returns:
- a syntax tree matching the provided source
- Throws:
ParsingException- if a parsing failure occurs- Since:
- 1.3.0
-
parse
public Tree parse(@NotNull @NotNull Path path, @NotNull @NotNull Tree oldTree) throws ParsingException
Use the parser to reparse source code from a file, re-using unchanged parts of theTreeto expedite the process.- Parameters:
path- the path of the file to be parsedoldTree- the syntax tree before changes were made- Returns:
- a syntax tree matching the provided source
- Throws:
ParsingException- if a parsing failure occurs- Since:
- 1.3.0
-
isNull
public final boolean isNull()
Checks whether the memory address associated with this external resource isnullptr.- Returns:
trueif the memory address is 0, otherwisefalse
-
close
public void close()
Delete the external resource, freeing all the memory that it used.- Specified by:
closein interfaceAutoCloseable
-
-