Package org.jline.reader.impl
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 TypeClassDescriptionclass
The result of a delimited buffer.static class
Class representing block comment delimiters.static enum
Enumeration of bracket types that can be used for EOF detection on unclosed brackets.Nested classes/interfaces inherited from interface org.jline.reader.Parser
Parser.ParseContext
-
Field Summary
Fields inherited from interface org.jline.reader.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) boolean
isDelimiter
(CharSequence buffer, int pos) Returns true if the specified character is a whitespace parameter.boolean
isDelimiterChar
(CharSequence buffer, int pos) Returns true if the character at the specified position if a delimiter.boolean
Checks if EOF should be returned on escaped newlines.boolean
Checks if EOF should be returned on unclosed quotes.boolean
isEscapeChar
(char ch) boolean
isEscapeChar
(CharSequence buffer, int pos) Check if this character is a valid escape char (i.e.boolean
isEscaped
(CharSequence buffer, int pos) Check if a character is escaped (i.e.boolean
isLineCommentStarted
(CharSequence buffer, int pos) boolean
isQuoteChar
(CharSequence buffer, int pos) boolean
isQuoted
(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.void
setBlockCommentDelims
(DefaultParser.BlockCommentDelims blockCommentDelims) Sets the block comment delimiters.void
setCommandGroup
(int commandGroup) Sets the command group for the regular expression.void
setEofOnEscapedNewLine
(boolean eofOnEscapedNewLine) Sets whether EOF should be returned on escaped newlines.void
setEofOnUnclosedBracket
(DefaultParser.Bracket... brackets) Sets the bracket types that should trigger EOF on unclosed brackets.void
setEofOnUnclosedQuote
(boolean eofOnUnclosedQuote) Sets whether EOF should be returned on unclosed quotes.void
setEscapeChars
(char[] chars) Sets the escape characters.void
setLineCommentDelims
(String[] lineCommentDelims) Sets the line comment delimiters.void
setQuoteChars
(char[] chars) Sets the quote characters.void
setRegexCommand
(String regexCommand) Sets the regular expression for identifying commands.void
setRegexVariable
(String regexVariable) Sets the regular expression for identifying variables.boolean
validCommandName
(String name) boolean
validVariableName
(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:
validCommandName
in interfaceParser
-
validVariableName
- Specified by:
validVariableName
in interfaceParser
-
getCommand
- Specified by:
getCommand
in interfaceParser
-
getVariable
- Specified by:
getVariable
in 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(java.lang.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:
isEscapeChar
in 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(java.lang.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.
-