diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java index 1472bedc7..35a750961 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java @@ -725,13 +725,11 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest "\n" + "public class Test {\n" + " public static void Main(string[] args) {\n" + - " string inputData = File.ReadAllText(args[0], Encoding.UTF8);\n" + + " var input = CharStreams.fromPath(args[0]);\n" + " using (FileStream fsOut = new FileStream(args[1], FileMode.Create, FileAccess.Write))\n" + " using (FileStream fsErr = new FileStream(args[2], FileMode.Create, FileAccess.Write))\n" + " using (TextWriter output = new StreamWriter(fsOut),\n" + " errorOutput = new StreamWriter(fsErr)) {\n" + - " CodePointCharStream input = new CodePointCharStream(inputData);\n" + - " input.name = args[0];\n" + " lex = new (input, output, errorOutput);\n" + " CommonTokenStream tokens = new CommonTokenStream(lex);\n" + " \n"+ @@ -780,8 +778,7 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest "\n" + "public class Test {\n" + " public static void Main(string[] args) {\n" + - " string inputData = File.ReadAllText(args[0], Encoding.UTF8);\n" + - " ICharStream input = new CodePointCharStream(inputData);\n" + + " var input = CharStreams.fromPath(args[0]);\n" + " using (FileStream fsOut = new FileStream(args[1], FileMode.Create, FileAccess.Write))\n" + " using (FileStream fsErr = new FileStream(args[2], FileMode.Create, FileAccess.Write))\n" + " using (TextWriter output = new StreamWriter(fsOut),\n" + diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.mono.csproj b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.mono.csproj index 74b0eb240..fc42da254 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.mono.csproj +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.mono.csproj @@ -52,6 +52,7 @@ + diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj index 948b9ea5e..b9117f367 100644 --- a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.vs2013.csproj @@ -54,6 +54,7 @@ + diff --git a/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs new file mode 100644 index 000000000..0ee9f38af --- /dev/null +++ b/runtime/CSharp/runtime/CSharp/Antlr4.Runtime/CharStreams.cs @@ -0,0 +1,93 @@ +/* Copyright (c) 2012-2016 The ANTLR Project. All rights reserved. + * Use of this file is governed by the BSD 3-clause license that + * can be found in the LICENSE.txt file in the project root. + */ +using System; +using System.IO; +using System.Text; +using Antlr4.Runtime; +using Antlr4.Runtime.Misc; +using Antlr4.Runtime.Sharpen; + +namespace Antlr4.Runtime +{ + /// Utility class to create s from various sources of + /// string data. + /// + /// The methods in this utility class support the full range of + /// Unicode code points up to U+10FFFF, unlike , + /// which is limited to 16-bit Unicode code units up to U+FFFF. + /// + public static class CharStreams + { + /// Creates an given a path to a UTF-8 + /// encoded file on disk. + /// + /// Reads the entire contents of the file into the result before returning. + /// + public static ICharStream fromPath(string path) + { + return fromPath(path, Encoding.UTF8); + } + + /// Creates an given a path to a + /// file on disk and the encoding of the bytes contained in the file. + /// + /// Reads the entire contents of the file into the result before returning. + /// + public static ICharStream fromPath(string path, Encoding encoding) + { + var pathContents = File.ReadAllText(path, encoding); + var result = new CodePointCharStream(pathContents); + result.name = path; + return result; + } + + /// Creates an given an opened + /// . + /// + /// Reads the entire contents of the TextReader then closes the reader before returning. + /// + public static ICharStream fromTextReader(TextReader textReader) + { + try { + var textReaderContents = textReader.ReadToEnd(); + return new CodePointCharStream(textReaderContents); + } finally { + textReader.Dispose(); + } + } + + /// Creates an given an opened + /// from which UTF-8 encoded bytes can be read. + /// + /// Reads the entire contents of the stream into the result then + /// closes the stream before returning. + /// + public static ICharStream fromStream(Stream stream) + { + return fromStream(stream, Encoding.UTF8); + } + + /// Creates an given an opened + /// as well as the encoding of the bytes + /// to be read from the stream. + /// + /// Reads the entire contents of the stream into the result then + /// closes the stream before returning. + /// + public static ICharStream fromStream(Stream stream, Encoding encoding) + { + using (StreamReader sr = new StreamReader(stream, encoding, false)) { + return fromTextReader(sr); + } + } + + /// Creates an given a . + /// + public static ICharStream fromstring(string s) + { + return new CodePointCharStream(s); + } + } +}