source: to-imperative/trunk/java/refal/refal/plus/XML.rf @ 3964

Last change on this file since 3964 was 3964, checked in by yura, 12 years ago
  • Library extentions (after hdd errors).
File size: 2.8 KB
Line 
1$module "refal.plus.XML";
2
3$use Convert Stream StdIO;
4
5$func? IsBlank t.term = ;
6IsBlank \{ ' '; '\n'; '\r'; '\t'; };
7
8$func? IsBlankOrDelim t.delim t.term = ;
9IsBlankOrDelim t.delim t.term, t.term : \{ ' '; '\n'; '\r'; '\t'; t.delim; };
10
11$func GetDelims s.stream t.delim = e.expr; // no e.delim at the end
12GetDelims s.stream t.delim =
13  GO $iter {
14    <Get_While s.stream &IsBlank> :: e, {
15      <Scanc s.stream t.delim> : v = e.expr STOP;
16      <Get_Until s.stream &IsBlankOrDelim t.delim> : v.head = e.expr ' ' v.head GO;
17      e.expr STOP;
18    };
19  } :: e.expr s.flag, s.flag : STOP =
20  { e.expr : ' ' e.head = e.head; e.expr; };
21
22$func? GetXMLFormat s.stream = e.comment;
23
24GetXMLFormat s.stream,
25  <Scans s.stream '<?'> : '<?' =
26  <Get_Delims s.stream '?>'> : e.format '?>',
27  '<?' e.format '?>';
28
29
30$func? GetComment s.stream = e.comment;
31
32GetComment s.stream,
33  <Scans s.stream '<!--'> : '<!--' =
34  <Get_Delims s.stream '-->'> : e.comment '-->',
35  '<!--' e.comment '-->';
36
37$func? GetCDATA s.stream = e.cdata;
38
39GetCDATA s.stream,
40  <Scans s.stream '<![CDATA['> : '<![CDATA[' =
41  <Get_Delims s.stream ']]>'> : e.cdata ']]>',
42  '<![CDATA[' e.cdata ']]>';
43
44$func? GetTag s.stream = e.text (e.tag);
45
46GetTag s.stream =
47  <GetDelims s.stream '<'> :: e.text,
48  //<Get_Delim s.stream '<'> : e.text '<',
49  <Ungets s.stream '<'>, {
50    <GetXMLFormat s.stream> : '<?' e.format '?>',
51      e.text ((XML_FORMAT) e.format) <GetTag s.stream>;
52    <GetComment s.stream> : '<!--' e.comment '-->',
53      e.text ((COMMENT) e.comment) <GetTag s.stream>;
54    <GetCDATA s.stream> : '<![CDATA[' e.cdata ']]>',
55      e.text ((CDATA) e.cdata) <GetTag s.stream>;
56    e.text (<GetDelims s.stream '>'> '>');
57    //e.text (<Get_Delim s.stream '>'>);
58  };
59
60$func SplitAttrs e = e;
61SplitAttrs e.attrs,
62  () (e.attrs) $iter {
63    e.attrs : e.key '=' '"' e.value '"' e.tail =
64      { e.tail : ' ' e.tail1 = e.tail1; e.tail; } :: e.tail,     
65      (e.keyvalues (<ToWord e.key> <ToWord e.value> )) (e.tail);
66  } :: (e.keyvalues) (e.attrs),
67  e.attrs : /* empty */ =
68  e.keyvalues;
69
70$func SplitName e = (e) e;
71
72SplitName {
73  e.name ' ' e.attrs = (e.name) <SplitAttrs e.attrs>;
74  e.name = (e.name);
75};
76
77$func? ReadTag s.stream e.tag = t;
78
79ReadTag s.stream e.tag, e.tag : {
80  '<' e.name '/>' =
81    <SplitName e.name> :: (e.name) e.attrs,
82    (<ToWord e.name> ((ATTRIBUTES) e.attrs));
83  '<' e.name '>' =
84    <SplitName e.name> :: (e.name) e.attrs,
85    <GetTag s.stream> $iter e.text <ReadTag s.stream e.tag> <GetTag s.stream> :: e.text (e.tag),
86    e.tag : '</' e.name '>',
87    (<ToWord e.name> ((ATTRIBUTES) e.attrs) e.text);
88};
89
90$public $func? Read s = e;
91
92Read s.stream = <GetTag s.stream> : e.text (e.tag), e.text <ReadTag s.stream e.tag>;
93
94$func Main = e;
95
96Main =
97  <WriteLn <Read <Expr_Open '<Z>as<C/><B>b<!--<Z></Z>--></B><C>c</C></Z>'>>>;
Note: See TracBrowser for help on using the repository browser.