Пример #1
0
        ClassDefinition GetClassDefinition(string code)
        {
            RubyParser     parser = new RubyParser();
            SourceUnitTree unit   = parser.CreateAst(@"test.rb", new StringTextBuffer(code));

            return(unit.Statements.First as ClassDefinition);
        }
Пример #2
0
        static Statements Parse(string code)
        {
            RubyParser     parser = new RubyParser();
            SourceUnitTree unit   = parser.CreateAst(@"snippet.rb", new StringTextBuffer(code));

            return(unit.Statements);
        }
Пример #3
0
 public void GetTreeAndCookie(out SourceUnitTree tree, out IAnalysisCookie cookie)
 {
     lock (this) {
         tree   = _node;
         cookie = _curCookie;
     }
 }
Пример #4
0
        public SourceUnitTree Parse(SourceUnit /*!*/ sourceUnit, RubyCompilerOptions /*!*/ options, ErrorSink /*!*/ errorSink)
        {
            Assert.NotNull(sourceUnit, options, errorSink);

            ErrorCounter counter = new ErrorCounter(errorSink);

            _tokenizer.ErrorSink     = counter;
            _tokenizer.Compatibility = options.Compatibility;

            _lexicalScopes.Clear();

            EnterScope(CreateTopScope(options.LocalNames));

            using (SourceCodeReader reader = sourceUnit.GetReader()) {
                _sourceUnit = sourceUnit;
                _tokenizer.Initialize(null, reader, sourceUnit, options.InitialLocation);

                // Default encoding when hosted (ignore KCODE, we are reading from Unicode buffer):
                _tokenizer.Encoding = (reader.Encoding != null) ? RubyEncoding.GetRubyEncoding(reader.Encoding) : RubyEncoding.UTF8;
                _tokenizer.AllowNonAsciiIdentifiers = _tokenizer.Encoding != RubyEncoding.Binary;

                try {
                    Parse();
                    LeaveScope();
                } catch (InternalSyntaxError) {
                    _ast = null;
                    _lexicalScopes.Clear();
                } finally {
                    ScriptCodeParseResult props;
                    if (counter.AnyError)
                    {
                        _ast = null;

                        if (_tokenizer.UnterminatedToken)
                        {
                            props = ScriptCodeParseResult.IncompleteToken;
                        }
                        else if (_tokenizer.EndOfFileReached)
                        {
                            props = ScriptCodeParseResult.IncompleteStatement;
                        }
                        else
                        {
                            props = ScriptCodeParseResult.Invalid;
                        }
                    }
                    else
                    {
                        props = ScriptCodeParseResult.Complete;
                    }

                    sourceUnit.CodeProperties = props;
                }

                return(_ast);
            }
        }
Пример #5
0
        public SourceUnitTree Parse(SourceUnit /*!*/ sourceUnit, RubyCompilerOptions /*!*/ options, ErrorSink /*!*/ errorSink)
        {
            Assert.NotNull(sourceUnit, options, errorSink);

            ErrorCounter counter = new ErrorCounter(errorSink);

            _tokenizer.ErrorSink     = counter;
            _tokenizer.Compatibility = options.Compatibility;

            _lexicalScopes.Clear();

            EnterScope(CreateTopScope(options.LocalNames));

            using (SourceCodeReader reader = sourceUnit.GetReader()) {
                _sourceUnit = sourceUnit;
                _tokenizer.Initialize(null, reader, sourceUnit, options.InitialLocation);

                // default encoding when hosted:
                _encoding = reader.Encoding ?? RubyEncoding.GetDefaultHostEncoding(options.Compatibility);

                try {
                    Parse();
                    LeaveScope();
                } catch (InternalSyntaxError) {
                    _ast = null;
                    _lexicalScopes.Clear();
                } finally {
                    ScriptCodeParseResult props;
                    if (counter.AnyError)
                    {
                        _ast = null;

                        if (_tokenizer.UnterminatedToken)
                        {
                            props = ScriptCodeParseResult.IncompleteToken;
                        }
                        else if (_tokenizer.IsEndOfFile)
                        {
                            props = ScriptCodeParseResult.IncompleteStatement;
                        }
                        else
                        {
                            props = ScriptCodeParseResult.Invalid;
                        }
                    }
                    else
                    {
                        props = ScriptCodeParseResult.Complete;
                    }

                    sourceUnit.CodeProperties = props;
                }

                return(_ast);
            }
        }
