# HG changeset patch # User Aziz K?ksal # Date 1201989080 -3600 # Node ID c204b6a9e0ef9bdf02215c9d3ffb3a61aec521f0 # Parent 7917811f811635018afc37bbaa83ebb7cc5acb60 Added new module dil.doc.Parser. diff -r 7917811f8116 -r c204b6a9e0ef trunk/src/dil/doc/Macro.d --- a/trunk/src/dil/doc/Macro.d Sat Feb 02 19:58:09 2008 +0100 +++ b/trunk/src/dil/doc/Macro.d Sat Feb 02 22:51:20 2008 +0100 @@ -4,6 +4,7 @@ +/ module dil.doc.Macro; +import dil.doc.Parser; import dil.lexer.Funcs; import dil.Unicode; import common; @@ -35,6 +36,12 @@ table[macro_.name] = macro_; } + void insert(Macro[] macros) + { + foreach (macro_; macros) + insert(macro_); + } + Macro search(string name) { auto pmacro = name in table; @@ -51,79 +58,15 @@ struct MacroParser { - char* p; - char* textEnd; - Macro[] parse(string text) { - if (!text.length) - return null; - if (text[$-1] != '\0') - text ~= '\0'; - p = text.ptr; - textEnd = p + text.length; - - Macro[] macros; - - char* idBegin, idEnd, bodyBegin; - char* nextIdBegin, nextIdEnd, nextBodyBegin; - - // Init. - findNextMacroId(idBegin, idEnd, bodyBegin); - // Continue. - while (findNextMacroId(nextIdBegin, nextIdEnd, nextBodyBegin)) - { - macros ~= new Macro(makeString(idBegin, idEnd), makeString(bodyBegin, nextIdBegin)); - idBegin = nextIdBegin; - idEnd = nextIdEnd; - bodyBegin = nextBodyBegin; - } - // Add last macro. - macros ~= new Macro(makeString(idBegin, idEnd), makeString(bodyBegin, textEnd)); + IdentValueParser parser; + auto idvalues = parser.parse(text); + auto macros = new Macro[idvalues.length]; + foreach (i, idvalue; idvalues) + macros[i] = new Macro(idvalue.ident, idvalue.value); return macros; } - - bool findNextMacroId(ref char* ref_idBegin, ref char* ref_idEnd, ref char* ref_bodyBegin) - { - while (p < textEnd) - { - skipWhitespace(); - auto idBegin = p; - if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart - { - do // IdChar* - p++; - while (isident(*p) || isUnicodeAlpha(p, textEnd)) - auto idEnd = p; - - skipWhitespace(); - if (*p == '=') - { - p++; - skipWhitespace(); - ref_idBegin = idBegin; - ref_idEnd = idEnd; - ref_bodyBegin = p; - return true; - } - } - skipLine(); - } - return false; - } - - void skipWhitespace() - { - while (p < textEnd && (isspace(*p) || *p == '\n')) - p++; - } - - void skipLine() - { - while (p < textEnd && *p != '\n') - p++; - p++; - } } char[] makeString(char* begin, char* end) diff -r 7917811f8116 -r c204b6a9e0ef trunk/src/dil/doc/Parser.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/src/dil/doc/Parser.d Sat Feb 02 22:51:20 2008 +0100 @@ -0,0 +1,103 @@ +/++ + Author: Aziz Köksal + License: GPL3 ++/ +module dil.doc.Parser; + +import dil.lexer.Funcs; +import dil.Unicode; +import common; + +class IdentValue +{ + string ident; + string value; + this (string ident, string value) + { + this.ident = ident; + this.value = value; + } +} + +/// Parses text of the form: +/// ident = value +/// ident2 = value2 +/// more text +struct IdentValueParser +{ + char* p; + char* textEnd; + + IdentValue[] parse(string text) + { + if (!text.length) + return null; + + p = text.ptr; + textEnd = p + text.length; + + IdentValue[] idvalues; + + string ident, nextIdent; + char* bodyBegin, nextBodyBegin; + + // Init. + findNextIdent(ident, bodyBegin); + // Continue. + while (findNextIdent(nextIdent, nextBodyBegin)) + { + idvalues ~= new IdentValue(ident, makeString(bodyBegin, nextIdent.ptr)); + ident = nextIdent; + bodyBegin = nextBodyBegin; + } + // Add last ident value. + idvalues ~= new IdentValue(ident, makeString(bodyBegin, textEnd)); + return idvalues; + } + + bool findNextIdent(ref string ident, ref char* bodyBegin) + { + while (p < textEnd) + { + skipWhitespace(); + auto idBegin = p; + if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart + { + do // IdChar* + p++; + while (p < textEnd && (isident(*p) || isUnicodeAlpha(p, textEnd))) + auto idEnd = p; + + skipWhitespace(); + if (*p == '=') + { + p++; + skipWhitespace(); + bodyBegin = p; + ident = makeString(idBegin, idEnd); + return true; + } + } + skipLine(); + } + return false; + } + + void skipWhitespace() + { + while (p < textEnd && (isspace(*p) || *p == '\n')) + p++; + } + + void skipLine() + { + while (p < textEnd && *p != '\n') + p++; + p++; + } +} + +char[] makeString(char* begin, char* end) +{ + return begin[0 .. end - begin]; +}