示例#1
0
        private static void ParseFile(string fileName, List <Parser.Error> errors)
        {
            //Console.WriteLine("Parsing " + fileName);
            FileStream      fs   = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            StreamReader    sr   = new StreamReader(fs, true);
            Lexer           l    = new Lexer(sr);
            TokenCollection toks = l.Lex();

            Parser p = null;
            CompilationUnitNode cu = null;

            p  = new Parser(fileName);
            cu = p.Parse(toks, l.StringLiterals);



            errors.AddRange(p.Errors);

            //Console.WriteLine(toks + "\n\n");
            //Console.WriteLine(p.CurrentState + "\n\n");

            //StringBuilder sb = new StringBuilder();
            //cu.ToSource(sb);
            //Console.WriteLine(sb.ToString());
        }
示例#2
0
        /// <summary>
        /// Invokes the parser. Signals the wait event supplied to the c'tor if parser has finished.
        /// </summary>
        /// <remarks>
        /// This method assumes that it will be executed in a separate thread that may be aborted
        /// by the calling thread. Takes care of the two different error conditions: Parser itself
        /// may throw an exception or thread is being aborted.
        /// </remarks>
        public void Parse()
        {
            try
            {
                try
                {
                    Lexer           l    = new Lexer(rd);
                    TokenCollection toks = l.Lex();
                    Parser          p    = new Parser(filename);

                    _result = p.Parse(toks, l.StringLiterals);
                    _errors = p.Errors;
                }
                catch (Exception exc)       // Exception thrown by parser -> save
                {                           // it (calling thread will rethrow this).
                    _exc = exc;
                }
                finally                     // Ensure signalling wait event, we are done (no
                {                           // matter whether errors/exceptions occured or not).
                    wait.Set();
                }
            }
            catch (ThreadAbortException)    // Thread that this method is running in was aborted
            {                               // by calling thread (because of a timeout) -> prevent
                Thread.ResetAbort();        // ThreadAbortException from being rethrown (see MSDN).
            }
        }
示例#3
0
 private void MergeNameTables(CompilationUnitNode source)
 {
     foreach (IdentifierName identifierName in source.NameTable)
     {
         // TODO: Check for duplicates.
         nameTable.AddIdentifier(identifierName);
     }
 }
 public string ParseContent(string content)
 {
   using (StringReader sr = new StringReader(content))
   {
     lexer = new Lexer(sr);
     TokenCollection tc = lexer.Lex();
     parser = new Parser();
     rootNode = parser.Parse(tc, lexer.StringLiterals);
     return rootNode.AcceptVisitor(visitor, null) as string;
   }
 }
示例#5
0
 public string ParseContent(string content)
 {
     using (StringReader sr = new StringReader(content))
     {
         lexer = new Lexer(sr);
         TokenCollection tc = lexer.Lex();
         parser   = new Parser();
         rootNode = parser.Parse(tc, lexer.StringLiterals);
         return(rootNode.AcceptVisitor(visitor, null) as string);
     }
 }
示例#6
0
        public virtual object VisitCompilationUnit(CompilationUnitNode compilationUnit, object data)
        {
            stackMap.Push(compilationUnit);
            compilationUnit.Attributes.AcceptVisitor(this, data);

            compilationUnit.DefaultNamespace.AcceptVisitor(this, data);


            compilationUnit.Namespaces.AcceptVisitor(this, data);

            stackMap.Pop();
            return(null);
        }
示例#7
0
        static void testCode(string code)
        {
            Lexer lex = new Lexer(new StreamReader(new MemoryStream(
                                                       Encoding.UTF8.GetBytes(code))));
            TokenCollection     tc = lex.Lex();
            Parser              p  = new Parser(string.Empty);
            CompilationUnitNode n  = p.Parse(tc, lex.StringLiterals);

            StringBuilder sb = new StringBuilder();

            n.ToSource(sb);
            Console.WriteLine(sb.ToString());
        }