Пример #6
0
        public static MSA.Expression <T> /*!*/ Transform <T>(SourceUnitTree /*!*/ ast, SourceUnit /*!*/ sourceUnit,
                                                             RubyCompilerOptions /*!*/ options, int sourceId)
        {
            var context   = (RubyContext)sourceUnit.LanguageContext;
            var siteNodes = new Dictionary <MSA.DynamicExpression, SourceSpan>();
            var generator = new TraceAstGenerator(siteNodes, context, options, sourceUnit, ast.Encoding);
            var lambda    = ast.Transform <T>(generator);

            return((MSA.Expression <T>) new CallSiteTraceInjector(siteNodes, sourceId).Visit(lambda));
        }
Пример #7
0
 public virtual object Walk(SourceUnitTree node)
 {
     if (Enter(node))
     {
         if (node.SourceElements != null)
             node.SourceElements.Accept(this);
     }
     Exit(node);
     return null;
 }
Пример #8
0
        public void Parse()
        {
            var srcunit = HostingHelpers.GetSourceUnit(source);
            var parser  = new IronRuby.Compiler.Parser(errorSink);

            ast = parser.Parse(srcunit, new RubyCompilerOptions(), errorSink);

            var transformer = new Transformer.Transformer(ast);

            transformations = transformer.Transform();
        }
Пример #9
0
 public virtual object Walk(SourceUnitTree node)
 {
     if (Enter(node))
     {
         if (node.SourceElements != null)
         {
             node.SourceElements.Accept(this);
         }
     }
     Exit(node);
     return(null);
 }
Пример #10
0
        public static MSA.Expression <T> /*!*/ Transform <T>(SourceUnitTree /*!*/ ast, SourceUnit /*!*/ sourceUnit,
                                                             RubyCompilerOptions /*!*/ options, int sourceId)
        {
            var siteNodes = new Dictionary <MSA.DynamicExpression, SourceSpan>();
            var context   = (RubyContext)sourceUnit.LanguageContext;

            context.CallSiteCreated = (expression, callSite) => siteNodes.Add(callSite, expression.Location);

            var generator = new AstGenerator(context, options, sourceUnit.Document, ast.Encoding, false);
            var lambda    = ast.Transform <T>(generator);

            return((MSA.Expression <T>) new CallSiteTraceInjector(siteNodes, sourceId).Visit(lambda));
        }
Пример #11
0
        public void UpdateTree(SourceUnitTree newAst, IAnalysisCookie newCookie)
        {
            lock (this) {
                _node      = newAst;
                _curCookie = newCookie;
            }
            var newParse = OnNewParseTree;

            if (newParse != null)
            {
                newParse(this, EventArgs.Empty);
            }
        }
Пример #12
0
        public void Parse(TextContentProvider /*!*/ content)
        {
            var            errorSink = new CollectingErrorSink();
            SourceUnitTree ast       = MakeParseTree(content, errorSink);

            ISnapshotTextContentProvider snapshotContent = content as ISnapshotTextContentProvider;

            if (snapshotContent != null)
            {
                // queue analysis of the parsed tree at High Pri so the active buffer is quickly re-analyzed
                var snapshot = snapshotContent.Snapshot;

                var analysis = AnalysisItem.GetAnalysis(snapshot.TextBuffer);

                // only update the AST when we're error free, this way we don't remove
                // a useful analysis with an incomplete and useless analysis.
                if (errorSink.Errors.Count == 0)
                {
                    analysis.UpdateTree(ast, new SnapshotCookie(snapshot));
                    _analysisQueue.Enqueue(analysis, AnalysisPriority.High);
                }

                SimpleTagger <ErrorTag> squiggles = _squiggleProvider.GetErrorTagger(snapshot.TextBuffer);
                squiggles.RemoveTagSpans(x => true);

                // update squiggles for the live buffer
                foreach (ErrorResult warning in errorSink.Warnings)
                {
                    var span  = warning.Span;
                    var tspan = CreateSpan(snapshot, span);
                    squiggles.CreateTagSpan(tspan, new ErrorTag("Warning", warning.Message));
                }

                foreach (ErrorResult error in errorSink.Errors)
                {
                    var span  = error.Span;
                    var tspan = CreateSpan(snapshot, span);
                    squiggles.CreateTagSpan(tspan, new ErrorTag("Error", error.Message));
                }
            }
            else
            {
                FileTextContentProvider fileContent = content as FileTextContentProvider;
                AnalysisItem            analysis;
                if (fileContent != null && _projectFiles.TryGetValue(fileContent.Path, out analysis))
                {
                    analysis.UpdateTree(ast, new FileCookie(fileContent.Path));
                    _analysisQueue.Enqueue(analysis, AnalysisPriority.Normal);
                }
            }
        }
        /// <summary>
        /// Creates a control either a UserControl or Form from the Ruby code.
        /// </summary>
        public IComponent CreateComponent(string rubyCode)
        {
            RubyParser     parser = new RubyParser();
            SourceUnitTree ast    = parser.CreateAst(@"Control.rb", new StringTextBuffer(rubyCode));

            Walk(ast);

            // Did we find the InitializeComponent method?
            if (!FoundInitializeComponentMethod)
            {
                throw new RubyComponentWalkerException("Unable to find InitializeComponents method.");
            }
            return(component);
        }
