1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
# this is a development utility to generate the files like
# bibxmlextinfo.g or gapdocdtdinfo.g
## not interesting enough to properly parse DTDs, we later give
## access to a proper validating parser instead
dtd := StringFile("bibxmlext.dtd");
NormalizeWhitespace(dtd);
# read entities
entities := rec();
pos := PositionSublist(dtd, "<!ENTITY % ");
while pos <> fail do
pos := pos+11;
tmp := Position(dtd, ' ', pos);
nam := dtd{[pos..tmp-1]};
cont := dtd{[tmp+2..Position(dtd, '"', tmp+1)-1]};
entities.(nam) := cont;
pos := PositionSublist(dtd, "<!ENTITY % ", pos);
od;
# substitute entities
while ForAny(RecNames(entities), a-> PositionSublist(dtd,
Concatenation("%",a)) <> fail) do
for a in RecNames(entities) do
dtd := SubstitutionSublist(dtd, Concatenation("%",a,";"), entities.(a));
od;
od;
# get elements
elements := rec();
pos := PositionSublist(dtd, "<!ELEMENT ");
while pos <> fail do
pos := pos+2;
tmp := Position(dtd, '>', pos);
if dtd[tmp-1] = ' ' then
tmp := tmp-1;
fi;
s := dtd{[pos..tmp-1]};
tmp := Position(s, ' ',8);
nam := s{[9..tmp-1]};
s := s{[tmp+1..Length(s)]};
s := SubstitutionSublist(s, "?", ", optional");
s := SubstitutionSublist(s, "*", ", repeated");
s := SubstitutionSublist(s, "(", "[");
s := SubstitutionSublist(s, ")", "]");
s := SubstitutionSublist(s, "|", ", or,");
s := SubstitutionSublist(s, "#", "");
if s{[1,Length(s)]} <> "[]" then
s := Concatenation("[",s,"]");
fi;
ss := "";
inword := false;
for x in s do
if (not inword and x in LETTERS) or (inword and not x in LETTERS) then
Add(ss,'"');
inword := not inword;
fi;
Add(ss, x);
od;
elements.(nam) := EvalString(ss);
pos := PositionSublist(dtd, "<!ELEMENT ", pos);
od;
# attributes
attributes := rec();
pos := PositionSublist(dtd, "<!ATTLIST");
while pos <> fail do
pos2 := Position(dtd, '>', pos);
a := dtd{[pos+10..pos2-1]};
pos := pos2;
tmp := WordsString(a);
attributes.(tmp[1]) := tmp{[2..Length(tmp)]};
pos := PositionSublist(dtd, "<!ATTLIST", pos);
od;
PrintTo("bibxmlextinfo.g", "Bibxmlext := \n", [elements, attributes], ";\n");
|