Exemplo n.º 1
0
        public override void ExitRelativeExpression(WeatherRuleParser.RelativeExpressionContext context)
        {
            /*
            DAYBEFORETODAY:'昨天';
            TODAY:'今天';
            TOMORROW:'明天';
            DAYAFTERTOMORROW:'后天';
            */
            DateTime whichDay = DateTime.Now;

            var tokenString = context.GetText();
            if (tokenString == "昨天")
                whichDay = whichDay.AddDays(-1);
            else if (tokenString == "今天")
                ;
            else if (tokenString == "明天")
                whichDay = whichDay.AddDays(1);
            else if (tokenString == "后天")
                whichDay = whichDay.AddDays(1);
            else
                throw new NotSupportedException();

            this.result.FromDate = whichDay;
            this.result.ToDate = whichDay;
        }
Exemplo n.º 2
0
        public override void ExitFixDayExpression(WeatherRuleParser.FixDayExpressionContext context)
        {
            var monthString=context.monthly().number().GetText();
            var dayString = context.date().number().GetText();

            int month = Utils.Parse2Int(monthString);
            int day = Utils.Parse2Int(dayString);

            DateTime date = new DateTime(DateTime.Now.Year, month, day);

            this.result.FromDate = date;
            this.result.ToDate = date;
        }
        public static ParseResult Parse(string sentence)
        {
            Antlr4.Runtime.AntlrInputStream input = new Antlr4.Runtime.AntlrInputStream(sentence);
            WeatherRuleLexer lexer = new WeatherRuleLexer(input);

            Antlr4.Runtime.UnbufferedTokenStream tokens = new Antlr4.Runtime.UnbufferedTokenStream(lexer);
            WeatherRuleParser parser = new WeatherRuleParser(tokens);
            parser.RemoveErrorListeners();

            var tree = parser.compileUnit();

            ParseTreeWalker walker = new ParseTreeWalker();

            WeatherListener lsn = new WeatherListener();

            walker.Walk(lsn, tree);

            return lsn.Result;
        }
Exemplo n.º 4
0
        public override void ExitPositiveExpression(WeatherRuleParser.PositiveExpressionContext context)
        {
            //      '这' number? '天'

            this.result.FromDate = DateTime.Now;

            var numToken = context.number();
            if (numToken == null)
            {
                this.result.ToDate = DateTime.Now;
                return;
            }

            var numberString = numToken.GetText();

            int day = Utils.Parse2Int(numberString);

            this.result.ToDate = DateTime.Now.AddDays(day);
        }
Exemplo n.º 5
0
 public override void ExitCity(WeatherRuleParser.CityContext context)
 {
     //在哪里
     result.Where = context.GetText();
 }