示例#8
0
        private void BuilderNameSpace(NamespaceNode cn, CompilationUnitNode unit)
        {


            NamespaceNode nspace = new NamespaceNode(new Token(TokenID.Namespace));
            nspace.Name = cn.Name;
            mEntityUnit.Namespaces.Add(nspace);
            foreach (InterfaceNode item in cn.Interfaces)
            {
                BuilderType(item,nspace);
            }

            


        }
示例#9
0
        private static CompilationUnitNode ParseUnit(string fileName, List <Parser.Error> errors)
        {
            Console.Write("\nParsing " + fileName);
            FileStream      fs   = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            StreamReader    sr   = new StreamReader(fs, true);
            Lexer           l    = new Lexer(sr);
            TokenCollection toks = l.Lex();

            Parser p = null;
            CompilationUnitNode cu = null;

            p  = new Parser(fileName);
            cu = p.Parse(toks, l.StringLiterals);

            if (p.Errors.Count != 0)
            {
                Console.WriteLine();
                PrintErrors(p.Errors);
                errors.AddRange(p.Errors);
                return(null);
            }
            return(cu);
        }
示例#10
0
        private static void ParseFile(string fileName, List <Parser.Error> errors, WhatToDo whatToDo, bool useMono)
        {
            bool   createExe          = true;
            String compilerOptionsStr = "";
            String basePath           = Path.GetDirectoryName(fileName) + Path.DirectorySeparatorChar;

            if (basePath.Length == 1)
            {
                basePath = "";
            }
            String outAssembly = Path.GetFileNameWithoutExtension(fileName) + ".exe";

            List <String> files = new List <string>();

            files.Add(fileName);

            String[] compilerOptions, dependencies;
            if (!GetExtraOptions(fileName, out compilerOptions, out dependencies))
            {
                Console.WriteLine("GetExtraOptions failed!");
            }
            if (compilerOptions != null)
            {
                foreach (String compOpt in compilerOptions)
                {
                    if (compOpt.Length == 0)
                    {
                        continue;
                    }
                    if (compOpt[0] != '-')
                    {
                        files.Add(basePath + compOpt);
                    }
                    else if (compOpt == "-t:library")
                    {
                        createExe   = false;
                        outAssembly = Path.GetFileNameWithoutExtension(fileName) + ".dll";
                    }
                    else if (compOpt == "-t:module")
                    {
                        createExe           = false;
                        compilerOptionsStr += "/t:module ";
                        outAssembly         = Path.GetFileNameWithoutExtension(fileName) + ".netmodule";
                    }
                    else if (compOpt == "-unsafe")
                    {
                        compilerOptionsStr += "/unsafe ";
                    }
                    else if (compOpt.StartsWith("-r:"))
                    {
                        compilerOptionsStr += "/" + compOpt.Substring(1) + " ";
                    }
                    else
                    {
                        int colonPos = compOpt.IndexOf(':');
                        if (colonPos == -1)
                        {
                            continue;
                        }

                        String optionName = compOpt.Substring(1, colonPos - 1);
                        if (optionName != "res" && optionName != "linkresource" && optionName != "addmodule" &&
                            optionName != "keyfile")
                        {
                            continue;
                        }

                        String optFileName;
                        String rest     = compOpt.Substring(optionName.Length + 2);
                        int    commaPos = rest.IndexOf(',');
                        if (commaPos == -1)
                        {
                            optFileName = rest;
                            rest        = "";
                        }
                        else
                        {
                            optFileName = rest.Substring(0, commaPos);
                            rest        = rest.Substring(commaPos);
                        }
                        compilerOptionsStr += "/" + optionName + ":\"" + basePath + optFileName + "\"" + rest + " ";
                    }
                }
            }

            String[] unparsedFiles = new String[files.Count];
            for (int i = 0; i < files.Count; i++)
            {
                CompilationUnitNode cu = ParseUnit(files[i], errors);
                if (cu == null)
                {
                    return;
                }

                StringBuilder sb = new StringBuilder();
                cu.ToSource(sb);
                unparsedFiles[i] = sb.ToString();
            }

            //Console.WriteLine(toks + "\n\n");
            //Console.WriteLine(p.CurrentState + "\n\n");

            if (whatToDo == WhatToDo.PrintToScreen)
            {
                Console.WriteLine();
                foreach (String unparsedFile in unparsedFiles)
                {
                    Console.WriteLine(unparsedFile);
                }
            }
            else if (whatToDo == WhatToDo.Compile)
            {
                CSharpCodeProvider compiler   = new CSharpCodeProvider();
                CompilerParameters compParams = new CompilerParameters();
                compParams.ReferencedAssemblies.Add("System.dll");
                compParams.ReferencedAssemblies.Add("System.XML.dll");
                compParams.ReferencedAssemblies.Add("System.Data.dll");
                if (createExe)
                {
                    compParams.GenerateExecutable = true;
                }

                if (basePath.Length > 0)
                {
                    compilerOptionsStr += "/lib:\"" + basePath.Substring(0, basePath.Length - 1) + "\" ";
                }
                compParams.OutputAssembly  = basePath + outAssembly;
                compParams.CompilerOptions = compilerOptionsStr;
                CompilerResults res = null;
                try
                {
                    res = compiler.CompileAssemblyFromSource(compParams, unparsedFiles);
                }
                catch (BadImageFormatException ex)
                {
                    if (compilerOptionsStr.Contains("/t:module"))
                    {
                        AddError(errors, fileName, "\nMono tried to load a module as an assembly\n"
                                 + "(see https://bugzilla.novell.com/show_bug.cgi?id=353536)");
                    }
                    else
                    {
                        AddError(errors, fileName, ex.Message);
                    }
                    return;
                }
                catch (Exception ex)
                {
                    AddError(errors, fileName, ex.Message);
                    return;
                }
                if (res.Errors.HasErrors)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("\nIllegal C# source code generated: ");
                    sb.Append(res.Errors.Count.ToString());
                    sb.Append(" Errors:\n");
                    foreach (CompilerError error in res.Errors)
                    {
                        sb.Append("Line: ");
                        sb.Append(error.Line.ToString());
                        sb.Append(" - ");
                        sb.Append(error.ErrorText);
                        sb.Append('\n');
                    }
                    AddError(errors, fileName, sb.ToString());
                    return;
                }
                Console.Write(". Compiled.");

                if (compParams.GenerateExecutable)
                {
                    Console.Write(" Testing");
                    ProcessStartInfo psi;
                    if (useMono)
                    {
                        psi = new ProcessStartInfo("mono", outAssembly);
                    }
                    else
                    {
                        psi = new ProcessStartInfo(basePath + outAssembly);
                    }

                    psi.CreateNoWindow   = true;
                    psi.UseShellExecute  = false;
                    psi.WorkingDirectory = basePath;
                    using (Process proc = Process.Start(psi))
                    {
                        for (int i = 0; i < 5 && !proc.HasExited; i++)
                        {
                            Console.Write('.');
                            proc.WaitForExit(1000);
                        }
                        if (!proc.HasExited)
                        {
                            proc.Kill();
                            AddError(errors, fileName, "Test seems to hang!");
                        }
                        else if (proc.ExitCode != 0)
                        {
                            AddError(errors, fileName, "Test failed! Exit code = " + proc.ExitCode);
                        }
                        else
                        {
                            Console.WriteLine(" Succeeded!");
                        }
                    }
                }
            }
            else if (whatToDo == WhatToDo.WriteToFiles)
            {
                for (int i = 0; i < files.Count; i++)
                {
                    String path = files[i].Substring(9);
                    Console.WriteLine(". Writing " + path);
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                    using (StreamWriter writer = new StreamWriter(path))
                        writer.WriteLine(unparsedFiles[i]);
                }
            }

            /*            TestVisitorVisitor visitor = new TestVisitorVisitor();
             *
             *          // if you forget to override the method 'public override object AcceptVisitor(AbstractVisitor visitor, object data)'
             *          // it will throw an exception
             *          cu.AcceptVisitor(visitor, null);*/
        }
