comparison trunk/src/dil/doc/Parser.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
children f88b5285b86b
comparison
equal deleted inserted replaced
726:7917811f8116 727:c204b6a9e0ef
1 /++
2 Author: Aziz Köksal
3 License: GPL3
4 +/
5 module dil.doc.Parser;
6
7 import dil.lexer.Funcs;
8 import dil.Unicode;
9 import common;
10
11 class IdentValue
12 {
13 string ident;
14 string value;
15 this (string ident, string value)
16 {
17 this.ident = ident;
18 this.value = value;
19 }
20 }
21
22 /// Parses text of the form:
23 /// ident = value
24 /// ident2 = value2
25 /// more text
26 struct IdentValueParser
27 {
28 char* p;
29 char* textEnd;
30
31 IdentValue[] parse(string text)
32 {
33 if (!text.length)
34 return null;
35
36 p = text.ptr;
37 textEnd = p + text.length;
38
39 IdentValue[] idvalues;
40
41 string ident, nextIdent;
42 char* bodyBegin, nextBodyBegin;
43
44 // Init.
45 findNextIdent(ident, bodyBegin);
46 // Continue.
47 while (findNextIdent(nextIdent, nextBodyBegin))
48 {
49 idvalues ~= new IdentValue(ident, makeString(bodyBegin, nextIdent.ptr));
50 ident = nextIdent;
51 bodyBegin = nextBodyBegin;
52 }
53 // Add last ident value.
54 idvalues ~= new IdentValue(ident, makeString(bodyBegin, textEnd));
55 return idvalues;
56 }
57
58 bool findNextIdent(ref string ident, ref char* bodyBegin)
59 {
60 while (p < textEnd)
61 {
62 skipWhitespace();
63 auto idBegin = p;
64 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
65 {
66 do // IdChar*
67 p++;
68 while (p < textEnd && (isident(*p) || isUnicodeAlpha(p, textEnd)))
69 auto idEnd = p;
70
71 skipWhitespace();
72 if (*p == '=')
73 {
74 p++;
75 skipWhitespace();
76 bodyBegin = p;
77 ident = makeString(idBegin, idEnd);
78 return true;
79 }
80 }
81 skipLine();
82 }
83 return false;
84 }
85
86 void skipWhitespace()
87 {
88 while (p < textEnd && (isspace(*p) || *p == '\n'))
89 p++;
90 }
91
92 void skipLine()
93 {
94 while (p < textEnd && *p != '\n')
95 p++;
96 p++;
97 }
98 }
99
100 char[] makeString(char* begin, char* end)
101 {
102 return begin[0 .. end - begin];
103 }