For a project refactoring, I need to execute 4 (regex) search and replace for, like, 80+ classes. Since the regex are very long, I'm currently copying and pasteing them from a txt note file... Ditto (a clipboard manager) helps me to speed up a little, but a better automation would be appreciated..! I tried with a couple of macro plug-ins but they don't work very well or are too much complicated (using envDTE). Anyone of you have ever needed to accomplish a similar task and found a solution to suggest?
Thanks for your help!
You can try my Visual Commander extension to automate this task. For example, to execute a search and replace with regex use the following code:
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
int options = (int)(EnvDTE.vsFindOptions.vsFindOptionsRegularExpression |
EnvDTE.vsFindOptions.vsFindOptionsMatchCase |
EnvDTE.vsFindOptions.vsFindOptionsMatchInHiddenText |
EnvDTE.vsFindOptions.vsFindOptionsSearchSubfolders |
EnvDTE.vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen);
DTE.Find.FindReplace(EnvDTE.vsFindAction.vsFindActionReplaceAll,
#"(\.Register\w*)\(""([^""]+)""",
options,
#"$1(nameof($2)",
EnvDTE.vsFindTarget.vsFindTargetCurrentDocument);
}
See DTE.Find.FindReplace documentation for more details.
Related
My question is fairly simple:
Visual studio offers settings for formatting the code. However I was not able to fix this:
I want this
ReturnType FunctionName(){ .. }
instead of
ReturnType FunctionName() { .. }
So I want to get rid of the space between () and {}. Any suggestions will be greatly appreciated!
Thanks!
Tools -> Options -> Text Editor -> C#
Inside there you can find lots of ways to format your text. It is hard to say what you want from your post but anything formatting is a good start to look there.
I have a folder, full of 38,000+ .pdf files. I was not the genius to put them all into one folder, but I now have the task of separating them. The files that are of value to us, all have the same basic naming convention, for example:
123456_20130604_NEST_IV
456789_20120209_VERT_IT
What I'm trying to do, if possible, is search the folder for only those files with that particular naming convention. As in, search only for files that have 6 digits, an underscore, and then 8 digits followed by another underscore. Kind of like *****_********. I've searched online but I haven't had much luck. Any help would be great!
var regex = new Regex(#"^\d{6}_\d{8}_", RegexOptions.Compiled);
string[] files = Directory.GetFiles(folderPath)
.Where(path => regex.Match(Path.GetFileName(path)).Success)
.ToArray();
files would contain paths to a files, that match criteria.
For my example C:\Temp\123456_20130604_NEST_IV 456789_20120209_VERT_IT.pdf, which I've added beforehand.
As a bonus, here is PowerShell script to do this (assuming you are in the correct folder, otherwise use gc "C:\temp" instead of dir):
dir | Where-Object {$_ -match "^\d{6}_\d{8}_"}
? - single character
* - multiple characters
So, I would say use ?????? _ ???????? _ ???? _ ??.* to get all your files
You can use move or copy command from a command prompt to do that.
If you want to do advanced searches such as pattern matching, use windows grep: http://www.wingrep.com/
Are you familiar with regular expressions? If not, they are a generalized way to search for strings of a special format. I see you tagged your question with C# so assuming you are writing a C# script you might try the .NET regular expression module.
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx
If you are a beginner, you may want to start here.
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial
There are numerous ways to handle this. What I like to do is to divide work into different steps with clear output/data in each step. Hence I would tackle this in the following way (since this seems easier for me instead of writing a master program in c# that does everything):
Open windows command prompt (start/run/cmd), navigate to correct
folder and then "dir *.pdf > pdf_files.txt". This would give you a
file containing all pdf-files inside the specific folder.
open up the txt-file (pdf_files.txt) in Notepad++ and then press "ctrl + f
(find)" activate radio button "regular expressions"
type: [0-9]{6}_[0-9]{8}_.*\.pdf and press "Find all in current document"
Copy results and save to new .txt-file
Now you have a text file containing all pdf-files that you can do what you want with (create a c# program that parses the files and move them depending on their name or whatever)
Does anyone know of a way in the Microsoft .NET framework to check the syntax, and only the syntax, of a given C# file?
For a little background, what I am interested in doing is setting up syntastic to check the syntax of .cs files. Out of the box, syntastic uses the Mono C# compiler with its --parse flag to perform this operation but I can find no equivalent in the Microsoft .NET framework.
My first attempt to get this to work was to use csc /target:library /nologo in place of mcs --parse, but the problem is that this is called on a per-file basis. As a result, it reports missing namespaces (which exist in the full project build) instead of only syntactic errors.
You can do this via the Roslyn CTP. It allows you to parse the .cs file entirely, and walk the full tree, looking for errors.
For details, I recommend downloading the Walkthrough: Getting Started with Syntax Analysis for C#, as it shows you the basic approach to looking at syntax trees in a C# file.
I've used NRefactory before from the icsharpcode IDE. It's quick and easy for basic stuff.
see this article:
Using NRefactory for analyzing C# code
I use it for creating VB.NET examples from C# examples. The method that does this is really straight-forward and can easily be adapted to your needs:
private static void ConvertLanguage(TextReader input, TextWriter output, SupportedLanguage language, Action<string> onError)
{
using (IParser parser = ParserFactory.CreateParser(language, input))
{
parser.Parse();
var specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
var result = parser.CompilationUnit;
//if (parser.Errors.Count > 0)
// MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");
IOutputAstVisitor outputVisitor;
if (language == SupportedLanguage.CSharp)
outputVisitor = new VBNetOutputVisitor();
else
outputVisitor = new CSharpOutputVisitor();
outputVisitor.Options.IndentationChar = ' ';
outputVisitor.Options.IndentSize = 4;
outputVisitor.Options.TabSize = 4;
using (SpecialNodesInserter.Install(specials, outputVisitor))
result.AcceptVisitor(outputVisitor, null);
if (outputVisitor.Errors.Count > 0 && onError != null)
onError(outputVisitor.Errors.ErrorOutput);
output.Write(outputVisitor.Text);
}
}
Note: The preceding code is from an older version and may not compile against the latest version of the NRefactory library.
I think I may have a solution to your question. If you are trying to check the syntax of you code without being in the debugger you can use an online compiler suck as compilr.
If you wish to output the resuts then you can use this amazing api called Html Agility to grab the results off of the online compiler with ease. Hope this helped!
After looking through posts for good C# parser generators, I stumbled across GPLEX and GPPG. I'd like to use GPLEX to generate tokens for GPPG to parse and create a tree (similar to the lex/yacc relationship). However, I can't seem to find an example on how these two interact together. With lex/yacc, lex returns tokens that are defined by yacc, and can store values in yylval. How is this done in GPLEX/GPPG (it is missing from their documentation)?
Attached is the lex code I would like to convert over to GPLEX:
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[Oo][Rr] return OR;
[Aa][Nn][Dd] return AND;
[Nn][Oo][Tt] return NOT;
[A-Za-z][A-Za-z0-9_]* yylval=yytext; return ID;
%%
Thanks!
Andrew
First: include the reference "QUT.ShiftReduceParser.dll" in your Project. It is provided in the download-package from GPLEX.
Sample-Code for Main-Program:
using System;
using ....;
using QUT.Gppg;
using Scanner;
using Parser;
namespace NCParser
{
class Program
{
static void Main(string[] args)
{
string pathTXT = #"C:\temp\testFile.txt";
FileStream file = new FileStream(pathTXT, FileMode.Open);
Scanner scanner = new Scanner();
scanner.SetSource(file, 0);
Parser parser = new Parser(scanner);
}
}
}
Sample-Code for GPLEX:
%using Parser; //include the namespace of the generated Parser-class
%Namespace Scanner //names the Namespace of the generated Scanner-class
%visibility public //visibility of the types "Tokens","ScanBase","Scanner"
%scannertype Scanner //names the Scannerclass to "Scanner"
%scanbasetype ScanBase //names the Scanbaseclass to "ScanBase"
%tokentype Tokens //names the Tokenenumeration to "Tokens"
%option codePage:65001 out:Scanner.cs /*see the documentation of GPLEX for further Options you can use */
%{ //user-specified code will be copied in the Output-file
%}
OR [Oo][Rr]
AND [Aa][Nn][Dd]
Identifier [A-Za-z][A-Za-z0-9_]*
%% //Rules Section
%{ //user-code that will be executed before getting the next token
%}
{OR} {return (int)Tokens.kwAND;}
{AND} {return (int)Tokens.kwAND;}
{Identifier} {yylval = yytext; return (int)Tokens.ID;}
%% //User-code Section
Sample-Code for GPPG-input-file:
%using Scanner //include the Namespace of the scanner-class
%output=Parser.cs //names the output-file
%namespace Parser //names the namespace of the Parser-class
%parsertype Parser //names the Parserclass to "Parser"
%scanbasetype ScanBase //names the ScanBaseclass to "ScanBase"
%tokentype Tokens //names the Tokensenumeration to "Tokens"
%token kwAND "AND", kwOR "OR" //the received Tokens from GPLEX
%token ID
%% //Grammar Rules Section
program : /* nothing */
| Statements
;
Statements : EXPR "AND" EXPR
| EXPR "OR" EXPR
;
EXPR : ID
;
%% User-code Section
// Don't forget to declare the Parser-Constructor
public Parser(Scanner scnr) : base(scnr) { }
c#parsegppggplex
I had a similar issue - not knowing how to use my output from GPLEX with GPPG due to an apparent lack of documentation. I think the problem stems from the fact that the GPLEX distribution includes gppg.exe along with gplex.exe, but only documentation for GPLEX.
If you go the GPPG homepage and download that distribution, you'll get the documentation for GPPG, which describes the requirements for the input file, how to construct your grammar, etc. Oh, and you'll also get both binaries again - gppg.exe and gplex.exe.
It almost seems like it would be simpler to just include everything in one package. It could definitely clear up some confusion, especially for those who may be new to lexical analysis (tokenization) and parsing (and may not be 100% familiar yet with the differences between the two).
So anyways, for those who may doing this for the first time:
GPLEX http://gplex.codeplex.com - used for tokenization/scanning/lexical analysis (same thing)
GPPG http://gppg.codeplex.com/ - takes output from a tokenizer as input to parse. For example, parsers use grammars and can do things a simple tokenizer cannot, like detect whether sets of parentheses match up.
Some time ago I have had the same need of using both GPLEX and GPPG together and for making the job much more easier I have created a nuget package for using GPPG and GPLEX together in Visual Studio.
This package can be installed in C# projects based on .Net Framework and adds some command-lets to the Package Manager Console in Visual Studio. This command-lets help you in configuring the C# project for integrating GPPG and GPLEX in the build process. Essentially in your project you will edit YACC and LEX files as source code and during the build of the project, the parser and the scanner will be generated. In addition the command-lets add to the projects the files needed for customizing the parser and the scanner.
You can find it here:
https://www.nuget.org/packages/YaccLexTools/
And here is a link to the blog post that explains how to use it:
http://ecianciotta-en.abriom.com/2013/08/yacclex-tools-v02.html
Have you considered using Roslyn? (This isn't a proper answer but I don't have enough reputation to post this as a comment)
Irony, because when I jumped into parsers in C# I started exactly from those 2 tools (about a year ago). Then lexer has tiny bug (easy to fix):
http://gplex.codeplex.com/workitem/11308
but parser had more severe:
http://gppg.codeplex.com/workitem/11344
Lexer should be fixed (release date is June 2013), but parser probably still has this bug (May 2012).
So I wrote my own suite :-) https://sourceforge.net/projects/naivelangtools/ and use and develop it since then.
Your example translates (in NLT) to:
/[Oo][Rr]/ -> OR;
/[Aa][Nn][Dd]/ -> AND;
/[Nn][Oo][Tt]/ -> NOT;
// by default text is returned as value
/[A-Za-z][A-Za-z0-9_]*/ -> ID;
Entire suite is similar to lex/yacc, when possible it does not rely on side effects (so you return appropriate value).
In an Antlr 3 grammar, is it possible to print out the full text matching a rule in a grammar targeting c#? Something like below:
rule : FIRST SECOND
{ Console.WriteLine($rule.text); };//does not work.
FIRST: 'first';
SECOND: 'second';
If $rule.text doesn't work (as #dana suggested), you might try $rule.Text or even $rule.GetText().
If all fails, please tell us which version of the C# port you're using (and where it can be downloaded), then I (or someone else) can perhaps give it a try.