Example #1
0
        public static bool TryParse(List <string> tokens, out ExponentialSection format)
        {
            format = null;

            string exponentialToken;

            int partCount = Parser.ParseNumberTokens(tokens, 0, out var beforeDecimal, out var decimalSeparator, out var afterDecimal);

            if (partCount == 0)
            {
                return(false);
            }

            int position = partCount;

            if (position < tokens.Count && Token.IsExponent(tokens[position]))
            {
                exponentialToken = tokens[position];
                position++;
            }
            else
            {
                return(false);
            }

            format = new ExponentialSection()
            {
                BeforeDecimal    = beforeDecimal,
                DecimalSeparator = decimalSeparator,
                AfterDecimal     = afterDecimal,
                ExponentialToken = exponentialToken,
                Power            = tokens.GetRange(position, tokens.Count - position)
            };

            return(true);
        }
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
            });
        }