Пример #14
0
        public SourceUnitTree ParseProgram()
        {
            if (Next().Is(TokenType.Eof))
            {
                return new SourceUnitTree {
                           Token = Next()
                }
            }
            ;
            var tree = new SourceUnitTree {
                Token = Next(), SourceElements = ParseSourceElements()
            };

            Match(TokenType.Eof);
            return(tree);
        }
Пример #15
0
 public Compiler(SourceUnitTree tree, string source = null)
 {
     if (source == null)
     {
         signature = new string[0];
     }
     else
     {
         signature = new string[] {
             "// " + source.MD5(),
             "// Generated by Fructose on " + DateTime.Now.ToShortDateString() + " by " + Environment.UserName,
             "//"
         };
     }
     this.tree = tree;
 }
Пример #16
0
        public ModelBuilder(SourceUnitTree /*!*/ tree)
        {
            _tree = tree;
            var top = new ModuleEntry("<main>", "<main>", tree, false, false);

            _outerName = new List <string>();
            _outerName.Add(null);

            _outerModules = new Stack <ModuleEntry>();
            _outerModules.Push(top);

            _outerMethods = new Stack <MethodEntry>();

            _definitions = new Dictionary <string, int>();
            _entries     = new List <ModuleEntry>();
            _entries.Add(top);
        }
Пример #17
0
		public ICompilationUnit Parse(IProjectContent projectContent, string fileName, ITextBuffer fileContent)
		{
			if (fileContent != null) { 
				try {
					SourceUnitTree ast = CreateAst(fileName, fileContent);
					RubyAstWalker walker = new RubyAstWalker(projectContent, fileName, sourceUnit);
					walker.Walk(ast);
					return walker.CompilationUnit;
				} catch (SyntaxErrorException) {
					// Ignore.
				}
			}
			
			DefaultCompilationUnit compilationUnit = new DefaultCompilationUnit(projectContent);
			compilationUnit.FileName = fileName;
			return compilationUnit;			
		}
Пример #18
0
 public override bool Enter(SourceUnitTree node)
 {
     Print("********************* Code Tree *********************");
     return true;
 }
Пример #19
0
 public virtual void Exit(SourceUnitTree node)
 {
 }
Пример #20
0
 public virtual bool Enter(SourceUnitTree node)
 {
     return true;
 }
Пример #21
0
        FunctionDefinition ParseFunctionDefinition()
        {
            var name = Expect(TokenType.Identifier).Value.ToString();
            List <ParameterDefinition> parameters = new List <ParameterDefinition>();

            if (Accept(TokenType.SymbolOpenParenthesis))
            {
                while (true)
                {
                    if (Accept(TokenType.SymbolCloseParenthesis))
                    {
                        break;
                    }
                    else
                    {
                        Accept(TokenType.SymbolComma);
                    }
                    if (Accept(TokenType.SymbolAsterisk))
                    {
                        parameters.Add(new ParameterDefinition(null, true));
                    }
                    else
                    {
                        var paramName = Expect(TokenType.Identifier).Value.ToString();
                        if (Accept(TokenType.SymbolEquals))
                        {
                            var start = _tokenizer.NextToken.Span.Start;
                            var exp   = new SourceUnitTree(
                                Enumerable.Empty <ClassDefinition>(),
                                Enumerable.Empty <FunctionDefinition>(),
                                Enumerable.Empty <PropertyDefinition>(),
                                new[] { new ExpressionStatement(ParseAssignmentExpression()) },
                                _context
                                );
                            var end = _tokenizer.NextToken.Span.End;
                            try
                            {
                                parameters.Add(new ParameterDefinition(paramName, exp.Transform <Func <object, object> >().Compile()(null)));
                            }
                            catch (MissingMemberException)
                            {
                                _tokenizer.ErrorSink.Add(_context.SourceUnit, "関数の既定値には定数のみが使用できます。", new SourceSpan(start, end), -2, Severity.Error);
                                throw new InternalInvalidSyntaxException();
                            }
                        }
                        else if (Accept(TokenType.SymbolAsterisk))
                        {
                            parameters.Add(new ParameterDefinition(paramName, true));
                        }
                        else
                        {
                            parameters.Add(new ParameterDefinition(paramName, false));
                        }
                    }
                }
            }
            List <Statement> statements = new List <Statement>();

            Expect(TokenType.SymbolOpenBrace);
            while (!Accept(TokenType.SymbolCloseBrace))
            {
                statements.Add(ParseStatement());
            }
            return(new FunctionDefinition(name, parameters, statements));
        }
