예제 #1
0
        public CanParseGenerics()
        {
            var lexer = LexerBuilder.Build()
                        .Match("nr", @"\d+", match => new Number {
                Value = int.Parse(match.Value)
            })
                        .Match("txt", @".+", match => new Text {
                Value = match.Value
            });

            _subject = ParserBuilder.Build(lexer)
                       .Match <Number, Text>((nr, t) => new KeyValuePair <Number, Text>(nr, t), MatchOptions.AtLeastOne)
                       .Create <KeyValuePair <Number, Text> >();
        }
예제 #2
0
        public CalculatorTest()
        {
            var lexer = LexerBuilder.Build()
                        .Match <PlusToken>("plus", @"\+")
                        .Match <MinusToken>("minus", @"-")
                        .Match <MultiToken>("multi", @"\*")
                        .Match <DivideToken>("divide", @"\/")
                        .Match("number", @"\b[0-9]+(\.[0-9]*)?\b", match => decimal.Parse(match.Value));

            _subject = ParserBuilder.Build(lexer)
                       .Match <decimal, DivideToken, decimal>((o1, by, o2) => o1 / o2)
                       .Match <decimal, MultiToken, decimal>((o1, times, o2) => o1 * o2)
                       .Match <decimal, MinusToken, decimal>((o1, minus, o2) => o1 - o2)
                       .Match <decimal, PlusToken, decimal>((o1, plus, o2) => o1 + o2)
                       .Create <decimal>();
        }
예제 #3
0
        public static Lexer CreateLexer()
        {
            LexerBuilder builder = new LexerBuilder();

            builder.Add("P", "C");
            builder.Add("C", "/", "IDN", "AS").Act(children => { var l = new List <IASTNode>()
                                                                 {
                                                                     children[0]
                                                                 }; l.AddRange(children[1].Children); return(new Command(l.ToArray())); });
            builder.Add("AS", "AS", "A").Act(children => new ASTList(new List <IASTNode>(children[0].Children)
            {
                children[1]
            }));
            builder.Add("AS");
            builder.Add("A", "IDN", ":", "E");
            builder.Add("A", "E");
            //builder.Add("P", "E");
            builder.Add("E", "E", "+", "T").Act(children => new Calc(children));
            builder.Add("E", "E", "-", "T").Act(children => new Calc(children));
            builder.Add("E", "T");
            builder.Add("T", "T", "*", "F").Act(children => new Calc(children));
            builder.Add("T", "T", "/", "F").Act(children => new Calc(children));
            builder.Add("T", "T", "%", "F").Act(children => new Calc(children));
            builder.Add("T", "F");
            builder.Add("F", "(", "E", ")").Act(children => children[1]);
            builder.Add("F", "IDN");
            builder.Add("F", "DIG");
            builder.Add("F", "DEC");

            Lexer lexer = builder.Build("P");

            builder.PrintBuilder();
            builder.PrintTable();
            //builder.PrintLexerCode();

            return(lexer);
        }