RegExp: simplify options handling

This commit is contained in:
Bas Leijdekkers
2017-02-08 17:43:49 +01:00
parent 4157ca0764
commit d5bf77bb62
5 changed files with 22 additions and 54 deletions

View File

@@ -333,14 +333,16 @@ public class RegExpParser implements PsiParser {
else if (type == RegExpTT.SET_OPTIONS) {
builder.advanceLexer();
final PsiBuilder.Marker o = builder.mark();
if (builder.getTokenType() == RegExpTT.OPTIONS_ON) {
final PsiBuilder.Marker o = builder.mark();
builder.advanceLexer();
o.done(RegExpElementTypes.OPTIONS);
}
if (builder.getTokenType() == RegExpTT.OPTIONS_OFF) {
final PsiBuilder.Marker o = builder.mark();
builder.advanceLexer();
o.done(RegExpElementTypes.OPTIONS);
}
o.done(RegExpElementTypes.OPTIONS);
if (builder.getTokenType() == RegExpTT.COLON) {
builder.advanceLexer();

View File

@@ -15,29 +15,15 @@
*/
package org.intellij.lang.regexp.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.Nullable;
/**
* Represents an inline options element (?x) or (?-x). Returned from {@link org.intellij.lang.regexp.psi.RegExpSetOptions}
*/
public interface RegExpOptions extends RegExpElement {
/**
* @param flag the flag to check, e.g. 'i'
* @return true, when the specified flag is switched on, false otherwise.
*/
boolean isSwitchedOn(char flag);
/**
* It's possible for a flag to be both switched on and switched off.
* @param flag the flag to check, e.g. 'd'
* @return true, when the specified flag is switched off, false otherwise.
* Checks whether a certain option is set.
* @return true, if the option is set, false otherwise.
*/
boolean isSwitchedOff(char flag);
boolean isSet(char option);
@Nullable
ASTNode getOptionsOn();
@Nullable
ASTNode getOptionsOff();
}

View File

@@ -16,11 +16,8 @@
package org.intellij.lang.regexp.psi.impl;
import com.intellij.lang.ASTNode;
import org.intellij.lang.regexp.RegExpTT;
import org.intellij.lang.regexp.psi.RegExpElementVisitor;
import org.intellij.lang.regexp.psi.RegExpOptions;
import org.jetbrains.annotations.Nullable;
public class RegExpOptionsImpl extends RegExpElementImpl implements RegExpOptions {
public RegExpOptionsImpl(ASTNode astNode) {
@@ -33,26 +30,7 @@ public class RegExpOptionsImpl extends RegExpElementImpl implements RegExpOption
}
@Override
public boolean isSwitchedOn(char flag) {
final ASTNode node = getOptionsOn();
return node != null && node.getText().indexOf(flag) >= 0;
}
@Override
public boolean isSwitchedOff(char flag) {
final ASTNode node = getOptionsOff();
return node != null && node.getText().indexOf(flag) > 0;
}
@Override
@Nullable
public ASTNode getOptionsOn() {
return getNode().findChildByType(RegExpTT.OPTIONS_ON);
}
@Override
@Nullable
public ASTNode getOptionsOff() {
return getNode().findChildByType(RegExpTT.OPTIONS_OFF);
public boolean isSet(char option) {
return getUnescapedText().indexOf(option) >= 0;
}
}

View File

@@ -61,21 +61,21 @@ public final class RegExpAnnotator extends RegExpElementVisitor implements Annot
}
@Override
public void visitRegExpOptions(RegExpOptions options) {
checkValidFlag(options.getOptionsOn(), options);
checkValidFlag(options.getOptionsOff(), options);
public void visitRegExpSetOptions(RegExpSetOptions options) {
checkValidFlag(options.getOnOptions(), false);
checkValidFlag(options.getOffOptions(), true);
}
private void checkValidFlag(@Nullable ASTNode optionsNode, @NotNull RegExpOptions context) {
if (optionsNode == null) {
private void checkValidFlag(@Nullable RegExpOptions options, boolean skipMinus) {
if (options == null) {
return;
}
final String text = optionsNode.getText();
final int start = (optionsNode.getElementType() == RegExpTT.OPTIONS_OFF) ? 1 : 0; // skip '-' if necessary
final String text = options.getText();
final int start = skipMinus ? 1 : 0; // skip '-' if necessary
for (int i = start, length = text.length(); i < length; i++) {
final int c = text.codePointAt(i);
if (!Character.isBmpCodePoint(c) || !myLanguageHosts.supportsInlineOptionFlag((char)c, context)) {
final int offset = optionsNode.getStartOffset() + i;
if (!Character.isBmpCodePoint(c) || !myLanguageHosts.supportsInlineOptionFlag((char)c, options)) {
final int offset = options.getTextOffset() + i;
myHolder.createErrorAnnotation(new TextRange(offset, offset + 1), "Unknown inline option flag");
}
}

View File

@@ -3,8 +3,9 @@ REGEXP_FILE
RegExpBranchImpl: <(?idm-suxU)one(?suxU-idm)two>
RegExpSetOptionsImpl: <(?idm-suxU)>
PsiElement(SET_OPTIONS)('(?')
RegExpOptionsImpl: <idm-suxU>
RegExpOptionsImpl: <idm>
PsiElement(OPTIONS_ON)('idm')
RegExpOptionsImpl: <-suxU>
PsiElement(OPTIONS_OFF)('-suxU')
PsiElement(GROUP_END)(')')
RegExpCharImpl: <o>
@@ -15,8 +16,9 @@ REGEXP_FILE
PsiElement(CHARACTER)('e')
RegExpSetOptionsImpl: <(?suxU-idm)>
PsiElement(SET_OPTIONS)('(?')
RegExpOptionsImpl: <suxU-idm>
RegExpOptionsImpl: <suxU>
PsiElement(OPTIONS_ON)('suxU')
RegExpOptionsImpl: <-idm>
PsiElement(OPTIONS_OFF)('-idm')
PsiElement(GROUP_END)(')')
RegExpCharImpl: <t>