Mercurial > projects > dil
annotate src/dil/doc/Parser.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/doc/Parser.d@3b34f6a95a27 |
children |
rev | line source |
---|---|
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
1 /++ |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
2 Author: Aziz Köksal |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
3 License: GPL3 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
4 +/ |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
5 module dil.doc.Parser; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
6 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
7 import dil.lexer.Funcs; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
8 import dil.Unicode; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
9 import common; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
10 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
11 /// A pair of strings. |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
12 class IdentValue |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
13 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
14 string ident; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
15 string value; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
16 this (string ident, string value) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
17 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
18 this.ident = ident; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
19 this.value = value; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
20 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
21 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
22 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
23 /// Parses text of the form: |
747
00f872d949ea
Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
745
diff
changeset
|
24 /// <pre> |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
25 /// ident = value |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
26 /// ident2 = value2 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
27 /// more text |
747
00f872d949ea
Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
745
diff
changeset
|
28 /// </pre> |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
29 struct IdentValueParser |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
30 { |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
31 char* p; /// Current pointer. |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
32 char* textEnd; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
33 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
34 IdentValue[] parse(string text) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
35 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
36 if (!text.length) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
37 return null; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
38 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
39 p = text.ptr; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
40 textEnd = p + text.length; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
41 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
42 IdentValue[] idvalues; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
43 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
44 string ident, nextIdent; |
768
d84349a60f5c
Fixed IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
751
diff
changeset
|
45 char* bodyBegin = p, nextBodyBegin; |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
46 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
47 // Init. |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
48 findNextIdent(ident, bodyBegin); |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
49 // Continue. |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
50 while (findNextIdent(nextIdent, nextBodyBegin)) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
51 { |
741
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
52 idvalues ~= new IdentValue(ident, textBody(bodyBegin, nextIdent.ptr)); |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
53 ident = nextIdent; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
54 bodyBegin = nextBodyBegin; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
55 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
56 // Add last ident value. |
741
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
57 idvalues ~= new IdentValue(ident, textBody(bodyBegin, textEnd)); |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
58 return idvalues; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
59 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
60 |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
61 /// Returns the text body. Trailing whitespace characters are not included. |
741
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
62 char[] textBody(char* begin, char* end) |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
63 { |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
64 // The body of A is empty, e.g.: |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
65 // A = |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
66 // B = some text |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
67 // ^- begin and end point to B (or to this.textEnd in the 2nd case.) |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
68 if (begin is end) |
745 | 69 return ""; |
741
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
70 // Remove trailing whitespace. |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
71 while (isspace(*--end) || *end == '\n') |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
72 {} |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
73 end++; |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
74 return makeString(begin, end); |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
75 } |
35184354a502
Added method textBody() to IdentValueParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
737
diff
changeset
|
76 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
77 /// Finds the next "Identifier =". |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
78 /// Params: |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
79 /// ident = set to Identifier. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
80 /// bodyBegin = set to the beginning of the text body (whitespace skipped.) |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
81 /// Returns: true if found. |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
82 bool findNextIdent(ref string ident, ref char* bodyBegin) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
83 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
84 while (p < textEnd) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
85 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
86 skipWhitespace(); |
737
f88b5285b86b
Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
727
diff
changeset
|
87 if (p >= textEnd) |
f88b5285b86b
Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
727
diff
changeset
|
88 break; |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
89 auto idBegin = p; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
90 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
91 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
92 do // IdChar* |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
93 p++; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
94 while (p < textEnd && (isident(*p) || isUnicodeAlpha(p, textEnd))) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
95 auto idEnd = p; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
96 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
97 skipWhitespace(); |
737
f88b5285b86b
Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
727
diff
changeset
|
98 if (p < textEnd && *p == '=') |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
99 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
100 p++; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
101 skipWhitespace(); |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
102 bodyBegin = p; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
103 ident = makeString(idBegin, idEnd); |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
104 return true; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
105 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
106 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
107 skipLine(); |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
108 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
109 return false; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
110 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
111 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
112 void skipWhitespace() |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
113 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
114 while (p < textEnd && (isspace(*p) || *p == '\n')) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
115 p++; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
116 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
117 |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
118 void skipLine() |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
119 { |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
120 while (p < textEnd && *p != '\n') |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
121 p++; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
122 p++; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
123 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
124 } |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
125 |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
126 /// Returns a string slice ranging from begin to end. |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
127 char[] makeString(char* begin, char* end) |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
128 { |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
768
diff
changeset
|
129 assert(begin && end && begin <= end); |
727
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
130 return begin[0 .. end - begin]; |
c204b6a9e0ef
Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
131 } |