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