private static Token parse(Reader rdr) throws ParseException { ListOfToken unparsedList = parseLevel(initStreamTokenizer(rdr)); if ((unparsedList.size() == 1) && (unparsedList.firstElement() instanceof Token)) { return (Token) unparsedList.firstElement(); } try { return Function.makeFunction(unparsedList); } catch (ParseException e) { return Expression.makeExpression(unparsedList); } }
/** * Return a vector of AbstractToken (Operator,Word,Value) and Vector elements * * @return */ private static ListOfToken parseLevel(StreamTokenizer input) throws ParseException { ListOfToken returned = new ListOfToken(); try { // Read input file and build array String currentInput = ""; boolean levelSeemsToBeFinished = false; boolean prefixedBy$ = false; while ((!levelSeemsToBeFinished) && (input.nextToken() != StreamTokenizer.TT_EOF)) { // System.out.println("currentInput="+currentInput+" input="+input); if (input.ttype == StreamTokenizer.TT_WORD) { // System.out.println("Found string: "+ input.sval); handlesCurrentInput(returned, currentInput); currentInput = ""; handlesWordAddition(returned, input.sval); } else if (input.ttype == StreamTokenizer.TT_NUMBER) { // System.out.println("Found double: "+ input.nval); handlesCurrentInput(returned, currentInput); currentInput = ""; Value value = Value.createValue(input.nval); value.setPrefixedBy$(prefixedBy$); returned.add(value); prefixedBy$ = false; } // looks for quotes indicating delimited strings else if (input.ttype == '"') { // Then the string will be in the sval field // System.out.println("Found delimited string: "+ input.sval); handlesCurrentInput(returned, currentInput); currentInput = ""; // handlesWordAddition(returned,input.sval); Value value = StringValue.createStringValue(input.sval); value.setPrefixedBy$(prefixedBy$); returned.add(value); prefixedBy$ = false; } else if (input.ttype == '\'') { // Then the string will be in the sval field // System.out.println("Found delimited string: "+ input.sval); handlesCurrentInput(returned, currentInput); currentInput = ""; // handlesWordAddition(returned,input.sval); Value value = CharValue.createCharValue(input.sval.charAt(0)); value.setPrefixedBy$(prefixedBy$); returned.add(value); prefixedBy$ = false; } else if (input.ttype == '$') { // Then the string will be in the sval field // System.out.println("Found delimited string: "+ input.sval); handlesCurrentInput(returned, currentInput); currentInput = ""; prefixedBy$ = true; } else { char foundChar = (char) input.ttype; // System.out.println("Found Ordinry Character: "+ foundChar); if (foundChar == '(') { handlesCurrentInput(returned, currentInput); currentInput = ""; returned.add(parseLevel(input)); } else if (foundChar == ')') { levelSeemsToBeFinished = true; } else { currentInput = currentInput + foundChar; } } } handlesCurrentInput(returned, currentInput); currentInput = ""; // System.out.println("Done"); return returned; } catch (IOException exception) { throw new ParseException("IOException occured: " + exception.getMessage()); } }