view src/kryshen/tema/FunctionDataParser.java @ 0:1d2fe61a3a62

Tema 0.1 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 16 May 2006 18:04:09 +0400
parents
children 548a93c24e55
line source
1 /*
2 * Copyright (C) 2006 Mikhail A. Kryshen
3 *
4 * $Id: FunctionDataParser.java,v 1.1.1.1 2006/05/16 14:04:09 mikhail Exp $
5 */
7 package kryshen.tema;
9 import java.io.*;
10 import java.util.*;
12 import static kryshen.tema.TemplateParser.Result;
13 import static kryshen.tema.TemplateParser.FunctionData;
14 import static kryshen.tema.TemplateParser.Terminator;
16 /**
17 * Parser for a function data.
18 *
19 * @author Mikhail A. Kryshen
20 */
21 public class FunctionDataParser {
22 static final char[] ARG_SEPARATORS = TemplateParser.LIST_SEPARATORS;
24 private TemplateParser tp;
25 private FunctionData fd;
26 private Reader in;
28 private boolean available = true;
29 private boolean first = true;
31 FunctionDataParser(TemplateParser tp, FunctionData fd,
32 Reader in) {
33 this.tp = tp;
34 this.fd = fd;
35 this.in = in;
36 }
38 private TemplateParser.Result parseData(Writer out, boolean argument)
39 throws IOException, TemplateException {
41 if (!available)
42 throw new TemplateException
43 ("Unexpected end of instruction data.");
45 TemplateParser.Result r;
47 if (fd == FunctionData.RECURSIVE) {
48 r = tp.parse(in, out, true,
49 argument ? ARG_SEPARATORS : null,
50 (argument || !first) ? ARG_SEPARATORS : null);
51 } else if (fd == FunctionData.NONRECURSIVE) {
52 r = tp.parse(in, out, false,
53 argument ? ARG_SEPARATORS : null,
54 (argument || !first) ? ARG_SEPARATORS : null);
55 } else if (fd == FunctionData.SUBFUNCTION && argument) {
56 /* Subfunction can pass a list of arguments */
57 StringWriter sw = new StringWriter();
58 tp.parseFunction(in, sw);
59 sw.close();
60 in = new StringReader(sw.toString());
61 fd = FunctionData.NONRECURSIVE;
62 r = parseData(out, true);
63 } else if (fd == FunctionData.SUBFUNCTION) {
64 r = new Result(null, tp.parseFunction(in, out), false);
65 } else {
66 throw new IllegalArgumentException("Invalid FunctiodData: " + fd);
67 }
69 if (r.terminator != TemplateParser.Terminator.SEPARATOR)
70 available = false; // No more function data available.
72 first = false;
73 return r;
74 }
76 public int parseData(Writer out) throws IOException, TemplateException {
77 return parseData(out, false).substitutions;
78 }
80 public String getData() throws IOException, TemplateException {
81 StringWriter sw = new StringWriter();
82 parseData(sw);
83 sw.close();
84 return sw.toString();
85 }
87 public int parseNextArg(Writer out) throws IOException, TemplateException {
88 TemplateParser.Result r = parseData(out, true);
90 // Ignore empty arguments.
91 while (r.empty && available) {
92 r = parseData(out, true);
93 }
95 return r.substitutions;
96 }
98 public String getNextArg() throws IOException, TemplateException {
99 StringWriter sw = new StringWriter();
100 parseNextArg(sw);
101 sw.close();
102 return sw.toString();
103 }
105 public List<String> getArgs() throws IOException, TemplateException {
106 List<String> l = new ArrayList<String>();
108 while (available) {
109 l.add(getNextArg());
110 }
112 return l;
113 }
115 public boolean hasMoreData() {
116 return available;
117 }
119 public TemplateParser getTemplateParser() {
120 return tp;
121 }
122 }