Skip to content
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

relative day and month expressions #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
115 changes: 97 additions & 18 deletions lib/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ DurationString
DurationAbbrev
= "ms"
/ "s"
/ "m"
/ "m" !"o"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change, and the others added in subsequent commit, can be eliminated by including Weekday in CalendarUnit, so it is matched before DurationAbbrev, eg,

CalendarUnit
    = string:CalendarString "s" {
        return string;
    }
    / CalendarString
    / Weekday
    / CalendarAbbrev

/ "h"
/ "d"
/ "w"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove stray spaces introduced here and elsewhere

CalendarUnit
= string:CalendarString "s" {
return string;
Expand All @@ -159,6 +159,33 @@ CalendarAbbrev
/ "M"
/ "y"

Weekday
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much terser way to specify these strings:

( "mon"i "day"? ) { return  1 }

(this would also match MON, but I don't think that is a bad thing)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weekday --> WeekdayString

= ( "monday" / "Monday" / "Mon" / "mon" ) { return 1 }
/ ( "tuesday" / "Tuesday" / "Tue" / "tue") { return 2 }
/ ( "wednesday"/ "Wednesday" / "Wed" / "wed") { return 3 }
/ ( "thursday"/ "Thursday" / "Thu" / "thu") { return 4 }
/ ( "friday"/ "Friday" / "Fri" / "fri") { return 5 }
/ ( "saturday"/ "Saturday" / "Sat" / "sat") { return 6 }
/ ( "sunday"/ "Sunday" / "Sun" / "sun") { return 7 }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to return a standardized weekday literal for all flavors of match, eg, "sunday" rather than 7, and convert this to a numeric offset in the generator using momentjs. But in any event, sunday's offset here should be 0 to agree with momentjs's indexing.


Month
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same suggestions as for Weekday.

= ( "january"/ "January"/ "jan"/ "Jan") { return 1 }
/ ("february"/ "February"/ "feb"/ "Feb") { return 2 }
/ ("march"/ "March"/ "mar"/ "Mar") { return 3 }
/ ("april"/ "April"/ "apr"/ "Apr") { return 4 }
/ ("may"/ "May") { return 5 }
/ ("june"/ "June"/ "jun"/ "Jun") { return 6 }
/ ("july"/ "July"/ "jul"/ "Jul") { return 7 }
/ ("august"/ "August"/ "aug"/ "Aug") { return 8 }
/ ("september"/ "September"/ "sep"/ "Sep") { return 9 }
/ ("october"/ "October"/ "oct"/ "Oct") { return 10 }
/ ("november"/ "November"/ "nov"/ "Nov") { return 11 }
/ ("december"/ "December"/ "dec"/ "Dec") { return 12 }


Unit = CalendarUnit / DurationUnit


HumanDuration
= num:Integer? _ unit:CalendarUnit {
return {
Expand Down Expand Up @@ -246,26 +273,16 @@ CalendarExpression
valueType: "moment"
};
}
/ "this" _ unit:( CalendarUnit / DurationUnit ) {
/ "next" _ unit:Unit {
var wdawd
return {
type: "CalendarExpression",
valueType: "moment",
direction: "down",
unit: unit,
expression: {
type: "NowLiteral"
}
};
}
/ "last" _ unit:( CalendarUnit / DurationUnit ) {
return {
type: "CalendarExpression",
valueType: "moment",
direction: "down",
unit: unit,
unit: unit.unit,
expression: {
type: "BinaryExpression",
operator: "-",
operator: "+",
left: {
type: "NowLiteral"
},
Expand All @@ -277,15 +294,75 @@ CalendarExpression
}
};
}
/ "next" _ unit:( CalendarUnit / DurationUnit ) {
/ month: Month {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the month/weekday handling in last/this/next expressions is essentially the same math whether months or weekdays are involved (eg, "this" means startof year/week + month/day offset), and ought to be collapsed as was done for the existing last/this/next calendar expressions. This will also simplify the generator.

return {
modifier: "",
type: "monthLiteration",
value: month
}
}

/ "next" _ month: Month {
return {
modifier: "next",
type: "monthLiteration",
value: month
}
}

/ "this" _ month: Month {
return {
modifier: "this",
type: "monthLiteration",
value: month
}
}
/ weekday: Weekday {
return {
modifier: "",
type: "weekdayLiteration",
value: weekday
}
}

/ "next" _ weekday: Weekday {
return {
modifier: "next",
type: "weekdayLiteration",
value: weekday
}
}

/ "this" _ weekday: Weekday {
return {
modifier: "this",
type: "weekdayLiteration",
value: weekday
}
}



/ "this" _ unit:Unit {
return {
type: "CalendarExpression",
valueType: "moment",
direction: "down",
unit: unit,
expression: {
type: "NowLiteral"
}
};
}
/ "last" _ unit:Unit {
return {
type: "CalendarExpression",
valueType: "moment",
direction: "down",
unit: unit,
expression: {
type: "BinaryExpression",
operator: "+",
operator: "-",
left: {
type: "NowLiteral"
},
Expand All @@ -297,6 +374,8 @@ CalendarExpression
}
};
}


/ unit:CalendarUnit _ "of" _ expr:MomentExpression {
return {
type: "CalendarExpression",
Expand Down