summaryrefslogtreecommitdiff
path: root/java/demo/Database/library/Grammar.java
diff options
context:
space:
mode:
authorJoe George <joe@zeroc.com>2015-03-03 17:30:50 -0500
committerJoe George <joe@zeroc.com>2015-05-12 11:41:55 -0400
commitd35bb9f5c19e34aee31f83d445695a8186ef675e (patch)
treed5324eaf44f5f9776495537c51653f50a66a7237 /java/demo/Database/library/Grammar.java
downloadice-d35bb9f5c19e34aee31f83d445695a8186ef675e.tar.bz2
ice-d35bb9f5c19e34aee31f83d445695a8186ef675e.tar.xz
ice-d35bb9f5c19e34aee31f83d445695a8186ef675e.zip
Ice 3.4.2 Source Distributionv3.4.2
Diffstat (limited to 'java/demo/Database/library/Grammar.java')
-rw-r--r--java/demo/Database/library/Grammar.java176
1 files changed, 176 insertions, 0 deletions
diff --git a/java/demo/Database/library/Grammar.java b/java/demo/Database/library/Grammar.java
new file mode 100644
index 00000000000..f5c66bffc44
--- /dev/null
+++ b/java/demo/Database/library/Grammar.java
@@ -0,0 +1,176 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+class Grammar
+{
+ Grammar(Parser p)
+ {
+ _parser = p;
+ _scanner = new Scanner(_parser);
+ }
+
+ void
+ parse()
+ {
+ while(true)
+ {
+ try
+ {
+ _token = _scanner.nextToken();
+ if(_token == null)
+ {
+ return;
+ }
+ else if(_token.type == Token.TOK_SEMI)
+ {
+ // Continue
+ }
+ else if(_token.type == Token.TOK_HELP)
+ {
+ _token = _scanner.nextToken();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+
+ _parser.usage();
+ }
+ else if(_token.type == Token.TOK_EXIT)
+ {
+ _token = _scanner.nextToken();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+
+ return;
+ }
+ else if(_token.type == Token.TOK_ADD_BOOK)
+ {
+ java.util.List<String> s = strings();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+ _parser.addBook(s);
+ }
+ else if(_token.type == Token.TOK_FIND_ISBN)
+ {
+ java.util.List<String> s = strings();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+ _parser.findIsbn(s);
+ }
+ else if(_token.type == Token.TOK_FIND_AUTHORS)
+ {
+ java.util.List<String> s = strings();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+ _parser.findAuthors(s);
+ }
+ else if(_token.type == Token.TOK_FIND_TITLE)
+ {
+ java.util.List<String> s = strings();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+ _parser.findTitle(s);
+ }
+ else if(_token.type == Token.TOK_NEXT_FOUND_BOOK)
+ {
+ _token = _scanner.nextToken();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+
+ _parser.nextFoundBook();
+ }
+ else if(_token.type == Token.TOK_PRINT_CURRENT)
+ {
+ _token = _scanner.nextToken();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+
+ _parser.printCurrent();
+ }
+ else if(_token.type == Token.TOK_RENT_BOOK)
+ {
+ java.util.List<String> s = strings();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+ _parser.rentCurrent(s);
+ }
+ else if(_token.type == Token.TOK_RETURN_BOOK)
+ {
+ _token = _scanner.nextToken();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+ _parser.returnCurrent();
+ }
+ else if(_token.type == Token.TOK_REMOVE_CURRENT)
+ {
+ _token = _scanner.nextToken();
+ if(_token.type != Token.TOK_SEMI)
+ {
+ throw new ParseError("Expected ';'");
+ }
+
+ _parser.removeCurrent();
+ }
+ else
+ {
+ _parser.error("parse error");
+ }
+ }
+ catch(ParseError e)
+ {
+ _parser.error("Parse error: " + e.getMessage());
+ }
+ }
+ }
+
+ private java.util.List<String>
+ strings()
+ {
+ java.util.List<String> l = new java.util.ArrayList<String>();
+ while(true)
+ {
+ _token = _scanner.nextToken();
+ if(_token.type != Token.TOK_STRING)
+ {
+ return l;
+ }
+ l.add(_token.value);
+ }
+ }
+
+ static private class ParseError extends RuntimeException
+ {
+ ParseError(String msg)
+ {
+ super(msg);
+ }
+ }
+
+ private Parser _parser;
+ private Scanner _scanner;
+ private Token _token;
+}