Пример #22
0
 public AstScopeNode(SourceUnitTree ast, ProjectEntry projectEntry)
 {
     _ast          = ast;
     _projectEntry = projectEntry;
 }
 public virtual void Exit(SourceUnitTree node)
 {
 }
Пример #24
0
 public override bool Enter(SourceUnitTree node)
 {
     Print("********************* Code Tree *********************");
     return(true);
 }
Пример #25
0
 public SourceUnitTree ParseProgram()
 {
     if (Next().Is(TokenType.Eof))
         return new SourceUnitTree { Token = Next() };
     var tree = new SourceUnitTree { Token = Next(), SourceElements = ParseSourceElements() };
     Match(TokenType.Eof);
     return tree;
 }
Пример #26
0
 private void DumpRegularExpressions(SourceUnitTree /*!*/ ast)
 {
     new RegexDumper(_regexLog).Walk(ast);
 }
Пример #27
0
        private void DumpFile(string /*!*/ path)
        {
            _log.WriteLine(path);

            try {
                string fullPath   = Path.GetFullPath(path);
                string root       = Path.GetPathRoot(fullPath);
                string outputPath = Path.ChangeExtension(Path.Combine(_targetDir, fullPath.Substring(root.Length).TrimEnd('\'', '/')), ".txt");
                Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

                using (TextWriter output = File.CreateText(outputPath)) {
                    output.WriteLine(fullPath);
                    output.WriteLine();
                    output.WriteLine("Tokens:");
                    output.WriteLine();

                    ErrorLog errors = new ErrorLog();
                    Parser   parser = new Parser();

                    parser.TokenSink = delegate(Tokens token, SourceSpan span) {
                        DumpTokenDetail(output, parser.Tokenizer, token);
                    };

                    if (_logProductions)
                    {
#if DEBUG
                        parser.EnableLogging(new CoverageParserLogger(parser, _parserLog));
#endif
                    }

                    _currentSourceFile = path;

                    SourceUnitTree ast = null;
                    try {
                        ast = parser.Parse(_context.CreateFileUnit(path), new RubyCompilerOptions(), errors);
                    } catch (Exception e) {
                        WriteException(e.ToString());
                    }

                    output.WriteLine();

                    if (errors.ErrorCount + errors.FatalErrorCount + errors.WarningCount > 0)
                    {
                        output.WriteLine();
                        output.WriteLine("Errors:");

                        foreach (string error in errors.Errors)
                        {
                            output.WriteLine(error);
                        }
                    }
                    else
                    {
                        Debug.Assert(ast != null);
                        DumpRegularExpressions(ast);
                    }

                    output.WriteLine(".");
                }
            } catch (Exception e) {
                _log.WriteLine("!{0}", e.Message);
            } finally {
                _currentSourceFile = null;
                _regexLog.Flush();
                _parserLog.Flush();
            }
        }
Пример #28
0
 public Transformer(SourceUnitTree AST)
 {
     this.AST = AST;
 }
Пример #29
0
 public override object Walk(SourceUnitTree node)
 {
     return node.SourceElements == null
         ? null
         : node.SourceElements.Accept(this);
 }
Пример #30
0
 public override void Exit(SourceUnitTree node)
 {
 }
 public virtual bool Enter(SourceUnitTree node)
 {
     return(true);
 }
Пример #32
0
 public override object Walk(SourceUnitTree node)
 {
     return(node.SourceElements == null
         ? null
         : node.SourceElements.Accept(this));
 }
Пример #33
0
 public override void Exit(SourceUnitTree node)
 {
 }