Class PipelineParser


  • public class PipelineParser
    extends java.lang.Object
    Parses a command line string into a Pipeline.

    By default, the parser recognizes all operators defined in Pipeline.Operator, using their default symbols. Custom symbol mappings can be provided via the PipelineParser(Map) constructor to override default symbols:

     // Replace > and >> with |> and |>> (e.g. for Groovy)
     new PipelineParser(Map.of("|>", Operator.REDIRECT, "|>>", Operator.APPEND))
     
    When a custom mapping targets an Pipeline.Operator that already has a default symbol, the default symbol is replaced. This allows languages where > has a different meaning to use alternative redirect syntax.

    The parser respects quoting (single and double) and bracket nesting, so operators inside quoted strings or brackets are not treated as pipeline operators.

    Subclasses can override matchOperator(String, int) to customize operator matching beyond what the constructor-based approach supports.

    Since:
    4.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  PipelineParser.Token
      A token from tokenization -- either text or an operator.
    • Constructor Summary

      Constructors 
      Constructor Description
      PipelineParser()
      Creates a new PipelineParser with default operator symbols from Pipeline.Operator.
      PipelineParser​(java.util.Map<java.lang.String,​Pipeline.Operator> customOperators)
      Creates a new PipelineParser with custom operator symbol mappings.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.String matchOperator​(java.lang.String line, int pos)
      Tries to match a pipeline operator at the given position.
      Pipeline parse​(java.lang.String line)
      Parses a command line into a Pipeline.
      • Methods inherited from class java.lang.Object

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

      • PipelineParser

        public PipelineParser()
        Creates a new PipelineParser with default operator symbols from Pipeline.Operator.
      • PipelineParser

        public PipelineParser​(java.util.Map<java.lang.String,​Pipeline.Operator> customOperators)
        Creates a new PipelineParser with custom operator symbol mappings.

        Each entry maps a symbol string to an Pipeline.Operator. If a custom mapping targets an operator that already has a default symbol, the default symbol is removed and replaced by the custom one. For example:

         // "|>" replaces ">" for REDIRECT, "|>>" replaces ">>" for APPEND
         new PipelineParser(Map.of("|>", Operator.REDIRECT, "|>>", Operator.APPEND))
         
        Operators not mentioned in the custom map keep their default symbols.
        Parameters:
        customOperators - a map from operator symbol to Pipeline.Operator
    • Method Detail

      • parse

        public Pipeline parse​(java.lang.String line)
        Parses a command line into a Pipeline.
        Parameters:
        line - the command line to parse
        Returns:
        the parsed pipeline
      • matchOperator

        protected java.lang.String matchOperator​(java.lang.String line,
                                                 int pos)
        Tries to match a pipeline operator at the given position. Returns the matched operator string, or null.

        The default implementation checks all operators in the operator table (longest-match first). Subclasses can override this method for fully custom matching logic.

        Parameters:
        line - the full command line string
        pos - the current position in the line
        Returns:
        the matched operator string, or null if no operator matches