Esempio n. 1
0
        public IkadnBaseObject Parse(IkadnReader reader)
        {
            reader.SkipWhiteSpaces();
            string expressionText = reader.ReadUntil(EndingChar);

            reader.Read();

            if (expressionText.Length == 0)
            {
                throw new FormatException("Expression at " + reader.PositionDescription + " is empty (zero length)");
            }

            ExpressionParser expressionParser = new ExpressionParser(expressionText);

            expressionParser.Parse();

            if (expressionParser.errors.count > 0)
            {
                throw new FormatException("Invalid expression at " + reader.PositionDescription + ". Errors: " + expressionParser.errors.errorMessages);
            }

            if (!reader.HasNextObject())
            {
                throw new FormatException("IKON value expected at " + reader.PositionDescription);
            }
            if (expressionParser.ParsedFormula.Variables.Count == 0)
            {
                throw new FormatException("Condition expression at " + reader.PositionDescription + " is constant. If intentional, use format witout condition.");
            }

            //TODO(later) substitute subformulas
            return(new ConditionalText(expressionParser.ParsedFormula, reader.ReadObject().To <IText>()));
        }
Esempio n. 2
0
        public IkadnBaseObject Parse(IkadnReader reader)
        {
            reader.SkipWhiteSpaces();
            char   formatterType      = reader.Read();
            string formatterParameter = reader.ReadWhile(c => !char.IsWhiteSpace(c));

            Func <double, string> formatter;

            switch (formatterType)
            {
            case 'd':
            case 'D':
                formatter = new DecimalsFormatter(0,
                                                  formatterParameter.Length > 0 ? int.Parse(formatterParameter, CultureInfo.InvariantCulture) : 0
                                                  ).Format;
                break;

            case 't':
            case 'T':
                formatter = (
                    (formatterParameter == AutomaticThousands) ?
                    new ThousandsFormatter() :
                    new ThousandsFormatter(formatterParameter)
                    ).Format;
                break;

            default:
                throw new FormatException("Unexpected expression formatter: " + formatterType);
            }

            reader.SkipWhiteSpaces();
            string expressionText = reader.ReadUntil(EndingChar);

            reader.Read();

            if (expressionText.Length == 0)
            {
                throw new FormatException("Expression at " + reader.PositionDescription + " is empty (zero length)");
            }

            var expParser = new ExpressionParser(expressionText);

            expParser.Parse();

            if (expParser.errors.count > 0)
            {
                throw new FormatException("Expression at " + reader.PositionDescription + " is invalid: " + expParser.errors.errorMessages);
            }

            //TODO(later) substitute subformulas
            return(new ExpressionText(expParser.ParsedFormula, formatter));
        }
        private void skipEndOfLine(IkadnReader reader)
        {
            char nextChar = reader.Peek();

            if (nextChar == '\r')
            {
                reader.Read();
                nextChar = reader.Peek();
            }

            if (nextChar != '\n')
            {
                throw new FormatException("No or unknown type of line ending method at " + reader.PositionDescription);
            }

            reader.Read();
        }
Esempio n. 4
0
        //TODO(v0.5): move to TextBlockFactory it is only caller
        public static string ParseString(IkadnReader reader, IEnumerable <int> terminatingCharacters, char escapeCharacter, Func <char, char> escapeCode)
        {
            var  terminatingCharsSet = new HashSet <int>(terminatingCharacters);
            bool escaping            = false;

            return(reader.ReadConditionally(c =>
            {
                if (c == escapeCharacter && !escaping)
                {
                    escaping = true;
                    return new ReadingDecision((char)c, CharacterAction.Skip);
                }
                escaping = false;
                return new ReadingDecision((char)c, terminatingCharsSet.Contains(c) ?
                                           CharacterAction.Stop :
                                           CharacterAction.AcceptAsIs);
            }));
        }
        private bool checkIndentation(IkadnReader reader, string indentation)
        {
            foreach (char expectedSpace in indentation)
            {
                if (reader.Peek() == expectedSpace)
                {
                    reader.Read();
                }
                else
                if (reader.Peek() == BlockCloseChar)
                {
                    reader.Read();
                    return(false);
                }
                else
                {
                    throw new FormatException("Unexpected character at " + reader.PositionDescription + ", expected block closing character or block content indentation.");
                }
            }

            return(true);
        }
Esempio n. 6
0
        public IkadnBaseObject Parse(IkadnReader reader)
        {
            string contextName = IkonParser.ReadIdentifier(reader);
            var    entries     = new Dictionary <string, IText>();

            while (reader.PeekNextNonwhite() != ClosingChar)
            {
                var id = IkonParser.ReadIdentifier(reader).ToUpperInvariant();

                if (!entries.ContainsKey(id))
                {
                    entries.Add(id, reader.ReadObject().To <IText>());
                }
                else
                {
                    AppData.ErrorReporter.Get.Report(new ArgumentException("Duplicate localization entry, id: " + id + " in context: " + contextName));
                }
            }

            reader.Read();

            return(new Context(contextName, entries));
        }
