Exemple #1
0
        private Parser <Value> GetExpressionLiteralParser()
        {
            Parser <Value> boolParser =
                (from t in Parse.String("true") select new Value(true)).Or(
                    from f in Parse.String("false") select new Value(false));

            Parser <Value> intParser =
                from negate in Parse.Char('-').Optional()
                from digits in Parse.Numeric.AtLeastOnce()
                select ParseLong(digits, negate.IsDefined);

            Parser <Value> floatParser =
                from negate in Parse.Char('-').Optional()
                from wholePart in Parse.Numeric.Many()
                from dot in Parse.Char('.')
                from fractionalPart in Parse.Numeric.AtLeastOnce()
                select ParseFloat(wholePart, fractionalPart, negate.IsDefined);

            var typeParser =
                from openAngle in Parse.Char('<')
                from type in EnumParser <Types> .Create()
                from closeAngle in Parse.Char('>')
                select new Value(type);

            var opcodeParser =
                from opcodeMarker in Parse.Char('@')
                from opcode in EnumParser <Opcode> .Create()
                select new Value(opcode);

            var instructionParser =
                from openBrace in Parse.Char('{')
                from opcodeMarker in Parse.Char('@')
                from opcode in GarplyParser.EnumParser <Opcode> .Create().Token()
                from operand in
                (from comma in Parse.Char(',')
                 from o in boolParser.Or(floatParser).Or(intParser).Or(typeParser).Or(opcodeParser)
                 select o).Optional()
                from closeBrace in Parse.Char('}')
                select Instruction.FromOpcodeAndOperand(opcode, operand.GetOrElse(default(Value)));

            var expressionParser =
                from header in Parse.String("expr")
                from openAngle in Parse.Char('<')
                from type in GarplyParser.EnumParser <Types> .Create()
                from closeAngle in Parse.Char('>')
                from openBracket in Parse.Char('[')
                from instructions in instructionParser.DelimitedBy(Parse.Char(',').Token())
                from closeBracket in Parse.Char(']')
                select GetCreateExpressionExpression(type, instructions.ToList());

            return(expressionParser);
        }