Beispiel #1
0
        private static bool ProcessFile(string script)
        {
            if (!File.Exists(script))
            {
                Console.Error.WriteLine("Failed to read file {0}", script);
                return(false);
            }

            timer.Restart();

            FileInfo fileInfo = new FileInfo(script);

            if (fileInfo.Length == 0)
            {
                Console.WriteLine("Source file empty, skipping.");
                return(false);
            }

            string[] scriptLines = File.ReadAllLines(script);
            string   scriptData  = scriptLines.Aggregate((a, b) => a + "\n" + b);

            Lexer.Lexer lexer = new Lexer.Lexer(scriptData);

            Console.WriteLine("Running lexical analysis. [+{0}ms]", timer.ElapsedMilliseconds);

            int error = lexer.Analyse();

            if (error != 0)
            {
                Console.Error.WriteLine("Failed due to error {0}", error);
                return(false);
            }

#if DEBUG
            {
                int preprocessors = lexer.Tokens.Count(token => token.GetType() == typeof(PreprocessorToken));
                int comments      = lexer.Tokens.Count(token => token.GetType() == typeof(CommentToken));
                int separators    = lexer.Tokens.Count(token => token.GetType() == typeof(SeparatorToken));
                int operators     = lexer.Tokens.Count(token => token.GetType() == typeof(OperatorToken));
                int literals      = lexer.Tokens.Count(token => token.GetType() == typeof(LiteralToken));
                int keywords      = lexer.Tokens.Count(token => token.GetType() == typeof(KeywordToken));
                int identifiers   = lexer.Tokens.Count(token => token.GetType() == typeof(IdentifierToken));

                Console.WriteLine("DEBUG: Preprocessor: {0} Comments: {1} Separators: {2} " +
                                  "Operators: {3} Literals: {4} Keywords: {5} Identifiers: {6}",
                                  preprocessors, comments, separators, operators, literals, keywords, identifiers);
            }

            {
                Console.WriteLine("DEBUG: Converting tokens back to source and comparing.");
                Output_Nss debugOutput = new Output_Nss();

                error = debugOutput.GetFromTokens(lexer.Tokens, out string data);
                if (error != 0)
                {
                    Console.Error.WriteLine("DEBUG: Failed due to error {0}", error);
                    return(false);
                }

                string[] reformattedData = data.Split('\n');

                int sourceLines = scriptLines.Count();
                int dataLines   = reformattedData.Count();

                if (sourceLines != dataLines)
                {
                    Console.Error.WriteLine("DEBUG: Failed due to mismatch in line count. " +
                                            "Source: {0}, Data: {1}", sourceLines, dataLines);

                    return(false);
                }

                for (int i = 0; i < scriptLines.Length; ++i)
                {
                    string sourceLine = scriptLines[i];
                    string dataLine   = reformattedData[i];

                    if (sourceLine != dataLine)
                    {
                        Console.Error.WriteLine("DEBUG: Failed due to mismatch in line contents. " +
                                                "Line {0}.\n" +
                                                "Source line len: {1}\nData line len:   {2}\n" +
                                                "Source line: {3}\nData line:   {4}",
                                                i, sourceLine.Length, dataLine.Length, sourceLine, dataLine);

                        break;
                    }
                }
            }
#endif

            Console.WriteLine("Running parser. [+{0}ms]", timer.ElapsedMilliseconds);
            Parser.Parser parser = new Parser.Parser();
            error = parser.Parse(Path.GetFileName(script), scriptLines, lexer.Tokens);
            if (error != 0)
            {
                Console.Error.WriteLine("Failed due to error {0}", error);
                foreach (string errStr in parser.Errors)
                {
                    Console.Error.WriteLine("  {0}", errStr);
                }

                return(false);
            }

            Console.WriteLine("Running output. [+{0}ms]", timer.ElapsedMilliseconds);
            Output_CSharp output = new Output_CSharp();
            error = output.GetFromCU(parser.CompilationUnit, out string outputStr, out string className);
            if (error != 0)
            {
                Console.Error.WriteLine("Failed due to error {0}", error);
                return(false);
            }

            string outputPath = Path.Combine(destDir, Path.ChangeExtension(className, ".cs"));

            File.WriteAllText(outputPath, outputStr);
            return(true);
        }
