|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Parser.java | 100% | 86.2% | 60% | 87% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Server - Virtual Reality Object Oriented MUD Server
|
|
4 |
Copyright (C) 2001 - 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.server.vrmoopl;
|
|
28 |
|
|
29 |
import java.io.BufferedReader;
|
|
30 |
import java.io.File;
|
|
31 |
import java.io.FileReader;
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.InputStream;
|
|
34 |
import java.io.InputStreamReader;
|
|
35 |
import java.io.Reader;
|
|
36 |
|
|
37 |
import org.vrmoo.common.exception.VRMooParseException;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* This class provides a simple interface for parsing a vrmoopl program.
|
|
41 |
*
|
|
42 |
* @author Jeff Weston
|
|
43 |
*/
|
|
44 |
public class Parser |
|
45 |
{ |
|
46 |
/**
|
|
47 |
* The presence of this text in the input indicates the beginning of a
|
|
48 |
* function.
|
|
49 |
*/
|
|
50 |
private static final String BEGIN_FUNCTION = "begin function "; |
|
51 |
|
|
52 |
/**
|
|
53 |
* The presence of this text in the input indicates the end of a function.
|
|
54 |
*/
|
|
55 |
private static final String END_FUNCTION = "end function"; |
|
56 |
|
|
57 |
/**
|
|
58 |
* The program we are creating from the input we are parsing.
|
|
59 |
*/
|
|
60 |
private Program program;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* The function we are currently processing. Set to <code>null</code> if we
|
|
64 |
* are not currently in a function.
|
|
65 |
*/
|
|
66 |
private Function curFunction;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Parse a vrmoopl program from a <code>File</code>.
|
|
70 |
*
|
|
71 |
* @param file the file containing the vrmoopl program
|
|
72 |
*
|
|
73 |
* @throws IOException for any IO errors that occur
|
|
74 |
* @throws VRMooParseException for any parsing errors that occur
|
|
75 |
*/
|
|
76 | 0 |
public void parse( File file ) throws IOException, VRMooParseException |
77 |
{ |
|
78 | 0 |
parse( new FileReader( file ), file.getName( ) );
|
79 |
} |
|
80 |
|
|
81 |
/**
|
|
82 |
* Parse a vrmoopl program from an <code>InputStream</code>.
|
|
83 |
*
|
|
84 |
* @param input the vrmoopl program
|
|
85 |
* @param name the name of the program
|
|
86 |
*
|
|
87 |
* @throws IOException for any IO errors that occur
|
|
88 |
* @throws VRMooParseException for any parsing errors that occur
|
|
89 |
*/
|
|
90 | 0 |
public void parse( InputStream input, String name ) |
91 |
throws IOException, VRMooParseException
|
|
92 |
{ |
|
93 | 0 |
try
|
94 |
{ |
|
95 | 0 |
parse( new InputStreamReader( input ), name );
|
96 |
} |
|
97 |
finally
|
|
98 |
{ |
|
99 | 0 |
input.close( ); |
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* Parse a vrmoopl program from a <code>Reader</code>.
|
|
105 |
*
|
|
106 |
* @param reader the vrmoopl program
|
|
107 |
* @param name the name of the program
|
|
108 |
*
|
|
109 |
* @throws IOException for any IO errors that occur
|
|
110 |
* @throws VRMooParseException for any parsing errors that occur
|
|
111 |
*/
|
|
112 | 4 |
public void parse( Reader reader, String name ) |
113 |
throws IOException, VRMooParseException
|
|
114 |
{ |
|
115 | 4 |
try
|
116 |
{ |
|
117 | 4 |
program = new Program( name );
|
118 | 4 |
curFunction = null;
|
119 | 4 |
BufferedReader bufReader = new BufferedReader( reader );
|
120 |
|
|
121 | 4 |
String line = bufReader.readLine( ); |
122 | 4 |
while ( line != null ) |
123 |
{ |
|
124 | 14 |
processLine( line ); |
125 | 12 |
line = bufReader.readLine( ); |
126 |
} |
|
127 |
|
|
128 | 2 |
if ( curFunction != null ) |
129 |
{ |
|
130 | 1 |
throw new VRMooParseException( "Unexpected end of input." ); |
131 |
} |
|
132 |
} |
|
133 |
finally
|
|
134 |
{ |
|
135 | 4 |
reader.close( ); |
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
/**
|
|
140 |
* Get the parsed program.
|
|
141 |
*
|
|
142 |
* @return the parsed program
|
|
143 |
*/
|
|
144 | 1 |
public Program getProgram( )
|
145 |
{ |
|
146 | 1 |
return program;
|
147 |
} |
|
148 |
|
|
149 |
/**
|
|
150 |
* Parse one line of the input.
|
|
151 |
*
|
|
152 |
* @param line the line of input to parse
|
|
153 |
*
|
|
154 |
* @throws VRMooParseException for any parsing errors that occur
|
|
155 |
*/
|
|
156 | 14 |
private void processLine( String line ) throws VRMooParseException |
157 |
{ |
|
158 | 14 |
line = line.trim( ); |
159 |
|
|
160 |
// don't do anything if the line has no text
|
|
161 | 14 |
if ( line.length( ) == 0 )
|
162 |
{ |
|
163 | 1 |
return;
|
164 |
} |
|
165 |
|
|
166 | 13 |
if ( curFunction == null ) |
167 |
{ |
|
168 | 5 |
if ( line.indexOf( BEGIN_FUNCTION ) == 0 )
|
169 |
{ |
|
170 | 4 |
String functionName = |
171 |
line.substring( BEGIN_FUNCTION.length( ) ).trim( ); |
|
172 | 4 |
curFunction = new Function( functionName );
|
173 |
} |
|
174 |
else
|
|
175 |
{ |
|
176 | 1 |
throw new VRMooParseException( |
177 |
"Expecting 'begin function', but got: " + line );
|
|
178 |
} |
|
179 |
} |
|
180 |
else
|
|
181 |
{ |
|
182 | 8 |
if ( line.equals( END_FUNCTION ) )
|
183 |
{ |
|
184 | 2 |
program.addFunction( curFunction ); |
185 | 2 |
curFunction = null;
|
186 |
} |
|
187 |
else
|
|
188 |
{ |
|
189 | 6 |
Instruction instruction = |
190 |
InstructionFactory.createInstruction( line ); |
|
191 | 5 |
curFunction.addInstruction( instruction ); |
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
|
|