Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changes type of CompilationOptions.Source from list[string] to list[I…

…Source].
  • Loading branch information...
commit fa09feaa867568af7582396296fba0c43e45c6a9 1 parent 6aa1e22
@someone-with-default-username someone-with-default-username authored
View
2  ncc/CompilationOptions.n
@@ -75,7 +75,7 @@ namespace Nemerle.Compiler
public mutable ReferencedLibraries : list [string];
public mutable LibraryPaths : list[string];
public mutable MacrosToLoad : list [string];
- public mutable Sources : list [string];
+ public mutable Sources : list [ISource];
public mutable Platform : string;
View
9 ncc/codedom/NemerleCodeCompiler.n
@@ -44,6 +44,7 @@ using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
+using SCG = System.Collections.Generic;
using Nemerle.Assertions;
using Nemerle.Collections;
@@ -130,11 +131,11 @@ namespace Nemerle.Compiler
err_event (true, loc, msg);
}
- mutable files = [];
+ def sources = SCG.List();
def opts = cOptions.GetCommonOptions () + [
Getopt.CliOption.NonOption (name = "",
help = "Specify file to compile",
- handler = fun (s) { files = s :: files })
+ handler = fun (s) { sources.Add(FileSource(s, cOptions.Warnings)) })
];
unless (options.CompilerOptions == null)
@@ -161,11 +162,13 @@ namespace Nemerle.Compiler
Message.Error ("need at least one file to compile");
else
{
+ foreach(fileName in fileNames)
+ sources.Add(FileSource(fileName, cOptions.Warnings));
+ cOptions.Sources = sources.ToNList();
def compilerThreadProc()
{
try
{
- cOptions.Sources = files + NList.FromArray (fileNames);
man.Run();
succeeded = !failed;
}
View
15 ncc/main.n
@@ -43,6 +43,7 @@ using Nemerle.Compiler;
using System;
using System.Diagnostics;
using System.Reflection;
+using SCG = System.Collections.Generic;
namespace Nemerle.CommandlineCompiler
{
@@ -167,7 +168,7 @@ namespace Nemerle.CommandlineCompiler
{
def cOptions = Options;
- mutable files = [];
+ def sources = SCG.List();
def print_version ()
{
@@ -216,18 +217,16 @@ namespace Nemerle.CommandlineCompiler
"remove '-debugger' option from command line")),
Getopt.CliOption.NonOption (name = "",
help = "Specify file to compile",
- handler = fun (s) { files = s :: files })
+ handler = fun (s) { sources.Add(FileSource(s, cOptions.Warnings)) })
];
help_opts = opts;
Getopt.Parse(opts);
- match (files) {
- | [] =>
- Getopt.Error ("need at least one file to compile\n" + Getopt.Usage (opts))
- | _ =>
- cOptions.Sources = files;
- }
+ if (sources.Count > 0)
+ cOptions.Sources = sources.ToNList();
+ else
+ Getopt.Error("need at least one file to compile\n" + Getopt.Usage(opts))
}
}
}
View
35 ncc/parsing/Parser.n
@@ -114,41 +114,6 @@ namespace Nemerle.Compiler
lexer.SwitchToIndentationMode();
DefaultParseResult(MainParser.Parse(lexer), lexer)
}
-
- public static ReadFile([NotNull] filePath : string, checkDuplicatedFiles : bool, checkLastLineForLF : bool) : ISource
- {
- try
- {
- using(file = IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read))
- using(stream = IO.StreamReader(file, Text.UTF8Encoding(true, true)))
- {
- def index =
- if(checkDuplicatedFiles)
- Location.AddFile(filePath)
- else
- Location.GetFileIndex(filePath);
- def code = stream.ReadToEnd();
- when(checkLastLineForLF)
- {
- _ = file.Seek (-1 :> Int64, IO.SeekOrigin.End);
- def lastByte = file.ReadByte();
- if (lastByte >= 0)
- {
- def ch = Convert.ToChar(lastByte);
- when (ch != '\n')
- Message.Warning(Location(index, 1, 1), "no new line at the end of the file");
- }
- else
- throw LexerBase.Error("unexpected end of file");
- }
- StringSource(index, code)
- }
- }
- catch
- {
- | e => Message.FatalError(Location(filePath, 1, 1, 1, 1), $"cannot open file `$filePath': $(e.Message)")
- }
- }
}
public sealed class DispatchingFileParser : IParser
View
82 ncc/parsing/Source.n
@@ -26,7 +26,11 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+using System;
+using System.Text;
+using System.IO;
using Nemerle;
+using Nemerle.Assertions;
using Nemerle.Utility;
namespace Nemerle.Compiler
@@ -40,9 +44,81 @@ namespace Nemerle.Compiler
[Record]
public sealed class StringSource : ISource
{
- [Accessor] fileIndex : int;
- text : string;
+ public FileIndex : int { get }
public GetText() : string { text }
+
+ private text : string;
}
-}
+
+ public sealed class FileSource : ISource
+ {
+ public this ([NotNull] filePath : string, checkDuplicatedFiles : bool, checkLastLineForLF : bool)
+ {
+ _filePath = filePath;
+ _checkDuplicatedFiles = checkDuplicatedFiles;
+ _checkLastLineForLF = fun () { checkLastLineForLF };
+ }
+
+ public this ([NotNull] filePath : string, [NotNull] warnings : WarningOptions)
+ {
+ _filePath = filePath;
+ _checkDuplicatedFiles = true;
+ _checkLastLineForLF = fun () { warnings.IsEnabled(10002) };
+ }
+
+ public FileIndex : int { get { InternalSource.FileIndex } }
+
+ public GetText() : string { InternalSource.GetText() }
+
+ private InternalSource : ISource
+ {
+ mutable _internalSource : ISource;
+ get
+ {
+ when (null == _internalSource)
+ _internalSource = ReadFile(_filePath, checkDuplicatedFiles = _checkDuplicatedFiles, checkLastLineForLF = _checkLastLineForLF());
+ _internalSource
+ }
+ }
+
+ public static ReadFile([NotNull] filePath : string, checkDuplicatedFiles : bool, checkLastLineForLF : bool) : ISource
+ {
+ try
+ {
+ using (file = FileStream(filePath, FileMode.Open, FileAccess.Read))
+ using (stream = StreamReader(file, UTF8Encoding(true, true)))
+ {
+ def index =
+ if (checkDuplicatedFiles)
+ Location.AddFile(filePath)
+ else
+ Location.GetFileIndex(filePath);
+ def code = stream.ReadToEnd();
+ when (checkLastLineForLF)
+ {
+ _ = file.Seek (-1 :> Int64, SeekOrigin.End);
+ def lastByte = file.ReadByte();
+ if (lastByte >= 0)
+ {
+ def ch = Convert.ToChar(lastByte);
+ when (ch != '\n')
+ Message.Warning(Location(index, 1, 1), "no new line at the end of the file");
+ }
+ else
+ throw LexerBase.Error("unexpected end of file");
+ }
+ StringSource(index, code)
+ }
+ }
+ catch
+ {
+ | e => Message.FatalError(Location(filePath, 1, 1, 1, 1), $"cannot open file `$filePath': $(e.Message)")
+ }
+ }
+
+ private _filePath : string;
+ private _checkDuplicatedFiles : bool;
+ private _checkLastLineForLF : void -> bool;
+ }
+}
View
10 ncc/passes.n
@@ -236,7 +236,7 @@ namespace Nemerle.Compiler
public virtual GetSource(fileIndex : int) : ISource
{
- DefaultParser.ReadFile(Location.GetFileName(fileIndex),
+ FileSource.ReadFile(Location.GetFileName(fileIndex),
checkDuplicatedFiles = false,
checkLastLineForLF = false)
}
@@ -660,13 +660,7 @@ namespace Nemerle.Compiler
Hierarchy = TypesManager (this);
- def parseResults = Options.Sources.RevMap(fun(filePath)
- {
- def source = DefaultParser.ReadFile(filePath,
- checkDuplicatedFiles = true,
- checkLastLineForLF = Options.Warnings.IsEnabled(10002));
- Parser.Parse(source)
- });
+ def parseResults = Options.Sources.Map(Parser.Parse);
Message.MaybeBailout(); // we have detected multiple files already
View
2  snippets/Nemerle.Test/Nemerle.Compiler.Test/HostedNcc.n
@@ -29,7 +29,7 @@ namespace Nemerle.Compiler.Test
_nccArguments + arguments);
options.IgnoreConfusion = true;
options.ProgressBar = false;
- options.Sources = files;
+ options.Sources = files.Map(FileSource(_, options.Warnings));
def compilerOutput = List();
View
2  snippets/csharp-parser/CSharpToNemerle.Macros/Include.n
@@ -18,7 +18,7 @@ namespace CSharpToNemerle
def projectDir = Path.GetDirectoryName(manager.Options.ProjectPath);
foreach(fileName in Directory.GetFiles(projectDir, files))
{
- def result = parser.Parse(DefaultParser.ReadFile(fileName, false, false));
+ def result = parser.Parse(FileSource.ReadFile(fileName, false, false));
result.TopDeclarations.Iter(manager.ScanningPipeline);
}
}
View
2  snippets/csharp-parser/CSharpToNemerleSource/CSharpToNemerleSrc/Convert.n
@@ -78,7 +78,7 @@ namespace CSharpToNemerle
public PrintNFile(file : string, out_file : string) : void
{
def writer = LocatableTextWriter(StringBuilder());
- def source = DefaultParser.ReadFile(file, true, false);
+ def source = FileSource.ReadFile(file, true, false);
def parser = DefaultParser(compiler);
def res = parser.Parse(source);
DeclPrettyPrint.PrintFile(res, writer, compiler);
View
2  snippets/csharp-parser/CSharpToNemerleSource/Tests/CSharpToNemerleTest/TestFiles/Convert.n
@@ -70,7 +70,7 @@ namespace CSharpToNemerle
public PrintNFile(file : string, out_file : string) : void
{
def writer = LocatableTextWriter(StringBuilder());
- def source = DefaultParser.ReadFile(file, true, false);
+ def source = FileSource.ReadFile(file, true, false);
def parser = DefaultParser(compiler);
def res = parser.Parse(source);
DeclPrettyPrint.PrintFile(res, writer, compiler);
View
2  snippets/csharp-parser/CSharpToNemerleSource/cs2nemerle/Converted/Convert.n
@@ -63,7 +63,7 @@ namespace CSharpToNemerle
public PrintNFile(file : string, out_file : string) : void
{
def writer = LocatableTextWriter(StringBuilder());
- def source = DefaultParser.ReadFile(file, true, false);
+ def source = FileSource.ReadFile(file, true, false);
def parser = DefaultParser(compiler);
def res = parser.Parse(source);
DeclPrettyPrint.PrintFile(res, writer, compiler);
View
8 tools/nemerlish/eval.n
@@ -64,7 +64,7 @@ namespace Nemerle.Evaluation
Options.CompileToMemory = true;
Options.IgnoreConfusion = true;
Options.ProgressBar = false;
- Options.Sources = [""];
+ Options.Sources = [StubSource()];
// HACK! HACK! HACK!
// This is broken, broken, broken! The values here shouldn't be
@@ -407,5 +407,11 @@ namespace Nemerle.Evaluation
null
}
}
+
+ internal class StubSource : ISource
+ {
+ public FileIndex : int { get { 0 } }
+ public GetText() : string { "" }
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.