Package org.jline.reader.impl
Class DefaultParser
- java.lang.Object
-
- org.jline.reader.impl.DefaultParser
-
- All Implemented Interfaces:
Parser
public class DefaultParser extends java.lang.Object implements Parser
Default implementation of theParser
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
DefaultParser.ArgumentList
The result of a delimited buffer.static class
DefaultParser.BlockCommentDelims
Class representing block comment delimiters.static class
DefaultParser.Bracket
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 Constructor Description DefaultParser()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DefaultParser
blockCommentDelims(DefaultParser.BlockCommentDelims blockCommentDelims)
Sets the block comment delimiters.DefaultParser
commandGroup(int commandGroup)
Sets the command group for the regular expression.DefaultParser
eofOnEscapedNewLine(boolean eofOnEscapedNewLine)
Sets whether EOF should be returned on escaped newlines.DefaultParser
eofOnUnclosedBracket(DefaultParser.Bracket... brackets)
Sets the bracket types that should trigger EOF on unclosed brackets.DefaultParser
eofOnUnclosedQuote(boolean eofOnUnclosedQuote)
Sets whether EOF should be returned on unclosed quotes.DefaultParser
escapeChars(char[] chars)
Sets the escape characters.DefaultParser.BlockCommentDelims
getBlockCommentDelims()
Gets the block comment delimiters.java.lang.String
getCommand(java.lang.String line)
char[]
getEscapeChars()
Gets the escape characters.java.lang.String[]
getLineCommentDelims()
Gets the line comment delimiters.char[]
getQuoteChars()
Gets the quote characters.java.lang.String
getVariable(java.lang.String line)
boolean
isDelimiter(java.lang.CharSequence buffer, int pos)
Returns true if the specified character is a whitespace parameter.boolean
isDelimiterChar(java.lang.CharSequence buffer, int pos)
Returns true if the character at the specified position if a delimiter.boolean
isEofOnEscapedNewLine()
Checks if EOF should be returned on escaped newlines.boolean
isEofOnUnclosedQuote()
Checks if EOF should be returned on unclosed quotes.boolean
isEscapeChar(char ch)
boolean
isEscapeChar(java.lang.CharSequence buffer, int pos)
Check if this character is a valid escape char (i.e.boolean
isEscaped(java.lang.CharSequence buffer, int pos)
Check if a character is escaped (i.e.boolean
isLineCommentStarted(java.lang.CharSequence buffer, int pos)
boolean
isQuoteChar(java.lang.CharSequence buffer, int pos)
boolean
isQuoted(java.lang.CharSequence buffer, int pos)
DefaultParser
lineCommentDelims(java.lang.String[] lineCommentDelims)
Sets the line comment delimiters.ParsedLine
parse(java.lang.String line, int cursor, Parser.ParseContext context)
DefaultParser
quoteChars(char[] chars)
Sets the quote characters.DefaultParser
regexCommand(java.lang.String regexCommand)
Sets the regular expression for identifying commands.DefaultParser
regexVariable(java.lang.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(java.lang.String[] lineCommentDelims)
Sets the line comment delimiters.void
setQuoteChars(char[] chars)
Sets the quote characters.void
setRegexCommand(java.lang.String regexCommand)
Sets the regular expression for identifying commands.void
setRegexVariable(java.lang.String regexVariable)
Sets the regular expression for identifying variables.boolean
validCommandName(java.lang.String name)
boolean
validVariableName(java.lang.String name)
-
-
-
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 interfaceParser
-
validVariableName
public boolean validVariableName(java.lang.String name)
- Specified by:
validVariableName
in interfaceParser
-
getCommand
public java.lang.String getCommand(java.lang.String line)
- Specified by:
getCommand
in interfaceParser
-
getVariable
public java.lang.String getVariable(java.lang.String line)
- Specified by:
getVariable
in interfaceParser
-
parse
public ParsedLine parse(java.lang.String line, int cursor, Parser.ParseContext context)
-
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 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
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 interfaceParser
-
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 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
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 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
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 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.
-
-