All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jparsercombinator.ParserCombinators Maven / Gradle / Ivy

package org.jparsercombinator;

import java.util.regex.MatchResult;
import java.util.regex.Pattern;

/**
 * Factory methods for creating common parser combinators.
 */
public final class ParserCombinators {

  private ParserCombinators() {
  }

  /**
   * Creates a {@code ParserCombinator} that accepts given string only.
   *
   * @param str string accepted by the parser
   * @return the string that was accepted
   */
  public static ParserCombinator string(String str) {
    return regex(Pattern.quote(str));
  }

  /**
   * Creates a {@code ParserCombinator} that accepts inputs matching given regex.
   *
   * @param regex pattern accepted by the parser
   * @return the matching input as a whole
   */
  public static ParserCombinator regex(String regex) {
    return regexMatchResult(regex).map(MatchResult::group);
  }

  /**
   * Creates a {@code ParserCombinator} that accepts inputs matching given regex.
   *
   * @param regex pattern accepted by the parser
   * @return the regex match result for further grouping etc.
   */
  public static ParserCombinator regexMatchResult(String regex) {
    return new RegexParserCombinator(Pattern.compile(regex));
  }

  /**
   * Creates a {@code SkipCombinator} for building a {@code ParserCombinator} that skips first then
   * applies next combinator. For example {@code skip(string("(")).next(string("foo")).skip(string(")"))}
   * parses parenthesized string "foo".
   *
   * @param skip a parser to be skipped
   * @return {@code SkipCombinator} that provides {@code next(ParserCombinator next)} for
   * building a new {@code ParserCombinator} that skips the first combinator then applies next.
   */
  public static SkipCombinator skip(ParserCombinator skip) {
    return new SkipCombinator(skip);
  }

  /**
   * Creates a {@code ParserCombinatorReference} that can be used to create self referencing
   * recursive parsers. {@code ParserCombinatorReference} is simply a minimal implementation of the
   * delegate pattern.
   *
   * @param  type of the parse result
   * @return a new {@code ParserCombinatorReference}
   */
  public static  ParserCombinatorReference newRef() {
    return new ParserCombinatorReference<>();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy