Browse Source

[Parser] Implemented own seekable stream reader.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
f81ecd7e29
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 32
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
  3. 98
      ICSharpCode.NRefactory.CSharp/Parser/SeekableStreamReader.cs
  4. 4
      ICSharpCode.NRefactory.CSharp/Parser/mcs/support.cs

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -400,6 +400,7 @@ @@ -400,6 +400,7 @@
<Compile Include="Refactoring\CodeActions\ExtractFieldAction.cs" />
<Compile Include="Completion\ICompletionContextProvider.cs" />
<Compile Include="Refactoring\CodeActions\ExtractMethod\VariableUsageAnalyzation.cs" />
<Compile Include="Parser\SeekableStreamReader.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

32
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

@ -3709,25 +3709,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3709,25 +3709,9 @@ namespace ICSharpCode.NRefactory.CSharp
get { return errorReportPrinter.Errors; }
}
public SyntaxTree Parse (ITextSource textSource, string fileName, int lineModifier = 0)
{
return Parse (textSource.CreateReader (), fileName, lineModifier);
}
public SyntaxTree Parse (TextReader reader, string fileName, int lineModifier = 0)
{
// TODO: can we optimize this to avoid the text->stream->text roundtrip?
using (MemoryStream stream = new MemoryStream ()) {
StreamWriter w = new StreamWriter (stream, Encoding.UTF8);
char[] buffer = new char[2048];
int read;
while ((read = reader.ReadBlock(buffer, 0, buffer.Length)) > 0)
w.Write (buffer, 0, read);
w.Flush (); // we can't close the StreamWriter because that would also close the MemoryStream
stream.Position = 0;
return Parse (stream, fileName, lineModifier);
}
return Parse(new StringTextSource (reader.ReadToEnd ()), fileName, lineModifier);
}
public SyntaxTree Parse(CompilerCompilationUnit top, string fileName, int lineModifier = 0)
@ -3771,18 +3755,23 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3771,18 +3755,23 @@ namespace ICSharpCode.NRefactory.CSharp
public SyntaxTree Parse (string program, string fileName)
{
return Parse (new StringReader (program), fileName);
return Parse (new StringTextSource (program), fileName);
}
internal static object parseLock = new object ();
public SyntaxTree Parse (Stream stream, string fileName, int lineModifier = 0)
{
return Parse (new StreamReader (stream), fileName, lineModifier);
}
public SyntaxTree Parse(Stream stream, string fileName, int lineModifier = 0)
public SyntaxTree Parse(ITextSource src, string fileName, int lineModifier = 0)
{
lock (parseLock) {
errorReportPrinter = new ErrorReportPrinter ("");
var ctx = new CompilerContext (compilerSettings.ToMono(), errorReportPrinter);
ctx.Settings.TabSize = 1;
var reader = new SeekableStreamReader (stream, Encoding.UTF8);
var reader = new SeekableStreamReader (src);
var file = new SourceFile (fileName, fileName, 0);
Location.Initialize (new List<SourceFile> (new [] { file }));
var module = new ModuleContainer (ctx);
@ -3885,8 +3874,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -3885,8 +3874,7 @@ namespace ICSharpCode.NRefactory.CSharp
errorReportPrinter = new ErrorReportPrinter("");
var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter);
ctx.Settings.TabSize = 1;
var stream = new MemoryStream(Encoding.Unicode.GetBytes(cref));
var reader = new SeekableStreamReader(stream, Encoding.Unicode);
var reader = new SeekableStreamReader(new StringTextSource (cref));
var file = new SourceFile("", "", 0);
Location.Initialize(new List<SourceFile> (new [] { file }));
var module = new ModuleContainer(ctx);

98
ICSharpCode.NRefactory.CSharp/Parser/SeekableStreamReader.cs

@ -0,0 +1,98 @@ @@ -0,0 +1,98 @@
//
// SeekableStreamReader.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.Editor;
using System.IO;
using System.Text;
namespace Mono.CSharp
{
public class SeekableStreamReader : IDisposable
{
public const int DefaultReadAheadSize = 2048;
readonly ITextSource textSource;
int pos;
public SeekableStreamReader (Stream stream, Encoding encoding, char[] sharedBuffer = null)
{
throw new NotImplementedException ();
}
public SeekableStreamReader (ITextSource source)
{
this.textSource = source;
}
public void Dispose ()
{
}
/// <remarks>
/// This value corresponds to the current position in a stream of characters.
/// The StreamReader hides its manipulation of the underlying byte stream and all
/// character set/decoding issues. Thus, we cannot use this position to guess at
/// the corresponding position in the underlying byte stream even though there is
/// a correlation between them.
/// </remarks>
public int Position {
get {
return pos;
}
set {
pos = value;
}
}
public char GetChar (int position)
{
return textSource.GetCharAt (position);
}
public char[] ReadChars (int fromPosition, int toPosition)
{
return textSource.GetText (fromPosition, toPosition - fromPosition).ToCharArray ();
}
public int Peek ()
{
if (pos >= textSource.TextLength)
return -1;
return textSource.GetCharAt (pos);
}
public int Read ()
{
if (pos >= textSource.TextLength)
return -1;
return textSource.GetCharAt (pos++);
}
}
}

4
ICSharpCode.NRefactory.CSharp/Parser/mcs/support.cs

@ -127,7 +127,7 @@ namespace Mono.CSharp { @@ -127,7 +127,7 @@ namespace Mono.CSharp {
return true;
}
}
#if !FULL_AST
/// <summary>
/// This is an arbitrarily seekable StreamReader wrapper.
///
@ -276,7 +276,7 @@ namespace Mono.CSharp { @@ -276,7 +276,7 @@ namespace Mono.CSharp {
return buffer [pos++];
}
}
#endif
public class UnixUtils {
[System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
extern static int _isatty (int fd);

Loading…
Cancel
Save