Skip to content

Commit

Permalink
Remove Char usage
Browse files Browse the repository at this point in the history
  • Loading branch information
warunalakshitha committed Mar 4, 2025
1 parent aba0cf1 commit 2dbcad5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
10 changes: 5 additions & 5 deletions ballerina/modules/parser/char_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@

public class CharReader {
private CharIterator iterator;
private string:Char?[] buffer;
private string?[] buffer;

public isolated function init(string document) {
self.iterator = document.iterator();
self.buffer = [];
}

public isolated function peek(int into = 0) returns string:Char? {
public isolated function peek(int into = 0) returns string? {
if self.buffer.length() > into {
return self.buffer[into];
}
int startIndex = self.buffer.length();
foreach int i in startIndex ... into {
string:Char? char = self.next();
string? char = self.next();
self.buffer.push(char);
}
return self.buffer[into];
}

public isolated function read() returns string:Char? {
public isolated function read() returns string? {
if self.buffer.length() > 0 {
return self.buffer.shift();
}
return self.next();
}

isolated function next() returns string:Char? {
isolated function next() returns string? {
CharIteratorNode? next = self.iterator.next();
if next is () {
return;
Expand Down
36 changes: 18 additions & 18 deletions ballerina/modules/parser/lexer.bal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Lexer {
}

isolated function readNextToken() returns Token|SyntaxError {
string:Char? char = self.charReader.peek();
string? char = self.charReader.peek();
if char == () {
return {
kind: T_EOF,
Expand All @@ -77,14 +77,14 @@ public class Lexer {
}
}

isolated function readSeparatorToken(string:Char char) returns Token? {
isolated function readSeparatorToken(string char) returns Token? {
Location location = self.currentLocation;
_ = self.readNextChar();
int kind = getTokenType(char);
return getToken(char, kind, location);
}

isolated function readSpecialCharacterToken(string:Char char) returns Token {
isolated function readSpecialCharacterToken(string char) returns Token {
Location location = self.currentLocation;
_ = self.readNextChar();
int kind = getTokenType(char);
Expand All @@ -104,7 +104,7 @@ public class Lexer {
Location location = self.currentLocation;
_ = self.readNextChar(); // Consume first double quote character

string:Char? char = self.charReader.peek();
string? char = self.charReader.peek();
boolean isEscaped = false;
int tokenTag = -1;
while char != () {
Expand Down Expand Up @@ -138,7 +138,7 @@ public class Lexer {
string line = "";
self.consumeIgnoreChars(3); // Consume first three double quote characters
int tokenTag = -1;
string:Char? currentChar = self.charReader.peek();
string? currentChar = self.charReader.peek();
boolean isEscaped = false;

while currentChar != () {
Expand Down Expand Up @@ -217,7 +217,7 @@ public class Lexer {
int tokenTag = -1;

while next is CharIteratorNode {
string:Char value = next.value;
string value = next.value;
if (symbolTable.hasKey(value)) {
tokenTag = symbolTable.get(value);
} else {
Expand All @@ -233,11 +233,11 @@ public class Lexer {
return i;
}

isolated function readNumericLiteral(string:Char firstChar) returns Token|SyntaxError {
isolated function readNumericLiteral(string firstChar) returns Token|SyntaxError {
Location location = self.currentLocation;
string numeral = firstChar; // Passing first char to handle negative numbers.
_ = self.readNextChar(); // Consume first char
string:Char? character = self.charReader.peek();
string? character = self.charReader.peek();
int tokenTag = -1;
while character != () {
if (symbolTable.hasKey(character)) {
Expand All @@ -263,15 +263,15 @@ public class Lexer {
return getToken(number, T_INT, location);
}

isolated function readFloatLiteral(string initialNumber, string:Char separator, Location location)
isolated function readFloatLiteral(string initialNumber, string separator, Location location)
returns Token|SyntaxError {
_ = self.readNextChar(); // Consume the separator character
boolean isExpExpected = separator == DOT;
boolean isDashExpected = !isExpExpected;
boolean isDigitExpected = true;

string numeral = initialNumber + separator;
string:Char? value = self.charReader.peek();
string? value = self.charReader.peek();
int tokenTag = -1;
while value != () {
if (symbolTable.hasKey(value)) {
Expand Down Expand Up @@ -308,7 +308,7 @@ public class Lexer {
isolated function readCommentToken() returns Token|SyntaxError {
Location location = self.currentLocation;
_ = self.readNextChar(); // Ignore first hash character
string:Char? character = self.charReader.peek();
string? character = self.charReader.peek();
string word = "#";
int tokenTag = -1;
while character != () {
Expand All @@ -334,7 +334,7 @@ public class Lexer {
int i = 0;
while i < 3 {
i += 1;
string:Char? c = self.readNextChar();
string? c = self.readNextChar();
if c == DOT {
ellipsis += c;
continue;
Expand All @@ -351,12 +351,12 @@ public class Lexer {
return getToken(ELLIPSIS, T_ELLIPSIS, location);
}

isolated function readIdentifierToken(string:Char firstChar) returns Token|SyntaxError {
isolated function readIdentifierToken(string firstChar) returns Token|SyntaxError {
Location location = self.currentLocation;
_ = self.readNextChar();
check validateFirstChar(firstChar, self.currentLocation);
string word = firstChar;
string:Char? char = self.charReader.peek();
string? char = self.charReader.peek();
int tokenTag = -1;
while char != () {
if (symbolTable.hasKey(char)) {
Expand Down Expand Up @@ -391,7 +391,7 @@ public class Lexer {
};
}

isolated function updateLocation(string:Char? char) {
isolated function updateLocation(string? char) {
if (char == ()) {
self.currentLocation.column += 1;
return;
Expand All @@ -409,14 +409,14 @@ public class Lexer {
}
}

isolated function readNextChar() returns string:Char? {
string:Char? char = self.charReader.read();
isolated function readNextChar() returns string? {
string? char = self.charReader.read();
self.updateLocation(char);
return char;
}
}

isolated function getTokenType(string:Char? value) returns int {
isolated function getTokenType(string? value) returns int {
if value == () {
return T_EOF;
}
Expand Down
2 changes: 1 addition & 1 deletion ballerina/modules/parser/symbol_table.bal
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public final map<int> & readonly symbolTable = {
",": COMMA_TAG,
"e": EXP_TAG,
"E": EXP_TAG
};
};
6 changes: 3 additions & 3 deletions ballerina/modules/parser/tests/char_reader_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ballerina/test;
isolated function testCharReaderForSimpleString() {
string s = "Hello";
CharReader reader = new (s);
string:Char? c = reader.read();
string? c = reader.read();
string expectedChar = "H";
test:assertEquals(c, expectedChar);

Expand Down Expand Up @@ -52,7 +52,7 @@ isolated function testCharReaderForSimpleString() {
isolated function testCharReaderForEof() {
string s = "";
CharReader reader = new (s);
string:Char? c = reader.read();
string? c = reader.read();
test:assertEquals(c, ());
}

Expand All @@ -62,7 +62,7 @@ isolated function testCharReaderForEof() {
isolated function testCharReaderForNewLine() {
string s = "\n\n\n";
CharReader reader = new (s);
string:Char? c = reader.read();
string? c = reader.read();
c = reader.read();
string expectedChar = "\n";
test:assertEquals(c, expectedChar);
Expand Down
2 changes: 1 addition & 1 deletion ballerina/modules/parser/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public type Scalar int|float|decimal|string|boolean;

type CharIteratorNode record {
string:Char value;
string value;
};

type CharIterator object {
Expand Down

0 comments on commit 2dbcad5

Please sign in to comment.