Created
July 16, 2012 18:39
-
-
Save paultyng/3124224 to your computer and use it in GitHub Desktop.
Vendor Hours Grammar (Irony)
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Irony.Parsing; | |
namespace IronyHours | |
{ | |
public class HoursGrammar : Grammar | |
{ | |
public HoursGrammar() : base(false) | |
{ | |
//misc | |
var COLON = ToTerm(":"); | |
//days of week | |
var sunday = new NonTerminal("sunday"); | |
var monday = new NonTerminal("monday"); | |
var tuesday = new NonTerminal("tuesday"); | |
var wednesday = new NonTerminal("wednesday"); | |
var thursday = new NonTerminal("thursday"); | |
var friday = new NonTerminal("friday"); | |
var saturday = new NonTerminal("saturday"); | |
sunday.Rule = ToTerm("Su") | ToTerm("Sun") | ToTerm("Sunday"); | |
monday.Rule = ToTerm("M") | ToTerm("Mon") | ToTerm("Monday"); | |
tuesday.Rule = ToTerm("Tu") | ToTerm("Tue") | ToTerm("Tues") | ToTerm("Tuesday"); | |
wednesday.Rule = ToTerm("W") | ToTerm("Wed") | ToTerm("Weds") | ToTerm("Wednesday"); | |
thursday.Rule = ToTerm("Th") | ToTerm("Thu") | ToTerm("Thur") | ToTerm("Thurs") | ToTerm("Thursday"); | |
friday.Rule = ToTerm("F") | ToTerm("Fr") | ToTerm("Fri") | ToTerm("Friday"); | |
saturday.Rule = ToTerm("Sa") | ToTerm("Sat") | ToTerm("Saturday"); | |
var dayOfWeek = new NonTerminal("dayOfWeek"); | |
dayOfWeek.Rule = sunday | monday | tuesday | wednesday | thursday | friday | saturday; | |
var dayList = new NonTerminal("dayList"); | |
dayList.Rule = MakePlusRule(dayList, ToTerm(","), dayOfWeek); | |
var dayRange = new NonTerminal("dayRange"); | |
dayRange.Rule = dayOfWeek + "-" + dayOfWeek; | |
var dayCouple = new NonTerminal("dayCouple"); | |
dayCouple.Rule = dayOfWeek + "&" + dayOfWeek; | |
//time | |
var closed = ToTerm("Closed"); | |
var time = new TimeLiteral("time"); | |
var timeRange = new NonTerminal("timeRange"); | |
timeRange.Rule = time + "-" + time; | |
var timeRangeList = new NonTerminal("timeRangeList"); | |
timeRangeList.Rule = MakePlusRule(timeRangeList, ToTerm(","), timeRange); | |
//entries | |
var entrySeparator = new NonTerminal("entrySeparator"); | |
entrySeparator.Rule = COLON | Empty; | |
var entryDay = new NonTerminal("entryDay"); | |
entryDay.Rule = dayList | dayRange | dayCouple; | |
var entryTime = new NonTerminal("entryTime"); | |
entryTime.Rule = timeRangeList | closed; | |
var entry = new NonTerminal("entry"); | |
entry.Rule = entryDay + entrySeparator + entryTime; | |
var timeset = new NonTerminal("timeset"); | |
timeset.Rule = MakeStarRule(timeset, entry); | |
var program = new NonTerminal("program"); | |
program.Rule = timeset | entryTime; | |
Root = program; | |
} | |
} | |
public sealed class TimeLiteral : RegexBasedTerminal | |
{ | |
//am/pm is not optional | |
const string pattern = @"(?:10|11|12|[1-9])(?:\:[0-5][0-9])?(?: ?[aApP][mM])"; | |
public TimeLiteral(string name) | |
: base(name, pattern) | |
{ | |
} | |
public TimeLiteral() | |
: base(pattern) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@kylefritz basic vendor hours grammar for Irony