public void GetReader_MoveIntoStreamCreateNewReaderAndCheckPosition()
        {
            // Setup tests data
            string testInput = _codeSnippets[CodeType.SevenLinesOfAssignemtStatements];

            // Count the code lines
            Regex           RE         = new Regex("\n", RegexOptions.Multiline);
            MatchCollection theMatches = RE.Matches(testInput);
            int             lines      = theMatches.Count + 1;

            // Create script source
            ScriptSource source = _testEng.CreateScriptSourceFromString(testInput,
                                                                        SourceCodeKind.Statements);
            // Create first reader
            SourceCodeReader srcFirstUnitReader = source.GetReader();

            // This could be a little fragile. Might be better just to hard code
            // expected value. - Save first line with first reader.
            Assert.IsTrue(srcFirstUnitReader.SeekLine(1));
            string expValue = srcFirstUnitReader.ReadLine();

            // Move to the middle of the stream (approximately).
            Assert.IsTrue(srcFirstUnitReader.SeekLine(lines / 2));

            // Create second unit reader
            SourceCodeReader srcSecondUnitReader = source.GetReader();

            Assert.AreEqual(srcSecondUnitReader.ReadLine(), expValue);
        }
        public override void Add(SourceUnit sourceUnit, string message, SourceSpan span, int errorCode, Severity severity)
        {
            if (severity == Severity.Warning && !ReportWarning(_context.Verbose, errorCode))
            {
                return;
            }

            CountError(severity);

            string       path;
            string       codeLine;
            RubyEncoding encoding;
            int          line = span.Start.Line;

            if (sourceUnit != null)
            {
                path = sourceUnit.Path;
                using (SourceCodeReader reader = sourceUnit.GetReader()) {
                    if (line > 0)
                    {
                        try {
                            reader.SeekLine(line);
                            codeLine = reader.ReadLine();
                        } catch (Exception) {
                            codeLine = null;
                        }
                    }
                    else
                    {
                        codeLine = null;
                    }
                    encoding = reader.Encoding != null?RubyEncoding.GetRubyEncoding(reader.Encoding) : RubyEncoding.UTF8;
                }
            }
            else
            {
                path     = null;
                codeLine = null;
                encoding = RubyEncoding.UTF8;
            }

            if (severity == Severity.Error || severity == Severity.FatalError)
            {
                throw new SyntaxError(message, path, line, span.Start.Column, codeLine);
            }
            else
            {
                WriteMessage(
                    MutableString.Create(RubyContext.FormatErrorMessage(message, "warning", path, line, span.Start.Column, null), encoding)
                    );
            }
        }
示例#3
0
        /// <summary>
        /// If the SCRIPT_LINES__ constant is set, we need to publish the file being loaded,
        /// along with the contents of the file
        /// </summary>
        private void AddScriptLines(SourceUnit file)
        {
            ConstantStorage storage;

            if (!_context.ObjectClass.TryResolveConstant(null, "SCRIPT_LINES__", out storage))
            {
                return;
            }

            IDictionary scriptLines = storage.Value as IDictionary;

            if (scriptLines == null)
            {
                return;
            }

            lock (scriptLines) {
                // Read in the contents of the file

                RubyArray        lines    = new RubyArray();
                SourceCodeReader reader   = file.GetReader();
                RubyEncoding     encoding = RubyEncoding.GetRubyEncoding(reader.Encoding);
                using (reader) {
                    reader.SeekLine(1);
                    while (true)
                    {
                        string lineStr = reader.ReadLine();
                        if (lineStr == null)
                        {
                            break;
                        }
                        MutableString line = MutableString.CreateMutable(lineStr.Length + 1, encoding);
                        line.Append(lineStr).Append('\n');
                        lines.Add(line);
                    }
                }

                // Publish the contents of the file, keyed by the file name
                MutableString path = MutableString.Create(file.Document.FileName, _context.GetPathEncoding());
                scriptLines[path] = lines;
            }
        }