Class OpsBuilder


  • public final class OpsBuilder
    extends java.lang.Object
    An OpsBuilder creates a list of Ops, which is turned into a Doc by DocBuilder.
    • Field Detail

      • input

        private final Input input
      • ops

        private final java.util.List<Op> ops
      • output

        private final Output output
      • tokenI

        private int tokenI
      • inputPosition

        private int inputPosition
      • depth

        int depth
        The number of unclosed open ops in the input stream.
      • lastPartialFormatBoundary

        private int lastPartialFormatBoundary
      • SPACE

        private static final Doc.Space SPACE
    • Constructor Detail

      • OpsBuilder

        public OpsBuilder​(Input input,
                          Output output)
        The OpsBuilder constructor.
        Parameters:
        input - the Input, used for retrieve information from the AST
        output - the Output, used here only to record blank-line information
    • Method Detail

      • actualSize

        public int actualSize​(int position,
                              int length)
        Returns:
        the actual size of the AST node at position, including comments.
      • actualStartColumn

        public java.lang.Integer actualStartColumn​(int position)
        Returns:
        the start column of the token at position, including leading comments.
      • add

        public final void add​(Op op)
        Add an Op, and record open/close ops for later validation of unclosed levels.
      • addAll

        public final void addAll​(java.util.List<Op> ops)
        Add a list of Ops.
      • getInput

        public final Input getInput()
        Get the OpsBuilder's Input.
      • depth

        public int depth()
        Returns the number of unclosed open ops in the input stream.
      • checkClosed

        public void checkClosed​(int previous)
        Checks that all open ops in the op stream have matching close ops.
        Throws:
        FormattingError - if any ops were unclosed
      • sync

        public final void sync​(int inputPosition)
        Sync to position in the input. If we've skipped outputting any tokens that were present in the input tokens, output them here and optionally complain.
        Parameters:
        inputPosition - the 0-based input position
      • drain

        public final void drain()
        Output any remaining tokens from the input stream (e.g. terminal whitespace).
      • open

        public final void open​(Indent plusIndent)
        Open a new level by emitting an OpenOp.
        Parameters:
        plusIndent - the extra indent for the new level
      • close

        public final void close()
        Close the current level, by emitting a CloseOp.
      • peekToken

        public final java.util.Optional<java.lang.String> peekToken()
        Return the text of the next Input.Token, or absent if there is none.
      • peekToken

        public final java.util.Optional<java.lang.String> peekToken​(int skip)
        Return the text of an upcoming Input.Token, or absent if there is none.
      • peekTokens

        public com.google.common.collect.ImmutableList<Input.Tok> peekTokens​(int startPosition,
                                                                             com.google.common.base.Predicate<Input.Tok> predicate)
        Returns the Input.Toks starting at the current source position, which are satisfied by the given predicate.
      • guessToken

        public final void guessToken​(java.lang.String token)
        Emit an optional token iff it exists on the input. This is used to emit tokens whose existence has been lost in the AST.
        Parameters:
        token - the optional token
      • token

        public final void token​(java.lang.String token,
                                Doc.Token.RealOrImaginary realOrImaginary,
                                Indent plusIndentCommentsBefore,
                                java.util.Optional<Indent> breakAndIndentTrailingComment)
      • op

        public final void op​(java.lang.String op)
        Emit a single- or multi-character op by breaking it into single-character Doc.Tokens.
        Parameters:
        op - the operator to emit
      • space

        public final void space()
        Emit a Doc.Space.
      • breakOp

        public final void breakOp()
        Emit a Doc.Break.
      • breakOp

        public final void breakOp​(Indent plusIndent)
        Emit a Doc.Break.
        Parameters:
        plusIndent - extra indent if taken
      • breakToFill

        public final void breakToFill()
        Emit a filled Doc.Break.
      • forcedBreak

        public final void forcedBreak()
        Emit a forced Doc.Break.
      • forcedBreak

        public final void forcedBreak​(Indent plusIndent)
        Emit a forced Doc.Break.
        Parameters:
        plusIndent - extra indent if taken
      • breakOp

        public final void breakOp​(java.lang.String flat)
        Emit a Doc.Break, with a specified flat value (e.g., " ").
        Parameters:
        flat - the Doc.Break when not broken
      • breakToFill

        public final void breakToFill​(java.lang.String flat)
        Emit a Doc.Break, with a specified flat value (e.g., " ").
        Parameters:
        flat - the Doc.Break when not broken
      • breakOp

        public final void breakOp​(Doc.FillMode fillMode,
                                  java.lang.String flat,
                                  Indent plusIndent)
        Emit a generic Doc.Break.
        Parameters:
        fillMode - the Doc.FillMode
        flat - the Doc.Break when not broken
        plusIndent - extra indent if taken
      • breakOp

        public final void breakOp​(Doc.FillMode fillMode,
                                  java.lang.String flat,
                                  Indent plusIndent,
                                  java.util.Optional<Output.BreakTag> optionalTag)
        Emit a generic Doc.Break.
        Parameters:
        fillMode - the Doc.FillMode
        flat - the Doc.Break when not broken
        plusIndent - extra indent if taken
        optionalTag - an optional tag for remembering whether the break was taken
      • markForPartialFormat

        public void markForPartialFormat()
        Make the boundary of a region that can be partially formatted. The boundary will be included in the following region, e.g.: [[boundary0, boundary1), [boundary1, boundary2), ...].
      • blankLineWanted

        public final void blankLineWanted​(OpsBuilder.BlankLineWanted wanted)
        Force or suppress a blank line here in the output.
        Parameters:
        wanted - whether to force (true) or suppress false) the blank line
      • getI

        private static int getI​(Input.Token token)
      • build

        public final com.google.common.collect.ImmutableList<Op> build()
        Build a list of Ops from the OpsBuilder.
        Returns:
        the list of Ops
      • isForcedBreak

        private static boolean isForcedBreak​(Op op)
      • makeComment

        private static java.util.List<Op> makeComment​(Input.Tok comment)
      • toString

        public final java.lang.String toString()
        Overrides:
        toString in class java.lang.Object