annotate trunk/src/dil/doc/Macro.d @ 753:6efa938dbef7

Added documentation comments.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 13 Feb 2008 17:08:47 +0100
parents 8caf18892c1b
children 5e3ef1b2011c
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.
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
18 string text; /// 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 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
33 MacroTable parent;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
34 Macro[string] table;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
35
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
36 this(MacroTable parent = null)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
37 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
38 this.parent = parent;
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
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
41 /// Inserts the macro m into the table.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
42 /// Overwrites the current macro if one exists.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
43 void insert(Macro m)
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
44 {
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
45 table[m.name] = m;
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
46 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
47
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
48 /// 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
49 void insert(Macro[] macros)
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
50 {
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
51 foreach (m; macros)
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
52 insert(m);
727
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
53 }
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
54
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
55 /// 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
56 void insert(string name, string text)
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
57 {
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
58 insert(new Macro(name, 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
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
61 /// 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
62 void insert(string[] names, string[] texts)
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
63 {
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
64 assert(names.length == texts.length);
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
65 foreach (i, name; names)
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
66 insert(name, texts[i]);
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
67 }
f88b5285b86b Implemented DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
68
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
69 /// Searches for a macro.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
70 ///
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
71 /// 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
72 /// continues upwards in the table hierarchy.
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
73 /// 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
74 Macro search(string name)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
75 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
76 auto pmacro = name in table;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
77 if (pmacro)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
78 return *pmacro;
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
79 if (!isRoot())
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
80 return parent.search(name);
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
81 return null;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
82 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
83
753
6efa938dbef7 Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 751
diff changeset
84 /// 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
85 bool isRoot()
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
86 { return parent is null; }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
87 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
88
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
89 /// Parses a text with macro definitions.
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
90 struct MacroParser
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
91 {
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
92 Macro[] parse(string text)
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
93 {
727
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
94 IdentValueParser parser;
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
95 auto idvalues = parser.parse(text);
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
96 auto macros = new Macro[idvalues.length];
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
97 foreach (i, idvalue; idvalues)
c204b6a9e0ef Added new module dil.doc.Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 725
diff changeset
98 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
99 return macros;
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
100 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
101
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
102 /// Scans for a macro invocation. E.g.: $&#40;DDOC&#41;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
103 /// 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
104 /// 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
105 static char* scanMacro(char* p, char* textEnd)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
106 {
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
107 assert(*p == '$');
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
108 if (p+2 < textEnd && p[1] == '(')
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
109 {
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
110 p += 2;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
111 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
112 {
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
113 do // IdChar*
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
114 p++;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
115 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
116 MacroExpander.scanArguments(p, textEnd);
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
117 p != textEnd && p++; // Skip ')'.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
118 return p;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
119 }
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
120 }
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
121 return null;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
122 }
718
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
123 }
be887ada3e3e Added new module dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
124
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
125 /// 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
126 struct MacroExpander
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
127 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
128 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
129 InfoManager infoMan; /// Collects warning messages.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
130 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
131
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
132 /// Starts expanding the macros.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
133 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
134 InfoManager infoMan = null)
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
135 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
136 MacroExpander me;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
137 me.mtable = mtable;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
138 me.infoMan = infoMan;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
139 me.filePath = filePath;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
140 return me.expandMacros(text);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
141 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
142
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
143 /// Reports a warning message.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
144 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
145 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
146 msg = Format(msg, macroName);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
147 if (infoMan)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
148 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
149 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
150
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
151 /// 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
152 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
153 {
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
154 // if (depth == 0)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
155 // return text;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
156 // depth--;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
157 char[] result;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
158 char* p = text.ptr;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
159 char* textEnd = p + text.length;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
160 char* macroEnd = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
161 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
162 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
163 if (*p == '$' && p[1] == '(')
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 // Copy string between macros.
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
166 if (macroEnd != p)
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
167 result ~= makeString(macroEnd, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
168 p += 2;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
169 auto idBegin = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
170 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
171 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
172 do // IdChar*
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
173 p++;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
174 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
175 // Create macro name.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
176 auto macroName = makeString(idBegin, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
177 // Get arguments.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
178 auto macroArgs = scanArguments(p, textEnd);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
179 if (p == textEnd)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
180 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
181 warning(MSG.UnterminatedDDocMacro, macroName);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
182 result ~= "$(" ~ macroName ~ " ";
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
183 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
184 else
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
185 p++;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
186 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
187
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
188 auto macro_ = mtable.search(macroName);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
189 if (macro_)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
190 { // Ignore recursive macro if:
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
191 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
192 if (macro_.callLevel != 0 &&
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
193 (macroArgs.length == 0/+ || // Macro has no arguments.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
194 prevArg0 == macroArg0+/)) // macroArg0 equals previous arg0.
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
195 { continue; }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
196 macro_.callLevel++;
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
197 // 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
198 auto expandedText = expandArguments(macro_.text, macroArgs);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
199 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
200 macro_.callLevel--;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
201 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
202 else
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
203 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
204 warning(MSG.UndefinedDDocMacro, macroName);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
205 //result ~= makeString(macroName.ptr-2, macroEnd);
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
206 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
207 continue;
725
84291c0a9e13 Added member callLevel to class Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 724
diff changeset
208 }
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
209 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
210 p++;
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
211 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
212 if (macroEnd == text.ptr)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
213 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
214 if (macroEnd < textEnd)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
215 result ~= makeString(macroEnd, textEnd);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
216 return result;
723
5dd17d4568ce Wrote code for expandMacros().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
217 }
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
218
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
219 /// 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
220 /// Returns: [arg0, arg1, arg2 ...].
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
221 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
222 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
223 body
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
224 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
225 // 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
226 // "" or '' strings, comments, or tags."
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
227 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
228 char[][] args;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
229
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
230 // Skip leading spaces.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
231 while (p < textEnd && isspace(*p))
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
232 p++;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
233
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
234 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
235 char* argBegin = p;
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
236 MainLoop:
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
237 while (p < textEnd)
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
238 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
239 switch (*p)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
240 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
241 case ',':
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
242 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
243 break;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
244 // Add a new argument.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
245 args ~= makeString(argBegin, p);
751
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
246 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
247 {}
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
248 argBegin = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
249 continue;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
250 case '(':
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
251 level++;
731
ca7607226caa Added new module cmd.DDoc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 727
diff changeset
252 break;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
253 case ')':
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
254 if (--level == 0)
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
255 break MainLoop;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
256 break;
751
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
257 // 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
258 // case '"', '\'':
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
259 // auto c = *p;
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
260 // 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
261 // {}
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
262 // assert(*p == c || p == textEnd);
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
263 // if (p == textEnd)
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
264 // break MainLoop;
8caf18892c1b Improved DDocEmitter and fixed bugs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 747
diff changeset
265 // break;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
266 case '<':
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
267 p++;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
268 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
269 {
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
270 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
271 // Scan to closing "-->".
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
272 while (++p < textEnd)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
273 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
274 p += 2; // Point to '>'.
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
275 } // <tag ...> or </tag>
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
276 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
277 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
278 {}
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
279 else
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
280 continue MainLoop;
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
281 if (p == textEnd)
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
282 break MainLoop;
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
283 assert(*p == '>');
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
284 break;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
285 default:
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
286 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
287 p++;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
288 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
289 assert(*p == ')' && level == 0 || p == textEnd);
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
290 if (arg0Begin == p)
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
291 return null;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
292 // 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
293 auto arg0 = makeString(arg0Begin, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
294 // Add last argument.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
295 args ~= makeString(argBegin, p);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
296 return arg0 ~ args;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
297 }
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
298
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
299 /// Expands "$ +", "$ 0" - "$ 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
300 /// Params:
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
301 /// 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
302 /// args = the first element, args[0], is the whole argument string and
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
303 /// the following elements are slices into it.
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
304 /// 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
305 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
306 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
307 body
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
308 {
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
309 char[] result;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
310 char* p = text.ptr;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
311 char* textEnd = p + text.length;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
312 char* placeholderEnd = p;
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
313
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
314 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
315 {
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
316 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
317 {
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
318 // Copy string between argument placeholders.
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
319 if (placeholderEnd != p-1)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
320 result ~= makeString(placeholderEnd, p-1);
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
321 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
322
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
323 if (args.length == 0)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
324 continue;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
325
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
326 if (*p == '+')
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
327 { // $+ = $2 to $n
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
328 if (args.length > 2)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
329 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
330 }
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
331 else
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
332 { // 0 - 9
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
333 uint nthArg = *p - '0';
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
334 if (nthArg < args.length)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
335 result ~= args[nthArg];
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
336 }
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
337 }
744
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
338 p++;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
339 }
747
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
340 if (placeholderEnd == text.ptr)
00f872d949ea Added method scanCommentText() to DDocEmitter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 744
diff changeset
341 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
342 if (placeholderEnd < textEnd)
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
343 result ~= makeString(placeholderEnd, textEnd);
7173ece1b696 Wrapped some macro functions inside struct MacroExpander.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 741
diff changeset
344 return result;
724
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
345 }
0b8a6e876b6d Added scanArguments() and expandArguments() to dil.doc.Macro.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 723
diff changeset
346 }