Skip to content

TOD literlas can now be parsed with 2 and 3 sections (#355) #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/codegen/tests/code_gen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ y := D#1970-01-01;
z := DATE_AND_TIME#1984-10-01-20:15:14;
z := DT#1970-01-01-16:20:04.123;
z := DT#1970-01-01-16:20:04.123456789;
z := DATE_AND_TIME#2000-01-01-20:15:00;
z := DATE_AND_TIME#2000-01-01-20:15;
z := DT#2000-01-01-20:15;
END_PROGRAM
"#,
);
Expand All @@ -600,6 +603,9 @@ entry:
store i64 465509714000, i64* %z, align 4
store i64 58804123, i64* %z, align 4
store i64 58804123, i64* %z, align 4
store i64 946757700000, i64* %z, align 4
store i64 946757700000, i64* %z, align 4
store i64 946757700000, i64* %z, align 4
ret void
}
"#;
Expand Down Expand Up @@ -718,6 +724,10 @@ y := TIME_OF_DAY#00:00:00;
y := TOD#01:00:00;
y := TIME_OF_DAY#01:00:00.001;
y := TOD#1:1:1;
y := TIME_OF_DAY#20:15:00;
y := TIME_OF_DAY#20:15;
y := TOD#11:11:00;
y := TOD#11:11;
END_PROGRAM
"#,
);
Expand All @@ -736,6 +746,10 @@ entry:
store i64 3600000, i64* %y, align 4
store i64 3600001, i64* %y, align 4
store i64 3661000, i64* %y, align 4
store i64 72900000, i64* %y, align 4
store i64 72900000, i64* %y, align 4
store i64 40260000, i64* %y, align 4
store i64 40260000, i64* %y, align 4
ret void
}
"#;
Expand Down
10 changes: 8 additions & 2 deletions src/lexer/tests/lexer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ fn date_literals_test() {

#[test]
fn date_and_time_literals_test() {
let mut lexer = lex("DATE_AND_TIME#1984-10-01-20:15:12 DT#1-1-1-1:1:1 DT#1-1-1-1:1:1.123");
let mut lexer = lex("DATE_AND_TIME#1984-10-01-20:15:12 DT#1-1-1-1:1:1 DT#1-1-1-1:1:1.123 DATE_AND_TIME#2000-01-01-20:15");
assert_eq!(lexer.token, LiteralDateAndTime);
lexer.advance();
assert_eq!(lexer.token, LiteralDateAndTime);
lexer.advance();
assert_eq!(lexer.token, LiteralDateAndTime);
Expand All @@ -307,7 +309,11 @@ fn date_and_time_literals_test() {

#[test]
fn time_of_day_literals_test() {
let mut lexer = lex("TIME_OF_DAY#20:15:12 TOD#1:1:1 TOD#1:1:1.123");
let mut lexer = lex("TIME_OF_DAY#20:15:12 TOD#1:1:1 TOD#1:1:1.123 TIME_OF_DAY#12:13 TOD#10:20");
assert_eq!(lexer.token, LiteralTimeOfDay);
lexer.advance();
assert_eq!(lexer.token, LiteralTimeOfDay);
lexer.advance();
assert_eq!(lexer.token, LiteralTimeOfDay);
lexer.advance();
assert_eq!(lexer.token, LiteralTimeOfDay);
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ pub enum Token {
LiteralDate,

#[regex(
"(DATE_AND_TIME|DT)#\\d+-\\d+-\\d+-\\d+:\\d+:\\d+(\\.\\d+)?",
"(DATE_AND_TIME|DT)#\\d+-\\d+-\\d+-\\d+:\\d+(:\\d+(\\.\\d+)?)?",
ignore(case)
)]
LiteralDateAndTime,

#[regex("(TIME_OF_DAY|TOD)#\\d+:\\d+:\\d+(\\.\\d+)?", ignore(case))]
#[regex("(TIME_OF_DAY|TOD)#\\d+:\\d+(:\\d+(\\.\\d+)?)?", ignore(case))]
LiteralTimeOfDay,

#[regex("T(IME)?#-?(\\d+(\\.\\d+)?(d|h|ms|m|s|us|ns))+", ignore(case))]
Expand Down
33 changes: 22 additions & 11 deletions src/parser/expressions_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
parser::parse_any_in_region,
Diagnostic,
};
use core::str::Split;
use regex::{Captures, Regex};
use std::str::FromStr;

Expand Down Expand Up @@ -620,12 +621,7 @@ fn parse_literal_date_and_time(lexer: &mut ParseSession) -> Result<AstStatement,

//we can safely expect 3 numbers
let mut segments = time.split(':');
let hour = parse_number::<u32>(segments.next().unwrap(), &location)?;
let min = parse_number::<u32>(segments.next().unwrap(), &location)?;
let sec_fraction = parse_number::<f64>(segments.next().unwrap(), &location)?;

let sec = sec_fraction as u32;
let milli = ((sec_fraction - sec as f64) * 1000_f64) as u32;
let (hour, min, sec, milli) = parse_time_of_day(&mut segments, &location)?;

Ok(AstStatement::LiteralDateAndTime {
location,
Expand Down Expand Up @@ -658,21 +654,36 @@ fn parse_literal_time_of_day(lexer: &mut ParseSession) -> Result<AstStatement, D
let (_, slice) = slice.split_at(hash_location + 1); //get rid of the prefix

let mut segments = slice.split(':');
let hour = parse_number::<u32>(segments.next().unwrap(), &location)?;
let min = parse_number::<u32>(segments.next().unwrap(), &location)?;
let (hour, min, sec, milli) = parse_time_of_day(&mut segments, &location)?;

let sec = parse_number::<f64>(segments.next().unwrap(), &location)?;
let milli = (sec.fract() * 1000_f64) as u32;
Ok(AstStatement::LiteralTimeOfDay {
hour,
min,
sec: sec.floor() as u32,
sec,
milli,
location,
id: lexer.next_id(),
})
}

fn parse_time_of_day(
time: &mut Split<char>,
location: &SourceRange,
) -> Result<(u32, u32, u32, u32), Diagnostic> {
let hour = parse_number::<u32>(time.next().unwrap(), location)?;
let min = parse_number::<u32>(time.next().unwrap(), location)?;

// doesn't necessarily have to have seconds, e.g [12:00] is also valid
let sec = match time.next() {
Some(v) => parse_number::<f64>(v, location)?,
None => 0.0,
};

let milli = (sec.fract() * 1000_f64) as u32;

Ok((hour, min, sec.floor() as u32, milli))
}

fn parse_literal_time(lexer: &mut ParseSession) -> Result<AstStatement, Diagnostic> {
const POS_D: usize = 0;
const POS_H: usize = 1;
Expand Down
17 changes: 17 additions & 0 deletions src/parser/tests/expressions_parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,7 @@ fn literal_time_of_day_test() {
TIME_OF_DAY#04:16:22;
TIME_OF_DAY#04:16:22.1;
TIME_OF_DAY#04:16:22.001;
TIME_OF_DAY#04:16;
END_PROGRAM
";
let result = parse(src).0;
Expand Down Expand Up @@ -1020,6 +1021,12 @@ fn literal_time_of_day_test() {
sec: 22,
milli: 1,
},
LiteralTimeOfDay {
hour: 4,
min: 16,
sec: 0,
milli: 0,
},
]"#;
assert_eq!(ast_string, expected_ast);
}
Expand All @@ -1031,6 +1038,7 @@ fn literal_date_and_time_test() {
DATE_AND_TIME#1984-10-01-16:40:22;
DT#2021-04-20-22:33:14;
DT#2021-04-20-22:33:14.999;
DATE_AND_TIME#2000-01-01-20:15;
END_PROGRAM
";
let result = parse(src).0;
Expand Down Expand Up @@ -1063,6 +1071,15 @@ fn literal_date_and_time_test() {
sec: 14,
milli: 999,
},
LiteralDateAndTime {
year: 2000,
month: 1,
day: 1,
hour: 20,
min: 15,
sec: 0,
milli: 0,
},
]"#;
assert_eq!(ast_string, expected_ast);
}
Expand Down
4 changes: 4 additions & 0 deletions src/resolver/tests/resolve_literals_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ fn date_literals_are_annotated() {
TIME#-12m;
TOD#00:00:12;
TIME_OF_DAY#04:16:22;
TIME_OF_DAY#04:16;
DATE_AND_TIME#1984-10-01-16:40:22;
DT#2021-04-20-22:33:14;
DATE_AND_TIME#2000-01-01-20:15;
DATE#1984-10-01;
D#2021-04-20;
END_PROGRAM",
Expand All @@ -106,6 +108,8 @@ fn date_literals_are_annotated() {
"TIME",
"TIME_OF_DAY",
"TIME_OF_DAY",
"TIME_OF_DAY",
"DATE_AND_TIME",
"DATE_AND_TIME",
"DATE_AND_TIME",
"DATE",
Expand Down