Exemple #1
0
        private void btnTranslate_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                btnTranslate.Enabled = false;
                var  translator = new AbnfToAntlrTranslator();
                bool performDirectTranslation = false;

                if (chkPerformDirectTranslation.Checked == true)
                {
                    performDirectTranslation = true;
                }

                var input = txtInput.Text;
                if (input.EndsWith("\r\n"))
                {
                    // do nothing
                }
                else
                {
                    input = input + "\r\n";
                }

                this.txtOutput.Text      = translator.Translate(input, performDirectTranslation);
                this.txtOutput.ForeColor = this.ForeColor;
                this.txtOutput.BackColor = this.BackColor; // readonly textbox forecolor only changes when backcolor is set
            }
            catch (TranslationException ex)
            {
                this.txtOutput.Text      = AntlrHelper.GetErrorMessages(ex.ParserRecognitionExceptions) + AntlrHelper.GetErrorMessages(ex.LexerRecognitionExceptions);
                this.txtOutput.ForeColor = Color.DarkRed;
                this.txtOutput.BackColor = this.BackColor; // readonly textbox forecolor only changes when backcolor is set
            }
            catch (RecognitionException ex)
            {
                this.txtOutput.Text      = AntlrHelper.GetErrorMessage(ex);
                this.txtOutput.ForeColor = Color.DarkRed;
                this.txtOutput.BackColor = this.BackColor; // readonly textbox forecolor only changes when backcolor is set
            }
            catch (Exception ex)
            {
                this.txtOutput.Text      = ex.Message;
                this.txtOutput.ForeColor = Color.DarkRed;
                this.txtOutput.BackColor = this.BackColor; // readonly textbox forecolor only changes when backcolor is set
            }
            finally
            {
                this.Cursor          = Cursors.Default;
                btnTranslate.Enabled = true;
                lblOutput.Visible    = true;
                txtOutput.Visible    = true;
            }
        }
Exemple #2
0
        private void PerformTranslationTest(string testName, string folderName, TranslationEnum translationTypeEnum, AbnfToAntlrTranslator translator)
        {
            var pathPrefix = Path.Combine(@"..\..\FileDrivenTests", folderName);

            var inputFileName  = testName + ".input.txt";
            var outputFileName = testName + ".output." + translationTypeEnum.ToString().ToLowerInvariant() + ".txt";

            var inputPath          = Path.Combine(pathPrefix, inputFileName);
            var expectedOutputPath = Path.Combine(pathPrefix, outputFileName);

            string inputText = null;

            if (File.Exists(inputPath))
            {
                // do nothing
            }
            else
            {
                File.WriteAllText(inputPath, "");
            }

            inputText = File.ReadAllText(inputPath);

            if (string.IsNullOrWhiteSpace(inputText))
            {
                TestContext.WriteLine(inputPath);
                throw new InvalidOperationException("File cannot be empty (\"" + inputPath + "\").");
            }

            var performDirectTranslation = (translationTypeEnum == TranslationEnum.Direct);

            var actualOutput = translator.Translate(inputText, performDirectTranslation);

            if (File.Exists(expectedOutputPath))
            {
                // do nothing
            }
            else
            {
                File.WriteAllText(expectedOutputPath, actualOutput);
            }

            var expectedOutput = File.ReadAllText(expectedOutputPath);

            if (actualOutput.Equals(expectedOutput, StringComparison.Ordinal))
            {
                // do nothing
            }
            else
            {
                TestContext.WriteLine(outputFileName);
                Assert.AreEqual(expectedOutput, actualOutput);
            }
        }
