ICE 3.4.2
[php5-ice-freebsdport.git] / java / demo / book / map_filesystem / Grammar.java
blobda64cf5658f2117758e7cd7f2ead4536eabb8de6
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 class Grammar
12 Grammar(Parser p)
14 _parser = p;
15 _scanner = new Scanner(_parser);
18 void
19 parse()
21 while(true)
23 try
25 _token = _scanner.nextToken();
26 if(_token == null)
28 return;
30 else if(_token.type == Token.TOK_SEMI)
32 // Continue
34 else if(_token.type == Token.TOK_HELP)
36 _token = _scanner.nextToken();
37 if(_token.type != Token.TOK_SEMI)
39 throw new ParseError("Expected ';'");
41 _parser.usage();
43 else if(_token.type == Token.TOK_EXIT)
45 _token = _scanner.nextToken();
46 if(_token.type != Token.TOK_SEMI)
48 throw new ParseError("Expected ';'");
50 return;
52 else if(_token.type == Token.TOK_LIST)
54 _token = _scanner.nextToken();
55 if(_token.type != Token.TOK_SEMI)
57 throw new ParseError("Expected ';'");
59 _parser.list(false);
61 else if(_token.type == Token.TOK_LIST_RECURSIVE)
63 _token = _scanner.nextToken();
64 if(_token.type != Token.TOK_SEMI)
66 throw new ParseError("Expected ';'");
68 _parser.list(true);
70 else if(_token.type == Token.TOK_CREATE_FILE)
72 java.util.List<String> s = strings();
73 if(_token.type != Token.TOK_SEMI)
75 throw new ParseError("Expected ';'");
77 if(s.size() == 0)
79 throw new ParseError("usage: mkfile FILE [FILE...]");
81 _parser.createFile(s);
83 else if(_token.type == Token.TOK_CREATE_DIR)
85 java.util.List<String> s = strings();
86 if(_token.type != Token.TOK_SEMI)
88 throw new ParseError("Expected ';'");
90 if(s.size() == 0)
92 throw new ParseError("usage: mkdir DIR [DIR...]");
94 _parser.createDir(s);
96 else if(_token.type == Token.TOK_PWD)
98 _token = _scanner.nextToken();
99 if(_token.type != Token.TOK_SEMI)
101 throw new ParseError("Expected ';'");
103 _parser.pwd();
105 else if(_token.type == Token.TOK_CD)
107 java.util.List<String> s = strings();
108 if(_token.type != Token.TOK_SEMI)
110 throw new ParseError("Expected ';'");
112 if(s.size() > 1)
114 throw new ParseError("usage: cd [DIR]");
116 else if(s.size() == 0)
118 _parser.cd("/");
120 else
122 _parser.cd((String)s.get(0));
125 else if(_token.type == Token.TOK_CAT)
127 java.util.List<String> s = strings();
128 if(_token.type != Token.TOK_SEMI)
130 throw new ParseError("Expected ';'");
132 if(s.size() != 1)
134 throw new ParseError("usage: cat FILE");
136 _parser.cat((String)s.get(0));
138 else if(_token.type == Token.TOK_WRITE)
140 java.util.LinkedList<String> s = strings();
141 if(_token.type != Token.TOK_SEMI)
143 throw new ParseError("Expected ';'");
145 if(s.size() == 0)
147 throw new ParseError("usage: write FILE [STRING...]");
149 _parser.write(s);
151 else if(_token.type == Token.TOK_RM)
153 java.util.List<String> s = strings();
154 if(_token.type != Token.TOK_SEMI)
156 throw new ParseError("Expected ';'");
158 if(s.size() == 0)
160 throw new ParseError("usage: rm NAME [NAME...]");
162 _parser.destroy(s);
164 else
166 _parser.error("parse error");
169 catch(ParseError e)
171 _parser.error("Parse error: " + e.getMessage());
176 private java.util.LinkedList<String>
177 strings()
179 java.util.LinkedList<String> l = new java.util.LinkedList<String>();
180 while(true)
182 _token = _scanner.nextToken();
183 if(_token.type != Token.TOK_STRING)
185 return l;
187 l.add(_token.value);
191 static private class ParseError extends RuntimeException
193 ParseError(String msg)
195 super(msg);
199 private Parser _parser;
200 private Scanner _scanner;
201 private Token _token;