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

org.aya.literate.parser.InterestingLanguage Maven / Gradle / Ivy

The newest version!
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.literate.parser;

import org.aya.literate.Literate;
import org.aya.util.error.SourcePos;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.BiFunction;
import java.util.function.Predicate;

public interface InterestingLanguage {
  boolean test(@NotNull String language);
  @NotNull T create(@NotNull String code, @Nullable SourcePos sourcePos);

  @NotNull InterestingLanguage ALL = of(_ -> true,
    (s, sourcePos) -> new Literate.CodeBlock("unknown", s, sourcePos));

  static @NotNull InterestingLanguage of(@NotNull String language) {
    return of(language::equalsIgnoreCase, (s, sourcePos) -> new Literate.CodeBlock(language, s, sourcePos));
  }

  static  @NotNull InterestingLanguage of(
    @NotNull Predicate test,
    @NotNull BiFunction factory
  ) {
    return new InterestingLanguage<>() {
      @Override public boolean test(@NotNull String language) { return test.test(language); }

      @Override public @NotNull T create(@NotNull String code, @Nullable SourcePos sourcePos) {
        return factory.apply(code, sourcePos);
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy