Initial import to Gerrit.
[profile/ivi/festival.git] / src / modules / java / cstr / festival / scheme / ReflectingSchemeReader.java
1
2
3  //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
4  //                                                                        \\
5  //                 Centre for Speech Technology Research                  \\
6  //                      University of Edinburgh, UK                       \\
7  //                        Copyright (c) 1996,1997                         \\
8  //                         All Rights Reserved.                           \\
9  //   Permission is hereby granted, free of charge, to use and distribute  \\
10  //   this software and its documentation without restriction, including   \\
11  //   without limitation the rights to use, copy, modify, merge, publish,  \\
12  //   distribute, sublicense, and/or sell copies of this work, and to      \\
13  //   permit persons to whom this work is furnished to do so, subject to   \\
14  //   the following conditions:                                            \\
15  //    1. The code must retain the above copyright notice, this list of    \\
16  //       conditions and the following disclaimer.                         \\
17  //    2. Any modifications must be clearly marked as such.                \\
18  //    3. Original authors' names are not deleted.                         \\
19  //    4. The authors' names are not used to endorse or promote products   \\
20  //       derived from this software without specific prior written        \\
21  //       permission.                                                      \\
22  //   THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        \\
23  //   DISCLAIM ALL WARRANTIES With REGARD TO THIS SOFTWARE, INCLUDING      \\
24  //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   \\
25  //   SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     \\
26  //   FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    \\
27  //   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   \\
28  //   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          \\
29  //   ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       \\
30  //   THIS SOFTWARE.                                                       \\
31  //                                                                        \\
32  //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
33  //                                                                        \\
34  //                  Author: Richard Caley (rjc@cstr.ed.ac.uk)             \\
35  //  --------------------------------------------------------------------  \\
36  //  A Scheme reader which returns the s expression as s atring suitable   \\
37  //  for passing on to a scheme interpreter, eg for sending to festival.   \\
38  //                                                                        \\
39  //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
40
41
42 package cstr.festival.scheme ;
43
44 import java.lang.*;
45 import java.util.*;
46 import java.awt.*;
47 import java.io.*;
48
49
50 public class ReflectingSchemeReader extends SchemeReader
51
52 {
53
54   public ReflectingSchemeReader(Reader r)
55     {
56       super(r);
57     }
58
59   private int parseSexp(StringBuffer b)
60                 throws IOException
61     {
62
63       tk.nextToken();
64
65       while (tk.ttype == StreamTokenizer.TT_EOL)
66         {
67           b.append(" ");
68           tk.nextToken();
69         }
70
71       if (tk.ttype == StreamTokenizer.TT_EOF)
72         return SE_EOF;
73
74       if (tk.ttype == '\'')
75         {
76           b.append("'");
77           int t = parseSexp(b);
78           return t;
79         }
80
81       if (tk.ttype == ')')
82         {
83           b.append(") ");
84           return SE_CB;
85         }
86
87       if (tk.ttype == '(')
88         {
89           b.append("(");
90           while (true)
91             {
92               int se_type = parseSexp(b);
93               
94               if (se_type == SE_EOF
95                   || se_type == SE_CB)
96                 return SE_LIST;
97             }
98         }
99
100       if (tk.ttype == StreamTokenizer.TT_WORD)
101         {
102           b.append(tk.sval);
103           b.append(" ");
104           return SE_ATOM;
105         }
106         
107       if (tk.ttype == '"')
108         {
109           b.append('"');
110           int s=b.length();
111           b.append(tk.sval);
112           for(int i=s; i<b.length(); i++)
113             if (b.charAt(i) == '"')
114               {
115                 b.insert(i++, '\\');
116                 b.setCharAt(i,'"');
117               }
118             else if (b.charAt(i) == '\n')
119               {
120                 b.insert(i++, '\\');
121                 b.setCharAt(i,'n');
122               }
123             else if (b.charAt(i) == '\r')
124               {
125                 b.insert(i++, '\\');
126                 b.setCharAt(i,'r');
127               }
128             else if (b.charAt(i) == '\t')
129               {
130                 b.insert(i++, '\\');
131                 b.setCharAt(i,'r');
132               }
133             else if (b.charAt(i) == '\\')
134               {
135                 b.insert(i++, '\\');
136                 b.setCharAt(i,'\\');
137               }
138           b.append("\" ");
139           return SE_ATOM;
140         }
141
142       if (tk.ttype >= ' ' && tk.ttype <= '\u00ff')
143         {
144           b.append((char)tk.ttype);
145           b.append(' ');
146           return SE_ATOM;
147         }
148
149       System.out.println("UNEXPECTED "+tk.ttype+" "+tk.sval);
150       return SE_EOF;
151     }
152
153   public Object nextExpr()
154                 throws IOException
155     {
156       return (Object)nextExprString();
157     }
158
159   public String nextExprString()
160                 throws IOException
161     {
162       StringBuffer exp = new StringBuffer(80);
163
164       int type = parseSexp(exp);
165
166       return type == SE_EOF ? (String)null : exp.toString();
167     }
168 }