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 Object
Parser - convert any written language into custom format
Author:
Kirill Semyonkin - Diversion Network 2021
  • Constructor Details

    • Parser

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

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

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

      public Parser(String name, InputStream is) throws 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:
      IOException - If stream reading error occurs.
    • Parser

      public Parser(File f) throws IOException
      Create Parser from a file.
      Parameters:
      f - File to parse.
      Throws:
      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(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(String name, 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(String name, InputStream is) throws 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:
      IOException - If stream reading error occurs.
    • parser

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

      public 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 String text()
      Get text that was inputted into this Parser.
      Returns:
      Input text to parse.
    • text

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

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

      public Parser<T> readFrom(InputStream is) throws IOException
      Set text to parse from a stream.
      Parameters:
      is - Input stream to get text from to parse.
      Throws:
      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(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(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(Predicate<ExpressionPiece> left, 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(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(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 List<T> build()
      Convert input text into a list of usable sentences.
      Returns:
      List of sentences.
    • buildExpressions

      public List<ExpressionPiece> buildExpressions()
      Convert input text into a list of ExpressionPieces.
      Returns:
      List of ExpressionPieces.
    • buildExpressionsGrouped

      public List<ExpressionPiece> buildExpressionsGrouped()
      Convert input text into a list of ExpressionPieces, grouped up with registered groupers.
      Returns:
      List of ExpressionPieces with group ExpressionPieces present.
    • group

      public List<ExpressionPiece> group(List<ExpressionPiece> content)
      Group up ExpressionPieces in the given list into ExpressionPieces which represent such groups.
      Returns:
      List of ExpressionPieces with group ExpressionPieces present.