Class DefaultParser

  • All Implemented Interfaces:
    Parser

    public class DefaultParser
    extends java.lang.Object
    implements Parser
    Default implementation of the Parser interface.

    This parser provides a flexible implementation for parsing command lines into tokens, with support for:

    • Quoted strings with customizable quote characters
    • Escaped characters with customizable escape characters
    • Bracket matching with customizable bracket pairs
    • Line and block comments with customizable delimiters
    • Command and variable name validation with customizable regex patterns

    The parser is highly configurable through its chainable setter methods, allowing applications to customize its behavior to match their specific syntax requirements.

    The parser also implements the CompletingParsedLine interface, which provides additional methods for handling completion with proper escaping of special characters.

    See Also:
    Parser, CompletingParsedLine, LineReader
    • Constructor Detail

      • DefaultParser

        public DefaultParser()
    • Method Detail

      • lineCommentDelims

        public DefaultParser lineCommentDelims​(java.lang.String[] lineCommentDelims)
        Sets the line comment delimiters.
        Parameters:
        lineCommentDelims - the line comment delimiters
        Returns:
        this parser instance
      • blockCommentDelims

        public DefaultParser blockCommentDelims​(DefaultParser.BlockCommentDelims blockCommentDelims)
        Sets the block comment delimiters.
        Parameters:
        blockCommentDelims - the block comment delimiters
        Returns:
        this parser instance
      • quoteChars

        public DefaultParser quoteChars​(char[] chars)
        Sets the quote characters.
        Parameters:
        chars - the quote characters
        Returns:
        this parser instance
      • escapeChars

        public DefaultParser escapeChars​(char[] chars)
        Sets the escape characters.
        Parameters:
        chars - the escape characters
        Returns:
        this parser instance
      • eofOnUnclosedQuote

        public DefaultParser eofOnUnclosedQuote​(boolean eofOnUnclosedQuote)
        Sets whether EOF should be returned on unclosed quotes.
        Parameters:
        eofOnUnclosedQuote - true if EOF should be returned on unclosed quotes
        Returns:
        this parser instance
      • eofOnUnclosedBracket

        public DefaultParser eofOnUnclosedBracket​(DefaultParser.Bracket... brackets)
        Sets the bracket types that should trigger EOF on unclosed brackets.
        Parameters:
        brackets - the bracket types
        Returns:
        this parser instance
      • eofOnEscapedNewLine

        public DefaultParser eofOnEscapedNewLine​(boolean eofOnEscapedNewLine)
        Sets whether EOF should be returned on escaped newlines.
        Parameters:
        eofOnEscapedNewLine - true if EOF should be returned on escaped newlines
        Returns:
        this parser instance
      • regexVariable

        public DefaultParser regexVariable​(java.lang.String regexVariable)
        Sets the regular expression for identifying variables.
        Parameters:
        regexVariable - the regular expression for variables
        Returns:
        this parser instance
      • regexCommand

        public DefaultParser regexCommand​(java.lang.String regexCommand)
        Sets the regular expression for identifying commands.
        Parameters:
        regexCommand - the regular expression for commands
        Returns:
        this parser instance
      • commandGroup

        public DefaultParser commandGroup​(int commandGroup)
        Sets the command group for the regular expression.
        Parameters:
        commandGroup - the command group
        Returns:
        this parser instance
      • setQuoteChars

        public void setQuoteChars​(char[] chars)
        Sets the quote characters.
        Parameters:
        chars - the quote characters
      • getQuoteChars

        public char[] getQuoteChars()
        Gets the quote characters.
        Returns:
        the quote characters
      • setEscapeChars

        public void setEscapeChars​(char[] chars)
        Sets the escape characters.
        Parameters:
        chars - the escape characters
      • getEscapeChars

        public char[] getEscapeChars()
        Gets the escape characters.
        Returns:
        the escape characters
      • setLineCommentDelims

        public void setLineCommentDelims​(java.lang.String[] lineCommentDelims)
        Sets the line comment delimiters.
        Parameters:
        lineCommentDelims - the line comment delimiters
      • getLineCommentDelims

        public java.lang.String[] getLineCommentDelims()
        Gets the line comment delimiters.
        Returns:
        the line comment delimiters
      • setBlockCommentDelims

        public void setBlockCommentDelims​(DefaultParser.BlockCommentDelims blockCommentDelims)
        Sets the block comment delimiters.
        Parameters:
        blockCommentDelims - the block comment delimiters
      • getBlockCommentDelims

        public DefaultParser.BlockCommentDelims getBlockCommentDelims()
        Gets the block comment delimiters.
        Returns:
        the block comment delimiters
      • setEofOnUnclosedQuote

        public void setEofOnUnclosedQuote​(boolean eofOnUnclosedQuote)
        Sets whether EOF should be returned on unclosed quotes.
        Parameters:
        eofOnUnclosedQuote - true if EOF should be returned on unclosed quotes
      • isEofOnUnclosedQuote

        public boolean isEofOnUnclosedQuote()
        Checks if EOF should be returned on unclosed quotes.
        Returns:
        true if EOF should be returned on unclosed quotes
      • setEofOnEscapedNewLine

        public void setEofOnEscapedNewLine​(boolean eofOnEscapedNewLine)
        Sets whether EOF should be returned on escaped newlines.
        Parameters:
        eofOnEscapedNewLine - true if EOF should be returned on escaped newlines
      • isEofOnEscapedNewLine

        public boolean isEofOnEscapedNewLine()
        Checks if EOF should be returned on escaped newlines.
        Returns:
        true if EOF should be returned on escaped newlines
      • setEofOnUnclosedBracket

        public void setEofOnUnclosedBracket​(DefaultParser.Bracket... brackets)
        Sets the bracket types that should trigger EOF on unclosed brackets.
        Parameters:
        brackets - the bracket types
      • setRegexVariable

        public void setRegexVariable​(java.lang.String regexVariable)
        Sets the regular expression for identifying variables.
        Parameters:
        regexVariable - the regular expression for variables
      • setRegexCommand

        public void setRegexCommand​(java.lang.String regexCommand)
        Sets the regular expression for identifying commands.
        Parameters:
        regexCommand - the regular expression for commands
      • setCommandGroup

        public void setCommandGroup​(int commandGroup)
        Sets the command group for the regular expression.
        Parameters:
        commandGroup - the command group
      • validCommandName

        public boolean validCommandName​(java.lang.String name)
        Specified by:
        validCommandName in interface Parser
      • validVariableName

        public boolean validVariableName​(java.lang.String name)
        Specified by:
        validVariableName in interface Parser
      • getCommand

        public java.lang.String getCommand​(java.lang.String line)
        Specified by:
        getCommand in interface Parser
      • getVariable

        public java.lang.String getVariable​(java.lang.String line)
        Specified by:
        getVariable in interface Parser
      • isDelimiter

        public boolean isDelimiter​(java.lang.CharSequence buffer,
                                   int pos)
        Returns true if the specified character is a whitespace parameter. Check to ensure that the character is not escaped by any of getQuoteChars(), and is not escaped by any of the getEscapeChars(), and returns true from isDelimiterChar(java.lang.CharSequence, int).
        Parameters:
        buffer - The complete command buffer
        pos - The index of the character in the buffer
        Returns:
        True if the character should be a delimiter
      • isQuoted

        public boolean isQuoted​(java.lang.CharSequence buffer,
                                int pos)
      • isQuoteChar

        public boolean isQuoteChar​(java.lang.CharSequence buffer,
                                   int pos)
      • isLineCommentStarted

        public boolean isLineCommentStarted​(java.lang.CharSequence buffer,
                                            int pos)
      • isEscapeChar

        public boolean isEscapeChar​(char ch)
        Specified by:
        isEscapeChar in interface Parser
      • isEscapeChar

        public boolean isEscapeChar​(java.lang.CharSequence buffer,
                                    int pos)
        Check if this character is a valid escape char (i.e. one that has not been escaped)
        Parameters:
        buffer - the buffer to check in
        pos - the position of the character to check
        Returns:
        true if the character at the specified position in the given buffer is an escape character and the character immediately preceding it is not an escape character.
      • isEscaped

        public boolean isEscaped​(java.lang.CharSequence buffer,
                                 int pos)
        Check if a character is escaped (i.e. if the previous character is an escape)
        Parameters:
        buffer - the buffer to check in
        pos - the position of the character to check
        Returns:
        true if the character at the specified position in the given buffer is an escape character and the character immediately preceding it is an escape character.
      • isDelimiterChar

        public boolean isDelimiterChar​(java.lang.CharSequence buffer,
                                       int pos)
        Returns true if the character at the specified position if a delimiter. This method will only be called if the character is not enclosed in any of the getQuoteChars(), and is not escaped by any of the getEscapeChars(). To perform escaping manually, override isDelimiter(java.lang.CharSequence, int) instead.
        Parameters:
        buffer - the buffer to check in
        pos - the position of the character to check
        Returns:
        true if the character at the specified position in the given buffer is a delimiter.