Class DefaultParser
java.lang.Object
org.jline.reader.impl.DefaultParser
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThe result of a delimited buffer.static classClass representing block comment delimiters.static enumEnumeration of bracket types that can be used for EOF detection on unclosed brackets.Nested classes/interfaces inherited from interface Parser
Parser.ParseContext -
Field Summary
Fields inherited from interface Parser
REGEX_COMMAND, REGEX_VARIABLE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionblockCommentDelims(DefaultParser.BlockCommentDelims blockCommentDelims) Sets the block comment delimiters.commandGroup(int commandGroup) Sets the command group for the regular expression.eofOnEscapedNewLine(boolean eofOnEscapedNewLine) Sets whether EOF should be returned on escaped newlines.eofOnUnclosedBracket(DefaultParser.Bracket... brackets) Sets the bracket types that should trigger EOF on unclosed brackets.eofOnUnclosedQuote(boolean eofOnUnclosedQuote) Sets whether EOF should be returned on unclosed quotes.escapeChars(char[] chars) Sets the escape characters.Gets the block comment delimiters.getCommand(String line) char[]Gets the escape characters.String[]Gets the line comment delimiters.char[]Gets the quote characters.getVariable(String line) booleanisDelimiter(CharSequence buffer, int pos) Returns true if the specified character is a whitespace parameter.booleanisDelimiterChar(CharSequence buffer, int pos) Returns true if the character at the specified position if a delimiter.booleanChecks if EOF should be returned on escaped newlines.booleanChecks if EOF should be returned on unclosed quotes.booleanisEscapeChar(char ch) booleanisEscapeChar(CharSequence buffer, int pos) Check if this character is a valid escape char (i.e.booleanisEscaped(CharSequence buffer, int pos) Check if a character is escaped (i.e.booleanisLineCommentStarted(CharSequence buffer, int pos) booleanisQuoteChar(CharSequence buffer, int pos) booleanisQuoted(CharSequence buffer, int pos) lineCommentDelims(String[] lineCommentDelims) Sets the line comment delimiters.parse(String line, int cursor, Parser.ParseContext context) quoteChars(char[] chars) Sets the quote characters.regexCommand(String regexCommand) Sets the regular expression for identifying commands.regexVariable(String regexVariable) Sets the regular expression for identifying variables.voidsetBlockCommentDelims(DefaultParser.BlockCommentDelims blockCommentDelims) Sets the block comment delimiters.voidsetCommandGroup(int commandGroup) Sets the command group for the regular expression.voidsetEofOnEscapedNewLine(boolean eofOnEscapedNewLine) Sets whether EOF should be returned on escaped newlines.voidsetEofOnUnclosedBracket(DefaultParser.Bracket... brackets) Sets the bracket types that should trigger EOF on unclosed brackets.voidsetEofOnUnclosedQuote(boolean eofOnUnclosedQuote) Sets whether EOF should be returned on unclosed quotes.voidsetEscapeChars(char[] chars) Sets the escape characters.voidsetLineCommentDelims(String[] lineCommentDelims) Sets the line comment delimiters.voidsetQuoteChars(char[] chars) Sets the quote characters.voidsetRegexCommand(String regexCommand) Sets the regular expression for identifying commands.voidsetRegexVariable(String regexVariable) Sets the regular expression for identifying variables.booleanvalidCommandName(String name) booleanvalidVariableName(String name)
-
Constructor Details
-
DefaultParser
public DefaultParser()
-
-
Method Details
-
lineCommentDelims
Sets the line comment delimiters.- Parameters:
lineCommentDelims- the line comment delimiters- Returns:
- this parser instance
-
blockCommentDelims
Sets the block comment delimiters.- Parameters:
blockCommentDelims- the block comment delimiters- Returns:
- this parser instance
-
quoteChars
Sets the quote characters.- Parameters:
chars- the quote characters- Returns:
- this parser instance
-
escapeChars
Sets the escape characters.- Parameters:
chars- the escape characters- Returns:
- this parser instance
-
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
Sets the bracket types that should trigger EOF on unclosed brackets.- Parameters:
brackets- the bracket types- Returns:
- this parser instance
-
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
Sets the regular expression for identifying variables.- Parameters:
regexVariable- the regular expression for variables- Returns:
- this parser instance
-
regexCommand
Sets the regular expression for identifying commands.- Parameters:
regexCommand- the regular expression for commands- Returns:
- this parser instance
-
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
Sets the line comment delimiters.- Parameters:
lineCommentDelims- the line comment delimiters
-
getLineCommentDelims
Gets the line comment delimiters.- Returns:
- the line comment delimiters
-
setBlockCommentDelims
Sets the block comment delimiters.- Parameters:
blockCommentDelims- the block comment delimiters
-
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
Sets the bracket types that should trigger EOF on unclosed brackets.- Parameters:
brackets- the bracket types
-
setRegexVariable
Sets the regular expression for identifying variables.- Parameters:
regexVariable- the regular expression for variables
-
setRegexCommand
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
- Specified by:
validCommandNamein interfaceParser
-
validVariableName
- Specified by:
validVariableNamein interfaceParser
-
getCommand
- Specified by:
getCommandin interfaceParser
-
getVariable
- Specified by:
getVariablein interfaceParser
-
parse
-
isDelimiter
Returns true if the specified character is a whitespace parameter. Check to ensure that the character is not escaped by any ofgetQuoteChars(), and is not escaped by any of thegetEscapeChars(), and returns true fromisDelimiterChar(CharSequence, int).- Parameters:
buffer- The complete command bufferpos- The index of the character in the buffer- Returns:
- True if the character should be a delimiter
-
isQuoted
-
isQuoteChar
-
isLineCommentStarted
-
isEscapeChar
public boolean isEscapeChar(char ch) - Specified by:
isEscapeCharin interfaceParser
-
isEscapeChar
Check if this character is a valid escape char (i.e. one that has not been escaped)- Parameters:
buffer- the buffer to check inpos- 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
Check if a character is escaped (i.e. if the previous character is an escape)- Parameters:
buffer- the buffer to check inpos- 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
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 thegetQuoteChars(), and is not escaped by any of thegetEscapeChars(). To perform escaping manually, overrideisDelimiter(CharSequence, int)instead.- Parameters:
buffer- the buffer to check inpos- the position of the character to check- Returns:
- true if the character at the specified position in the given buffer is a delimiter.
-