Mercurial > projects > dil
annotate trunk/src/cmd/Generate.d @ 759:9c47f377ca0b
Revised module cmd.Generate.
Added class TagMapLoader.
Fixed StringExpression.getString() and related code in the Parser.
Added options 'xml_map' and 'html_map' to config.d
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Fri, 15 Feb 2008 02:07:53 +0100 |
parents | e4b60543c5e8 |
children | 307905dadf5d |
rev | line source |
---|---|
363 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module cmd.Generate; | |
578
c769bc239006
Moved Parser.d to new package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
6 |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
7 import dil.ast.DefaultVisitor; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
8 import dil.ast.Node, |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
9 dil.ast.Declaration, |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
10 dil.ast.Statement, |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
11 dil.ast.Expression, |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
12 dil.ast.Types; |
596
39fac5531b85
Moved dil.Token to dil.lexer.Token.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
582
diff
changeset
|
13 import dil.lexer.Lexer; |
578
c769bc239006
Moved Parser.d to new package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
14 import dil.parser.Parser; |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
752
diff
changeset
|
15 import dil.SourceText; |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
16 import dil.Information; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
17 import dil.SettingsLoader; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
18 import dil.Settings; |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
752
diff
changeset
|
19 import common; |
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
752
diff
changeset
|
20 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
21 import tango.io.GrowBuffer; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
22 import tango.io.Print; |
363 | 23 |
582
724603deed37
Added comments and renamed a few variables.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
581
diff
changeset
|
24 /// Options for the generate command. |
363 | 25 enum DocOption |
26 { | |
392
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
27 Empty, |
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
28 Tokens = 1, |
363 | 29 Syntax = 1<<1, |
392
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
30 HTML = 1<<2, |
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
31 XML = 1<<3 |
363 | 32 } |
33 | |
582
724603deed37
Added comments and renamed a few variables.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
581
diff
changeset
|
34 /// Executes the command. |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
35 void execute(string filePath, DocOption options, InfoManager infoMan) |
364 | 36 { |
392
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
37 assert(options != DocOption.Empty); |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
38 auto mapFilePath = options & DocOption.HTML ? GlobalSettings.htmlMapFile |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
39 : GlobalSettings.xmlMapFile; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
40 auto map = TagMapLoader(infoMan).load(mapFilePath); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
41 auto tags = new TagMap(map); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
42 |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
43 if (infoMan.hasInfo) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
44 return; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
45 |
364 | 46 if (options & DocOption.Syntax) |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
47 syntaxToDoc(filePath, tags, Stdout, options); |
364 | 48 else |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
49 tokensToDoc(filePath, tags, Stdout, options); |
364 | 50 } |
51 | |
582
724603deed37
Added comments and renamed a few variables.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
581
diff
changeset
|
52 /// Escapes the characters '<', '>' and '&' with named character entities. |
363 | 53 char[] xml_escape(char[] text) |
54 { | |
55 char[] result; | |
56 foreach(c; text) | |
57 switch(c) | |
58 { | |
59 case '<': result ~= "<"; break; | |
60 case '>': result ~= ">"; break; | |
61 case '&': result ~= "&"; break; | |
62 default: result ~= c; | |
63 } | |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
64 if (result.length != text.length) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
65 return result; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
66 // Nothing escaped. Return original text. |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
67 delete result; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
68 return text; |
363 | 69 } |
70 | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
71 class TagMap |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
72 { |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
73 string[string] table; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
74 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
75 this(string[string] table) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
76 { |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
77 this.table = table; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
78 Identifier = this["Identifier", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
79 String = this["String", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
80 Char = this["Char", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
81 Number = this["Number", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
82 Keyword = this["Keyword", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
83 LineC = this["LineC", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
84 BlockC = this["BlockC", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
85 NestedC = this["NestedC", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
86 Shebang = this["Shebang", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
87 HLine = this["HLine", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
88 Filespec = this["Filespec", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
89 Illegal = this["Illegal", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
90 Newline = this["Newline", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
91 SpecialToken = this["SpecialToken", "{0}"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
92 Declaration = this["Declaration", "d"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
93 Statement = this["Statement", "s"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
94 Expression = this["Expression", "e"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
95 Type = this["Type", "t"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
96 Other = this["Other", "o"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
97 EOF = this["EOF", ""]; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
98 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
99 |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
100 string opIndex(string str, string fallback = "") |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
101 { |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
102 auto p = str in table; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
103 if (p) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
104 return *p; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
105 return fallback; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
106 } |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
107 |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
108 string Identifier, String, Char, Number, Keyword, LineC, BlockC, |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
109 NestedC, Shebang, HLine, Filespec, Illegal, Newline, SpecialToken, |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
110 Declaration, Statement, Expression, Type, Other, EOF; |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
111 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
112 /// Returns the tag for the category 'nc'. |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
113 string getTag(NodeCategory nc) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
114 { |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
115 string tag; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
116 switch (nc) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
117 { alias NodeCategory NC; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
118 case NC.Declaration: tag = Declaration; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
119 case NC.Statement: tag = Statement; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
120 case NC.Expression: tag = Expression; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
121 case NC.Type: tag = Type; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
122 case NC.Other: tag = Other; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
123 default: assert(0); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
124 } |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
125 return tag; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
126 } |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
127 } |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
128 |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
129 /// Find the last occurrence of object in subject. |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
130 /// Returns: the index if found, or -1 if not. |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
131 int rfind(char[] subject, char object) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
132 { |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
133 foreach_reverse(i, c; subject) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
134 if (c == object) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
135 return i; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
136 return -1; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
137 } |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
138 |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
139 /// Returns: the short class name of an instance descending from Node. |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
140 char[] getShortClassName(Node node) |
363 | 141 { |
142 static char[][] name_table; | |
143 if (name_table is null) | |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
144 name_table = new char[][NodeKind.max+1]; // Create a new table. |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
145 // Look up in table. |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
146 char[] name = name_table[node.kind]; |
363 | 147 if (name !is null) |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
148 return name; // Return cached name. |
363 | 149 |
582
724603deed37
Added comments and renamed a few variables.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
581
diff
changeset
|
150 name = node.classinfo.name; // Get the fully qualified name of the class. |
724603deed37
Added comments and renamed a few variables.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
581
diff
changeset
|
151 name = name[rfind(name, '.')+1 .. $]; // Remove package and module name. |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
152 |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
153 uint suffixLength; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
154 switch (node.category) |
363 | 155 { |
156 alias NodeCategory NC; | |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
157 case NC.Declaration: |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
158 suffixLength = "Declaration".length; |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
159 break; |
363 | 160 case NC.Statement: |
682
7541c64fc423
Renamed FunctionBody, CatchBody and FinallyBody.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
681
diff
changeset
|
161 suffixLength = "Statement".length; |
363 | 162 break; |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
163 case NC.Expression: |
695
170ddcdc9711
Renamed VoidInitializer, ArrayInitializer and StructInitializer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
682
diff
changeset
|
164 suffixLength = "Expression".length; |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
165 break; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
166 case NC.Type: |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
167 suffixLength = "Type".length; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
168 break; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
169 case NC.Other: |
640
05645f5613c1
Added an error message.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
619
diff
changeset
|
170 break; |
363 | 171 default: |
640
05645f5613c1
Added an error message.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
619
diff
changeset
|
172 assert(0); |
363 | 173 } |
174 // Remove common suffix. | |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
175 name = name[0 .. $ - suffixLength]; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
176 // Store the name in the table. |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
177 name_table[node.kind] = name; |
363 | 178 return name; |
179 } | |
180 | |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
181 /// Extended token structure. |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
182 struct TokenEx |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
183 { |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
184 Token* token; /// The lexer token. |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
185 Node[] beginNodes; /// beginNodes[n].begin == token |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
186 Node[] endNodes; /// endNodes[n].end == token |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
187 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
188 |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
189 /// Builds an array of TokenEx items. |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
190 class TokenExBuilder : DefaultVisitor |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
191 { |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
192 private TokenEx*[Token*] tokenTable; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
193 |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
194 TokenEx[] build(Node root, Token* first) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
195 { |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
196 Token* token = first; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
197 |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
198 uint count; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
199 while (token) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
200 { |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
201 count++; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
202 token = token.next; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
203 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
204 |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
205 auto toks = new TokenEx[count]; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
206 token = first; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
207 foreach (ref tokEx; toks) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
208 { |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
209 tokEx.token = token; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
210 if (!token.isWhitespace) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
211 tokenTable[token] = &tokEx; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
212 token = token.next; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
213 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
214 |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
215 super.visitN(root); |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
216 tokenTable = null; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
217 return toks; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
218 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
219 |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
220 TokenEx* getTokenEx()(Token* t) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
221 { |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
222 auto p = t in tokenTable; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
223 assert(p, t.srcText~" is not in tokenTable"); |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
224 return *p; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
225 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
226 |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
227 void push()(Node n) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
228 { |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
229 auto begin = n.begin; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
230 if (begin) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
231 { assert(n.end); |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
232 auto txbegin = getTokenEx(begin); |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
233 auto txend = getTokenEx(n.end); |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
234 txbegin.beginNodes ~= n; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
235 txend.endNodes ~= n; |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
236 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
237 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
238 |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
755
diff
changeset
|
239 // Override dispatch function. |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
755
diff
changeset
|
240 override Node dispatch(Node n) |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
755
diff
changeset
|
241 { |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
755
diff
changeset
|
242 push(n); |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
755
diff
changeset
|
243 return super.dispatch(n); |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
755
diff
changeset
|
244 } |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
245 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
246 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
247 void printErrors(Lexer lx, TagMap tags, Print!(char) print) |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
248 { |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
249 foreach (e; lx.errors) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
250 print.format(tags["LexerError"], e.filePath, e.loc, e.col, xml_escape(e.getMsg)); |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
251 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
252 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
253 void printErrors(Parser parser, TagMap tags, Print!(char) print) |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
254 { |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
255 foreach (e; parser.errors) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
256 print.format(tags["ParserError"], e.filePath, e.loc, e.col, xml_escape(e.getMsg)); |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
257 } |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
258 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
259 void syntaxToDoc(string filePath, TagMap tags, Print!(char) print, DocOption options) |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
260 { |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
752
diff
changeset
|
261 auto parser = new Parser(new SourceText(filePath, true)); |
368 | 262 auto root = parser.start(); |
619
933cd8d24467
Renamed Parser.lx to Parser.lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
598
diff
changeset
|
263 auto lx = parser.lexer; |
363 | 264 |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
265 auto builder = new TokenExBuilder(); |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
266 auto tokenExList = builder.build(root, lx.firstToken()); |
363 | 267 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
268 print(tags["DocHead"]); |
363 | 269 if (lx.errors.length || parser.errors.length) |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
270 { // Output error messages. |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
271 print(tags["CompBegin"]); |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
272 printErrors(lx, tags, print); |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
273 printErrors(parser, tags, print); |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
274 print(tags["CompEnd"]); |
363 | 275 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
276 print(tags["SourceBegin"]); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
277 |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
278 auto tagNodeBegin = tags["NodeBegin"]; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
279 auto tagNodeEnd = tags["NodeEnd"]; |
363 | 280 |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
281 // Iterate over list of tokens. |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
282 foreach (ref tokenEx; tokenExList) |
363 | 283 { |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
284 auto token = tokenEx.token; |
363 | 285 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
286 token.ws && print(token.wsChars); // Print preceding whitespace. |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
287 // <node> |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
288 foreach (node; tokenEx.beginNodes) |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
289 print.format(tagNodeBegin, tags.getTag(node.category), getShortClassName(node)); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
290 // Token text. |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
291 printToken(token, tags, print); |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
292 // </node> |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
293 if (options & DocOption.HTML) |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
294 foreach_reverse (node; tokenEx.endNodes) |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
295 print(tagNodeEnd); |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
296 else |
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
297 foreach_reverse (node; tokenEx.endNodes) |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
298 print.format(tagNodeEnd, tags.getTag(node.category)); |
363 | 299 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
300 print(tags["SourceEnd"]); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
301 print(tags["DocEnd"]); |
363 | 302 } |
303 | |
582
724603deed37
Added comments and renamed a few variables.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
581
diff
changeset
|
304 /// Prints all tokens of a source file using the buffer print. |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
305 void tokensToDoc(string filePath, TagMap tags, Print!(char) print, DocOption options) |
363 | 306 { |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
752
diff
changeset
|
307 auto lx = new Lexer(new SourceText(filePath, true)); |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
308 lx.scanAll(); |
363 | 309 |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
310 print(tags["DocHead"]); |
363 | 311 if (lx.errors.length) |
312 { | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
313 print(tags["CompBegin"]); |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
314 printErrors(lx, tags, print); |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
315 print(tags["CompEnd"]); |
363 | 316 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
317 print(tags["SourceBegin"]); |
363 | 318 |
319 // Traverse linked list and print tokens. | |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
320 auto token = lx.firstToken(); |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
321 while (token) |
363 | 322 { |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
323 token.ws && print(token.wsChars); // Print preceding whitespace. |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
324 printToken(token, tags, print); |
752
51e9dfe27f20
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
709
diff
changeset
|
325 token = token.next; |
363 | 326 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
327 print(tags["SourceEnd"]); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
328 print(tags["DocEnd"]); |
363 | 329 } |
330 | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
331 void printToken(Token* token, string[] tags, Print!(char) print) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
332 {} |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
333 |
582
724603deed37
Added comments and renamed a few variables.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
581
diff
changeset
|
334 /// Prints a token with tags using the buffer print. |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
335 void printToken(Token* token, TagMap tags, Print!(char) print) |
363 | 336 { |
679
ff6971637f88
Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
668
diff
changeset
|
337 switch(token.kind) |
363 | 338 { |
339 case TOK.Identifier: | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
340 print.format(tags.Identifier, token.srcText); |
363 | 341 break; |
342 case TOK.Comment: | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
343 string formatStr; |
363 | 344 switch (token.start[1]) |
345 { | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
346 case '/': formatStr = tags.LineC; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
347 case '*': formatStr = tags.BlockC; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
348 case '+': formatStr = tags.NestedC; break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
349 default: assert(0); |
363 | 350 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
351 print.format(formatStr, xml_escape(token.srcText)); |
363 | 352 break; |
353 case TOK.String: | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
354 print.format(tags.String, xml_escape(token.srcText)); |
363 | 355 break; |
507
996041463028
Removed TOK.WCharLiteral and TOK.DCharLiteral.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
356 case TOK.CharLiteral: |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
357 print.format(tags.Char, xml_escape(token.srcText)); |
363 | 358 break; |
359 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, | |
360 TOK.Float32, TOK.Float64, TOK.Float80, | |
361 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
362 print.format(tags.Number, token.srcText); |
363 | 363 break; |
364 case TOK.Shebang: | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
365 print.format(tags.Shebang, xml_escape(token.srcText)); |
363 | 366 break; |
367 case TOK.HashLine: | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
368 auto formatStr = tags.HLine; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
369 // The text to be inserted into formatStr. |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
370 auto buffer = new GrowBuffer; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
371 auto print2 = new Print!(char)(Format, buffer); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
372 |
363 | 373 void printWS(char* start, char* end) |
374 { | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
375 start != end && print2(start[0 .. end - start]); |
363 | 376 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
377 |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
378 auto num = token.tokLineNum; |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
379 if (num is null) |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
380 { // Malformed #line |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
381 print.format(formatStr, token.srcText); |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
382 break; |
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
383 } |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
384 |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
385 // Print whitespace between #line and number. |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
386 auto ptr = token.start; |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
387 printWS(ptr, num.start); // Prints "#line" as well. |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
388 printToken(num, tags, print2); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
389 |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
390 if (auto filespec = token.tokLineFilespec) |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
391 { // Print whitespace between number and filespec. |
363 | 392 printWS(num.end, filespec.start); |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
393 print2.format(tags.Filespec, xml_escape(filespec.srcText)); |
363 | 394 ptr = filespec.end; |
395 } | |
396 else | |
397 ptr = num.end; | |
398 // Print remaining whitespace | |
399 printWS(ptr, token.end); | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
400 // Finally print the whole token. |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
401 print.format(formatStr, cast(char[])buffer.slice()); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
402 break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
403 case TOK.Illegal: |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
404 print.format(tags.Illegal, token.srcText()); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
405 break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
406 case TOK.Newline: |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
407 print.format(tags.Newline, token.srcText()); |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
408 break; |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
409 case TOK.EOF: |
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
410 print(tags.EOF); |
363 | 411 break; |
412 default: | |
413 if (token.isKeyword()) | |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
414 print.format(tags.Keyword, token.srcText); |
363 | 415 else if (token.isSpecialToken) |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
416 print.format(tags.SpecialToken, token.srcText); |
363 | 417 else |
759
9c47f377ca0b
Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
418 print(tags[token.srcText]); |
363 | 419 } |
420 } |