Skip to content

Instantly share code, notes, and snippets.

@1ec5
Last active June 1, 2023 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1ec5/a8aa66b6b286783949aa18918f513327 to your computer and use it in GitHub Desktop.
Save 1ec5/a8aa66b6b286783949aa18918f513327 to your computer and use it in GitHub Desktop.
Peggy grammar for opening_hours syntax
// Time domain
time_domain
= rule_sequence|1.., any_rule_separator|
rule_sequence
= selector_sequence space rule_modifier
// Rule separators
any_rule_separator
= normal_rule_separator / additional_rule_separator / fallback_rule_separator
normal_rule_separator
= ";" space
additional_rule_separator
= "," space
fallback_rule_separator
= space "||" space
// Rule modifiers
rule_modifier
= "open" (space comment)?
/ ("closed"/"off") (space comment)?
/ "unknown" (space comment)?
/ comment
/ ""
// Selectors
selector_sequence
= "24/7"
/ wide_range_selectors small_range_selectors
wide_range_selectors
= year_selector? monthday_selector? week_selector? separator_for_readability?
/ comment ":"
small_range_selectors
= weekday_selector space time_selector
/ weekday_selector
/ time_selector
separator_for_readability
= ":"
// Time selector
time_selector
= timespan|1.., ","|
timespan
= time "+"
/ time "-" extended_time "+"
/ time "-" extended_time "/" hour_minutes
/ time "-" extended_time "/" minute
/ time "-" extended_time
/ time
time
= hour_minutes / variable_time
extended_time
= extended_hour_minutes / variable_time
variable_time
= "(" event plus_or_minus hour_minutes ")"
/ event
event
= "dawn"/"sunrise"/"sunset"/"dusk"
// Weekday selector
weekday_selector
= weekday_sequence "," holiday_sequence
/ weekday_sequence
/ holiday_sequence "," weekday_sequence
/ holiday_sequence space weekday_sequence
/ holiday_sequence
weekday_sequence
= weekday_range|1.., ","|
weekday_range
= wday "-" wday
/ wday "[" nth_entry|1.., ","| "]" day_offset?
/ wday "[" nth_entry|1.., ","| "]"
/ wday
holiday_sequence
= holiday|1.., ","|
holiday
= public_holiday day_offset?
/ school_holiday
public_holiday
= "PH"
school_holiday
= "SH"
nth_entry
= nth "-" nth
/ nth
/ "-" nth
nth
= [1-5] {
return parseInt(text(), 10);
}
day_offset
= space plus_or_minus positive_number space "day" "s"?
// Week selector
week_selector
= "week" week|1.., ","|
week
= weeknum "-" weeknum "/" positive_number
/ weeknum "-" weeknum
/ weeknum
// Month selector
monthday_selector
= monthday_range|1.., ","|
monthday_range
= date_from date_offset? "+"
/ date_from date_offset? "-" date_to date_offset?
/ date_from
/ year? month "-" month
/ year? month
date_offset
= (plus_or_minus wday)? day_offset?
date_from
= year? month daynum
/ year? variable_date
date_to
= date_from
/ daynum
variable_date
= "easter"
// Year selector
year_selector
= year_range|1.., ","|
year_range
= year "-" year "/" positive_number
/ year "-" year
/ year "+"
/ year
// Basic elements
plus_or_minus
= [+-]
hour
= ([01][0-9]/"2"[0-4]) {
return parseInt(text(), 10);
}
extended_hour
= ([0-3][0-9]/"4"[0-8]) {
return parseInt(text(), 10);
}
minute
= [0-5][0-9] {
return parseInt(text(), 10);
}
hour_minutes
= hour ":" minute
extended_hour_minutes
= extended_hour ":" minute
wday
= "Su"/"Mo"/"Tu"/"We"/"Th"/"Fr"/"Sa"
daynum
= ([0-2][0-9]/"3"[01]) {
return parseInt(text(), 10);
}
weeknum
= ([0-4][0-9]/"5"[0-3]) {
return parseInt(text(), 10);
}
month
= "Jan"/"Feb"/"Mar"/"Apr"/"May"/"Jun"/"Jul"/"Aug"/"Sep"/"Oct"/"Nov"/"Dec"
year
= ("19"[0-9]|2|/[2-9][0-9]|3|) {
return parseInt(text(), 10);
}
positive_number
= [1-9][0-9]* {
return parseInt(text(), 10);
}
comment
= "\"" ($[^"]+) "\""
space
= " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment