ExtendedParser extends BaseParser { protected final float readFactor() throws ParserException { if (Character.isDigit(peek())) { return readNumber(); } else if (Character.isAlphabetic(peek())) { StringBuilder builder = new StringBuilder(); while (available() && (Character.isAlphabetic(peek()) || peek() == '_')) { builder.append(pop()); } String variableName = builder.toString(); if (variables.containsKey(variableName)) { return variables.get(variableName); } else { die("Unknown variable"); return 0; } } else if (peek() == '(') { check('('); skipWhitespaces(); float expression = readExpression(); skipWhitespaces(); check(')'); return expression; } else if (peek() == '+') { check('+'); return readFactor(); } else if (peek() == '-') { check('-'); return -readFactor(); } else { die("Expected mathematical expression."); return 0; } } ExtendedParser(String toParse);   }
@Test public void testFactor() throws ParserException { ExtendedParser parser = new ExtendedParser("-5"); assertEquals(-5f, parser.readFactor(), 0.0001); parser = new ExtendedParser("--3"); assertEquals(3f, parser.readFactor(), 0.0001); parser = new ExtendedParser("10"); assertEquals(10f, parser.readFactor(), 0.0001); parser = new ExtendedParser("varname"); parser.variables.put("varname", 42f); assertEquals(42f, parser.readFactor(), 0.0001); parser = new ExtendedParser("-varname"); parser.variables.put("varname", 42f); assertEquals(-42f, parser.readFactor(), 0.0001); }