Exemple #3
0
        private void btnTranslate_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                btnTranslate.Enabled = false;
                var  translator = new AbnfToAntlrTranslator();
                bool performDirectTranslation = false;

                if (chkPerformDirectTranslation.Checked == true)
                {
                    performDirectTranslation = true;
                }

                var input = txtInput.Text;
                if (input.EndsWith("\r\n"))
                {
                    // do nothing
                }
                else
                {
                    input = input + "\r\n";
                }

                this.txtOutput.Text      = translator.Translate(input, performDirectTranslation);
                this.txtOutput.ForeColor = Color.Black;
            }
            catch (Exception ex)
            {
                this.txtOutput.Text      = ex.Message;
                this.txtOutput.ForeColor = Color.Red;
            }
            finally
            {
                this.Cursor          = Cursors.Default;
                btnTranslate.Enabled = true;
                lblOutput.Visible    = true;
                txtOutput.Visible    = true;
            }
        }
        protected void butTranslate_Click(object sender, EventArgs e)
        {
            try
            {
                var  translator = new AbnfToAntlrTranslator();
                bool performDirectTranslation = false;

                if (chkPerformDirectTranslation.Checked == true)
                {
                    performDirectTranslation = true;
                }

                var input = txtInput.Text;
                if (input.EndsWith("\r\n"))
                {
                    // do nothing
                }
                else
                {
                    input = input + "\r\n";
                }

                this.txtOutput.Text = translator.Translate(input, performDirectTranslation);

                this.lblOutput.Visible = true;
                this.txtOutput.Visible = true;
                this.txtError.Visible  = false;
            }
            catch (Exception ex)
            {
                this.txtError.Text    = ex.Message;
                this.txtError.Visible = true;

                this.lblOutput.Visible = false;
                this.txtOutput.Visible = false;
            }
        }
        static int ConsoleMain(string[] args)
        {
            bool shouldShowSyntax = false;
            bool shouldPerformDirectTranslation = false;
            int  fileArgIndex = 0;

            if (args.Length == 0)
            {
                shouldShowSyntax = true;
            }
            else
            {
                switch (args[0])
                {
                case "-h":
                case "/h":
                case "-?":
                case "/?":
                case "-help":
                case "/help":
                case "--help":
                    shouldShowSyntax = true;
                    break;

                case "--direct":
                    shouldPerformDirectTranslation = true;
                    fileArgIndex = 1;
                    break;
                }
            }

            if (shouldShowSyntax || fileArgIndex >= args.Length || args.Length > fileArgIndex + 1)
            {
                ShowSyntax();
                return(1);
            }

            string path = null;

            System.IO.TextReader reader;
            string input;
            string output;

            try
            {
                // open input stream
                if (args[fileArgIndex] == "--stdin")
                {
                    path   = "stdin";
                    reader = System.Console.In;
                }
                else
                {
                    path   = args[0];
                    reader = new System.IO.StreamReader(path);
                }

                input = reader.ReadToEnd();
                if (input.EndsWith("\r\n"))
                {
                    // do nothing
                }
                else
                {
                    input = input + "\r\n";
                }

                var translator = new AbnfToAntlrTranslator();

                output = translator.Translate(input, shouldPerformDirectTranslation);

                System.Console.Write(output);

#if DEBUG
                // when debugging, output the resulting string builder to a file
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.IO.File.WriteAllText(@"..\..\_AbnfToAntlr_Debug_Output.txt", output);
                }
#endif
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(string.Format("An error occurred while processing '{0}':", path));
                Console.Error.WriteLine(ex.Message);
                return(2);
            }

            return(0);
        }
Exemple #6
0
        public int ConsoleMain(string[] args, TextReader stdin, TextWriter stdout, TextWriter stderr)
        {
            bool shouldShowSyntax = false;
            bool shouldPerformDirectTranslation = false;
            int  fileArgIndex = 0;

            if (args.Length == 0)
            {
                shouldShowSyntax = true;
            }
            else
            {
                switch (args[0].ToLowerInvariant())
                {
                case "-h":
                case "/h":
                case "--h":

                case "-?":
                case "/?":
                case "--?":

                case "-help":
                case "/help":
                case "--help":
                    shouldShowSyntax = true;
                    break;

                case "--direct":
                    shouldPerformDirectTranslation = true;
                    fileArgIndex = 1;
                    break;
                }
            }

            if (shouldShowSyntax || fileArgIndex >= args.Length || args.Length > fileArgIndex + 1)
            {
                ShowSyntax(stderr);
                return(1);
            }

            string path = null;

            System.IO.TextReader reader;
            string input;
            string output;

            try
            {
                // open input stream
                if (args[fileArgIndex] == "--stdin")
                {
                    path   = "stdin";
                    reader = stdin;
                }
                else
                {
                    path   = args[fileArgIndex];
                    reader = new System.IO.StreamReader(path);
                }

                input = reader.ReadToEnd();
                if (input.EndsWith("\r\n"))
                {
                    // do nothing
                }
                else
                {
                    input = input + "\r\n";
                }

                var translator = new AbnfToAntlrTranslator();

                output = translator.Translate(input, shouldPerformDirectTranslation);

                stdout.Write(output);
            }
            catch (Exception ex)
            {
                stderr.WriteLine(string.Format("An error occurred while processing '{0}':", path));
                stderr.WriteLine();
                stderr.WriteLine(ex.Message);
                return(2);
            }

            return(0);
        }