private static bool IsCalledAsProcedure(ParserRuleContext context) { var callStmt = context.GetAncestor <VBAParser.CallStmtContext>(); if (callStmt == null) { return(false); } //If we are in an argument list, the value is used somewhere in defining the argument. var argumentListParent = context.GetAncestor <VBAParser.ArgumentListContext>(); if (argumentListParent != null) { return(false); } //Member accesses are parsed right-to-left, e.g. 'foo.Bar' is the parent of 'foo'. //Thus, having a member access parent means that the return value is used somehow. var ownFunctionCallExpression = context.Parent is VBAParser.MemberAccessExprContext methodCall ? methodCall : context; var memberAccessParent = ownFunctionCallExpression.GetAncestor <VBAParser.MemberAccessExprContext>(); if (memberAccessParent != null) { return(false); } //If we are in an output list, the value is used somewhere in defining the argument. var outputListParent = context.GetAncestor <VBAParser.OutputListContext>(); return(outputListParent == null); }
private static VBAParser.EndOfLineContext PreviousEndOfLine(ParserRuleContext context) { var moduleContext = context.GetAncestor <VBAParser.ModuleContext>(); var endOfLineListener = new EndOfLineListener(); ParseTreeWalker.Default.Walk(endOfLineListener, moduleContext); var previousEol = endOfLineListener.Contexts .OrderBy(eol => eol.Start.TokenIndex) .LastOrDefault(eol => eol.stop.TokenIndex < context.start.TokenIndex); return(previousEol); }
private static void RemoveEntireLine(IModuleRewriter rewriter, ParserRuleContext contextInCommentOrAnnotation) { var previousEndOfLineContext = PreviousEndOfLine(contextInCommentOrAnnotation); var containingCommentOrAnnotationContext = contextInCommentOrAnnotation.GetAncestor <VBAParser.CommentOrAnnotationContext>(); if (previousEndOfLineContext == null) { //We are on the first logical line. rewriter.RemoveRange(0, containingCommentOrAnnotationContext.stop.TokenIndex); } else if (containingCommentOrAnnotationContext.Eof() != null) { //We are on the last logical line. So swallow the NEWLINE from the previous end of line. rewriter.RemoveRange(previousEndOfLineContext.stop.TokenIndex, containingCommentOrAnnotationContext.stop.TokenIndex); } else { rewriter.RemoveRange(previousEndOfLineContext.stop.TokenIndex + 1, containingCommentOrAnnotationContext.stop.TokenIndex); } }
private static string GetType(ParserRuleContext context) { var declarationContext = context.GetAncestor <CLanguageParser.DeclarationContext>(); if (declarationContext == null) { throw new InvalidOperationException($"Could not get the type for context \"{context.GetName()}\""); } string text = declarationContext.GetContextText(); if (text.ContainsInvariant(SEMICOLUMN_TOKEN)) { text = text.TrimEnd(SEMICOLUMN_TOKEN); } if (text.ContainsInvariant(EQUALS_TOKEN)) { text = text.Substring(0, text.InvariantLastIndexOf(EQUALS_TOKEN)); } text = text.TrimEnd(SEPARATOR_TOKEN); string result; if (text.Contains(POINTER_TOKEN)) { result = string.Format("{0} {1}", text.Substring(0, text.InvariantIndexOf(POINTER_TOKEN)).TrimEnd(SEPARATOR_TOKEN), text.Substring(text.InvariantIndexOf(POINTER_TOKEN), text.InvariantLastIndexOf(POINTER_TOKEN) - text.InvariantIndexOf(POINTER_TOKEN) + 1)); } else { result = text.Substring(0, text.InvariantLastIndexOf(SEPARATOR_TOKEN)); } result = result.RemoveDuplicateSpaces(); return(result); }