示例#11
0
 private void MergeNameTables(CompilationUnitNode source)
 {
     foreach (IdentifierName identifierName in source.NameTable)
     {
         // TODO: Check for duplicates.
         nameTable.AddIdentifier(identifierName);
     }
 }
示例#12
0
		public CompilationUnitNode Parse(TokenCollection tokens, List<string> strings)
		{			
			this.tokens = tokens;
			this.strings = strings;
			curmods = Modifier.Empty;
			curAttributes = new NodeCollection<AttributeNode>();
            curTypeParameters = new List<TypeParameterNode>();
            curTypeParameterConstraints = new Dictionary<string, Constraint>();

            blockStack = new Stack<BlockStatement>();

			curState = new ParseStateCollection();

			cu = new CompilationUnitNode();
			namespaceStack = new Stack<NamespaceNode>();
			namespaceStack.Push(cu.DefaultNamespace);
			typeStack = new Stack<ClassNode>();
            genericList = new List<IGeneric>();
            constructedTypes = new Dictionary<string, ConstructedTypeNode>();

            iteratorsClass = new StringCollection();

            iteratorsClass.Add( "IEnumerator" );
            iteratorsClass.Add( "IEnumerable" );
            iteratorsClass.Add("Collections.IEnumerator");
            iteratorsClass.Add( "Collections.IEnumerable" );
            iteratorsClass.Add("System.Collections.IEnumerator");
            iteratorsClass.Add("System.Collections.IEnumerable");

            iteratorsClass.Add( "IEnumerator<>" );
            iteratorsClass.Add( "IEnumerable<>" );
            iteratorsClass.Add("Generic.IEnumerator<>");
            iteratorsClass.Add( "Generic.IEnumerable<>" );
            iteratorsClass.Add("Collections.Generic.IEnumerator<>");
            iteratorsClass.Add( "Collections.Generic.IEnumerable<>" );
            iteratorsClass.Add("System.Collections.Generic.IEnumerator<>");
            iteratorsClass.Add( "System.Collections.Generic.IEnumerable<>" );

			exprStack = new Stack<ExpressionNode>();

			// begin parse
            curTokNode = tokens.First;
			Advance();
			ParseNamespaceOrTypes();

            this.cu.NameTable = this.nameTable;

			return cu;
		}
