[ Newer Entries ] [ Older Entries ]

.NET Regular Expression Evaluator

26-10-2011 16:38:56
Programming


I have previously created a plugin for Visual Studio for testing .NET regular expressions. To play around with JQuery and Ajax I have ported that plugin to the web at http://dotnetregexevaluator.andreasandersen.dk.

The regular expression editor supports bracket matching which is achieved by using CodeMirror - "in-browser code editing made bearable". It's a cool tool with a nice and simple interface.

The Visual Studio version of the tool I have made into an extension which is now available for download on the Visual Studio Gallery.



Comments (0)

Reverse Polish Notation

15-10-2011 23:32:25
Programming

RPNEvaluator.zip


I couldn't find an online RPN evaluator when I needed it, so I have created one myself. It converts expressions both ways between RPN and infix notation and of course evaluates the result. For creating a parser for infix expressions I used the GOLD Parsing System which works quite nicely. The ASP.NET code for the evaluator can be downloaded above, and the evaluator itself can be found at http://rpnevaluator.andreasandersen.dk

Comments (0)

Visual Studio Regular Expression Plugin

01-11-2010 21:48:18
Programming

Regular Expression Tester Code


Whenever I program using regular expressions I find myself spending a lot of time debugging them, tweaking them to do exactly what I want. Visual Studio is not very helpful in that regard so I decided to try to implement my own regular expression tester add-in for Visual Studio. It turned out to be quite easy as there is a project type for it - Other Projects -> Extensibility -> Visual Studio Add-in. Creating a project gives you a Connect class with some methods to implement. Below are the important ones.
/* 
 * Run when Visual Studio starts. This is where the plugin
 * is registered and attached to the appropriate menus.
 */
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    if (connectMode == ext_ConnectMode.ext_cm_UISetup)
    {
        object[] contextGUIDS = new object[] { };
        Commands2 commands = (Commands2)_applicationObject.Commands;
        // There are many different command bars in Visual Studio.
        // Find the appropriate one by looking at what values are
        // available in the map. In this case I want the context menu
        // in the code editor.
        Microsoft.VisualStudio.CommandBars.CommandBar editContextCommandBar =
                ((Microsoft.VisualStudio.CommandBars.CommandBars)
                _applicationObject.CommandBars)["Editor Context Menus"];

        CommandBarPopup toolsPopup = (CommandBarPopup)editContextCommandBar.Controls["Code Window"];

        //Add a command to the Commands collection:
        Command command = commands.AddNamedCommand2(_addInInstance,
            "RegularExpressionEvaluator",
            "Test regular expression",
            "Test regular expression",
            true,
            59,
            ref contextGUIDS,
            (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
            (int)vsCommandStyle.vsCommandStyleText,
            vsCommandControlType.vsCommandControlTypeButton);
        // This binds the command to a shortcut.
        command.Bindings = "Text Editor::Ctrl+r, Ctrl+x";
        if (command != null && toolsPopup != null && toolsPopup.CommandBar != null)
        {
            command.AddControl(toolsPopup.CommandBar, 1);
        }
    }
}

/*
 * Determines when the button for the add-in should be available.
 */
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText,
                        ref vsCommandStatus status, ref object commandText)
{
    if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
    {
        if (commandName == "RegularExpressionEvaluator.Connect.RegularExpressionEvaluator")
        {
            status = vsCommandStatus.vsCommandStatusSupported;
            // If text is selected the context menu should be available.
            if (_applicationObject.ActiveDocument != null &&
               !string.IsNullOrEmpty((
                 _applicationObject.ActiveDocument.Object() as TextDocument).Selection.Text))
            {
                status |= vsCommandStatus.vsCommandStatusEnabled;
            }
        }
    }
}

/*
 * This is executed when the add-in is activated via the command button
 * added on startup. In this case I just open a form allowing the user
 * to edit the regular expression currently selected in the code editor.
 */
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn,
                 ref object varOut, ref bool handled)
{
    handled = false;
    if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if (commandName == "RegularExpressionEvaluator.Connect.RegularExpressionEvaluator")
        {
            using (RegexEvaluatorForm form = new RegexEvaluatorForm())
            {
                TextSelection selection = (_applicationObject.ActiveDocument.Object() as TextDocument).Selection;
                form.RegularExpression = selection.Text;
                if (form.ShowDialog() == DialogResult.OK)
                {
                    selection.Text = form.RegularExpression;
                }
            }
            handled = true;
            return;
        }
    }
}

And below is the GUI that is shown when highlighting some text in Visual Studio and pressing Ctrl R, Ctrl X. The plugin is available for download above.
GUI for regular expression evaluator plugin.
GUI for regular expression evaluator plugin.


Comments (0)