Class Parser
- All Implemented Interfaces:
AutoCloseable
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A class representing a cancellation flag. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Get the ranges of text that the parser should include when parsing.Get the language that the parser will use for parsing.long
Get the maximum duration in microseconds that parsing should be allowed to take before halting.parse
(ParseCallback callback, InputEncoding encoding) Parse source code from a callback and create a syntax tree.parse
(ParseCallback callback, InputEncoding encoding, @Nullable Tree oldTree) Parse source code from a callback and create a syntax tree.Parse source code from a string and create a syntax tree.parse
(String source, InputEncoding encoding) Parse source code from a string and create a syntax tree.parse
(String source, InputEncoding encoding, @Nullable Tree oldTree) Parse source code from a string and create a syntax tree.Parse source code from a string and create a syntax tree.void
reset()
Instruct the parser to start the next parse from the beginning.setCancellationFlag
(Parser.CancellationFlag cancellationFlag) Set the parser's current cancellation flag.setIncludedRanges
(List<Range> includedRanges) Set the ranges of text that the parser should include when parsing.setLanguage
(Language language) Set the language that the parser will use for parsing.Set the logger that the parser will use during parsing.setTimeoutMicros
(long timeoutMicros) Set the maximum duration in microseconds that parsing should be allowed to take before halting.toString()
-
Constructor Details
-
Parser
public Parser()Creates a new instance with anull
language.- API Note:
- Parsing cannot be performed while the language is
null
.
-
Parser
Creates a new instance with the given language.
-
-
Method Details
-
getLanguage
-
setLanguage
-
getTimeoutMicros
Get the maximum duration in microseconds that parsing should be allowed to take before halting. -
setTimeoutMicros
-
setLogger
Set the logger that the parser will use during parsing.Example
import java.util.logging.Logger; Logger logger = Logger.getLogger("tree-sitter"); Parser parser = new Parser().setLogger( (type, message) -> logger.info("%s - %s".formatted(type.name(), message)));
-
setCancellationFlag
Set the parser's current cancellation flag.The parser will periodically read from this flag during parsing. If it reads a non-zero value, it will halt early.
-
getIncludedRanges
-
setIncludedRanges
Set the ranges of text that the parser should include when parsing.This allows you to parse only a portion of a document but still return a syntax tree whose ranges match up with the document as a whole. You can also pass multiple disjoint ranges.
- Throws:
IllegalArgumentException
- If the ranges overlap or are not in ascending order.
-
parse
Parse source code from a string and create a syntax tree.- Returns:
- An optional Tree which is empty if parsing was halted.
- Throws:
IllegalStateException
- If the parser does not have a language assigned.
-
parse
Parse source code from a string and create a syntax tree.- Returns:
- An optional Tree which is empty if parsing was halted.
- Throws:
IllegalStateException
- If the parser does not have a language assigned.
-
parse
Parse source code from a string and create a syntax tree.If you have already parsed an earlier version of this document and the document has since been edited, pass the previous syntax tree to
oldTree
so that the unchanged parts of it can be reused. This will save time and memory.
For this to work correctly, you must have already edited the old syntax tree using theTree.edit(io.github.treesitter.jtreesitter.InputEdit)
method in a way that exactly matches the source code changes.- Returns:
- An optional Tree which is empty if parsing was halted.
- Throws:
IllegalStateException
- If the parser does not have a language assigned.
-
parse
public Optional<Tree> parse(String source, InputEncoding encoding, @Nullable Tree oldTree) throws IllegalStateException Parse source code from a string and create a syntax tree.If you have already parsed an earlier version of this document and the document has since been edited, pass the previous syntax tree to
oldTree
so that the unchanged parts of it can be reused. This will save time and memory.
For this to work correctly, you must have already edited the old syntax tree using theTree.edit(io.github.treesitter.jtreesitter.InputEdit)
method in a way that exactly matches the source code changes.- Returns:
- An optional Tree which is empty if parsing was halted.
- Throws:
IllegalStateException
- If the parser does not have a language assigned.
-
parse
public Optional<Tree> parse(ParseCallback callback, InputEncoding encoding) throws IllegalStateException Parse source code from a callback and create a syntax tree.- Returns:
- An optional Tree which is empty if parsing was halted.
- Throws:
IllegalStateException
- If the parser does not have a language assigned.
-
parse
public Optional<Tree> parse(ParseCallback callback, InputEncoding encoding, @Nullable Tree oldTree) throws IllegalStateException Parse source code from a callback and create a syntax tree.If you have already parsed an earlier version of this document and the document has since been edited, pass the previous syntax tree to
oldTree
so that the unchanged parts of it can be reused. This will save time and memory.
For this to work correctly, you must have already edited the old syntax tree using theTree.edit(io.github.treesitter.jtreesitter.InputEdit)
method in a way that exactly matches the source code changes.- Returns:
- An optional Tree which is empty if parsing was halted.
- Throws:
IllegalStateException
- If the parser does not have a language assigned.
-
reset
public void reset()Instruct the parser to start the next parse from the beginning.- API Note:
- If the parser previously stopped because of a timeout
or cancellation, it will resume where it left off.
If you intend to parse another document instead, you must call this method first.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
RuntimeException
-
toString
-