Skip to content

Commit

Permalink
Support the new line character in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunayf committed Mar 8, 2024
1 parent fd6b3bb commit cfdee1f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ballerina/modules/lexer/scan.bal
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ isolated function scanPlanarChar(LexerState state) returns boolean|LexicalError
}

// Step back from the white spaces if EOL or ':' is reached
if state.peek() == () || state.peek() == "\n" {
if state.peek() == () || state.isNewLine() {
state.forward(-numWhitespace);
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions ballerina/modules/lexer/state.bal
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,10 @@ public class LexerState {
}

public isolated function isFlowCollection() returns boolean => self.numOpenedFlowCollections > 0;

# Check if the current character is a new line.
# This should be replaced by the os module once it supports an API: #4931.
#
# + return - True if the current character is a new line
public isolated function isNewLine() returns boolean => self.peek() == "\n" || self.peek() == "\r\n";
}
2 changes: 1 addition & 1 deletion ballerina/modules/parser/state.bal
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class ParserState {
if self.lineIndex >= self.numLines {
return generateGrammarError(self, message);
}
string[] lines = <string[]>self.lines;
string[] lines = self.lines;
line = lines[self.lineIndex];

self.explicitDoc = false;
Expand Down
6 changes: 2 additions & 4 deletions ballerina/tests/api_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import ballerina/io;
import ballerina/test;

@test:Config {
groups: ["api"],
enable: false
groups: ["api"]
}
function testReadYamlString() returns error? {
string input = string `
Expand Down Expand Up @@ -76,8 +75,7 @@ function testWriteYamlFile() returns error? {

@test:Config {
dataProvider: yamlSchemaDataGen,
groups: ["api"],
enable: false
groups: ["api"]
}
function testReadYAMLSchema(YAMLSchema schema, json expectedOutput) returns error? {
string input = string `
Expand Down
6 changes: 3 additions & 3 deletions ballerina/yaml.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import yaml.serializer;
# + config - Configuration for reading a YAML file
# + return - YAML map object on success. Else, returns an error
public isolated function readString(string yamlString, *ReadConfig config) returns json|Error {
io:ReadableByteChannel readableChannel = check io:createReadableChannel(yamlString.toBytes());
io:ReadableCharacterChannel readableCharChannel = new (readableChannel, io:DEFAULT_ENCODING);
string[] lines = check readableCharChannel.readAllLines();
io:ReadableByteChannel byteChannel = check io:createReadableChannel(yamlString.toBytes());
io:ReadableCharacterChannel charChannel = new (byteChannel, io:DEFAULT_ENCODING);
string[] lines = check charChannel.readAllLines();
return readLines(lines, config);
}

Expand Down

0 comments on commit cfdee1f

Please sign in to comment.