Skip to content

Commit

Permalink
Escape in BLangNodeBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Nov 22, 2023
1 parent 3957cf1 commit cb6948b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6049,10 +6049,10 @@ private BLangIdentifier createIdentifier(Location pos, String value, String orig
}

if (value.startsWith(IDENTIFIER_LITERAL_PREFIX)) {
bLIdentifer.setValue(Utils.unescapeUnicodeCodepoints(value.substring(1)));
bLIdentifer.setValue(Utils.unescapeBallerina(value.substring(1)));
bLIdentifer.setLiteral(true);
} else {
bLIdentifer.setValue(Utils.unescapeUnicodeCodepoints(value));
bLIdentifer.setValue(Utils.unescapeBallerina(value));
bLIdentifer.setLiteral(false);
}
bLIdentifer.setOriginalValue(originalValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private static String encodeIdentifier(String identifier) {
* @return a new unescaped {@code String}, {@code null} if null string input
*/
public static String unescapeJava(String str) {
return StringEscapeUtils.unescapeJava(str);
String result = str.replaceAll("\\\\(\\d)", "$1");
return StringEscapeUtils.unescapeJava(result);
}

private static Identifier encodeGeneratedName(String identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public void testInvalidImportOnMultipleFiles() {
assertEquals(result.getErrorCount(), index);
}

@Test(description = "Test identifiers containing escape character")
public void testEscapedIdentifier() {
CompileResult result = BCompileUtil.compile("test-src/identifiers/identifier_with_escape_char.bal");
int index = 0;
validateError(result, index++, "redeclared symbol 'a3'", 19, 9);
validateError(result, index++, "redeclared symbol 'student-performance'", 21, 9);
assertEquals(result.getErrorCount(), index);
}

@Test(dataProvider = "functionsWithSelfAsIdentifier")
public void testSelfAsIdentifier(String function) {
CompileResult result = BCompileUtil.compile("test-src/identifiers/self_as_identifier.bal");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

public function foo() {
int a\3 = 0;
int a3 = 2;
int student\-performance = 3;
int student\u{002D}performance = 2;
}

0 comments on commit cb6948b

Please sign in to comment.