Beispiel #2
0
        static void Main(string[] scriptsRawArr)
        {
            List <string> scripts = scriptsRawArr.ToList();

            // Expand wildcards
            for (int i = scripts.Count - 1; i >= 0; --i)
            {
                string script    = scripts[i];
                string directory = Path.GetDirectoryName(script);
                string fileName  = Path.GetFileName(script);

                if (fileName.Contains("*"))
                {
                    scripts.RemoveAt(i);
                    foreach (string expanded in Directory.GetFiles(directory, fileName))
                    {
                        scripts.Add(expanded);
                    }
                }
            }

            Stopwatch timer = new Stopwatch();

            // Process each file
            foreach (string script in scripts)
            {
                if (File.Exists(script))
                {
                    timer.Restart();

                    do
                    {
                        Console.WriteLine("Loading {0}", script);
                        string[] sourceFile = File.ReadAllLines(script);

                        if (sourceFile.Length == 0)
                        {
                            Console.WriteLine("Source file empty, skipping.");
                            break;
                        }

                        Console.WriteLine("Running lexical analysis. [+{0}ms]", timer.ElapsedMilliseconds);
                        Lexer_Nss analysis = new Lexer_Nss();
                        int       err      = analysis.Analyse(sourceFile.Aggregate((a, b) => a + "\n" + b));
                        if (err != 0)
                        {
                            Console.Error.WriteLine("Failed due to error {0}", err);
                            break;
                        }

#if DEBUG
                        {
                            int preprocessors = analysis.Tokens.Count(token => token.GetType() == typeof(NssPreprocessor));
                            int comments      = analysis.Tokens.Count(token => token.GetType() == typeof(NssComment));
                            int separators    = analysis.Tokens.Count(token => token.GetType() == typeof(NssSeparator));
                            int operators     = analysis.Tokens.Count(token => token.GetType() == typeof(NssOperator));
                            int literals      = analysis.Tokens.Count(token => token.GetType() == typeof(NssLiteral));
                            int keywords      = analysis.Tokens.Count(token => token.GetType() == typeof(NssKeyword));
                            int identifiers   = analysis.Tokens.Count(token => token.GetType() == typeof(NssIdentifier));

                            Console.WriteLine("DEBUG: Preprocessor: {0} Comments: {1} Separators: {2} " +
                                              "Operators: {3} Literals: {4} Keywords: {5} Identifiers: {6}",
                                              preprocessors, comments, separators, operators, literals, keywords, identifiers);
                        }

                        {
                            Console.WriteLine("DEBUG: Converting tokens back to source and comparing.");
                            Output_Nss debugOutput = new Output_Nss();

                            err = debugOutput.GetFromTokens(analysis.Tokens, out string data);
                            if (err != 0)
                            {
                                Console.Error.WriteLine("DEBUG: Failed due to error {0}", err);
                                break;
                            }

                            string[] reformattedData = data.Split('\n');

                            int sourceLines = sourceFile.Count();
                            int dataLines   = reformattedData.Count();

                            if (sourceLines != dataLines)
                            {
                                Console.Error.WriteLine("DEBUG: Failed due to mismatch in line count. " +
                                                        "Source: {0}, Data: {1}", sourceLines, dataLines);

                                break;
                            }

                            for (int i = 0; i < sourceFile.Length; ++i)
                            {
                                string sourceLine = sourceFile[i];
                                string dataLine   = reformattedData[i];

                                if (sourceLine != dataLine)
                                {
                                    Console.Error.WriteLine("DEBUG: Failed due to mismatch in line contents. " +
                                                            "Line {0}.\n" +
                                                            "Source line len: {1}\nData line len:   {2}\n" +
                                                            "Source line: {3}\nData line:   {4}",
                                                            i, sourceLine.Length, dataLine.Length, sourceLine, dataLine);

                                    break;
                                }
                            }
                        }
#endif

                        Console.WriteLine("Running parser. [+{0}ms]", timer.ElapsedMilliseconds);
                        Parser_Nss parser = new Parser_Nss();
                        err = parser.Parse(Path.GetFileName(script), sourceFile, analysis.Tokens);
                        if (err != 0)
                        {
                            Console.Error.WriteLine("Failed due to error {0}", err);
                            foreach (string errStr in parser.Errors)
                            {
                                Console.Error.WriteLine("  {0}", errStr);
                            }

                            break;
                        }

                        Console.WriteLine("Running output. [+{0}ms]", timer.ElapsedMilliseconds);
                        Output_CSharp output = new Output_CSharp();
                        err = output.GetFromCU(parser.CompilationUnit, out string outputStr);
                        if (err != 0)
                        {
                            Console.Error.WriteLine("Failed due to error {0}", err);
                            break;
                        }

                        File.WriteAllText(Path.ChangeExtension(script, ".cs"), outputStr);
                    }while (false);

                    Console.WriteLine("Processed in {0}ms\n", timer.ElapsedMilliseconds);
                }
                else
                {
                    Console.Error.WriteLine("Failed to read file {0}", script);
                }
            }
        }