Code review comment: Since this will be called more than once (in a general scenario when compiling a Ballerina program), I think it is better to compile this Regex once (declare as a top level variable) to reduce the overhead. WDYT?

Code before review:
public void exitSimpleLiteral(BallerinaParser.SimpleLiteralContext ctx) {
if (isInErrorState) {
return;
}
TerminalNode node;
DiagnosticPos pos = getCurrentPos(ctx);
Set<Whitespace> ws = getWS(ctx);
Object value;
BallerinaParser.IntegerLiteralContext integerLiteralContext = ctx.integerLiteral();
if (integerLiteralContext != null && (value = getIntegerLiteral(ctx, ctx.integerLiteral())) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.INT, value, ctx.getText());
} else if (ctx.floatingPointLiteral() != null) {
if ((node = ctx.floatingPointLiteral().DecimalFloatingPointNumber()) != null) {
String nodeValue = getNodeValue(ctx, node);
int literalTypeTag = NumericLiteralSupport.isDecimalDiscriminated(nodeValue)
? TypeTags.DECIMAL : TypeTags.FLOAT;
this.pkgBuilder.addLiteralValue(pos, ws, literalTypeTag, nodeValue, node.getText());
} else if ((node = ctx.floatingPointLiteral().HexadecimalFloatingPointLiteral()) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.FLOAT, getHexNodeValue(ctx, node), node.getText());
}
} else if ((node = ctx.BooleanLiteral()) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.BOOLEAN, Boolean.parseBoolean(node.getText()),
node.getText());
} else if ((node = ctx.QuotedStringLiteral()) != null) {
String text = node.getText();
text = text.substring(1, text.length() - 1);
String originalText = text;
Pattern pattern = Pattern.compile(Constants.UNICODE_REGEX);
Matcher matcher = pattern.matcher(text);
int position = 0;
while (matcher.find(position)) {
String hexStringVal = matcher.group(1);
int hexDecimalVal = Integer.parseInt(hexStringVal, 16);
if ((hexDecimalVal >= 0xD800 && hexDecimalVal <= 0xDFFF) || hexDecimalVal > 0x10FFFF) {
String hexStringWithBraces = matcher.group(0);
int offset = originalText.indexOf(hexStringWithBraces) + 1;
dlog.error(new DiagnosticPos(diagnosticSrc, pos.sLine, pos.eLine, pos.sCol + offset,
pos.sCol + offset + hexStringWithBraces.length()),
DiagnosticCode.INVALID_UNICODE, hexStringWithBraces);
}
text = matcher.replaceFirst("\\\\u" + fillWithZeros(hexStringVal));
position = matcher.end() - 2;
matcher = pattern.matcher(text);
}
text = StringEscapeUtils.unescapeJava(text);
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.STRING, text, node.getText());
} else if (ctx.NullLiteral() != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.NIL, null, "null");
} else if (ctx.nilLiteral() != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.NIL, null, "()");
} else if (ctx.blobLiteral() != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.BYTE_ARRAY, ctx.blobLiteral().getText());
}
}

Line under review: Pattern pattern = Pattern.compile(Constants.UNICODE_REGEX);

Code after addressing the review:
public void exitSimpleLiteral(BallerinaParser.SimpleLiteralContext ctx) {
if (isInErrorState) {
return;
}
TerminalNode node;
DiagnosticPos pos = getCurrentPos(ctx);
Set<Whitespace> ws = getWS(ctx);
Object value;
BallerinaParser.IntegerLiteralContext integerLiteralContext = ctx.integerLiteral();
if (integerLiteralContext != null && (value = getIntegerLiteral(ctx, ctx.integerLiteral())) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.INT, value, ctx.getText());
} else if (ctx.floatingPointLiteral() != null) {
if ((node = ctx.floatingPointLiteral().DecimalFloatingPointNumber()) != null) {
String nodeValue = getNodeValue(ctx, node);
int literalTypeTag = NumericLiteralSupport.isDecimalDiscriminated(nodeValue)
? TypeTags.DECIMAL : TypeTags.FLOAT;
this.pkgBuilder.addLiteralValue(pos, ws, literalTypeTag, nodeValue, node.getText());
} else if ((node = ctx.floatingPointLiteral().HexadecimalFloatingPointLiteral()) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.FLOAT, getHexNodeValue(ctx, node), node.getText());
}
} else if ((node = ctx.BooleanLiteral()) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.BOOLEAN, Boolean.parseBoolean(node.getText()),
node.getText());
} else if ((node = ctx.QuotedStringLiteral()) != null) {
String text = node.getText();
text = text.substring(1, text.length() - 1);
String originalText = text;
Matcher matcher = pattern.matcher(text);
int position = 0;
while (matcher.find(position)) {
String hexStringVal = matcher.group(1);
int hexDecimalVal = Integer.parseInt(hexStringVal, 16);
if ((hexDecimalVal >= Constants.MIN_UNICODE && hexDecimalVal <= Constants.MIDDLE_LIMIT_UNICODE)
|| hexDecimalVal > Constants.MAX_UNICODE) {
String hexStringWithBraces = matcher.group(0);
int offset = originalText.indexOf(hexStringWithBraces) + 1;
dlog.error(new DiagnosticPos(diagnosticSrc, pos.sLine, pos.eLine, pos.sCol + offset,
pos.sCol + offset + hexStringWithBraces.length()),
DiagnosticCode.INVALID_UNICODE, hexStringWithBraces);
}
text = matcher.replaceFirst("\\\\u" + fillWithZeros(hexStringVal));
position = matcher.end() - 2;
matcher = pattern.matcher(text);
}
text = StringEscapeUtils.unescapeJava(text);
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.STRING, text, node.getText());
} else if (ctx.NullLiteral() != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.NIL, null, "null");
} else if (ctx.nilLiteral() != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.NIL, null, "()");
} else if (ctx.blobLiteral() != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.BYTE_ARRAY, ctx.blobLiteral().getText());
}
}