示例#1
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);
            }
        }