annotate trunk/src/dil/doc/Macro.d @ 786:3b34f6a95a27

Added and revised documenation comments.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 24 Feb 2008 02:41:11 +0100
parents 5e3ef1b2011c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
1 /++
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
2 Author: Aziz Köksal
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
3 License: GPL3
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
4 +/
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
5 module dil.doc.Macro;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
6
727
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
7 import dil.doc.Parser;
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
8 import dil.lexer.Funcs;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
9 import dil.Unicode;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
10 import dil.Information;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
11 import dil.Messages;
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
12 import common;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
13
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
14 /// The DDoc macro class.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
15 class Macro
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
16 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
17 string name; /// The name of the macro.
769
5e3ef1b2011c Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 753
diff changeset
18 string text; /// The substitution text.
725
84291c0a9e13 Added member callLevel to class Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 724
diff changeset
19 uint callLevel; /// Recursive call level.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
20 this (string name, string text)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
21 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
22 this.name = name;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
23 this.text = text;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
24 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
25 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
26
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
27 /// Maps macro names to Macro objects.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
28 ///
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
29 /// MacroTables can be chained so that they build a linear hierarchy.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
30 /// Macro definitions in the current table override the ones in the parent tables.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
31 class MacroTable
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
32 {
786
3b34f6a95a27 Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 769
diff changeset
33 /// The parent in the hierarchy. Or null if this is the root.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
34 MacroTable parent;
786
3b34f6a95a27 Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 769
diff changeset
35 Macro[string] table; /// The associative array that holds the macro definitions.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
36
786
3b34f6a95a27 Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 769
diff changeset
37 /// Constructs a MacroTable instance.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
38 this(MacroTable parent = null)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
39 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
40 this.parent = parent;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
41 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
42
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
43 /// Inserts the macro m into the table.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
44 /// Overwrites the current macro if one exists.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
45 void insert(Macro m)
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
46 {
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
47 table[m.name] = m;
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
48 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
49
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
50 /// Inserts an array of macros into the table.
727
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
51 void insert(Macro[] macros)
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
52 {
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
53 foreach (m; macros)
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
54 insert(m);
727
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
55 }
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
56
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
57 /// Creates a macro using name and text and inserts that into the table.
737
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
58 void insert(string name, string text)
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
59 {
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
60 insert(new Macro(name, text));
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
61 }
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
62
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
63 /// Creates a macro using name[n] and text[n] and inserts that into the table.
737
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
64 void insert(string[] names, string[] texts)
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
65 {
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
66 assert(names.length == texts.length);
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
67 foreach (i, name; names)
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
68 insert(name, texts[i]);
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
69 }
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
70
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
71 /// Searches for a macro.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
72 ///
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
73 /// If the macro isn't found in this table the search
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
74 /// continues upwards in the table hierarchy.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
75 /// Returns: the macro if found, or null if not.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
76 Macro search(string name)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
77 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
78 auto pmacro = name in table;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
79 if (pmacro)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
80 return *pmacro;
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
81 if (!isRoot())
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
82 return parent.search(name);
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
83 return null;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
84 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
85
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
86 /// Returns: true if this is the root of the hierarchy.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
87 bool isRoot()
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
88 { return parent is null; }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
89 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
90
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
91 /// Parses a text with macro definitions.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
92 struct MacroParser
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
93 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
94 Macro[] parse(string text)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
95 {
727
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
96 IdentValueParser parser;
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
97 auto idvalues = parser.parse(text);
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
98 auto macros = new Macro[idvalues.length];
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
99 foreach (i, idvalue; idvalues)
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
100 macros[i] = new Macro(idvalue.ident, idvalue.value);
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
101 return macros;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
102 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
103
769
5e3ef1b2011c Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 753
diff changeset
104 /// Scans for a macro invocation. E.g.: &#36;(DDOC)
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
105 /// Returns: a pointer set to one char past the closing parenthesis,
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
106 /// or null if this isn't a macro invocation.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
107 static char* scanMacro(char* p, char* textEnd)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
108 {
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
109 assert(*p == '$');
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
110 if (p+2 < textEnd && p[1] == '(')
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
111 {
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
112 p += 2;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
113 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
114 {
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
115 do // IdChar*
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
116 p++;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
117 while (p < textEnd && (isident(*p) || isUnicodeAlpha(p, textEnd)))
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
118 MacroExpander.scanArguments(p, textEnd);
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
119 p != textEnd && p++; // Skip ')'.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
120 return p;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
121 }
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
122 }
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
123 return null;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
124 }
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
125 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
126
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
127 /// Expands DDoc macros in a text.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
128 struct MacroExpander
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
129 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
130 MacroTable mtable; /// Used to look up macros.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
131 InfoManager infoMan; /// Collects warning messages.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
132 char[] filePath; /// Used in warning messages.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
133
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
134 /// Starts expanding the macros.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
135 static char[] expand(MacroTable mtable, char[] text, char[] filePath,
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
136 InfoManager infoMan = null)
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
137 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
138 MacroExpander me;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
139 me.mtable = mtable;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
140 me.infoMan = infoMan;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
141 me.filePath = filePath;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
142 return me.expandMacros(text);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
143 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
144
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
145 /// Reports a warning message.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
146 void warning(char[] msg, char[] macroName)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
147 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
148 msg = Format(msg, macroName);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
149 if (infoMan)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
150 infoMan ~= new Warning(new Location(filePath, 0), msg);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
151 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
152
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
153 /// Expands the macros from the table in the text.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
154 char[] expandMacros(char[] text, char[] prevArg0 = null/+, uint depth = 1000+/)
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
155 {
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
156 // if (depth == 0)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
157 // return text;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
158 // depth--;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
159 char[] result;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
160 char* p = text.ptr;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
161 char* textEnd = p + text.length;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
162 char* macroEnd = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
163 while (p+3 < textEnd) // minimum 4 chars: $(x)
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
164 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
165 if (*p == '$' && p[1] == '(')
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
166 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
167 // Copy string between macros.
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
168 if (macroEnd != p)
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
169 result ~= makeString(macroEnd, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
170 p += 2;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
171 auto idBegin = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
172 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
173 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
174 do // IdChar*
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
175 p++;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
176 while (p < textEnd && (isident(*p) || isUnicodeAlpha(p, textEnd)))
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
177 // Create macro name.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
178 auto macroName = makeString(idBegin, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
179 // Get arguments.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
180 auto macroArgs = scanArguments(p, textEnd);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
181 if (p == textEnd)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
182 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
183 warning(MSG.UnterminatedDDocMacro, macroName);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
184 result ~= "$(" ~ macroName ~ " ";
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
185 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
186 else
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
187 p++;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
188 macroEnd = p; // Point past ')'.
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
189
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
190 auto macro_ = mtable.search(macroName);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
191 if (macro_)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
192 { // Ignore recursive macro if:
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
193 auto macroArg0 = macroArgs.length ? macroArgs[0] : null;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
194 if (macro_.callLevel != 0 &&
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
195 (macroArgs.length == 0/+ || // Macro has no arguments.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
196 prevArg0 == macroArg0+/)) // macroArg0 equals previous arg0.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
197 { continue; }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
198 macro_.callLevel++;
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
199 // Expand the arguments in the macro text.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
200 auto expandedText = expandArguments(macro_.text, macroArgs);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
201 result ~= expandMacros(expandedText, macroArg0/+, depth+/);
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
202 macro_.callLevel--;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
203 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
204 else
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
205 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
206 warning(MSG.UndefinedDDocMacro, macroName);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
207 //result ~= makeString(macroName.ptr-2, macroEnd);
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
208 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
209 continue;
725
84291c0a9e13 Added member callLevel to class Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 724
diff changeset
210 }
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
211 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
212 p++;
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
213 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
214 if (macroEnd == text.ptr)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
215 return text; // No macros found. Return original text.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
216 if (macroEnd < textEnd)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
217 result ~= makeString(macroEnd, textEnd);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
218 return result;
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
219 }
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
220
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
221 /// Scans until the closing parenthesis is found. Sets p to one char past it.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
222 /// Returns: [arg0, arg1, arg2 ...].
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
223 static char[][] scanArguments(ref char* p, char* textEnd)
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
224 out(args) { assert(args.length != 1); }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
225 body
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
226 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
227 // D specs: "The argument text can contain nested parentheses,
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
228 // "" or '' strings, comments, or tags."
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
229 uint level = 1; // Nesting level of the parentheses.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
230 char[][] args;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
231
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
232 // Skip leading spaces.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
233 while (p < textEnd && isspace(*p))
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
234 p++;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
235
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
236 char* arg0Begin = p; // Whole argument list.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
237 char* argBegin = p;
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
238 MainLoop:
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
239 while (p < textEnd)
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
240 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
241 switch (*p)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
242 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
243 case ',':
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
244 if (level != 1) // Ignore comma if inside ().
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
245 break;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
246 // Add a new argument.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
247 args ~= makeString(argBegin, p);
751
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
248 while (++p < textEnd && isspace(*p)) // Skip spaces.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
249 {}
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
250 argBegin = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
251 continue;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
252 case '(':
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
253 level++;
731
ca7607226caa Added new module cmd.DDoc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 727
diff changeset
254 break;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
255 case ')':
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
256 if (--level == 0)
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
257 break MainLoop;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
258 break;
751
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
259 // Commented out: causes too many problems in the expansion pass.
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
260 // case '"', '\'':
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
261 // auto c = *p;
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
262 // while (++p < textEnd && *p != c) // Scan to next " or '.
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
263 // {}
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
264 // assert(*p == c || p == textEnd);
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
265 // if (p == textEnd)
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
266 // break MainLoop;
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
267 // break;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
268 case '<':
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
269 p++;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
270 if (p+2 < textEnd && *p == '!' && p[1] == '-' && p[2] == '-') // <!--
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
271 {
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
272 p += 2; // Point to 2nd '-'.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
273 // Scan to closing "-->".
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
274 while (++p < textEnd)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
275 if (p+2 < textEnd && *p == '-' && p[1] == '-' && p[2] == '>')
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
276 p += 2; // Point to '>'.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
277 } // <tag ...> or </tag>
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
278 else if (p < textEnd && (isalpha(*p) || *p == '/'))
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
279 while (++p < textEnd && *p != '>') // Skip to closing '>'.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
280 {}
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
281 else
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
282 continue MainLoop;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
283 if (p == textEnd)
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
284 break MainLoop;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
285 assert(*p == '>');
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
286 break;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
287 default:
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
288 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
289 p++;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
290 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
291 assert(*p == ')' && level == 0 || p == textEnd);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
292 if (arg0Begin == p)
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
293 return null;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
294 // arg0 spans the whole argument list.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
295 auto arg0 = makeString(arg0Begin, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
296 // Add last argument.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
297 args ~= makeString(argBegin, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
298 return arg0 ~ args;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
299 }
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
300
769
5e3ef1b2011c Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 753
diff changeset
301 /// Expands "&#36;+", "&#36;0" - "&#36;9" with args[n] in text.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
302 /// Params:
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
303 /// text = the text to scan for argument placeholders.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
304 /// args = the first element, args[0], is the whole argument string and
769
5e3ef1b2011c Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 753
diff changeset
305 /// the following elements are slices into it.$(BR)
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
306 /// The array is empty if there are no arguments.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
307 char[] expandArguments(char[] text, char[][] args)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
308 in { assert(args.length != 1, "zero or more than 1 args expected"); }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
309 body
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
310 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
311 char[] result;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
312 char* p = text.ptr;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
313 char* textEnd = p + text.length;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
314 char* placeholderEnd = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
315
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
316 while (p+1 < textEnd)
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
317 {
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
318 if (*p == '$' && (*++p == '+' || isdigit(*p)))
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
319 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
320 // Copy string between argument placeholders.
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
321 if (placeholderEnd != p-1)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
322 result ~= makeString(placeholderEnd, p-1);
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
323 placeholderEnd = p+1; // Set new placeholder end.
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
324
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
325 if (args.length == 0)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
326 continue;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
327
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
328 if (*p == '+')
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
329 { // $+ = $2 to $n
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
330 if (args.length > 2)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
331 result ~= makeString(args[2].ptr, args[0].ptr + args[0].length);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
332 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
333 else
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
334 { // 0 - 9
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
335 uint nthArg = *p - '0';
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
336 if (nthArg < args.length)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
337 result ~= args[nthArg];
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
338 }
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
339 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
340 p++;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
341 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
342 if (placeholderEnd == text.ptr)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
343 return text; // No placeholders found. Return original text.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
344 if (placeholderEnd < textEnd)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
345 result ~= makeString(placeholderEnd, textEnd);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
346 return result;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
347 }
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
348 }