public void consume() throws UcumException { token = null; type = TokenType.NONE; start = index; if (index < source.length()) { char ch = nextChar(); if (!(checkSingle(ch, '/', TokenType.SOLIDUS) || checkSingle(ch, '.', TokenType.PERIOD) || checkSingle(ch, '(', TokenType.OPEN) || checkSingle(ch, ')', TokenType.CLOSE) || checkAnnotation(ch) || checkNumber(ch) || checkNumberOrSymbol(ch))) throw new UcumException("Error processing unit '"+source+"': unexpected character '"+ch+"' at position "+Integer.toString(start)); } }
private boolean checkNumberOrSymbol(char ch) throws UcumException { boolean isSymbol = false; boolean inBrackets = false; if (isValidSymbolChar(ch, true)) { token = String.valueOf(ch); isSymbol = isSymbol || !((ch >= '0' && ch <= '9')); inBrackets = checkBrackets(ch, inBrackets); ch = peekChar(); inBrackets = checkBrackets(ch, inBrackets); while (isValidSymbolChar(ch, !isSymbol || inBrackets)) { token = token + ch; isSymbol = isSymbol || ((ch != NO_CHAR) && !((ch >= '0' && ch <= '9'))); index++; ch = peekChar(); inBrackets = checkBrackets(ch, inBrackets); } if (isSymbol) type = TokenType.SYMBOL; else type = TokenType.NUMBER; return true; } else return false; }
private Component parseComp(Lexer lexer) throws UcumException { if (lexer.getType() == TokenType.NUMBER) { Factor fact = new Factor(lexer.getTokenAsInt()); lexer.consume(); return fact; } else if (lexer.getType() == TokenType.SYMBOL) return parseSymbol(lexer); else if (lexer.getType() == TokenType.NONE) lexer.error("unexpected end of expression looking for a symbol or a number"); else if (lexer.getType() == TokenType.OPEN) { lexer.consume(); Term res = parseTerm(lexer, true); if (lexer.getType() == TokenType.CLOSE) lexer.consume(); else lexer.error("Unexpected Token Type '"+lexer.getType().toString()+"' looking for a close bracket"); return res; } else lexer.error("unexpected token looking for a symbol or a number"); return null; // we never get to here }
private Term parseTerm(Lexer lexer, boolean first) throws UcumException { Term res = new Term(); if (first && lexer.getType() == TokenType.NONE) { res.setComp(new Factor(1)); } else if (lexer.getType() == TokenType.SOLIDUS) { res.setOp(Operator.DIVISION); lexer.consume(); res.setTerm(parseTerm(lexer, false)); } else { if (lexer.getType() == TokenType.ANNOTATION) { res.setComp(new Factor(1)); // still lose the annotation lexer.consume(); } else res.setComp(parseComp(lexer)); if (lexer.getType() != TokenType.NONE && lexer.getType() != TokenType.CLOSE) { if (lexer.getType() == TokenType.SOLIDUS) { res.setOp(Operator.DIVISION); lexer.consume(); } else if (lexer.getType() == TokenType.PERIOD) { res.setOp(Operator.MULTIPLICATION); lexer.consume(); } else if (lexer.getType() == TokenType.ANNOTATION) res.setOp(Operator.MULTIPLICATION); // implicit else lexer.error("Expected '/' or '.'"); res.setTerm(parseTerm(lexer, false)); } } return res; }
/** * @param source * @throws UcumException * @ */ public Lexer(String source) throws UcumException { super(); this.source = source; if (source == null) source = ""; index = 0; consume(); }
private Component parseSymbol(Lexer lexer) throws UcumException { Symbol symbol = new Symbol(); String sym = lexer.getToken(); symbol.setUnit(unit); else if (!sym.equals("1")) lexer.error("The unit '"+sym+"' is unknown"); lexer.consume(); if (lexer.getType() == TokenType.NUMBER) { symbol.setExponent(lexer.getTokenAsInt()); lexer.consume(); } else symbol.setExponent(1);
private Term parseTerm(Lexer lexer, boolean first) throws UcumException { Term res = new Term(); if (first && lexer.getType() == TokenType.NONE) { res.setComp(new Factor(1)); } else if (lexer.getType() == TokenType.SOLIDUS) { res.setOp(Operator.DIVISION); lexer.consume(); res.setTerm(parseTerm(lexer, false)); } else { if (lexer.getType() == TokenType.ANNOTATION) { res.setComp(new Factor(1)); // still lose the annotation lexer.consume(); } else res.setComp(parseComp(lexer)); if (lexer.getType() != TokenType.NONE && lexer.getType() != TokenType.CLOSE) { if (lexer.getType() == TokenType.SOLIDUS) { res.setOp(Operator.DIVISION); lexer.consume(); } else if (lexer.getType() == TokenType.PERIOD) { res.setOp(Operator.MULTIPLICATION); lexer.consume(); } else if (lexer.getType() == TokenType.ANNOTATION) res.setOp(Operator.MULTIPLICATION); // implicit else lexer.error("Expected '/' or '.'"); res.setTerm(parseTerm(lexer, false)); } } return res; }
/** * @param source * @throws UcumException * @ */ public Lexer(String source) throws UcumException { super(); this.source = source; if (source == null) source = ""; index = 0; consume(); }
private Component parseComp(Lexer lexer) throws UcumException { if (lexer.getType() == TokenType.NUMBER) { Factor fact = new Factor(lexer.getTokenAsInt()); lexer.consume(); return fact; } else if (lexer.getType() == TokenType.SYMBOL) return parseSymbol(lexer); else if (lexer.getType() == TokenType.NONE) lexer.error("unexpected end of expression looking for a symbol or a number"); else if (lexer.getType() == TokenType.OPEN) { lexer.consume(); Term res = parseTerm(lexer, true); if (lexer.getType() == TokenType.CLOSE) lexer.consume(); else lexer.error("Unexpected Token Type '"+lexer.getType().toString()+"' looking for a close bracket"); return res; } else lexer.error("unexpected token looking for a symbol or a number"); return null; // we never get to here }
public void consume() throws UcumException { token = null; type = TokenType.NONE; start = index; if (index < source.length()) { char ch = nextChar(); if (!(checkSingle(ch, '/', TokenType.SOLIDUS) || checkSingle(ch, '.', TokenType.PERIOD) || checkSingle(ch, '(', TokenType.OPEN) || checkSingle(ch, ')', TokenType.CLOSE) || checkAnnotation(ch) || checkNumber(ch) || checkNumberOrSymbol(ch))) throw new UcumException("Error processing unit '"+source+"': unexpected character '"+ch+"' at position "+Integer.toString(start)); } }
private boolean checkNumberOrSymbol(char ch) throws UcumException { boolean isSymbol = false; boolean inBrackets = false; if (isValidSymbolChar(ch, true)) { token = String.valueOf(ch); isSymbol = isSymbol || !((ch >= '0' && ch <= '9')); inBrackets = checkBrackets(ch, inBrackets); ch = peekChar(); inBrackets = checkBrackets(ch, inBrackets); while (isValidSymbolChar(ch, !isSymbol || inBrackets)) { token = token + ch; isSymbol = isSymbol || ((ch != NO_CHAR) && !((ch >= '0' && ch <= '9'))); index++; ch = peekChar(); inBrackets = checkBrackets(ch, inBrackets); } if (isSymbol) type = TokenType.SYMBOL; else type = TokenType.NUMBER; return true; } else return false; }
private Component parseSymbol(Lexer lexer) throws UcumException { Symbol symbol = new Symbol(); String sym = lexer.getToken(); symbol.setUnit(unit); else if (!sym.equals("1")) lexer.error("The unit '"+sym+"' is unknown"); lexer.consume(); if (lexer.getType() == TokenType.NUMBER) { symbol.setExponent(lexer.getTokenAsInt()); lexer.consume(); } else symbol.setExponent(1);