Example #1
0
        public static bool TryParse(List <string> tokens, out DecimalSection format)
        {
            if (Parser.ParseNumberTokens(tokens, 0, out var beforeDecimal, out var decimalSeparator, out var afterDecimal) == tokens.Count)
            {
                bool thousandSeparator;
                var  divisor    = GetTrailingCommasDivisor(tokens, out thousandSeparator);
                var  multiplier = GetPercentMultiplier(tokens);

                format = new DecimalSection()
                {
                    BeforeDecimal     = beforeDecimal,
                    DecimalSeparator  = decimalSeparator,
                    AfterDecimal      = afterDecimal,
                    PercentMultiplier = multiplier,
                    ThousandDivisor   = divisor,
                    ThousandSeparator = thousandSeparator
                };

                return(true);
            }

            format = null;
            return(false);
        }
Example #2
0
        public static Section ParseSection(Tokenizer reader, out bool syntaxError)
        {
            bool          hasDateParts     = false;
            bool          hasDurationParts = false;
            bool          hasGeneralPart   = false;
            bool          hasTextPart      = false;
            Condition     condition        = null;
            Color         color            = null;
            string        token;
            List <string> tokens = new List <string>();

            syntaxError = false;
            while ((token = ReadToken(reader, out syntaxError)) != null)
            {
                if (token == ";")
                {
                    break;
                }

                if (Token.IsDatePart(token))
                {
                    hasDateParts     |= true;
                    hasDurationParts |= Token.IsDurationPart(token);
                    tokens.Add(token);
                }
                else if (Token.IsGeneral(token))
                {
                    hasGeneralPart |= true;
                    tokens.Add(token);
                }
                else if (token == "@")
                {
                    hasTextPart |= true;
                    tokens.Add(token);
                }
                else if (token.StartsWith("["))
                {
                    // Does not add to tokens. Absolute/elapsed time tokens
                    // also start with '[', but handled as date part above
                    var expression = token.Substring(1, token.Length - 2);
                    if (TryParseCondition(expression, out var parseCondition))
                    {
                        condition = parseCondition;
                    }
                    else if (TryParseColor(expression, out var parseColor))
                    {
                        color = parseColor;
                    }
                }
                else
                {
                    tokens.Add(token);
                }
            }

            if (syntaxError || tokens.Count == 0)
            {
                return(null);
            }

            if (
                (hasDateParts && (hasGeneralPart || hasTextPart)) ||
                (hasGeneralPart && (hasDateParts || hasTextPart)) ||
                (hasTextPart && (hasGeneralPart || hasDateParts)))
            {
                // Cannot mix date, general and/or text parts
                syntaxError = true;
                return(null);
            }

            SectionType        type;
            FractionSection    fraction                = null;
            ExponentialSection exponential             = null;
            DecimalSection     number                  = null;
            List <string>      generalTextDateDuration = null;

            if (hasDateParts)
            {
                if (hasDurationParts)
                {
                    type = SectionType.Duration;
                    generalTextDateDuration = tokens;
                }
                else
                {
                    type = SectionType.Date;
                    ParseDate(tokens, out generalTextDateDuration);
                }
            }
            else if (hasGeneralPart)
            {
                type = SectionType.General;
                generalTextDateDuration = tokens;
            }
            else if (hasTextPart)
            {
                type = SectionType.Text;
                generalTextDateDuration = tokens;
            }
            else if (FractionSection.TryParse(tokens, out fraction))
            {
                type = SectionType.Fraction;
            }
            else if (ExponentialSection.TryParse(tokens, out exponential))
            {
                type = SectionType.Exponential;
            }
            else if (DecimalSection.TryParse(tokens, out number))
            {
                type = SectionType.Number;
            }
            else
            {
                // Unable to parse format string
                syntaxError = true;
                return(null);
            }

            return(new Section()
            {
                Type = type,
                Color = color,
                Condition = condition,
                Fraction = fraction,
                Exponential = exponential,
                Number = number,
                GeneralTextDateDurationParts = generalTextDateDuration
            });
        }