Package org.jparsec

Class Scanners


  • public final class Scanners
    extends java.lang.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.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Scanners()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static Parser<java.lang.Void> among​(java.lang.String chars)
      A scanner that succeeds and consumes the current character if it equals to any character in chars.
      static Parser<java.lang.Void> among​(java.lang.String chars, java.lang.String name)
      Deprecated.
      Use Patterns.among(chars).toScanner(name).
      static Parser<java.lang.Void> blockComment​(java.lang.String begin, java.lang.String end)
      A scanner for non-nested block comment that starts with begin and ends with end.
      static Parser<java.lang.Void> blockComment​(java.lang.String begin, java.lang.String end, Pattern commented)
      A scanner for a non-nestable block comment that starts with begin and ends with end.
      static Parser<java.lang.Void> blockComment​(Parser<java.lang.Void> begin, Parser<java.lang.Void> end, Parser<?> commented)
      A scanner for a non-nestable block comment that starts with begin and ends with end.
      private static Pattern escapedChar​(char escape)  
      static Parser<java.lang.Void> isChar​(char ch)
      A scanner that succeeds and consumes the current character if it is equal to ch.
      static Parser<java.lang.Void> isChar​(char ch, java.lang.String name)
      Deprecated.
      Use isChar(char) instead or use Patterns.isChar(ch).toScanner(name).
      static Parser<java.lang.Void> isChar​(CharPredicate predicate)
      A scanner that succeeds and consumes the current character if it satisfies the given CharPredicate.
      static Parser<java.lang.Void> isChar​(CharPredicate predicate, java.lang.String name)
      Deprecated.
      Implement Object.toString() in the CharPredicate, or use Patterns.isChar(predicate).toScanner(name).
      static Parser<java.lang.Void> lineComment​(java.lang.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.
      static Parser<java.lang.Void> many​(CharPredicate predicate)
      A scanner that scans greedily for 0 or more characters that satisfies the given CharPredicate.
      static Parser<java.lang.Void> many​(Pattern pattern, java.lang.String name)
      Deprecated.
      Use pattern.many().toScanner(name).
      static Parser<java.lang.Void> many1​(CharPredicate predicate)
      A scanner that scans greedily for 1 or more characters that satisfies the given CharPredicate.
      static Parser<java.lang.Void> many1​(Pattern pattern, java.lang.String name)
      Deprecated.
      Use pattern.many1().toScanner(name).
      static Parser<java.lang.Void> nestableBlockComment​(java.lang.String begin, java.lang.String end)
      A scanner for a nestable block comment that starts with begin and ends with end.
      static Parser<java.lang.Void> nestableBlockComment​(java.lang.String begin, java.lang.String end, Pattern commented)
      A scanner for a nestable block comment that starts with begin and ends with end.
      static Parser<java.lang.Void> nestableBlockComment​(Parser<?> begin, Parser<?> end, Parser<?> commented)
      A scanner for a nestable block comment that starts with begin and ends with end.
      static Parser<java.lang.Void> nestedScanner​(Parser<?> outer, Parser<java.lang.Void> inner)
      A scanner that after character level outer succeeds, subsequently feeds the recognized characters to inner for a nested scanning.
      static Parser<java.lang.Void> notAmong​(java.lang.String chars)
      A scanner that succeeds and consumes the current character if it is not equal to any character in chars.
      static Parser<java.lang.Void> notAmong​(java.lang.String chars, java.lang.String name)
      Deprecated.
      Use Patterns.among(chars).not().toScanner(name), or isChar(CharPredicates.notAmong(chars), name).
      static Parser<java.lang.Void> notChar​(char ch)
      A scanner that succeeds and consumes the current character if it is not equal to ch.
      static Parser<java.lang.Void> notChar​(char ch, java.lang.String name)
      Deprecated.
      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.
      static Parser<java.lang.Void> pattern​(Pattern pattern, java.lang.String name)
      Deprecated.
      Use pattern.toScanner(name).
      static Parser<java.lang.String> quoted​(char begin, char end)
      A scanner for a quoted string that starts with character begin and ends with character end.
      static Parser<java.lang.String> quoted​(Parser<java.lang.Void> begin, Parser<java.lang.Void> end, Parser<?> quoted)
      Deprecated.
      Use Parsers.sequence(begin, quoted.skipMany(), end).source().
      private static Parser<java.lang.Void> quotedBy​(Parser<java.lang.Void> parser, Parser<?> quote)  
      static Parser<java.lang.Void> string​(java.lang.String str)
      Matches the input against the specified string.
      static Parser<java.lang.Void> string​(java.lang.String str, java.lang.String name)
      Deprecated.
      Use Patterns.string(str).toScanner(name).
      static Parser<java.lang.Void> stringCaseInsensitive​(java.lang.String str)
      A scanner that matches the input against the specified string case insensitively.
      static Parser<java.lang.Void> stringCaseInsensitive​(java.lang.String str, java.lang.String name)
      Deprecated.
      Use Patterns.stringCaseInsensitive(str).toScanner(name).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • WHITESPACES

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

        public static final Parser<java.lang.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<java.lang.Void> JAVA_LINE_COMMENT
        Scanner for c++/java style line comment.
      • SQL_LINE_COMMENT

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

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

        private static final Parser<java.lang.Void> JAVA_BLOCK_COMMENTED
      • JAVA_BLOCK_COMMENT

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

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

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

        public static final Parser<java.lang.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<java.lang.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<java.lang.String> SINGLE_QUOTE_CHAR
        Scanner for a c/c++/java style character literal. such as 'a' or '\\'.
      • JAVA_DELIMITER

        public static final Parser<java.lang.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<java.lang.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<java.lang.Void> SQL_DELIMITER
        Scanner for the SQL style delimiter of tokens. For example, whitespaces and line comment.
      • IDENTIFIER

        public static final Parser<java.lang.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<java.lang.String> INTEGER
        Scanner for an integer.
      • DECIMAL

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

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

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

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

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

      • Scanners

        private Scanners()
    • Method Detail

      • many

        public static Parser<java.lang.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<java.lang.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<java.lang.Void> many​(Pattern pattern,
                                                  java.lang.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<java.lang.Void> many1​(Pattern pattern,
                                                   java.lang.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<java.lang.Void> string​(java.lang.String str)
        Matches the input against the specified string.
        Parameters:
        str - the string to match
        Returns:
        the scanner.
      • string

        @Deprecated
        public static Parser<java.lang.Void> string​(java.lang.String str,
                                                    java.lang.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<java.lang.Void> pattern​(Pattern pattern,
                                                     java.lang.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<java.lang.Void> stringCaseInsensitive​(java.lang.String str,
                                                                   java.lang.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<java.lang.Void> stringCaseInsensitive​(java.lang.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<java.lang.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<java.lang.Void> isChar​(CharPredicate predicate,
                                                    java.lang.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<java.lang.Void> isChar​(char ch,
                                                    java.lang.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<java.lang.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<java.lang.Void> notChar​(char ch,
                                                     java.lang.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<java.lang.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<java.lang.Void> among​(java.lang.String chars,
                                                   java.lang.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<java.lang.Void> among​(java.lang.String chars)
        A scanner that succeeds and consumes the current character if it equals to any character in chars.
      • notAmong

        @Deprecated
        public static Parser<java.lang.Void> notAmong​(java.lang.String chars,
                                                      java.lang.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<java.lang.Void> notAmong​(java.lang.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<java.lang.Void> lineComment​(java.lang.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<java.lang.Void> blockComment​(java.lang.String begin,
                                                          java.lang.String end)
        A scanner for non-nested block comment that starts with begin and ends with end.
      • blockComment

        public static Parser<java.lang.Void> blockComment​(java.lang.String begin,
                                                          java.lang.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<java.lang.Void> blockComment​(Parser<java.lang.Void> begin,
                                                          Parser<java.lang.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<java.lang.Void> nestableBlockComment​(java.lang.String begin,
                                                                  java.lang.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<java.lang.Void> nestableBlockComment​(java.lang.String begin,
                                                                  java.lang.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<java.lang.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<java.lang.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<java.lang.String> quoted​(Parser<java.lang.Void> begin,
                                                      Parser<java.lang.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<java.lang.Void> nestedScanner​(Parser<?> outer,
                                                           Parser<java.lang.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<java.lang.Void> quotedBy​(Parser<java.lang.Void> parser,
                                                       Parser<?> quote)
      • escapedChar

        private static Pattern escapedChar​(char escape)