Exemple #1
0
        public static IParser Integer(
            bool allowLeadingZero = false,
            bool allowNegative    = true,
            string name           = "Integer")
        {
            var parser = allowLeadingZero
                ? ParserBuilder.OneOrMore(ParserBuilder.Class("0-9"))
                : ParserBuilder.Choice(
                ParserBuilder.T("0"),
                ParserBuilder.Sequence(
                    ParserBuilder.Class("1-9"),
                    ParserBuilder.ZeroOrMore(ParserBuilder.Class("0-9"))
                    )
                );

            if (allowNegative)
            {
                parser = ParserBuilder.Sequence(
                    ParserBuilder.Optional(ParserBuilder.T("-")),
                    parser
                    );
            }

            return(AddName(parser, name));
        }
Exemple #2
0
        public static IParser Decimal(
            bool allowLeadingZero        = false,
            bool allowNegative           = true,
            bool allowIntegerOnly        = true,
            bool allowEmptyDecimalDigits = false, // Ex: 12. instead of 12.0
            string name = "Decimal")
        {
            var intPart = Integer(
                allowLeadingZero,
                allowNegative,
                null);

            var decimalDigitsPart = allowEmptyDecimalDigits
                ? ParserBuilder.ZeroOrMore(ParserBuilder.Class("0-9"))
                : ParserBuilder.OneOrMore(ParserBuilder.Class("0-9"));

            var decimalPart = ParserBuilder.Sequence(
                ParserBuilder.T("."),
                decimalDigitsPart);

            if (allowIntegerOnly)
            {
                decimalPart = ParserBuilder.Optional(decimalPart);
            }

            var parser = ParserBuilder.Sequence(
                intPart,
                decimalPart);

            return(AddName(parser, name));
        }
Exemple #3
0
        public static IParser Identifier(
            string name = "Identifier")
        {
            var parser = ParserBuilder.Sequence(
                ParserBuilder.Choice(
                    ParserBuilder.T('_'),
                    ParserBuilder.Letter
                    ),
                ParserBuilder.ZeroOrMore(ParserBuilder.Choice(
                                             ParserBuilder.T('_'),
                                             ParserBuilder.LetterOrDigit
                                             ))
                );

            return(AddName(parser, name));
        }