Class MCBuildVisitor

java.lang.Object
de.monticore.antlr4.MCBuildVisitor

public abstract class MCBuildVisitor extends Object
BuildVisitors are used in the second stage of the two-phase-parsers. Phase one results in an ANTLR parse tree (without any AST-specific actions), and phase is done by ANTLR ParseTreeVisitor-instances of this class building the MontiCore-AST representation of the input.

This class mainly provides SourcePosition and Comment related functionality. The concrete ParseTree-to-AST transformation is handled in the generated instances of this class.

  • Field Details

    • fileName

      protected String fileName
    • bufferedTokenStream

      protected org.antlr.v4.runtime.BufferedTokenStream bufferedTokenStream
    • debug

      public static boolean debug
      A flag for further debugging of comment-handling. Not printed by default due to performance
    • pevPreCommentTokenIndex

      protected int pevPreCommentTokenIndex
    • tokenIndex

      protected int tokenIndex
    • commentChannel

      protected int commentChannel
    • commentSet

      protected org.antlr.v4.runtime.misc.IntervalSet commentSet
  • Constructor Details

    • MCBuildVisitor

      public MCBuildVisitor(String fileName, org.antlr.v4.runtime.BufferedTokenStream bufferedTokenStream)
  • Method Details

    • getFileName

      public String getFileName()
    • setSourcePos

      protected <T extends ASTNodeBuilder<?>> T setSourcePos(T builder, org.antlr.v4.runtime.ParserRuleContext ctx)
    • computePosition

      public de.se_rwth.commons.SourcePosition computePosition(org.antlr.v4.runtime.Token token)
    • computeEndPosition

      public de.se_rwth.commons.SourcePosition computeEndPosition(@Nonnull org.antlr.v4.runtime.Token token)
    • handlePreComments

      protected void handlePreComments(ASTNodeBuilder<?> builder, org.antlr.v4.runtime.ParserRuleContext ctx)
      Collect fresh tokens on the commentChannel channel and store them as pre-comments
      Parameters:
      builder - the builder to store the pre-comments on
      ctx - the context object to walk up to
    • handleInnerComments

      protected void handleInnerComments(ASTNodeBuilder<?> builder, org.antlr.v4.runtime.ParserRuleContext ctx)
      Collect all fresh tokens of the same line on the commentChannel channel and store them as post-comments
      Parameters:
      builder - the builder to store the post-comments on
      ctx - the context object in which line we check
    • addFinalComments

      public void addFinalComments(ASTNode node, org.antlr.v4.runtime.ParserRuleContext ctx)
      Finally, add all comment-tokens after the final AST-element as post-comments to the element.
      Parameters:
      node - the final ASTNode
      ctx - the parse tree
    • doComments

      protected void doComments(Consumer<Comment> addConsumer, int start, int stop)
      Find all (not yet found) hidden/comment tokens and calls the consumer for each
      Parameters:
      addConsumer - e.g. ASTNode.add_PreComment(Comment)
      start - IntervalSet.complement(int, int)
      stop - IntervalSet.complement(int, int)
    • isComment

      protected boolean isComment(org.antlr.v4.runtime.Token token)
    • createComment

      protected Comment createComment(org.antlr.v4.runtime.Token token)
      Create a new MontiCore comment from an ANTLR token. Sets the start and end position, as well as the text.
      Parameters:
      token - the token
      Returns:
      the comment