public StatementParser(ILangParser parent) : base(parent) { }
/// <summary> /// Parses a date. /// </summary> /// <param name="parser"></param> /// <returns></returns> public static DateTime ParseDate(ILangParser parser) { var year = DateTime.Now.Year; var month = DateTime.Now.Month; double day = DateTime.Now.Day; var time = new TimeSpan(0, 0, 0); var tokenIt = parser.TokenIt; TokenData n = null; if (tokenIt.NextToken.Token.Kind == TokenKind.LiteralDate) { var parsedDate = (DateTime)tokenIt.NextToken.Token.Value; year = parsedDate.Year; month = parsedDate.Month; day = parsedDate.Day; } else { // 1. 1st token is definitely month name in long or short form. "oct" or "october". var monthNameOrAbbr = tokenIt.ExpectId(true, true); month = _months[monthNameOrAbbr.ToLower()]; // 2. Check for "," after month. if (tokenIt.NextToken.Token == Tokens.Comma) { tokenIt.Advance(); } // 3. 2nd token is the day 10. day = tokenIt.ExpectNumber(false); // 4. Check for "st nd rd th" as in 1st, 2nd, 3rd 4th for the day part. n = tokenIt.Peek(); var text = n.Token.Text.ToLower(); if (text == "st" || text == "nd" || text == "rd" || text == "th") { tokenIt.Advance(); } // 5. Check for another "," after day part. n = tokenIt.Peek(); var n2 = tokenIt.Peek(2); // IMPORTANT: Make sure not to interpret the "," as feb 12, if the the "," is part of a comma in array/map. if (n.Token == Tokens.Comma && n2.Token.Type == TokenTypes.LiteralNumber) { tokenIt.Advance(); } // 6. Finally check for year n = tokenIt.Peek(); if (n.Token.Type == TokenTypes.LiteralNumber) { year = Convert.ToInt32(n.Token.Text); tokenIt.Advance(); } } // 8. Now check for time. n = tokenIt.Peek(); if (n.Token.Text == "at") { time = TimeExprPlugin.ParseTime(parser, true, true); } else { tokenIt.Advance(); } var date = new DateTime(year, month, (int)day, time.Hours, time.Minutes, time.Seconds); return(date); }
public MethodParser(ILangParser parser) : base(parser) { }
/// <summary> /// Parses the time in format 12[:minutes:seconds] am|pm. /// </summary> /// <param name="parser"></param> /// <param name="advance"></param> /// <param name="expectAt"></param> /// <returns></returns> public static TimeSpan ParseTime(ILangParser parser, bool advance, bool expectAt) { double minutes = 0; double seconds = 0; double hours = DateTime.Now.Hour; var tokenIt = parser.TokenIt; if(advance) tokenIt.Advance(); if (expectAt) { // 1. Expect 'at" tokenIt.ExpectIdText("at", true); } // Next token could be a Literal time token // 1. time is a basic datatype // 2. time could be represented as 9:30 am and could have been // lexically parsed by a lex plugin. // NOTE: This plugin(any all other plugins) should NOT // know or depend on any other plugin. // However, they can know about tokens / basic types. if (tokenIt.NextToken.Token.Kind == TokenKind.LiteralTime) { var timeVal = (TimeSpan)tokenIt.NextToken.Token.Value; tokenIt.Advance(); return timeVal; } string tokenText = tokenIt.NextToken.Token.Text.ToLower(); if (_aliases.ContainsKey(tokenText)) { tokenIt.Advance(); return _aliases[tokenText]; } // 3. Hour part hours = tokenIt.ExpectNumber(true); // 4. Time specified without colon: e.g. 1030 pm ? var next = tokenIt.NextToken.Token.Text; if ((next == "am" || next == "pm") && tokenText.Length > 2) { var time = hours; // 130 - 930 am|pm if (time < 1000) { hours = Convert.ToDouble(tokenText[0].ToString()); minutes = Convert.ToDouble(tokenText.Substring(1)); } // 1030 - 1230 am|pm else { hours = Convert.ToDouble(tokenText.Substring(0, 2)); minutes = Convert.ToDouble(tokenText.Substring(2)); } } // 5. Time specified with ":" 10:30 pm else if (next == ":") { tokenIt.Advance(); // 6. Minutes part. minutes = tokenIt.ExpectNumber(true); if (tokenIt.NextToken.Token == Tokens.Colon) { tokenIt.Advance(); seconds = tokenIt.ExpectNumber(true); } } var text = tokenIt.NextToken.Token.Text; if (text != "am" && text != "pm") throw tokenIt.BuildSyntaxExpectedException("am/pm"); if (text == "pm" && hours >= 1 && hours <= 11) hours += 12; tokenIt.Advance(); return new TimeSpan((int)hours, (int)minutes, (int)seconds); }
/// <summary> /// Parses the time in format 12[:minutes:seconds] am|pm. /// </summary> /// <param name="parser"></param> /// <param name="advance"></param> /// <param name="expectAt"></param> /// <returns></returns> public static TimeSpan ParseTime(ILangParser parser, bool advance, bool expectAt) { double minutes = 0; double seconds = 0; double hours = DateTime.Now.Hour; var tokenIt = parser.TokenIt; if (advance) { tokenIt.Advance(); } if (expectAt) { // 1. Expect 'at" tokenIt.ExpectIdText("at", true); } // Next token could be a Literal time token // 1. time is a basic datatype // 2. time could be represented as 9:30 am and could have been // lexically parsed by a lex plugin. // NOTE: This plugin(any all other plugins) should NOT // know or depend on any other plugin. // However, they can know about tokens / basic types. if (tokenIt.NextToken.Token.Kind == TokenKind.LiteralTime) { var timeVal = (TimeSpan)tokenIt.NextToken.Token.Value; tokenIt.Advance(); return(timeVal); } string tokenText = tokenIt.NextToken.Token.Text.ToLower(); if (_aliases.ContainsKey(tokenText)) { tokenIt.Advance(); return(_aliases[tokenText]); } // 3. Hour part hours = tokenIt.ExpectNumber(true); // 4. Time specified without colon: e.g. 1030 pm ? var next = tokenIt.NextToken.Token.Text; if ((next == "am" || next == "pm") && tokenText.Length > 2) { var time = hours; // 130 - 930 am|pm if (time < 1000) { hours = Convert.ToDouble(tokenText[0].ToString()); minutes = Convert.ToDouble(tokenText.Substring(1)); } // 1030 - 1230 am|pm else { hours = Convert.ToDouble(tokenText.Substring(0, 2)); minutes = Convert.ToDouble(tokenText.Substring(2)); } } // 5. Time specified with ":" 10:30 pm else if (next == ":") { tokenIt.Advance(); // 6. Minutes part. minutes = tokenIt.ExpectNumber(true); if (tokenIt.NextToken.Token == Tokens.Colon) { tokenIt.Advance(); seconds = tokenIt.ExpectNumber(true); } } var text = tokenIt.NextToken.Token.Text; if (text != "am" && text != "pm") { throw tokenIt.BuildSyntaxExpectedException("am/pm"); } if (text == "pm" && hours >= 1 && hours <= 11) { hours += 12; } tokenIt.Advance(); return(new TimeSpan((int)hours, (int)minutes, (int)seconds)); }
public AssignmentParser(ILangParser parser) : base(parser) { }
/// <summary> /// Parses a date. /// </summary> /// <param name="parser"></param> /// <returns></returns> public static DateTime ParseDate(ILangParser parser) { var year = DateTime.Now.Year; var month = DateTime.Now.Month; double day = DateTime.Now.Day; var time = new TimeSpan(0, 0, 0); var tokenIt = parser.TokenIt; TokenData n = null; if (tokenIt.NextToken.Token.Kind == TokenKind.LiteralDate) { var parsedDate = (DateTime)tokenIt.NextToken.Token.Value; year = parsedDate.Year; month = parsedDate.Month; day = parsedDate.Day; } else { // 1. 1st token is definitely month name in long or short form. "oct" or "october". var monthNameOrAbbr = tokenIt.ExpectId(true, true); month = _months[monthNameOrAbbr.ToLower()]; // 2. Check for "," after month. if (tokenIt.NextToken.Token == Tokens.Comma) tokenIt.Advance(); // 3. 2nd token is the day 10. day = tokenIt.ExpectNumber(false); // 4. Check for "st nd rd th" as in 1st, 2nd, 3rd 4th for the day part. n = tokenIt.Peek(); var text = n.Token.Text.ToLower(); if (text == "st" || text == "nd" || text == "rd" || text == "th") tokenIt.Advance(); // 5. Check for another "," after day part. n = tokenIt.Peek(); var n2 = tokenIt.Peek(2); // IMPORTANT: Make sure not to interpret the "," as feb 12, if the the "," is part of a comma in array/map. if (n.Token == Tokens.Comma && n2.Token.Type == TokenTypes.LiteralNumber) { tokenIt.Advance(); } // 6. Finally check for year n = tokenIt.Peek(); if (n.Token.Type == TokenTypes.LiteralNumber) { year = Convert.ToInt32(n.Token.Text); tokenIt.Advance(); } } // 8. Now check for time. n = tokenIt.Peek(); if (n.Token.Text == "at") time = TimeExprPlugin.ParseTime(parser, true, true); else tokenIt.Advance(); var date = new DateTime(year, month, (int)day, time.Hours, time.Minutes, time.Seconds); return date; }