Пример #1
0
 public static CompilationUnitNode ParseDocument(string 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 = new Parser(fileName);
     CompilationUnitNode cu = p.Parse(toks, l.StringLiterals);
     return cu;
 }
 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;
   }
 }
Пример #3
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());
    }
Пример #4
0
        public System.IO.Stream Builder(System.IO.Stream stream)
        {
           
            CodeDomProvider proider = new Microsoft.CSharp.CSharpCodeProvider();
            System.IO.StringWriter sw = new System.IO.StringWriter();
            Console.SetOut(sw);
            Console.SetError(sw);
            // CSharpParser p = new CSharpParser(null, stream, null);
            System.IO.Stream result = new System.IO.MemoryStream();
            System.IO.StreamWriter writer = new System.IO.StreamWriter(result, Encoding.UTF8);
            CompilationUnitNode unit = null;
            using (System.IO.StreamReader sr = new StreamReader(stream, Encoding.UTF8))
            {
                Lexer lexer = new Lexer(sr);
                TokenCollection tc = lexer.Lex();
                Parser parser = new Parser();
                unit = parser.Parse(tc, lexer.StringLiterals);

            }

            if (unit != null)
            {
                try
                {
                   
                    Microsoft.CSharp.CSharpCodeProvider csp = new Microsoft.CSharp.CSharpCodeProvider();
                    foreach (UsingDirectiveNode item in unit.DefaultNamespace.UsingDirectives)
                    {
                        mEntityUnit.DefaultNamespace.UsingDirectives.Add(item);
                    }
                    foreach (NamespaceNode cn in unit.Namespaces)
                    {
                        BuilderNameSpace(cn, unit);
                    }   
                    StringBuilder sb = new StringBuilder();
                    mEntityUnit.ToSource(sb);
                    writer.Write(sb.ToString());

                }
                catch (Exception e_)
                {
                    writer.WriteLine(e_.Message + e_.StackTrace);
                }
                writer.Flush();
            }
            return result;

        }
Пример #5
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;
        }
Пример #6
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());
        }
Пример #7
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).
            }
        }
Пример #8
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);

            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);

            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());
        }