diff options
Diffstat (limited to 'java/demo/Freeze/library/Grammar.java')
-rw-r--r-- | java/demo/Freeze/library/Grammar.java | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/java/demo/Freeze/library/Grammar.java b/java/demo/Freeze/library/Grammar.java new file mode 100644 index 00000000000..e1f81d6c8f0 --- /dev/null +++ b/java/demo/Freeze/library/Grammar.java @@ -0,0 +1,188 @@ +// ********************************************************************** +// +// Copyright (c) 2001 +// MutableRealms, Inc. +// Huntsville, AL, USA +// +// All Rights Reserved +// +// ********************************************************************** + +class Grammar +{ + Grammar(Parser p) + { + _parser = p; + _scanner = new Scanner(_parser); + } + + + static private class ParseError extends RuntimeException + { + ParseError(String msg) + { + super(msg); + } + } + + 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.LinkedList 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.LinkedList 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.LinkedList s = strings(); + if (_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.findAuthors(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.LinkedList 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 if (_token.type == Token.TOK_SET_EVICTOR_SIZE) + { + java.util.LinkedList s = strings(); + if (_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.setEvictorSize(s); + } + else if (_token.type == Token.TOK_SHUTDOWN) + { + _token = _scanner.nextToken(); + if (_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + + _parser.shutdown(); + } + else + { + _parser.error("parse error"); + } + } + catch(ParseError e) + { + _parser.error("Parse error: " + e.getMessage()); + } + } + } + + private java.util.LinkedList + strings() + { + java.util.LinkedList l = new java.util.LinkedList(); + while (true) + { + _token = _scanner.nextToken(); + if (_token.type != Token.TOK_STRING) + { + return l; + } + l.add(_token.value); + } + } + + private Parser _parser; + private Scanner _scanner; + private Token _token; +} |