annotate src/dil/SourceText.d @ 818:372fa4fbbb1d

Added error messages and applied fixes.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 13 Mar 2008 02:21:26 +0100
parents bcb74c9b895c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
1 /++
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
2 Author: Aziz Köksal
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
3 License: GPL3
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
4 +/
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
5 module dil.SourceText;
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
6
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
7 import dil.Converter;
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
8 import dil.Information;
818
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
9 import dil.Location;
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
10 import dil.Messages;
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
11 import common;
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
12
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
13 import tango.io.File;
818
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
14 import tango.io.FilePath;
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
15
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
16 /// Represents D source code.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
17 ///
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
18 /// The source text may come from a file or from a memory buffer.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
19 final class SourceText
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
20 {
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
21 string filePath; /// The file path to the source text. Mainly used for error messages.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
22 char[] data; /// The UTF-8, zero-terminated source text.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
23
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 755
diff changeset
24 /// Constructs a SourceText object.
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
25 /// Params:
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
26 /// filePath = file path to the source file.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
27 /// loadFile = whether to load the file in the constructor.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
28 this(string filePath, bool loadFile = false)
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
29 {
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
30 this.filePath = filePath;
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
31 loadFile && load();
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
32 }
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
33
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 755
diff changeset
34 /// Constructs a SourceText object.
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
35 /// Params:
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
36 /// filePath = file path for error messages.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
37 /// data = memory buffer.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
38 this(string filePath, char[] data)
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
39 {
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
40 this(filePath);
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
41 this.data = data;
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
42 addSentinelCharacter();
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
43 }
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
44
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 755
diff changeset
45 /// Loads the source text from a file.
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
46 void load(InfoManager infoMan = null)
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
47 {
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
48 if (!infoMan)
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
49 infoMan = new InfoManager;
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
50 assert(filePath.length);
818
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
51
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
52 scope(failure)
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
53 {
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
54 if (!(new FilePath(this.filePath)).exists())
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
55 infoMan ~= new LexerError(new Location(filePath, 0),
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
56 MSG.InexistantFile);
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
57 else
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
58 infoMan ~= new LexerError(new Location(filePath, 0),
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
59 MSG.CantReadFile);
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
60 data = "\0";
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
61 return;
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
62 }
372fa4fbbb1d Added error messages and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
63
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
64 // Read the file.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
65 auto rawdata = cast(ubyte[]) (new File(filePath)).read();
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
66 // Convert the data.
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
67 auto converter = Converter(filePath, infoMan);
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
68 data = converter.data2UTF8(rawdata);
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
69 addSentinelCharacter();
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
70 }
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
71
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 755
diff changeset
72 /// Adds '\0' to the text (if not already there.)
755
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
73 private void addSentinelCharacter()
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
74 {
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
75 if (data.length == 0 || data[$-1] != 0)
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
76 data ~= 0;
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
77 }
90668b83ae5e Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
78 }