Mercurial > projects > dil
comparison src/dil/Location.d @ 806:bcb74c9b895c
Moved out files in the trunk folder to the root.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sun, 09 Mar 2008 00:12:19 +0100 |
parents | trunk/src/dil/Location.d@c24be8d4f6ab |
children |
comparison
equal
deleted
inserted
replaced
805:a3fab8b74a7d | 806:bcb74c9b895c |
---|---|
1 /++ | |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module dil.Location; | |
6 | |
7 import dil.lexer.Funcs; | |
8 import dil.Unicode; | |
9 | |
10 /// Represents a location in a source text. | |
11 final class Location | |
12 { | |
13 char[] filePath; /// The file path. | |
14 size_t lineNum; /// The line number. | |
15 char* lineBegin, to; /// Used to calculate the column. | |
16 | |
17 /// Forwards the parameters to the second constructor. | |
18 this(char[] filePath, size_t lineNum) | |
19 { | |
20 set(filePath, lineNum); | |
21 } | |
22 | |
23 /// Constructs a Location object. | |
24 this(char[] filePath, size_t lineNum, char* lineBegin, char* to) | |
25 { | |
26 set(filePath, lineNum, lineBegin, to); | |
27 } | |
28 | |
29 void set(char[] filePath, size_t lineNum) | |
30 { | |
31 set(filePath, lineNum, null, null); | |
32 } | |
33 | |
34 void set(char[] filePath, size_t lineNum, char* lineBegin, char* to) | |
35 { | |
36 this.filePath = filePath; | |
37 set(lineNum, lineBegin, to); | |
38 } | |
39 | |
40 void set(size_t lineNum, char* lineBegin, char* to) | |
41 { | |
42 assert(lineBegin <= to); | |
43 this.lineNum = lineNum; | |
44 this.lineBegin = lineBegin; | |
45 this.to = to; | |
46 } | |
47 | |
48 void setFilePath(char[] filePath) | |
49 { | |
50 this.filePath = filePath; | |
51 } | |
52 | |
53 /// This is a primitive method to count the number of characters in a string. | |
54 /// Note: Unicode compound characters and other special characters are not | |
55 /// taken into account. The tabulator character is counted as one. | |
56 uint calculateColumn() | |
57 { | |
58 uint col; | |
59 auto p = lineBegin; | |
60 if (!p) | |
61 return 0; | |
62 for (; p <= to; ++p) | |
63 { | |
64 assert(delegate () | |
65 { | |
66 // Check that there is no newline between p and to. | |
67 // But 'to' may point to a newline. | |
68 if (p != to && isNewline(*p)) | |
69 return false; | |
70 if (to-p >= 2 && isUnicodeNewline(p)) | |
71 return false; | |
72 return true; | |
73 }() == true | |
74 ); | |
75 | |
76 // Skip this byte if it is a trail byte of a UTF-8 sequence. | |
77 if (isTrailByte(*p)) | |
78 continue; // *p == 0b10xx_xxxx | |
79 // Only count ASCII characters and the first byte of a UTF-8 sequence. | |
80 ++col; | |
81 } | |
82 return col; | |
83 } | |
84 alias calculateColumn colNum; | |
85 } |