use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class ExpressionGenerator method visitBinaryExpression.
@Override
public void visitBinaryExpression(@NotNull GrBinaryExpression expression) {
final GrExpression left = expression.getLeftOperand();
GrExpression right = expression.getRightOperand();
final PsiType ltype = left.getType();
final PsiElement token = expression.getOperationToken();
final IElementType op = expression.getOperationTokenType();
if (op == GroovyTokenTypes.mREGEX_FIND) {
builder.append(GroovyCommonClassNames.JAVA_UTIL_REGEX_PATTERN).append(".compile(");
if (right != null) {
right.accept(this);
}
builder.append(").matcher(");
left.accept(this);
builder.append(')');
return;
}
if (op == GroovyTokenTypes.mREGEX_MATCH) {
builder.append(GroovyCommonClassNames.JAVA_UTIL_REGEX_PATTERN).append(".matches(");
if (right != null) {
right.accept(this);
}
builder.append(", ");
left.accept(this);
builder.append(')');
return;
}
if ((op == GroovyTokenTypes.mEQUAL || op == GroovyTokenTypes.mNOT_EQUAL) && (GrInspectionUtil.isNull(left) || right != null && GrInspectionUtil.isNull(right))) {
writeSimpleBinaryExpression(token, left, right);
return;
}
if (op == GroovyTokenTypes.kIN && right instanceof GrReferenceExpression && InheritanceUtil.isInheritor(right.getType(), CommonClassNames.JAVA_LANG_CLASS)) {
final PsiType type = com.intellij.psi.util.PsiUtil.substituteTypeParameter(right.getType(), CommonClassNames.JAVA_LANG_CLASS, 0, true);
writeInstanceof(left, type, expression);
return;
}
if (shouldNotReplaceOperatorWithMethod(ltype, right, op)) {
writeSimpleBinaryExpression(token, left, right);
return;
}
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod) {
if (right == null) {
right = factory.createExpressionFromText("null");
}
if (op == GroovyTokenTypes.mNOT_EQUAL && "equals".equals(((PsiMethod) resolved).getName())) {
builder.append('!');
}
invokeMethodOn(((PsiMethod) resolved), left, new GrExpression[] { right }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
if (op == GroovyTokenTypes.mGE) {
builder.append(" >= 0");
} else if (op == GroovyTokenTypes.mGT) {
builder.append(" > 0");
} else if (op == GroovyTokenTypes.mLT) {
builder.append(" < 0");
} else if (op == GroovyTokenTypes.mLE)
builder.append(" <= 0");
} else {
writeSimpleBinaryExpression(token, left, right);
}
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class ExpressionGenerator method getRValue.
/**
* returns rValue for lValue = expr
* lValue+Rvalue for lValue += rValue
*/
@Nullable
private GrExpression getRValue(GrAssignmentExpression expression) {
GrExpression rValue = expression.getRValue();
if (rValue == null)
return null;
GrExpression lValue = expression.getLValue();
IElementType opToken = expression.getOperationTokenType();
if (opToken == GroovyTokenTypes.mASSIGN)
return rValue;
Pair<String, IElementType> pair = GenerationUtil.getBinaryOperatorType(opToken);
LOG.assertTrue(pair != null);
return factory.createExpressionFromText(lValue.getText() + pair.getFirst() + rValue.getText(), expression);
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class FormSourceCodeGenerator method lexemsEqual.
private static boolean lexemsEqual(final PsiClass classToBind, final PsiClass newClass) {
Lexer oldTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST);
Lexer newTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST);
String oldBuffer = classToBind.getText();
String newBuffer = newClass.getText();
oldTextLexer.start(oldBuffer);
newTextLexer.start(newBuffer);
while (true) {
IElementType oldLexem = oldTextLexer.getTokenType();
IElementType newLexem = newTextLexer.getTokenType();
if (oldLexem == null || newLexem == null) {
// must terminate at the same time
return oldLexem == null && newLexem == null;
}
if (oldLexem != newLexem) {
return false;
}
if (oldLexem != TokenType.WHITE_SPACE && oldLexem != JavaDocElementType.DOC_COMMENT) {
int oldStart = oldTextLexer.getTokenStart();
int newStart = newTextLexer.getTokenStart();
int oldLength = oldTextLexer.getTokenEnd() - oldTextLexer.getTokenStart();
int newLength = newTextLexer.getTokenEnd() - newTextLexer.getTokenStart();
if (oldLength != newLength) {
return false;
}
for (int i = 0; i < oldLength; i++) {
if (oldBuffer.charAt(oldStart + i) != newBuffer.charAt(newStart + i)) {
return false;
}
}
}
oldTextLexer.advance();
newTextLexer.advance();
}
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class CommandLineParserUtil method bound_argument.
static void bound_argument(@NotNull final PsiBuilder b, final int i) {
final IElementType tokenType = b.getTokenType();
final IElementType leftElement = b.rawLookup(-1);
final IElementType rightElement = b.rawLookup(1);
if (leftElement == null || TokenType.WHITE_SPACE.equals(leftElement)) {
return;
}
/**
* At '=' position: if no whitespace to left and right, we move to argument.
* And we report error if whitespace to the left.
*/
if (tokenType == CommandLineElementTypes.EQ) {
if (leftElement.equals(CommandLineElementTypes.LONG_OPTION_NAME_TOKEN)) {
if (rightElement == null || TokenType.WHITE_SPACE.equals(rightElement)) {
b.error("Space between argument is its value is unexpected");
}
b.advanceLexer();
}
}
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class RestLexerTest method doTest.
private static void doTest(String text, String... expected) throws IOException {
_RestFlexLexer lexer = createLexer(text);
for (String expectedTokenText : expected) {
IElementType type = lexer.advance();
if (type == null) {
fail("Not enough tokens");
}
String tokenText = "[" + lexer.yytext() + ", " + type + "]";
assertEquals(expectedTokenText, tokenText);
}
}
Aggregations