Class Parser<T extends Sentence>

java.lang.Object
net.diversionmc.parser.Parser<T>
Type Parameters:
T - Parsed text output sentence type.

public final class Parser<T extends Sentence>
extends java.lang.Object
Parser - convert any written language into custom format
Author:
Kirill Semyonkin - Diversion Network 2021
  • Constructor Summary

    Constructors
    Constructor Description
    Parser()
    Create Parser without any input attached and automatically set name.
    Parser​(java.io.File f)
    Create Parser from a file.
    Parser​(java.lang.String name)
    Create Parser with a title.
    Parser​(java.lang.String name, java.io.InputStream is)
    Create Parser from an input stream with a title.
    Parser​(java.lang.String name, java.lang.String text)
    Create Parser of a text with a title.
  • Method Summary

    Modifier and Type Method Description
    java.util.List<T> build()
    Convert input text into a list of usable sentences.
    FilePointer end()
    Get file pointer pointing to the end of the file (line += 1, column = 1).
    <L extends ExpressionPiece,​ R extends ExpressionPiece>
    Parser<T>
    group​(java.util.function.Predicate<ExpressionPiece> left, java.util.function.Predicate<ExpressionPiece> right, GroupSupplier<L,​R> supplier)
    Create a grouper for specific pieces.
    java.lang.String name()
    Get title used on creation of this parser.
    static <T extends Sentence>
    Parser<T>
    parser()
    Create Parser without any input attached and automatically set name.
    static <T extends Sentence>
    Parser<T>
    parser​(java.io.File f)
    Create Parser from a file.
    static <T extends Sentence>
    Parser<T>
    parser​(java.lang.String name)
    Create Parser of a text with a title.
    static <T extends Sentence>
    Parser<T>
    parser​(java.lang.String name, java.io.InputStream is)
    Create Parser from an input stream with a title.
    static <T extends Sentence>
    Parser<T>
    parser​(java.lang.String name, java.lang.String text)
    Create Parser of a text with a title.
    ParsePattern<T> pattern​(java.lang.String id)
    Get a pattern that was already added to this parser.
    Parser<T> pattern​(java.lang.String id, ParsePattern<T> pattern)
    Add a piece to sentence converter.
    ParsePattern<T>[] patterns()
    Get all patterns that were added to this parser.
    Parser<T> piece​(PiecePredicate check, PieceSupplier supplier)
    Add a parse piece.
    Parser<T> piece​(PieceSupplier supplier)
    Add a parse piece that is always accepted.
    Parser<T> pieceFinish​(java.util.function.Consumer<ExpressionPiece> event)
    Add an action to run after a piece is completed.
    Parser<T> pre​(java.lang.Runnable pre)
    Set the first action to perform on build() before starting to parse text.
    Parser<T> readFrom​(java.io.File f)
    Set text to parse from a file, as well as text title.
    Parser<T> readFrom​(java.io.InputStream is)
    Set text to parse from a stream.
    java.lang.String text()
    Get text that was inputted into this Parser.
    Parser<T> text​(java.lang.String text)
    Set text to parse.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Parser

      public Parser()
      Create Parser without any input attached and automatically set name.
    • Parser

      public Parser​(java.lang.String name)
      Create Parser with a title.
      Parameters:
      name - Text title; usually filename.
    • Parser

      public Parser​(java.lang.String name, java.lang.String text)
      Create Parser of a text with a title.
      Parameters:
      name - Text title; usually filename.
      text - Input text to parse.
    • Parser

      public Parser​(java.lang.String name, java.io.InputStream is) throws java.io.IOException
      Create Parser from an input stream with a title.
      Parameters:
      name - Text title; usually filename.
      is - Input stream to get text from to parse.
      Throws:
      java.io.IOException - If stream reading error occurs.
    • Parser

      public Parser​(java.io.File f) throws java.io.IOException
      Create Parser from a file.
      Parameters:
      f - File to parse.
      Throws:
      java.io.IOException - If file reading error occurs.
  • Method Details

    • parser

      public static <T extends Sentence> Parser<T> parser()
      Create Parser without any input attached and automatically set name.
      Type Parameters:
      T - Parsed text output sentence type.
      Returns:
      Parser()
    • parser

      public static <T extends Sentence> Parser<T> parser​(java.lang.String name)
      Create Parser of a text with a title.
      Type Parameters:
      T - Parsed text output sentence type.
      Parameters:
      name - Text title; usually filename.
      Returns:
      Parser(String)
    • parser

      public static <T extends Sentence> Parser<T> parser​(java.lang.String name, java.lang.String text)
      Create Parser of a text with a title.
      Type Parameters:
      T - Parsed text output sentence type.
      Parameters:
      name - Text title; usually filename.
      text - Input text to parse.
      Returns:
      Parser(String, String)
    • parser

      public static <T extends Sentence> Parser<T> parser​(java.lang.String name, java.io.InputStream is) throws java.io.IOException
      Create Parser from an input stream with a title.
      Type Parameters:
      T - Parsed text output sentence type.
      Parameters:
      name - Text title; usually filename.
      is - Input stream to get text from to parse.
      Returns:
      Parser(String, InputStream)
      Throws:
      java.io.IOException - If stream reading error occurs.
    • parser

      public static <T extends Sentence> Parser<T> parser​(java.io.File f) throws java.io.IOException
      Create Parser from a file.
      Type Parameters:
      T - Parsed text output sentence type.
      Parameters:
      f - File to parse.
      Returns:
      Parser(File)
      Throws:
      java.io.IOException - If file reading error occurs.
    • name

      public java.lang.String name()
      Get title used on creation of this parser. If title was not specified, it is Object.hashCode().
      Returns:
      Text title; usually filename.
    • text

      public java.lang.String text()
      Get text that was inputted into this Parser.
      Returns:
      Input text to parse.
    • text

      public Parser<T> text​(java.lang.String text)
      Set text to parse.
      Parameters:
      text - Input text to parse.
    • readFrom

      public Parser<T> readFrom​(java.io.File f) throws java.io.IOException
      Set text to parse from a file, as well as text title.
      Parameters:
      f - File to parse.
      Throws:
      java.io.IOException - If file reading error occurs.
    • readFrom

      public Parser<T> readFrom​(java.io.InputStream is) throws java.io.IOException
      Set text to parse from a stream.
      Parameters:
      is - Input stream to get text from to parse.
      Throws:
      java.io.IOException - If stream reading error occurs.
    • end

      public FilePointer end()
      Get file pointer pointing to the end of the file (line += 1, column = 1).
      Returns:
      End file pointer.
    • pre

      public Parser<T> pre​(java.lang.Runnable pre)
      Set the first action to perform on build() before starting to parse text.
      Parameters:
      pre - Action to run.
    • piece

      public Parser<T> piece​(PieceSupplier supplier)
      Add a parse piece that is always accepted.
      Parameters:
      supplier - Piece supplier, null is same as not accepting a piece.
    • piece

      public Parser<T> piece​(PiecePredicate check, PieceSupplier supplier)
      Add a parse piece.
      Parameters:
      check - Check whether the piece is acceptable for a character at a position.
      supplier - Piece supplier, null is same as not accepting a piece.
    • pieceFinish

      public Parser<T> pieceFinish​(java.util.function.Consumer<ExpressionPiece> event)
      Add an action to run after a piece is completed.
      Parameters:
      event - Action to run.
    • group

      public <L extends ExpressionPiece,​ R extends ExpressionPiece> Parser<T> group​(java.util.function.Predicate<ExpressionPiece> left, java.util.function.Predicate<ExpressionPiece> right, GroupSupplier<L,​R> supplier)
      Create a grouper for specific pieces.
      Type Parameters:
      L - Opening piece type.
      R - Closing piece type.
      Parameters:
      left - Group opening piece.
      right - Group closing piece.
      supplier - Group creator.
    • pattern

      public Parser<T> pattern​(java.lang.String id, ParsePattern<T> pattern)
      Add a piece to sentence converter.
      Parameters:
      id - Name of the pattern.
      pattern - The pattern.
    • pattern

      public ParsePattern<T> pattern​(java.lang.String id)
      Get a pattern that was already added to this parser.
      Parameters:
      id - Name of the pattern.
    • patterns

      public ParsePattern<T>[] patterns()
      Get all patterns that were added to this parser.
      Returns:
      Array of the patterns to use in matchOne().
    • build

      public java.util.List<T> build()
      Convert input text into a list of usable sentences.
      Returns:
      List of sentences.