示例#1
0
        public Language(string code, IEnumerable <TracableStream> dataSources)
        {
            this.Code = code;

            foreach (var source in dataSources)
            {
                var parser = new IkadnParser(source.Stream);
                parser.RegisterFactory(new ContextFactory());
                parser.RegisterFactory(new SingleLineFactory());
                parser.RegisterFactory(new TextBlockFactory());
                parser.RegisterFactory(new ExpressionTextFactory());
                parser.RegisterFactory(new ConditionalTextFactory());

                try
                {
                    while (parser.HasNext())
                    {
                        var conext = parser.ParseNext().To <Context>();
                        contexts.Add((string)conext.Tag, conext);
                    }
                }
                catch (IOException e)
                {
                    throw new IOException(source.SourceInfo, e);
                }
                catch (FormatException e)
                {
                    throw new FormatException(source.SourceInfo, e);
                }
            }
        }
        public IkadnBaseObject Parse(IkadnParser parser)
        {
            parser.Reader.SkipWhiteSpaces();
            string expressionText = parser.Reader.ReadUntil(EndingChar);

            parser.Reader.Read();

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

            ExpressionParser expressionParser = new ExpressionParser(expressionText);

            expressionParser.Parse();

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

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

            return(new ConditionalText(expressionParser.ParsedFormula, parser.ParseNext().To <IText>()));
        }
        public IkadnBaseObject Parse(IkadnParser parser)
        {
            parser.Reader.SkipWhiteSpaces();
            char   formatterType      = parser.Reader.Read();
            string formatterParameter = parser.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) : 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);
            }

            parser.Reader.SkipWhiteSpaces();
            string expressionText = parser.Reader.ReadUntil(EndingChar);

            parser.Reader.Read();

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

            ExpressionParser expParser = new ExpressionParser(expressionText);

            expParser.Parse();

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

            //TODO(later) substitute subformulas
            return(new ExpressionText(expParser.ParsedFormula, formatter));
        }
示例#4
0
        public Language(string code, IEnumerable <NamedStream> dataSources)
        {
            this.Code = code;
            var factories = new IIkadnObjectFactory[] {
                new ContextFactory(),
                new SingleLineFactory(),
                new TextBlockFactory(),
                new ExpressionTextFactory(),
                new ConditionalTextFactory()
            };

            using (var parser = new IkadnParser(dataSources, factories))
                foreach (var context in parser.ParseAll().Select(x => x.Value.To <Context>()))
                {
                    contexts.Add((string)context.Tag, context);
                }
        }
示例#5
0
        public IkadnBaseObject Parse(IkadnParser parser)
        {
            string contextName = IkonParser.ReadIdentifier(parser.Reader);
            var    entries     = new Dictionary <string, IText>();

            while (parser.Reader.PeekNextNonwhite() != ClosingChar)
            {
                var id = IkonParser.ReadIdentifier(parser.Reader).ToLower();

                if (!entries.ContainsKey(id))
                {
                    entries.Add(id, parser.ParseNext().To <IText>());
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine("Duplicate localization entry, id: " + id + " in context: " + contextName);
                }
            }

            parser.Reader.Read();

            return(new Context(contextName, entries));
        }
示例#6
0
        public IkadnBaseObject Parse(IkadnParser parser)
        {
            parser.Reader.SkipWhiteSpaces();
            string expressionText = parser.Reader.ReadUntil(EndingChar);

            parser.Reader.Read();

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

            var expParser = new ExpressionParser(expressionText);

            expParser.Parse();

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

            return(new Expression(expParser.ParsedFormula));
        }
示例#7
0
        public IkadnBaseObject Parse(IkadnParser parser)
        {
            string contextName = IkonParser.ReadIdentifier(parser.Reader);
            var    entries     = new Dictionary <string, IText>();

            while (parser.Reader.PeekNextNonwhite() != ClosingChar)
            {
                var id = IkonParser.ReadIdentifier(parser.Reader).ToLower();

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

            parser.Reader.Read();

            return(new Context(contextName, entries));
        }
 public IkadnBaseObject Parse(IkadnParser parser)
 {
     return(new NoValue());
 }
 public IkadnBaseObject Parse(IkadnParser parser)
 {
     parser.Reader.SkipWhiteSpaces();
     return(new IkonText(parser.Reader.ReadWhile(x => !char.IsWhiteSpace(x)).Trim()));
 }
        public IkadnBaseObject Parse(IkadnParser parser)
        {
            string textIndent =
                parser.Reader.LineIndentation +
                readIndentSpec(parser.Reader.ReadUntil('\n', '\r').Trim());

            skipEndOfLine(parser.Reader);

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

            if (!checkIndentation(parser.Reader, textIndent))
            {
                return(new SingleLineText(""));
            }

            while (true)
            {
                if (parser.Reader.Peek() == SubstitutionOpenChar)
                {
                    parser.Reader.Read();
                    string substitutionName = parser.Reader.ReadUntil(SubstitutionCloseChar);
                    parser.Reader.Read();
                    if (substitutionName.Length == 0)
                    {
                        throw new FormatException("Substitution name at " + parser.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            = parser.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 (parser.Reader.Peek() != SubstitutionOpenChar)
                    {
                        textRunBuilder.AppendLine(textPart);
                        skipEndOfLine(parser.Reader);
                        if (!checkIndentation(parser.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 (parser.Reader.SkipWhiteSpaces().EndOfStream)
                {
                    throw new EndOfStreamException("Unexpectedend of stream at " + parser.Reader.PositionDescription);
                }

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

                substitutions[substitutionName] = parser.ParseNext().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()));
        }
 public IkadnBaseObject Parse(IkadnParser parser)
 {
     return(new SingleLineText(parser.Reader.ReadUntil('\n', '\r', IkadnReader.EndOfStreamResult).Trim()));
 }