Esempio n. 7
0
        protected override IkadnBaseObject ParseObject(IkadnReader reader)
        {
            reader.SkipWhiteSpaces();
            string expressionText = reader.ReadUntil(EndingChar);

            reader.Read();

            if (expressionText.Length == 0)
            {
                throw new FormatException("Expression at " + reader.PositionDescription + " is empty (zero length)");
            }

            var expParser = new ExpressionParser(expressionText);

            expParser.Parse();

            if (expParser.errors.count > 0)
            {
                throw new FormatException("Expression at " + reader.PositionDescription + " is invalid: " + expParser.errors.errorMessages);
            }

            return(new Expression(expParser.ParsedFormula.Substitute(this.subformulas)));
        }
Esempio n. 8
0
 protected override IkadnBaseObject ParseObject(IkadnReader reader)
 {
     return(new NoValue());
 }
Esempio n. 9
0
 protected override IkadnBaseObject ParseObject(IkadnReader reader)
 {
     reader.SkipWhiteSpaces();
     return(new IkonText(reader.ReadWhile(x => !char.IsWhiteSpace(x)).Trim()));
 }
Esempio n. 10
0
        public IkadnBaseObject Parse(IkadnReader reader)
        {
            string textIndent =
                reader.LineIndentation +
                readIndentSpec(reader.ReadUntil('\n', '\r').Trim());

            skipEndOfLine(reader);

            var textRuns       = new Queue <string>();
            var substitutions  = new Dictionary <string, IText>();
            var textRunBuilder = new StringBuilder();

            if (!checkIndentation(reader, textIndent))
            {
                return(new SingleLineText(""));
            }

            while (true)
            {
                if (reader.Peek() == SubstitutionOpenChar)
                {
                    reader.Read();
                    string substitutionName = reader.ReadUntil(SubstitutionCloseChar);
                    reader.Read();
                    if (substitutionName.Length == 0)
                    {
                        throw new FormatException("Substitution name at " + reader + " is empty (zero length)");
                    }

                    if (textRunBuilder.Length > 0)
                    {
                        textRuns.Enqueue(textRunBuilder.ToString());
                        textRunBuilder.Clear();
                    }
                    textRuns.Enqueue(null);
                    textRuns.Enqueue(substitutionName);

                    if (!substitutions.ContainsKey(substitutionName) && !substitutionName.StartsWith(PlaceholderMark, StringComparison.Ordinal))
                    {
                        substitutions.Add(substitutionName, null);
                    }
                }
                else
                {
                    var terminatingCharsSet = new int[] { SubstitutionOpenChar, '\n', '\r' };
                    var escaping            = false;
                    var textPart            = reader.ReadConditionally(c =>
                    {
                        if (c == EscapeChar && !escaping)
                        {
                            escaping = true;
                            return(new ReadingDecision((char)c, CharacterAction.Skip));
                        }
                        escaping = false;
                        return(new ReadingDecision((char)c, terminatingCharsSet.Contains(c) ?
                                                   CharacterAction.Stop :
                                                   CharacterAction.AcceptAsIs));
                    });

                    if (reader.Peek() != SubstitutionOpenChar)
                    {
                        textRunBuilder.AppendLine(textPart);
                        skipEndOfLine(reader);
                        if (!checkIndentation(reader, textIndent))
                        {
                            break;
                        }
                    }
                    else
                    {
                        textRuns.Enqueue(textRunBuilder.ToString() + textPart);
                        textRunBuilder.Clear();
                    }
                }
            }
            if (textRunBuilder.Length > Environment.NewLine.Length)
            {
                textRunBuilder.Length -= Environment.NewLine.Length;
                textRuns.Enqueue(textRunBuilder.ToString());
            }

            for (int i = 0; i < substitutions.Count; i++)
            {
                if (reader.SkipWhiteSpaces().EndOfStream)
                {
                    throw new EndOfStreamException("Unexpectedend of stream at " + reader.PositionDescription);
                }

                string substitutionName = reader.ReadUntil(c =>
                                                           c != IkadnReader.EndOfStreamResult && char.IsWhiteSpace((char)c)
                                                           );

                substitutions[substitutionName] = reader.ReadObject().To <IText>();
            }

            var texts = new List <IText>();

            while (textRuns.Count > 0)
            {
                string textRun = textRuns.Dequeue();

                if (textRun != null)
                {
                    texts.Add(new SingleLineText(textRun));
                }
                else
                {
                    string textKey = textRuns.Dequeue();

                    if (textKey.StartsWith(PlaceholderMark, StringComparison.Ordinal))
                    {
                        texts.Add(new PlaceholderText(textKey.Substring(PlaceholderMark.Length)));
                    }
                    else
                    {
                        texts.Add(substitutions[textKey]);
                    }
                }
            }

            return(new ChainText(texts.ToArray()));
        }
Esempio n. 11
0
 public IkadnBaseObject Parse(IkadnReader reader)
 {
     return(new SingleLineText(reader.ReadUntil('\n', '\r', IkadnReader.EndOfStreamResult).Trim()));
 }