Package org.jparsec

Class Terminals

java.lang.Object
org.jparsec.Lexicon
org.jparsec.Terminals

public final class Terminals extends Lexicon
Provides convenient API to build lexer and parsers for terminals. The following example is a parser snippet for Java generic type expression such as List<String>:
   
   Terminals terms = Terminals
       .operators("?", "<", ">", ",")
       .words(Scanners.IDENTIFIER)
       .keywords("super", "extends")
       .build();
   Parser<String> typeName = Terminals.identifier();
   Parser<?> wildcardWithUpperBound = terms.phrase("?", "extends");
   ...
   parser.from(terms.tokenizer(), Scanners.WHITESPACES.optional()).parse("List<String>");
 
  • Field Details

  • Constructor Details

    • Terminals

      private Terminals(Lexicon lexicon)
  • Method Details

    • caseInsensitive

      @Deprecated public static Terminals caseInsensitive(String[] ops, String[] keywords)
      Deprecated.
      Use operators(ops) .words(Scanners.IDENTIFIER) .caseInsensitiveKeywords(keywords) .build() instead.
      Returns a Terminals object for lexing and parsing the operators with names specified in ops, and for lexing and parsing the keywords case insensitively. Parsers for operators and keywords can be obtained through Lexicon.token(java.lang.String...); parsers for identifiers through identifier().

      In detail, keywords and operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. Words that are not among keywords are lexed as Fragment with Tokens.Tag.IDENTIFIER tag.

      A word is defined as an alphanumeric string that starts with [_a - zA - Z], with 0 or more [0 - 9_a - zA - Z] following.

      Parameters:
      ops - the operator names.
      keywords - the keyword names.
      Returns:
      the Terminals instance.
    • caseSensitive

      @Deprecated public static Terminals caseSensitive(String[] ops, String[] keywords)
      Deprecated.
      Use operators(ops) .words(Scanners.IDENTIFIER) .keywords(keywords) .build() instead.
      Returns a Terminals object for lexing and parsing the operators with names specified in ops, and for lexing and parsing the keywords case sensitively. Parsers for operators and keywords can be obtained through Lexicon.token(java.lang.String...); parsers for identifiers through identifier().

      In detail, keywords and operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. Words that are not among keywords are lexed as Fragment with Tokens.Tag.IDENTIFIER tag.

      A word is defined as an alphanumeric string that starts with [_a - zA - Z], with 0 or more [0 - 9_a - zA - Z] following.

      Parameters:
      ops - the operator names.
      keywords - the keyword names.
      Returns:
      the Terminals instance.
    • caseInsensitive

      @Deprecated public static Terminals caseInsensitive(Parser<String> wordScanner, String[] ops, String[] keywords)
      Deprecated.
      Use operators(ops) .words(wordScanner) .caseInsensitiveKeywords(keywords) .build() instead.
      Returns a Terminals object for lexing and parsing the operators with names specified in ops, and for lexing and parsing the keywords case insensitively. Parsers for operators and keywords can be obtained through Lexicon.token(java.lang.String...); parsers for identifiers through identifier().

      In detail, keywords and operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. Words that are not among keywords are lexed as Fragment with Tokens.Tag.IDENTIFIER tag.

      Parameters:
      wordScanner - the scanner that returns a word in the language.
      ops - the operator names.
      keywords - the keyword names.
      Returns:
      the Terminals instance.
    • caseSensitive

      @Deprecated public static Terminals caseSensitive(Parser<String> wordScanner, String[] ops, String[] keywords)
      Deprecated.
      Use operators(ops) .words(wordScanner) .keywords(keywords) .build() instead.
      Returns a Terminals object for lexing and parsing the operators with names specified in ops, and for lexing and parsing the keywords case sensitively. Parsers for operators and keywords can be obtained through Lexicon.token(java.lang.String...); parsers for identifiers through identifier().

      In detail, keywords and operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. Words that are not among keywords are lexed as Fragment with Tokens.Tag.IDENTIFIER tag.

      Parameters:
      wordScanner - the scanner that returns a word in the language.
      ops - the operator names.
      keywords - the keyword names.
      Returns:
      the Terminals instance.
    • caseInsensitive

      @Deprecated public static Terminals caseInsensitive(Parser<String> wordScanner, String[] ops, String[] keywords, Function<String,?> wordMap)
      Deprecated.
      Use operators(ops) .words(wordScanner) .tokenizeWordsWith(wordMap) .caseInsensitiveKeywords(keywords) .build() instead.
      Returns a Terminals object for lexing and parsing the operators with names specified in ops, and for lexing and parsing the keywords case insensitively. Parsers for operators and keywords can be obtained through Lexicon.token(java.lang.String...); parsers for identifiers through identifier().

      In detail, keywords and operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. Words that are not among keywords are lexed as Fragment with Tokens.Tag.IDENTIFIER tag.

      Parameters:
      wordScanner - the scanner that returns a word in the language.
      ops - the operator names.
      keywords - the keyword names.
      wordMap - maps the text to a token value for non-keywords recognized by wordScanner.
      Returns:
      the Terminals instance.
    • caseSensitive

      @Deprecated public static Terminals caseSensitive(Parser<String> wordScanner, String[] ops, String[] keywords, Function<String,?> wordMap)
      Deprecated.
      Use operators(ops) .words(wordScanner) .tokenizeWordsWith(wordMap) .keywords(keywords) .build() instead.
      Returns a Terminals object for lexing and parsing the operators with names specified in ops, and for lexing and parsing the keywords case sensitively. Parsers for operators and keywords can be obtained through Lexicon.token(java.lang.String...); parsers for identifiers through identifier().

      In detail, keywords and operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. Words that are not among keywords are lexed as Fragment with Tokens.Tag.IDENTIFIER tag.

      Parameters:
      wordScanner - the scanner that returns a word in the language.
      ops - the operator names.
      keywords - the keyword names.
      wordMap - maps the text to a token value for non-keywords recognized by wordScanner.
      Returns:
      the Terminals instance.
    • operators

      public static Terminals operators(String... ops)
      Returns a Terminals object for lexing the operators with names specified in ops. Operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. For example, to get the parser for operator "?", simply call token("?").

      If words and keywords need to be parsed, they can be configured via words(org.jparsec.Parser<java.lang.String>).

      Parameters:
      ops - the operator names.
      Returns:
      the Terminals instance.
    • operators

      public static Terminals operators(Collection<String> ops)
      Returns a Terminals object for lexing the operators with names specified in ops. Operators are lexed as Tokens.Fragment with Tokens.Tag.RESERVED tag. For example, to get the parser for operator "?", simply call token("?").

      If words and keywords need to be parsed, they can be configured via words(org.jparsec.Parser<java.lang.String>).

      Parameters:
      ops - the operator names.
      Returns:
      the Terminals instance.
      Since:
      2.2
    • words

      public Terminals.Builder words(Parser<String> wordScanner)
      Starts to build a new Terminals instance that recognizes words not already recognized by this Terminals instance (typically operators).

      By default identifiers are recognized through identifier() during token-level parsing phase. Use Terminals.Builder.tokenizeWordsWith(java.util.function.Function<java.lang.String, ?>) to tokenize differently, and choose an alternative token-level parser accordingly.

      Parameters:
      wordScanner - defines words recognized by the new instance
      Since:
      2.2
    • identifier

      public static Parser<String> identifier()
      Returns a Parser that recognizes identifiers (a.k.a words, variable names etc). Equivalent to Terminals.Identifier.PARSER.
      Since:
      2.2
    • fragment

      public static Parser<String> fragment(Object... tags)
      Returns a Parser that recognizes Tokens.Fragment token values tagged with one of tags.
    • fromFragment

      static TokenMap<String> fromFragment(Object... tags)
      Returns a TokenMap object that only recognizes Tokens.Fragment token values tagged with one of tags.
    • checkDup

      static void checkDup(Iterable<String> a, Iterable<String> b)