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

com.squarespace.compiler.parse.Parser Maven / Gradle / Ivy

The newest version!
/**
 * Copyright, 2017, Squarespace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.squarespace.compiler.parse;

import static com.squarespace.compiler.common.Maybe.just;
import static com.squarespace.compiler.common.Maybe.nothing;
import static com.squarespace.compiler.parse.Pair.pair;
import static java.util.Collections.emptyList;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import com.squarespace.compiler.common.Maybe;
import com.squarespace.compiler.match.Recognizers.Recognizer;


/**
 * Monadic parser.
 */
public interface Parser {

  Maybe> parse(CharSequence s);

  static Parser matcher(Recognizer pattern) {
    return s -> {
      int length = s.length();
      int end = pattern.match(s, 0, length);
      return end == -1 ? nothing() : just(pair(s.subSequence(0, end), s.subSequence(end, length)));
    };
  }

  default  Parser prefix(Parser parser) {
    return parser.flatMap(o -> this);
  }

  default Parser suffix(Parser parser) {
    return this.flatMap(t -> parser.map(o -> t));
  }

  default  Parser map(Function f) {
    return s -> parse(s).map(p -> pair(f.apply(p._1), p._2));
}

  default  Parser flatMap(Function> f) {
    return s -> parse(s).flatMap(p -> f.apply(p._1).parse(p._2));
  }

  default Parser> zeroOrMore() {
    return oneOrMore().orDefault(emptyList());
  }

  default Parser> oneOrMore() {
    return flatMap(x -> zeroOrMore().map(xs -> cons(x, xs)));
  }

  default Parser or(Parser alt) {
    return s -> parse(s).orElse(() -> alt.parse(s));
  }

  default Parser orDefault(T v) {
    return s -> parse(s).orElse(() -> just(pair(v, s)));
  }

  default  Parser> separated(Parser delimiter) {
    Parser skipped = prefix(delimiter);
    return flatMap(t -> skipped.zeroOrMore().map(ts -> cons(t, ts)));
  }

  static  List cons(T x, List xs) {
    List result = new ArrayList<>();
    result.add(x);
    result.addAll(xs);
    return result;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy