comparison trunk/src/dil/doc/Macro.d @ 727:c204b6a9e0ef

Added new module dil.doc.Parser.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 02 Feb 2008 22:51:20 +0100
parents 84291c0a9e13
children ca7607226caa
comparison
equal deleted inserted replaced
726:7917811f8116 727:c204b6a9e0ef
2 Author: Aziz Köksal 2 Author: Aziz Köksal
3 License: GPL3 3 License: GPL3
4 +/ 4 +/
5 module dil.doc.Macro; 5 module dil.doc.Macro;
6 6
7 import dil.doc.Parser;
7 import dil.lexer.Funcs; 8 import dil.lexer.Funcs;
8 import dil.Unicode; 9 import dil.Unicode;
9 import common; 10 import common;
10 11
11 class Macro 12 class Macro
31 } 32 }
32 33
33 void insert(Macro macro_) 34 void insert(Macro macro_)
34 { 35 {
35 table[macro_.name] = macro_; 36 table[macro_.name] = macro_;
37 }
38
39 void insert(Macro[] macros)
40 {
41 foreach (macro_; macros)
42 insert(macro_);
36 } 43 }
37 44
38 Macro search(string name) 45 Macro search(string name)
39 { 46 {
40 auto pmacro = name in table; 47 auto pmacro = name in table;
49 { return parent is null; } 56 { return parent is null; }
50 } 57 }
51 58
52 struct MacroParser 59 struct MacroParser
53 { 60 {
54 char* p;
55 char* textEnd;
56
57 Macro[] parse(string text) 61 Macro[] parse(string text)
58 { 62 {
59 if (!text.length) 63 IdentValueParser parser;
60 return null; 64 auto idvalues = parser.parse(text);
61 if (text[$-1] != '\0') 65 auto macros = new Macro[idvalues.length];
62 text ~= '\0'; 66 foreach (i, idvalue; idvalues)
63 p = text.ptr; 67 macros[i] = new Macro(idvalue.ident, idvalue.value);
64 textEnd = p + text.length;
65
66 Macro[] macros;
67
68 char* idBegin, idEnd, bodyBegin;
69 char* nextIdBegin, nextIdEnd, nextBodyBegin;
70
71 // Init.
72 findNextMacroId(idBegin, idEnd, bodyBegin);
73 // Continue.
74 while (findNextMacroId(nextIdBegin, nextIdEnd, nextBodyBegin))
75 {
76 macros ~= new Macro(makeString(idBegin, idEnd), makeString(bodyBegin, nextIdBegin));
77 idBegin = nextIdBegin;
78 idEnd = nextIdEnd;
79 bodyBegin = nextBodyBegin;
80 }
81 // Add last macro.
82 macros ~= new Macro(makeString(idBegin, idEnd), makeString(bodyBegin, textEnd));
83 return macros; 68 return macros;
84 }
85
86 bool findNextMacroId(ref char* ref_idBegin, ref char* ref_idEnd, ref char* ref_bodyBegin)
87 {
88 while (p < textEnd)
89 {
90 skipWhitespace();
91 auto idBegin = p;
92 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
93 {
94 do // IdChar*
95 p++;
96 while (isident(*p) || isUnicodeAlpha(p, textEnd))
97 auto idEnd = p;
98
99 skipWhitespace();
100 if (*p == '=')
101 {
102 p++;
103 skipWhitespace();
104 ref_idBegin = idBegin;
105 ref_idEnd = idEnd;
106 ref_bodyBegin = p;
107 return true;
108 }
109 }
110 skipLine();
111 }
112 return false;
113 }
114
115 void skipWhitespace()
116 {
117 while (p < textEnd && (isspace(*p) || *p == '\n'))
118 p++;
119 }
120
121 void skipLine()
122 {
123 while (p < textEnd && *p != '\n')
124 p++;
125 p++;
126 } 69 }
127 } 70 }
128 71
129 char[] makeString(char* begin, char* end) 72 char[] makeString(char* begin, char* end)
130 { 73 {