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 theParserinterface.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
CompletingParsedLineinterface, 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 classDefaultParser.ArgumentListThe result of a delimited buffer.static classDefaultParser.BlockCommentDelimsClass representing block comment delimiters.static classDefaultParser.BracketEnumeration 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 DefaultParserblockCommentDelims(DefaultParser.BlockCommentDelims blockCommentDelims)Sets the block comment delimiters.DefaultParsercommandGroup(int commandGroup)Sets the command group for the regular expression.DefaultParsereofOnEscapedNewLine(boolean eofOnEscapedNewLine)Sets whether EOF should be returned on escaped newlines.DefaultParsereofOnUnclosedBracket(DefaultParser.Bracket... brackets)Sets the bracket types that should trigger EOF on unclosed brackets.DefaultParsereofOnUnclosedQuote(boolean eofOnUnclosedQuote)Sets whether EOF should be returned on unclosed quotes.DefaultParserescapeChars(char[] chars)Sets the escape characters.DefaultParser.BlockCommentDelimsgetBlockCommentDelims()Gets the block comment delimiters.java.lang.StringgetCommand(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.StringgetVariable(java.lang.String line)booleanisDelimiter(java.lang.CharSequence buffer, int pos)Returns true if the specified character is a whitespace parameter.booleanisDelimiterChar(java.lang.CharSequence buffer, int pos)Returns true if the character at the specified position if a delimiter.booleanisEofOnEscapedNewLine()Checks if EOF should be returned on escaped newlines.booleanisEofOnUnclosedQuote()Checks if EOF should be returned on unclosed quotes.booleanisEscapeChar(char ch)booleanisEscapeChar(java.lang.CharSequence buffer, int pos)Check if this character is a valid escape char (i.e.booleanisEscaped(java.lang.CharSequence buffer, int pos)Check if a character is escaped (i.e.booleanisLineCommentStarted(java.lang.CharSequence buffer, int pos)booleanisQuoteChar(java.lang.CharSequence buffer, int pos)booleanisQuoted(java.lang.CharSequence buffer, int pos)DefaultParserlineCommentDelims(java.lang.String[] lineCommentDelims)Sets the line comment delimiters.ParsedLineparse(java.lang.String line, int cursor, Parser.ParseContext context)DefaultParserquoteChars(char[] chars)Sets the quote characters.DefaultParserregexCommand(java.lang.String regexCommand)Sets the regular expression for identifying commands.DefaultParserregexVariable(java.lang.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(java.lang.String[] lineCommentDelims)Sets the line comment delimiters.voidsetQuoteChars(char[] chars)Sets the quote characters.voidsetRegexCommand(java.lang.String regexCommand)Sets the regular expression for identifying commands.voidsetRegexVariable(java.lang.String regexVariable)Sets the regular expression for identifying variables.booleanvalidCommandName(java.lang.String name)booleanvalidVariableName(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:
validCommandNamein interfaceParser
-
validVariableName
public boolean validVariableName(java.lang.String name)
- Specified by:
validVariableNamein interfaceParser
-
getCommand
public java.lang.String getCommand(java.lang.String line)
- Specified by:
getCommandin interfaceParser
-
getVariable
public java.lang.String getVariable(java.lang.String line)
- Specified by:
getVariablein 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:
isEscapeCharin 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.
-
-