Package org.jparsec

Class Scanners

java.lang.Object
org.jparsec.Scanners

public final class Scanners extends Object
Provides common Parser implementations that scan the source and match certain string patterns.

Some scanners like IDENTIFIER and INTEGER return the matched string, while others like WHITESPACES return nothing, as indicated by the Void type parameter. In case the matched string is still needed nonetheless, use the Parser.source() method.

  • Field Details

    • WHITESPACES

      public static final Parser<Void> WHITESPACES
      A scanner that scans greedily for 1 or more whitespace characters.
    • ANY_CHAR

      public static final Parser<Void> ANY_CHAR
      Matches any character in the input. Different from Parsers.always(), it fails on EOF. Also it consumes the current character in the input.
    • JAVA_LINE_COMMENT

      public static final Parser<Void> JAVA_LINE_COMMENT
      Scanner for c++/java style line comment.
    • SQL_LINE_COMMENT

      public static final Parser<Void> SQL_LINE_COMMENT
      Scanner for SQL style line comment.
    • HASKELL_LINE_COMMENT

      public static final Parser<Void> HASKELL_LINE_COMMENT
      Scanner for haskell style line comment. (--)
    • JAVA_BLOCK_COMMENTED

      private static final Parser<Void> JAVA_BLOCK_COMMENTED
    • JAVA_BLOCK_COMMENT

      public static final Parser<Void> JAVA_BLOCK_COMMENT
      Scanner for c++/java style block comment.
    • SQL_BLOCK_COMMENT

      public static final Parser<Void> SQL_BLOCK_COMMENT
      Scanner for SQL style block comment.
    • HASKELL_BLOCK_COMMENT

      public static final Parser<Void> HASKELL_BLOCK_COMMENT
      Scanner for haskell style block comment. {- -}
    • SINGLE_QUOTE_STRING

      public static final Parser<String> SINGLE_QUOTE_STRING
      Scanner with a pattern for SQL style string literal. A SQL string literal is a string quoted by single quote, a single quote character is escaped by 2 single quotes.
    • DOUBLE_QUOTE_STRING

      public static final Parser<String> DOUBLE_QUOTE_STRING
      Scanner with a pattern for double quoted string literal. Backslash '\' is used as escape character.
    • SINGLE_QUOTE_CHAR

      public static final Parser<String> SINGLE_QUOTE_CHAR
      Scanner for a c/c++/java style character literal. such as 'a' or '\\'.
    • JAVA_DELIMITER

      public static final Parser<Void> JAVA_DELIMITER
      Scanner for the c++/java style delimiter of tokens. For example, whitespaces, line comment and block comment.
    • HASKELL_DELIMITER

      public static final Parser<Void> HASKELL_DELIMITER
      Scanner for the haskell style delimiter of tokens. For example, whitespaces, line comment and block comment.
    • SQL_DELIMITER

      public static final Parser<Void> SQL_DELIMITER
      Scanner for the SQL style delimiter of tokens. For example, whitespaces and line comment.
    • IDENTIFIER

      public static final Parser<String> IDENTIFIER
      Scanner for a regular identifier, that starts with either an underscore or an alpha character, followed by 0 or more alphanumeric characters.
    • INTEGER

      public static final Parser<String> INTEGER
      Scanner for an integer.
    • DECIMAL

      public static final Parser<String> DECIMAL
      Scanner for a decimal number.
    • DEC_INTEGER

      public static final Parser<String> DEC_INTEGER
      Scanner for a decimal number. 0 is not allowed as the leading digit.
    • OCT_INTEGER

      public static final Parser<String> OCT_INTEGER
      Scanner for a octal number. 0 is the leading digit.
    • HEX_INTEGER

      public static final Parser<String> HEX_INTEGER
      Scanner for a hexadecimal number. Has to start with 0x or 0X.
    • SCIENTIFIC_NOTATION

      public static final Parser<String> SCIENTIFIC_NOTATION
      Scanner for a scientific notation.
  • Constructor Details

    • Scanners

      private Scanners()
  • Method Details

    • many

      public static Parser<Void> many(CharPredicate predicate)
      A scanner that scans greedily for 0 or more characters that satisfies the given CharPredicate.
      Parameters:
      predicate - the predicate object.
      Returns:
      the Parser object.
    • many1

      public static Parser<Void> many1(CharPredicate predicate)
      A scanner that scans greedily for 1 or more characters that satisfies the given CharPredicate.
      Parameters:
      predicate - the predicate object.
      Returns:
      the Parser object.
    • many

      @Deprecated public static Parser<Void> many(Pattern pattern, String name)
      Deprecated.
      Use pattern.many().toScanner(name).
      A scanner that scans greedily for 0 or more occurrences of the given pattern.
      Parameters:
      pattern - the pattern object.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the Parser object.
    • many1

      @Deprecated public static Parser<Void> many1(Pattern pattern, String name)
      Deprecated.
      Use pattern.many1().toScanner(name).
      A scanner that scans greedily for 1 or more occurrences of the given pattern.
      Parameters:
      pattern - the pattern object.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the Parser object.
    • string

      public static Parser<Void> string(String str)
      Matches the input against the specified string.
      Parameters:
      str - the string to match
      Returns:
      the scanner.
    • string

      @Deprecated public static Parser<Void> string(String str, String name)
      Deprecated.
      Use Patterns.string(str).toScanner(name).
      Matches the input against the specified string.
      Parameters:
      str - the string to match
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the scanner.
    • pattern

      @Deprecated public static Parser<Void> pattern(Pattern pattern, String name)
      Deprecated.
      Use pattern.toScanner(name).
      A scanner that scans the input for an occurrence of a string pattern.
      Parameters:
      pattern - the pattern object.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the Parser object.
    • stringCaseInsensitive

      @Deprecated public static Parser<Void> stringCaseInsensitive(String str, String name)
      Deprecated.
      Use Patterns.stringCaseInsensitive(str).toScanner(name).
      A scanner that matches the input against the specified string case insensitively.
      Parameters:
      str - the string to match
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the scanner.
    • stringCaseInsensitive

      public static Parser<Void> stringCaseInsensitive(String str)
      A scanner that matches the input against the specified string case insensitively.
      Parameters:
      str - the string to match
      Returns:
      the scanner.
    • isChar

      public static Parser<Void> isChar(CharPredicate predicate)
      A scanner that succeeds and consumes the current character if it satisfies the given CharPredicate.
      Parameters:
      predicate - the predicate.
      Returns:
      the scanner.
    • isChar

      @Deprecated public static Parser<Void> isChar(CharPredicate predicate, String name)
      Deprecated.
      Implement Object.toString() in the CharPredicate, or use Patterns.isChar(predicate).toScanner(name).
      A scanner that succeeds and consumes the current character if it satisfies the given CharPredicate.
      Parameters:
      predicate - the predicate.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the scanner.
    • isChar

      @Deprecated public static Parser<Void> isChar(char ch, String name)
      Deprecated.
      Use isChar(char) instead or use Patterns.isChar(ch).toScanner(name).
      A scanner that succeeds and consumes the current character if it is equal to ch.
      Parameters:
      ch - the expected character.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the scanner.
    • isChar

      public static Parser<Void> isChar(char ch)
      A scanner that succeeds and consumes the current character if it is equal to ch.
      Parameters:
      ch - the expected character.
      Returns:
      the scanner.
    • notChar

      @Deprecated public static Parser<Void> notChar(char ch, String name)
      Deprecated.
      A scanner that succeeds and consumes the current character if it is equal to ch.
      Parameters:
      ch - the expected character.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the scanner.
    • notChar

      public static Parser<Void> notChar(char ch)
      A scanner that succeeds and consumes the current character if it is not equal to ch.
      Parameters:
      ch - the expected character.
      Returns:
      the scanner.
    • among

      @Deprecated public static Parser<Void> among(String chars, String name)
      Deprecated.
      Use Patterns.among(chars).toScanner(name).
      A scanner that succeeds and consumes the current character if it equals to any character in chars.
      Parameters:
      chars - the characters.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the scanner.
    • among

      public static Parser<Void> among(String chars)
      A scanner that succeeds and consumes the current character if it equals to any character in chars.
    • notAmong

      @Deprecated public static Parser<Void> notAmong(String chars, String name)
      Deprecated.
      Use Patterns.among(chars).not().toScanner(name), or isChar(CharPredicates.notAmong(chars), name).
      A scanner that succeeds and consumes the current character if it is not equal to any character in chars.
      Parameters:
      chars - the characters.
      name - the name of what's expected logically. Is used in error message.
      Returns:
      the scanner.
    • notAmong

      public static Parser<Void> notAmong(String chars)
      A scanner that succeeds and consumes the current character if it is not equal to any character in chars.
    • lineComment

      public static Parser<Void> lineComment(String begin)
      A scanner that succeeds and consumes all the characters until the '\n' character if the current input starts with the string literal begin. The '\n' character isn't consumed.
    • blockComment

      public static Parser<Void> blockComment(String begin, String end)
      A scanner for non-nested block comment that starts with begin and ends with end.
    • blockComment

      public static Parser<Void> blockComment(String begin, String end, Pattern commented)
      A scanner for a non-nestable block comment that starts with begin and ends with end.
      Parameters:
      begin - begins a block comment
      end - ends a block comment
      commented - the commented pattern.
      Returns:
      the Scanner for the block comment.
    • blockComment

      public static Parser<Void> blockComment(Parser<Void> begin, Parser<Void> end, Parser<?> commented)
      A scanner for a non-nestable block comment that starts with begin and ends with end.
      Parameters:
      begin - begins a block comment
      end - ends a block comment
      commented - the commented pattern.
      Returns:
      the Scanner for the block comment.
    • nestableBlockComment

      public static Parser<Void> nestableBlockComment(String begin, String end)
      A scanner for a nestable block comment that starts with begin and ends with end.
      Parameters:
      begin - begins a block comment
      end - ends a block comment
      Returns:
      the block comment scanner.
    • nestableBlockComment

      public static Parser<Void> nestableBlockComment(String begin, String end, Pattern commented)
      A scanner for a nestable block comment that starts with begin and ends with end.
      Parameters:
      begin - begins a block comment
      end - ends a block comment
      commented - the commented pattern except for nested comments.
      Returns:
      the block comment scanner.
    • nestableBlockComment

      public static Parser<Void> nestableBlockComment(Parser<?> begin, Parser<?> end, Parser<?> commented)
      A scanner for a nestable block comment that starts with begin and ends with end.
      Parameters:
      begin - starts a block comment
      end - ends a block comment
      commented - the commented pattern except for nested comments.
      Returns:
      the block comment scanner.
    • quoted

      public static Parser<String> quoted(char begin, char end)
      A scanner for a quoted string that starts with character begin and ends with character end.
    • quoted

      @Deprecated public static Parser<String> quoted(Parser<Void> begin, Parser<Void> end, Parser<?> quoted)
      Deprecated.
      Use Parsers.sequence(begin, quoted.skipMany(), end).source().
      A scanner for a quoted string that starts with begin and ends with end.
      Parameters:
      begin - begins a quote
      end - ends a quote
      quoted - the parser that recognizes the quoted pattern.
      Returns:
      the scanner.
    • nestedScanner

      public static Parser<Void> nestedScanner(Parser<?> outer, Parser<Void> inner)
      A scanner that after character level outer succeeds, subsequently feeds the recognized characters to inner for a nested scanning.

      Is useful for scenarios like parsing string interpolation grammar, with parsing errors correctly pointing to the right location in the original source.

    • notChar2

      private static Pattern notChar2(char c1, char c2)
      Matches a character if the input has at least 1 character, or if the input has at least 2 characters with the first 2 characters not being c1 and c2.
      Returns:
      the Pattern object.
    • quotedBy

      private static Parser<Void> quotedBy(Parser<Void> parser, Parser<?> quote)
    • escapedChar

      private static Pattern escapedChar(char escape)