Created
May 6, 2022 22:23
-
-
Save brubsby/fc68974bf7b35d01bcbbc32070d7a161 to your computer and use it in GitHub Desktop.
pellet (date format shorthand) peggy grammar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// peggy grammar to parse an esoteric date format shorthand named pellet | |
// year day of month hour of day (0-23) | |
// v v v | |
// [0-9]{0,4}[a-lA-L]{1}[0-9]{1,2}[a-dA-D]{0,1}[0-9]{0,2} | |
// ^ ^ | |
// month (1-12 as char) char quarter hour (00,15,30,45) | |
// | |
// example: 22E6D14 | |
// 2022 May 6 14:45 | |
// | |
// this is intentionally an ambiguous shorthand | |
// more examples: | |
// example: actual: | |
// E6 2022 May 6(current year assumed) | |
// 21E6 2021 May 6 | |
// 2023E6A4 2023 May 6 04:00 | |
// 123E6B14 0123 May 6 14:15 | |
// 23E6D4 2023 May 6 04:45 | |
{{ | |
function date(year, month, day, quarter, hour) { | |
year = !year && year !== 0 ? new Date().getFullYear() : year | |
year = year < 100? year + 2000 : year | |
let minute = quarter * 15 || 0 | |
hour = hour || 0 | |
let retDate = new Date(year, month, day, hour, minute) | |
retDate.setFullYear(year) | |
return retDate | |
} | |
function stringToNum(str) { | |
return str.toUpperCase().charCodeAt(0) - 65 | |
} | |
}} | |
Expression | |
= year:Year month:Month day:Day quarter:Quarter hour:Hour | |
{ return date(year, month, day, quarter, hour) } | |
/ year:Year month:Month day:Day | |
{ return date(year, month, day) } | |
/ month:Month day:Day quarter:Quarter hour:Hour | |
{ return date(null, month, day, quarter, hour) } | |
/ month:Month day:Day | |
{ return date(null, month, day) } | |
/ month:Month | |
{ return date(null, month) } | |
Year "year" | |
= [0] { return 0 } | |
/ !"0" n:$[0-9]+ { return parseInt(n, 10); } | |
Hour "hour" | |
= [0] { return parseInt(text(), 10); } | |
/ [1][1-9] { return parseInt(text(), 10); } | |
/ [2][1-3] { return parseInt(text(), 10); } | |
/ [1-9] { return parseInt(text(), 10); } | |
Quarter "quarter" | |
= quarter:[a-dA-D] { return stringToNum(quarter) } | |
Month "month" | |
= month:[a-lA-L] { return stringToNum(month) } | |
Day "day" | |
= [0] { return parseInt(text(), 10); } | |
/ [1-2][1-9] { return parseInt(text(), 10); } | |
/ [3][0-1] { return parseInt(text(), 10); } | |
/ [1-9] { return parseInt(text(), 10); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment