Пример #1
0
        public IReaderMode Read(int indention, string line)
        {
            if (line.IsEmpty())
            {
                return(this);
            }

            if (line.IsHeaderOne())
            {
                _fixture.title = line.Trim().TrimStart('#', ' ');
                return(this);
            }

            if (line.IsHeaderTwo())
            {
                var grammar = new GrammarMode(_fixture, g => _fixture.AddGrammar(g));
                grammar.Read(indention, line);

                return(grammar);
            }

            var mode = new CommentMode(_fixture);

            mode.Read(indention, line);

            return(mode);
        }
Пример #2
0
        public override IReaderMode Read(int indention, string text)
        {
            if (text.StartsWith("*"))
            {
                var mode = new GrammarMode(_fixture, g => _paragraph.AddChild(g));
                mode.Read(indention, text);

                return(mode);
            }

            // It's a new grammar, so get on out of here
            if (text.IsHeaderTwo() || text.IsHeaderThree())
            {
                return(null);
            }

            return(this);
        }
Пример #3
0
        public IReaderMode Read(int indention, string line)
        {
            if (_hasAdded && _paragraph == null)
            {
                return(null);
            }

            if (line.IsHeaderTwo())
            {
                // Get out of here, this is a new grammar!
                if (_hasAdded)
                {
                    return(null);
                }

                var value = line.Trim().TrimStart('#', ' ');

                if (value.Contains(' '))
                {
                    _title = value;
                    _key   = _title.Replace("  ", " ").Split(' ').Select(x => x.Capitalize()).Join("");
                }
                else
                {
                    _key = value;

                    // Default title
                    _title = _key.SplitPascalCase();
                }


                return(this);
            }

            if (line.IsHeaderThree())
            {
                _title = line.Trim().TrimStart('#', ' ');
                return(this);
            }

            if (line.StartsWith("*"))
            {
                if (WithinParagraph)
                {
                    var title = line.TrimStart('*').Trim();
                    if (_title.IsEmpty())
                    {
                        _title = title;
                        return(this);
                    }

                    // There's some internal clumsiness about reading children
                    // within a Paragraph
                    if (_title == title)
                    {
                        return(this);
                    }

                    if (!_hasAdded)
                    {
                        addSentence();
                    }

                    return(null);
                }

                var childGrammar = new GrammarMode(_fixture, g => _paragraph.AddChild(g))
                {
                    WithinParagraph = true
                };

                if (_paragraph == null)
                {
                    _hasAdded = true;

                    _paragraph = new Paragraph
                    {
                        key   = _key,
                        title = _title
                    };

                    _adder(_paragraph);
                }
                else
                {
                    childGrammar.Read(indention, line);
                }


                return(childGrammar);
            }

            if (line.StartsWith("fact", StringComparison.OrdinalIgnoreCase))
            {
                if (!_hasAdded)
                {
                    var sentence = addSentence();
                    sentence.fact = true;

                    return(this);
                }
            }

            if (line.StartsWith("embeds", StringComparison.OrdinalIgnoreCase))
            {
                return(_paragraph == null?buildEmbed(line) : this);
            }

            if (line.IsTableLine())
            {
                var values = line.ToTableValues();
                if (values.Any())
                {
                    switch (values[0].ToLower())
                    {
                    case "sentence":
                        var sentence = addSentence();
                        return(new SentenceMode(sentence));

                    case "table":
                        return(buildTable());

                    case "set":
                        return(buildSet(false));

                    case "ordered-set":
                        return(buildSet(true));

                    default:
                        throw new ArgumentOutOfRangeException($"'{values[0]}' is not a valid option here. Valid options are: 'sentence', 'table', 'set', or 'ordered-set'");
                    }
                }

                addSentence();
            }

            if (line.IsEmpty() && !_hasAdded && _title.IsNotEmpty())
            {
                addSentence();
            }

            return(null);
        }