public static void main(String[] args) { char a[] = { 'a', '5', '?', 'A', ' ', '$', 'жа' }; for (int i = 0; i < a.length; i++) { if (Character.isDigit(a[i])) System.out.println(a[i] + " is a digit."); if (Character.isLetter(a[i])) System.out.println(a[i] + " is a letter."); if (Character.isWhitespace(a[i])) System.out.println(a[i] + " is whitespace."); if (Character.isUpperCase(a[i])) System.out.println(a[i] + " is uppercase."); if (Character.isLowerCase(a[i])) System.out.println(a[i] + " is lowercase."); if (Character.isJavaIdentifierPart(a[i])) System.out.println(a[i] + " may be part of java Identifier part."); if (Character.isJavaIdentifierStart(a[i])) System.out.println(a[i] + " may be part of java Identifier Start."); if (Character.isUnicodeIdentifierPart(a[i])) System.out.println(a[i] + " may be part of a Unicode identifier ."); if (Character.isUnicodeIdentifierStart(a[i])) System.out .println(a[i] + " may be the first character in a Unicode identifier."); } }
/** * Called when an operand is expected next. * * @return one of: * <UL> * <LI>a {@link BigDecimal} value; * <LI>the {@link String} name of a variable; * <LI>{@link Tokeniser#START_NEW_EXPRESSION} when an opening parenthesis is found: * <LI>or {@link Operator} when a unary operator is found in front of an operand * </UL> * * @throws RuntimeException if the end of the string is reached unexpectedly. */ Object getOperand() { /* Skip whitespace */ final int len = this.string.length(); char ch = 0; while (this.position < len && Character.isWhitespace(ch = this.string.charAt(this.position))) { this.position++; } if (this.position == len) { throw new RuntimeException("operand expected but end of string found"); } if (ch == '(') { this.position++; return START_NEW_EXPRESSION; } else if (ch == '-') { this.position++; return Operator.NEG; } else if (ch == '+') { this.position++; return Operator.PLUS; } else if (ch == '.' || Character.isDigit(ch)) { return getBigDecimal(); } else if (Character.isUnicodeIdentifierStart(ch)) { int start = this.position++; while (this.position < len && Character.isUnicodeIdentifierPart(this.string.charAt(this.position))) { this.position++; } String name = this.string.substring(start, this.position); /* Is variable name actually a keyword unary operator? */ if (name.equals("abs")) { return Operator.ABS; } else if (name.equals("int")) { return Operator.INT; } else if (name.equals("ln")) { return Operator.LN; } /* Return variable name */ return name; } throw new RuntimeException("operand expected but '" + ch + "' found"); }
private void validateUserId(String uid) { if (!User.userIdAvailable(uid)) { addFieldError("username", getText("error.uid.unavailable", new String[] {uid})); return; } int n = uid.length(); if (n > UID_MAX_LENGTH) { addFieldError("username", getText("error.string.toolong", new String[] {uid})); return; } char[] cs = uid.toCharArray(); for (int i = 0; i < n; i++) { char c = cs[i]; // White space cannot be part of a valid uid! if (Character.isWhitespace(c)) addFieldError( "username", "You chose '" + uid + "' as your username. Character '" + c + "' at position " + i + " cannot be used in a user name!"); // No special characters allowed! if (((i == 0) && !Character.isUnicodeIdentifierStart(c)) || ((i > 0) && !Character.isUnicodeIdentifierPart(c) && (c != '_'))) { addFieldError( "username", "You chose '" + uid + " as your username. Character '" + c + "' at position " + i + " cannot be used in a user name! Start with a letter and use letters, numbers, or _ elsewhere!"); } } }
/* * @see DocumentTemplateContext#getCompletionPosition(); */ @Override public int getStart() { if (fIsManaged && getCompletionLength() > 0) return super.getStart(); try { IDocument document = getDocument(); int start = getCompletionOffset(); int end = getCompletionOffset() + getCompletionLength(); while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1))) start--; while (start != end && Character.isWhitespace(document.getChar(start))) start++; if (start == end) start = getCompletionOffset(); return start; } catch (BadLocationException e) { return super.getStart(); } }
Operator getOperator(char endOfExpressionChar) { /* Use any pushed back operator. */ if (this.pushedBackOperator != null) { Operator operator = this.pushedBackOperator; this.pushedBackOperator = null; return operator; } /* Skip whitespace */ final int len = this.string.length(); char ch = 0; while (this.position < len && Character.isWhitespace(ch = this.string.charAt(this.position))) { this.position++; } if (this.position == len) { if (endOfExpressionChar == 0) { return Operator.END; } else { throw new RuntimeException("missing " + endOfExpressionChar); } } this.position++; if (ch == endOfExpressionChar) { return Operator.END; } switch (ch) { case '+': { return Operator.ADD; } case '-': { return Operator.SUB; } case '/': { return Operator.DIV; } case '%': { return Operator.REMAINDER; } case '*': { return Operator.MUL; } case '?': { return Operator.TERNARY; } case '>': { if (this.position < len && this.string.charAt(this.position) == '=') { this.position++; return Operator.GE; } return Operator.GT; } case '<': { if (this.position < len) { switch (this.string.charAt(this.position)) { case '=': this.position++; return Operator.LE; case '>': this.position++; return Operator.NE; } } return Operator.LT; } case '=': { if (this.position < len && this.string.charAt(this.position) == '=') { this.position++; return Operator.EQ; } throw new RuntimeException("use == for equality at position " + this.position); } case '!': { if (this.position < len && this.string.charAt(this.position) == '=') { this.position++; return Operator.NE; } throw new RuntimeException("use != or <> for inequality at position " + this.position); } case '&': { if (this.position < len && this.string.charAt(this.position) == '&') { this.position++; return Operator.AND; } throw new RuntimeException("use && for AND at position " + this.position); } case '|': { if (this.position < len && this.string.charAt(this.position) == '|') { this.position++; return Operator.OR; } throw new RuntimeException("use || for OR at position " + this.position); } default: { /* Is this an identifier name for an operator function? */ if (Character.isUnicodeIdentifierStart(ch)) { int start = this.position - 1; while (this.position < len && Character.isUnicodeIdentifierPart(this.string.charAt(this.position))) { this.position++; } String name = this.string.substring(start, this.position); if (name.equals("pow")) { return Operator.POW; } if (name.equals("spt")) { return Operator.SPT; } } throw new RuntimeException( "operator expected at position " + this.position + " instead of '" + ch + "'"); } } }
private Token identifier(int start, int length) { int tokenKind = IToken.tIDENTIFIER; boolean isPartOfIdentifier = true; int c = fCharPhase3; while (true) { switch (c) { case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': break; case '\\': // universal character name markPhase3(); switch (nextCharPhase3()) { case 'u': case 'U': length++; break; default: restorePhase3(); isPartOfIdentifier = false; break; } break; case END_OF_INPUT: if (fSupportContentAssist) { tokenKind = IToken.tCOMPLETION; } isPartOfIdentifier = false; break; case ' ': case '\t': case 0xb: case '\f': case '\r': case '\n': isPartOfIdentifier = false; break; case '$': isPartOfIdentifier = fOptions.fSupportDollarInIdentifiers; break; case '@': isPartOfIdentifier = fOptions.fSupportAtSignInIdentifiers; break; case '{': case '}': case '[': case ']': case '#': case '(': case ')': case '<': case '>': case '%': case ':': case ';': case '.': case '?': case '*': case '+': case '-': case '/': case '^': case '&': case '|': case '~': case '!': case '=': case ',': case '"': case '\'': isPartOfIdentifier = false; break; default: isPartOfIdentifier = Character.isUnicodeIdentifierPart((char) c); break; } if (!isPartOfIdentifier) { break; } length++; c = nextCharPhase3(); } return newToken(tokenKind, start, length); }
/** * Returns a parser that produces a unicode identifier part character. * * @param missing The error if there is no character on the stream to produce a unicode * identifier part character with. * @param sat The error if the produced character is not a unicode identifier part character. * @return A parser that produces a unicode identifier part character. * @see Character#isUnicodeIdentifierPart(char) */ public static <E> Parser<Stream<Character>, Character, E> unicodeIdentiferPart( final F0<E> missing, final F<Character, E> sat) { return StreamParser.satisfy(missing, sat, c -> Character.isUnicodeIdentifierPart(c)); }