Class Group

All Implemented Interfaces:
AbstractState<Term,ASTTransition>, RegexASTVisitorIterable, JsonConvertible
Direct Known Subclasses:
ConditionalBackReferenceGroup

public class Group extends QuantifiableTerm implements RegexASTVisitorIterable
Groups are the top-most elements of regular expression ASTs.

A Group is composed of several alternatives, each of which is a Sequence of other Terms. A Group can be wrapped in parentheses and used anywhere a Term is expected. Therefore, Group extends Term.

Corresponds to the goal symbol Disjunction and the right-hand sides ( Disjunction ) and ( ? : Disjunction ) of the goal symbol Atom in the ECMAScript RegExp syntax.

On top of managing alternation and capturing, the Group AST nodes are also the primitives used to represent loops (quantifiers * and +). A Group can be marked as looping, in which case successfully matching one of its non-empty alternatives should be followed by attempting to match the Group itself again.

The collections of alternatives that make up a Group is ordered and the order indicates the priority of the alternatives: if matching with an earlier alternative is possible, that match result is preferred to those from later alternatives.

  • Constructor Details

    • Group

      protected Group(Group copy)
  • Method Details

    • copy

      public Group copy(RegexAST ast)
      Description copied from class: RegexASTNode
      Copy this node only, without any child nodes. The ID and minPath of the copied nodes is left unset.
      Specified by:
      copy in class QuantifiableTerm
      Parameters:
      ast - RegexAST the node should belong to.
      Returns:
      A shallow copy of this node.
    • copyRecursive

      public Group copyRecursive(RegexAST ast, CompilationBuffer compilationBuffer)
      Description copied from class: RegexASTNode
      Recursively copy this subtree. This method should be used instead of CopyVisitor if the copying process is required to be thread-safe. The ID and minPath of the copied nodes is left unset.
      Specified by:
      copyRecursive in class Term
      Parameters:
      ast - RegexAST the new nodes should belong to.
      Returns:
      A deep copy of this node.
    • isLoop

      public boolean isLoop()
      Returns whether this group loops. A looping group differs from a non-looping one in that when you match one of the group's non-empty alternatives, instead of continuing to the next node after the group, the same group is attempted again.
    • setLoop

      public void setLoop(boolean loop)
      Sets whether this group should loop. If the group is set to be looping, this updates the 'next' and 'prev' pointers on the non-empty alternatives to point to the group itself.
      Parameters:
      loop - true if this group should loop
      See Also:
    • isLocalFlags

      public boolean isLocalFlags()
      Returns whether this group declares local flags, e.g. "(?i:...)".
    • setLocalFlags

      public void setLocalFlags(boolean loop)
      Sets whether this group declares local flags, e.g. "(?i:...)".
      See Also:
    • getGroupNumber

      public int getGroupNumber()
      Returns the number of this capturing group. If this group is not a capturing group, returns -1.
    • getBoundaryIndexStart

      public int getBoundaryIndexStart()
      Returns the index corresponding to this capture group's BEGIN in a result array returned by a capture-group aware DFA.
    • getBoundaryIndexEnd

      public int getBoundaryIndexEnd()
      Returns the index corresponding to this capture group's END in a result array returned by a capture-group aware DFA.
    • groupNumberToBoundaryIndexStart

      public static int groupNumberToBoundaryIndexStart(int groupNumber)
      Returns the index corresponding to a capture group's BEGIN in a result array returned by a capture-group aware DFA.
    • groupNumberToBoundaryIndexEnd

      public static int groupNumberToBoundaryIndexEnd(int groupNumber)
      Returns the index corresponding to a capture group's END in a result array returned by a capture-group aware DFA.
    • isCapturing

      public boolean isCapturing()
      Returns whether this group is a capturing group.

      This is the case when this Group was built using the Group(int) constructor or if setGroupNumber(int) was called.

    • setGroupNumber

      public void setGroupNumber(int groupNumber)
      Marks this Group as capturing and sets its group number.
      Parameters:
      groupNumber -
    • hasGroupWithGuardsIndex

      public boolean hasGroupWithGuardsIndex()
    • getGroupsWithGuardsIndex

      public int getGroupsWithGuardsIndex()
    • setGroupsWithGuardsIndex

      public void setGroupsWithGuardsIndex(int groupsWithGuardsIndex)
    • clearGroupNumber

      public void clearGroupNumber()
      Marks this Group as non-capturing and clears its group number.
    • getEnclosedCaptureGroupsLow

      public int getEnclosedCaptureGroupsLow()
      Gets the (inclusive) lower bound of the range of capture groups contained within this group.
    • getCaptureGroupsLow

      public int getCaptureGroupsLow()
      Gets the (inclusive) lower bound of the range of capture groups in this term. In contrast to getEnclosedCaptureGroupsLow(), this range contains the group itself if it is a capturing group.
    • setEnclosedCaptureGroupsLow

      public void setEnclosedCaptureGroupsLow(int enclosedCaptureGroupsLow)
      Sets the (inclusive) lower bound of the range of capture groups contained within this group.
    • getEnclosedCaptureGroupsHigh

      public int getEnclosedCaptureGroupsHigh()
      Gets the (exclusive) upper bound of the range of capture groups contained within this group.
    • getCaptureGroupsHigh

      public int getCaptureGroupsHigh()
      Gets the (exclusive) upper bound of the range of capture groups in this term.
    • setEnclosedCaptureGroupsHigh

      public void setEnclosedCaptureGroupsHigh(int enclosedCaptureGroupsHigh)
      Sets the (exclusive) upper bound of the range of capture groups contained within this group.
    • hasEnclosedCaptureGroups

      public boolean hasEnclosedCaptureGroups()
    • isAlwaysZeroWidth

      public boolean isAlwaysZeroWidth()
      Returns true iff all alternatives of this group match only the empty string.
    • isUnrollingCandidate

      public boolean isUnrollingCandidate()
      Description copied from class: QuantifiableTerm
      Returns true iff the parser should try to unroll this term's quantifier.
      Specified by:
      isUnrollingCandidate in class QuantifiableTerm
    • getAlternatives

      public ArrayList<Sequence> getAlternatives()
      Returns the list of alternatives that make up this Group.

      Elements should not be added or removed from this list. Use the add(Sequence), insertFirst(Sequence) and addSequence(RegexAST) methods instead.

    • setAlternatives

      public void setAlternatives(ArrayList<Sequence> alternatives)
    • getFirstAlternative

      public Sequence getFirstAlternative()
    • size

      public int size()
    • isEmpty

      public boolean isEmpty()
    • add

      public void add(Sequence sequence)
      Adds a new alternative to this group. The new alternative will be appended to the end, meaning it will have the lowest priority among all the alternatives.
      Parameters:
      sequence -
    • insertFirst

      public void insertFirst(Sequence sequence)
      Inserts a new alternative to this group. The new alternative will be inserted at the beginning, meaning it will have the highest priority among all the alternatives.
      Parameters:
      sequence -
    • addSequence

      public Sequence addSequence(RegexAST ast)
      Creates a new empty alternatives and adds it to the end of the list of alternatives.
      Parameters:
      ast - The AST that the new alternative should belong to
      Returns:
      The newly created alternative
    • getLastAlternative

      public Sequence getLastAlternative()
    • removeLastSequence

      public void removeLastSequence()
    • isLiteral

      public boolean isLiteral()
    • visitorHasNext

      public boolean visitorHasNext()
      Specified by:
      visitorHasNext in interface RegexASTVisitorIterable
    • visitorGetNext

      public RegexASTNode visitorGetNext(boolean reverse)
      Specified by:
      visitorGetNext in interface RegexASTVisitorIterable
    • resetVisitorIterator

      public void resetVisitorIterator()
      Specified by:
      resetVisitorIterator in interface RegexASTVisitorIterable
    • alternativesToString

      public String alternativesToString()
    • loopToString

      public String loopToString()
    • equalsSemantic

      public boolean equalsSemantic(RegexASTNode obj, boolean ignoreQuantifier)
      Specified by:
      equalsSemantic in class QuantifiableTerm
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toJson

      public JsonValue toJson()
      Specified by:
      toJson in interface JsonConvertible