Class Parser

  • All Implemented Interfaces:
    AutoCloseable

    public class Parser
    extends Object
    Parsers are stateful objects that can be assigned a language and used to produce a Tree based on some source code. Instances of this class can not be created without an initially set language.
    • Constructor Summary

      Constructors 
      Constructor Description
      Parser​(Language language)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Delete the parser, freeing all the memory that it used.
      boolean equals​(Object obj)  
      long getTimeout()
      Get the duration in microseconds that parsing is allowed to take.
      int hashCode()  
      boolean isNull()  
      Tree parseFile​(Path path)
      Use the parser to parse some source code found in a file at the specified path.
      Tree parseString​(String source)
      Use the parser to parse some source code and create a syntax tree.
      Tree parseString​(String source, Tree oldTree)
      Use the parser to incrementally parse a changed source code string, reusing unchanged parts of the tree to speed up the process.
      void setLanguage​(Language language)
      Set the language that the parser should use for parsing.
      void setTimeout​(long timeout)
      Set the maximum duration in microseconds that parsing should be allowed to take before halting.
      void setTimeout​(long timeout, TimeUnit timeUnit)
      Set the maximum duration that parsing should be allowed to take before halting.
      void setTimeout​(Duration duration)
      Set the maximum duration that parsing should be allowed to take before halting.
      String toString()  
    • Constructor Detail

      • Parser

        public Parser​(Language language)
        Parameters:
        language - The language used for parsing.
        Throws:
        NullPointerException - if the specified language is null
        UnsatisfiedLinkError - if the specified language has not been linked to the system library
    • Method Detail

      • close

        public void close()
        Delete the parser, freeing all the memory that it used.
        Specified by:
        close in interface AutoCloseable
      • setLanguage

        public void setLanguage​(Language language)
        Set the language that the parser should use for parsing.
        Parameters:
        language - The language used for parsing.
        Throws:
        NullPointerException - if the specified language is null
        UnsatisfiedLinkError - if the specified language has not been linked to the system library
      • 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.
      • setTimeout

        public void setTimeout​(Duration duration)
        Set the maximum duration that parsing should be allowed to take before halting. If parsing takes longer than this, an exception is thrown. Note that the supplied duration will be rounded down to 0 if the duration is expressed in nanoseconds.
        Parameters:
        duration - the timeout duration.
        Throws:
        NullPointerException - if the duration is null
      • setTimeout

        public void setTimeout​(long timeout,
                               TimeUnit timeUnit)
        Set the maximum duration that parsing should be allowed to take before halting. If parsing takes longer than this, an exception is thrown. Note that the supplied duration will be rounded down to 0 if the duration is expressed in nanoseconds.
        Parameters:
        timeout - the timeout duration amount.
        timeUnit - the duration time unit.
        Throws:
        NullPointerException - if the time unit is null
        IllegalArgumentException - if the timeout value is negative
      • setTimeout

        public void setTimeout​(long timeout)
        Set the maximum duration in microseconds that parsing should be allowed to take before halting. If parsing takes longer than this, an exception is thrown.
        Parameters:
        timeout - the timeout in microseconds.
        Throws:
        IllegalArgumentException - if the timeout value is negative.
      • parseString

        public Tree parseString​(String source)
                         throws UnsupportedEncodingException
        Use the parser to parse some source code and create a syntax tree.
        Parameters:
        source - The source code string to be parsed.
        Returns:
        A syntax tree matching the provided source.
        Throws:
        ParsingException - If a parsing failure occurs (e.g. timeout)
        UnsupportedEncodingException - If the UTF-16LE character set is not supported
      • parseString

        public Tree parseString​(String source,
                                Tree oldTree)
                         throws UnsupportedEncodingException
        Use the parser to incrementally parse a changed source code string, reusing unchanged parts of the tree to speed up the process.
        Parameters:
        source - The source code string to be parsed.
        oldTree - The syntax tree before changes were made.
        Returns:
        A syntax tree matching the provided source.
        Throws:
        ParsingException - If a parsing failure occurs (e.g. timeout)
        UnsupportedEncodingException - If the UTF-16LE character set is not supported
      • parseFile

        public Tree parseFile​(Path path)
                       throws IOException
        Use the parser to parse some source code found in a file at the specified path.
        Parameters:
        path - The path of the file to be parsed.
        Returns:
        A tree-sitter Tree matching the provided source.
        Throws:
        IOException - If an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read.
        OutOfMemoryError - If the file is extremely large, for example larger than 2GB.
      • isNull

        public final boolean isNull()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object