Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS: Add ECMAScript 2024 v Flag Operators for Regex Parsing #18899

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added support for -- subtraction opetor.
  • Loading branch information
Napalys committed Mar 3, 2025
commit 3664d507727ef46e2a8ed076c39dc057975b4aac
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.semmle.js.ast.regexp;

import com.semmle.js.ast.SourceLocation;
import java.util.List;

public class CharacterClassSubtraction extends RegExpTerm {
private final List<RegExpTerm> subtraction;

public CharacterClassSubtraction(SourceLocation loc, List<RegExpTerm> subtraction) {
super(loc, "CharacterClassSubtraction");
this.subtraction = subtraction;
}

@Override
public void accept(Visitor v) {
v.visit(this);
}

public List<RegExpTerm> getSubtraction() {
return subtraction;
}
}
Original file line number Diff line number Diff line change
@@ -65,4 +65,6 @@ public interface Visitor {
public void visit(CharacterClassQuotedString nd);

public void visit(CharacterClassIntersection nd);

public void visit(CharacterClassSubtraction nd);
}
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import com.semmle.js.ast.regexp.CharacterClassEscape;
import com.semmle.js.ast.regexp.CharacterClassQuotedString;
import com.semmle.js.ast.regexp.CharacterClassRange;
import com.semmle.js.ast.regexp.CharacterClassSubtraction;
import com.semmle.js.ast.regexp.Constant;
import com.semmle.js.ast.regexp.ControlEscape;
import com.semmle.js.ast.regexp.ControlLetter;
@@ -96,6 +97,7 @@ public RegExpExtractor(TrapWriter trapwriter, LocationManager locationManager) {
termkinds.put("UnicodePropertyEscape", 27);
termkinds.put("CharacterClassQuotedString", 28);
termkinds.put("CharacterClassIntersection", 29);
termkinds.put("CharacterClassSubtraction", 30);
}

private static final String[] errmsgs =
@@ -362,6 +364,14 @@ public void visit(CharacterClassIntersection nd) {
for (RegExpTerm element : nd.getIntersections())
visit(element, lbl, i++);
}

@Override
public void visit(CharacterClassSubtraction nd) {
Label lbl = extractTerm(nd, parent, idx);
int i = 0;
for (RegExpTerm element : nd.getSubtraction())
visit(element, lbl, i++);
}
}

public void extract(String src, SourceMap sourceMap, Node parent, boolean isSpeculativeParsing, String flags) {
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import com.semmle.js.ast.regexp.CharacterClassEscape;
import com.semmle.js.ast.regexp.CharacterClassQuotedString;
import com.semmle.js.ast.regexp.CharacterClassRange;
import com.semmle.js.ast.regexp.CharacterClassSubtraction;
import com.semmle.js.ast.regexp.Constant;
import com.semmle.js.ast.regexp.ControlEscape;
import com.semmle.js.ast.regexp.ControlLetter;
@@ -566,6 +567,7 @@ private RegExpTerm parseCharacterClass() {
private enum CharacterClassType {
STANDARD,
INTERSECTION,
SUBTRACTION,
}

// ECMA 2024 `v` flag allows nested character classes.
@@ -588,6 +590,10 @@ else if (lookahead("&&")) {
this.match("&&");
classType = CharacterClassType.INTERSECTION;
}
else if (lookahead("--")) {
this.match("--");
classType = CharacterClassType.SUBTRACTION;
}
else {
elements.add(this.parseCharacterClassElement());
}
@@ -597,6 +603,8 @@ else if (lookahead("&&")) {
switch (classType) {
case INTERSECTION:
return this.finishTerm(new CharacterClass(loc, Collections.singletonList(new CharacterClassIntersection(loc, elements)), inverted));
case SUBTRACTION:
return this.finishTerm(new CharacterClass(loc, Collections.singletonList(new CharacterClassSubtraction(loc, elements)), inverted));
case STANDARD:
default:
return this.finishTerm(new CharacterClass(loc, elements, inverted));
@@ -614,7 +622,7 @@ private RegExpTerm parseCharacterClassElement() {
return atom;
}
}
if (!this.lookahead("-]") && this.match("-") && !(atom instanceof CharacterClassEscape))
if (!this.lookahead("-]") && !this.lookahead("--") && this.match("-") && !(atom instanceof CharacterClassEscape))
return this.finishTerm(new CharacterClassRange(loc, atom, this.parseCharacterClassAtom()));
return atom;
}
Loading
Oops, something went wrong.