Skip to content

Commit

Permalink
Merge pull request #12802 from sanket143/date
Browse files Browse the repository at this point in the history
[TOML Parser] Updated fieldTag and added date support

Renamed `fieldDate` to `fieldDateTime` and added date support in TOML Parser
solved #12801 and added ``date`` support as stated in #7104 

[Contributed by @sanket143]
[Reviewed by @ben-albrecht]
  • Loading branch information
ben-albrecht committed Apr 22, 2019
2 parents 68938ea + 437df79 commit 4390a79
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
48 changes: 43 additions & 5 deletions modules/packages/TOML.chpl
Expand Up @@ -144,6 +144,7 @@ module TomlParser {
kv = compile('|'.join(doubleQuotes, singleQuotes, digit, keys)),
dt = compile('^\\d{4}-\\d{2}-\\d{2}[ T]\\d{2}:\\d{2}:\\d{2}$'),
realNum = compile("\\+\\d*\\.\\d+|\\-\\d*\\.\\d+|\\d*\\.\\d+"),
ld = compile('^\\d{4}-\\d{2}-\\d{2}$'),
ints = compile("(\\d+|\\+\\d+|\\-\\d+)"),
inBrackets = compile("(\\[.*?\\])"),
corner = compile("(\\[.+\\])"),
Expand Down Expand Up @@ -359,6 +360,16 @@ module TomlParser {
Datetime = date;
return Datetime;
}
// Date
else if ld.match(val) {
var raw = getToken(source).split("-");
var d = new date(raw[1]: int,
raw[2]: int,
raw[3]: int);
var Date: unmanaged Toml;
Date = d;
return Date;
}
// Real
else if realNum.match(val) {
var token = getToken(source);
Expand Down Expand Up @@ -433,7 +444,8 @@ pragma "no doc"
fieldReal,
fieldString,
fieldEmpty,
fieldDate };
fieldDate,
fieldDateTime };
use fieldtag;

pragma "no doc"
Expand Down Expand Up @@ -476,12 +488,22 @@ pragma "no doc"
}
}

pragma "no doc"
proc =(ref t: unmanaged Toml, ld: date) {
if t == nil {
t = new unmanaged Toml(ld);
} else {
t.tag = fieldDate;
t.ld = ld;
}
}

pragma "no doc"
proc =(ref t: unmanaged Toml, dt: datetime) {
if t == nil {
t = new unmanaged Toml(dt);
} else {
t.tag = fieldDate;
t.tag = fieldDateTime;
t.dt = dt;
}
}
Expand Down Expand Up @@ -520,6 +542,7 @@ used to recursively hold tables and respective values
boo: bool,
re: real,
s: string,
ld: date,
dt: datetime,
dom: domain(1),
arr: [dom] unmanaged Toml,
Expand All @@ -545,10 +568,16 @@ used to recursively hold tables and respective values
this.tag = fieldToml;
}

// Date
proc init(ld: date) {
this.ld = ld;
this.tag = fieldDate;
}

// Datetime
proc init(dt: datetime) {
this.dt = dt;
this.tag = fieldDate;
this.tag = fieldDateTime;
}

// Int
Expand Down Expand Up @@ -585,6 +614,7 @@ used to recursively hold tables and respective values
this.re = root.re;
this.dom = root.dom;
for idx in root.dom do this.arr[idx] = new unmanaged Toml(root.arr[idx]);
this.ld = root.ld;
this.dt = root.dt;
this.s = root.s;
this.D = root.D;
Expand Down Expand Up @@ -760,6 +790,9 @@ used to recursively hold tables and respective values
when fieldDate {
f.write(key, ' = ', toString(value));
}
when fieldDateTime {
f.write(key, ' = ', toString(value));
}
otherwise {
throw new owned TomlError("Not yet supported");
}
Expand Down Expand Up @@ -813,6 +846,9 @@ used to recursively hold tables and respective values
when fieldDate {
f.writef('%s"%s": {"type": "%s", "value": "%s"}', ' '*indent, key, value.tomlType, toString(value));
}
when fieldDateTime {
f.writef('%s"%s": {"type": "%s", "value": "%s"}', ' '*indent, key, value.tomlType, toString(value));
}
otherwise {
throw new owned TomlError("Not yet supported");
}
Expand Down Expand Up @@ -848,7 +884,8 @@ used to recursively hold tables and respective values
when fieldReal do return val.re:string;
when fieldString do return ('"' + val.s + '"');
when fieldEmpty do return ""; // empty
when fieldDate do return val.dt.isoformat();
when fieldDate do return val.ld.isoformat();
when fieldDateTime do return val.dt.isoformat();
otherwise {
throw new owned TomlError("Error in printing " + val.s);
return val.s;
Expand Down Expand Up @@ -886,7 +923,8 @@ used to recursively hold tables and respective values
when fieldReal do return 'float';
when fieldString do return 'string';
when fieldEmpty do return 'empty';
when fieldDate do return 'datetime';
when fieldDate do return 'date';
when fieldDateTime do return 'datetime';
when fieldToml do return 'toml';
otherwise {
throw new owned TomlError("Unknown type");
Expand Down
16 changes: 16 additions & 0 deletions test/library/packages/TOML/test/date.chpl
@@ -0,0 +1,16 @@

use TOML;

config const str: string = """[owner]
name = "Foo Bar"
dob = 2000-04-30
""";

proc main() {
var TomlData = parseToml(str);
var dob = TomlData["owner"]["dob"];
writeln(dob.toString());

delete TomlData;
}

1 change: 1 addition & 0 deletions test/library/packages/TOML/test/date.good
@@ -0,0 +1 @@
2000-04-30

0 comments on commit 4390a79

Please sign in to comment.