示例#13
0
        /// <summary>
        /// Invokes the parser. Signals the wait event supplied to the c'tor if parser has finished.
        /// </summary>
        /// <remarks>
        /// This method assumes that it will be executed in a separate thread that may be aborted
        /// by the calling thread. Takes care of the two different error conditions: Parser itself
        /// may throw an exception or thread is being aborted.
        /// </remarks>
        public void Parse()
        {
            try
            {
                try
                {
                    Lexer l = new Lexer(rd);
                    TokenCollection toks = l.Lex();
                    Parser p = new Parser(filename);

                    _result = p.Parse(toks, l.StringLiterals);
                    _errors = p.Errors;
                }
                catch (Exception exc)       // Exception thrown by parser -> save
                {                           // it (calling thread will rethrow this).
                    _exc = exc;
                }
                finally                     // Ensure signalling wait event, we are done (no
                {                           // matter whether errors/exceptions occured or not).
                    wait.Set();
                }
            }
            catch (ThreadAbortException)    // Thread that this method is running in was aborted
            {                               // by calling thread (because of a timeout) -> prevent
                Thread.ResetAbort();        // ThreadAbortException from being rethrown (see MSDN).
            }
        }
示例#14
0
        public virtual object VisitCompilationUnit(CompilationUnitNode compilationUnit, object data)
        {
            stackMap.Push(compilationUnit);
            compilationUnit.Attributes.AcceptVisitor(this, data);

            compilationUnit.DefaultNamespace.AcceptVisitor(this, data);


            compilationUnit.Namespaces.AcceptVisitor(this, data);

            stackMap.Pop();
            return null;

        }