regexp: a named character is a character too

This commit is contained in:
Bas Leijdekkers
2017-02-02 19:00:25 +01:00
parent 9eba3dd4ed
commit 10f36e1621
6 changed files with 35 additions and 23 deletions
@@ -18,22 +18,30 @@ package org.intellij.lang.regexp.psi;
import org.jetbrains.annotations.NotNull;
/**
* Represents a simple or escaped character
* Represents a simple, escaped, encoded or named character
*/
public interface RegExpChar extends RegExpAtom, RegExpClassElement, RegExpCharRange.Endpoint {
/**
* Character type enumeration. Represents either a plain character ("a"),
* a hex encoded character value ("\x61"),
* an octal encoded character value ("\0141"), or
* a unicode escape character ("\u0061")
*/
/** Character type */
enum Type {
CHAR, HEX, OCT, UNICODE, INVALID
/** a plain character ("a") */
CHAR,
/** a hex encoded character value ("\x61") */
HEX,
/** an octal encoded character value ("\0141") */
OCT,
/** a unicode escape character ("\u0061") */
UNICODE,
/** a named character (\N{LATIN SMALL LETTER A}) */
NAMED,
}
/**
* Returns the type of this character.
* @see org.intellij.lang.regexp.psi.RegExpChar.Type
* @see Type
*/
@NotNull
Type getType();
@@ -20,6 +20,7 @@ import com.intellij.psi.PsiElementVisitor;
public class RegExpElementVisitor extends PsiElementVisitor {
public void visitRegExpElement(RegExpElement element) {
visitElement(element);
}
public void visitRegExpChar(RegExpChar ch) {
@@ -51,7 +52,7 @@ public class RegExpElementVisitor extends PsiElementVisitor {
}
public void visitRegExpNamedCharacter(RegExpNamedCharacter namedCharacter) {
visitRegExpElement(namedCharacter);
visitRegExpChar(namedCharacter);
}
public void visitRegExpBranch(RegExpBranch branch) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public interface RegExpNamedCharacter extends RegExpAtom, RegExpClassElement {
public interface RegExpNamedCharacter extends RegExpChar {
@Nullable
ASTNode getNameNode();
@@ -17,7 +17,6 @@ package org.intellij.lang.regexp.psi.impl;
import com.intellij.lang.ASTNode;
import com.intellij.psi.StringEscapesTokenTypes;
import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.intellij.lang.regexp.RegExpTT;
@@ -47,8 +46,8 @@ public class RegExpCharImpl extends RegExpElementImpl implements RegExpChar {
return Type.HEX;
} else if (UNICODE_CHARS.contains(t)) {
return Type.UNICODE;
} else if (t == TokenType.ERROR_ELEMENT) {
return Type.INVALID;
} else if (t == RegExpTT.NAMED_CHARACTER) {
return Type.NAMED;
} else {
return Type.CHAR;
}
@@ -92,6 +91,15 @@ public class RegExpCharImpl extends RegExpElementImpl implements RegExpChar {
return '\b';
case 'c':
return (char)(ch ^ 64);
case 'N':
if (length < idx + 3 || s.charAt(idx + 1) != '{' || s.charAt(length - 1) != '}') {
return -1;
}
final int codePoint = UnicodeCharacterNames.getCodePoint(s.substring(idx + 2, length - 1));
if (codePoint == -1) {
return -1;
}
return codePoint;
case 'x':
if (length <= idx + 1) return -1;
if (s.charAt(idx + 1) == '{') {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,8 @@ import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class RegExpNamedCharacterImpl extends RegExpElementImpl implements RegExpNamedCharacter {
public class RegExpNamedCharacterImpl extends RegExpCharImpl implements RegExpNamedCharacter {
public RegExpNamedCharacterImpl(ASTNode node) {
super(node);
}
@@ -477,12 +477,6 @@ public final class RegExpAnnotator extends RegExpElementVisitor implements Annot
myLength++;
}
@Override
public void visitRegExpNamedCharacter(RegExpNamedCharacter namedCharacter) {
super.visitRegExpNamedCharacter(namedCharacter);
myLength++;
}
@Override
public void visitRegExpBackref(RegExpBackref backref) {
super.visitRegExpBackref(backref);