Exemplo n.º 1
0
        protected void TokenizerAssertEquals(String TemplateString, params String[] Tokens)
        {
            var StringTokens = TemplateTokenizer.Tokenize(new TokenizerStringReader(TemplateString)).Select(Token => Token.Text).ToArray();

            foreach (var StringToken in StringTokens)
            {
                Console.WriteLine(StringToken);
            }
            CollectionAssert.AreEqual(StringTokens, Tokens);
        }
        public void Should_Parse_Optional_Value(string template, string name)
        {
            // Given, When
            var result = TemplateTokenizer.Tokenize(template);

            // Then
            result.Count.ShouldBe(1);
            result[0].TokenKind.ShouldBe(TemplateToken.Kind.OptionalValue);
            result[0].Value.ShouldBe(name);
        }
        public void Should_Parse_Short_Option()
        {
            // Given, When
            var result = TemplateTokenizer.Tokenize("-f");

            // Then
            result.Count.ShouldBe(1);
            result[0].TokenKind.ShouldBe(TemplateToken.Kind.ShortName);
            result[0].Value.ShouldBe("f");
        }
        public void Should_Throw_If_Value_Is_Unterminated(string template)
        {
            // Given, When
            var result = Record.Exception(() => TemplateTokenizer.Tokenize(template));

            // Then
            result.ShouldBeOfType <TemplateException>().And(e =>
            {
                e.Message.ShouldBe("Encountered unterminated value name 'FOO'.");
                e.Template.ShouldBe(template);
            });
        }
        public void Should_Parse_Multiple_Options()
        {
            // Given, When
            var result = TemplateTokenizer.Tokenize("-f|--foo|--bar|-b");

            // Then
            result.Count.ShouldBe(4);
            result[0].TokenKind.ShouldBe(TemplateToken.Kind.ShortName);
            result[0].Value.ShouldBe("f");
            result[1].TokenKind.ShouldBe(TemplateToken.Kind.LongName);
            result[1].Value.ShouldBe("foo");
            result[2].TokenKind.ShouldBe(TemplateToken.Kind.LongName);
            result[2].Value.ShouldBe("bar");
            result[3].TokenKind.ShouldBe(TemplateToken.Kind.ShortName);
            result[3].Value.ShouldBe("b");
        }
        private static void Test(string template, params ITemplateToken[] expectedTokens)
        {
            var actualTokens = TemplateTokenizer.Tokenize(template, new PropertyTokensFactory()).ToArray();

            actualTokens.Should().HaveCount(expectedTokens.Length);

            foreach (var(actual, expected) in actualTokens.Zip(expectedTokens, (actual, expected) => (actual, expected)))
            {
                if (expected is TextToken expectedText)
                {
                    actual.Should().BeOfType <TextToken>().Which.ToString().Should().Be(expectedText.ToString());
                }
                else if (expected is PropertyToken expectedProperty)
                {
                    actual.Should().BeOfType <PropertyToken>().And.BeEquivalentTo(expectedProperty);
                }
                else
                {
                    throw new InvalidOperationException("Expected tokens can only be TextTokens or PropertyTokens.");
                }
            }
        }
Exemplo n.º 7
0
    public static ArgumentResult ParseArgumentTemplate(string template)
    {
        var valueName = default(string);
        var required  = false;

        foreach (var token in TemplateTokenizer.Tokenize(template))
        {
            if (token.TokenKind == TemplateToken.Kind.ShortName ||
                token.TokenKind == TemplateToken.Kind.LongName)
            {
                throw CommandTemplateException.ArgumentCannotContainOptions(template, token);
            }

            if (token.TokenKind == TemplateToken.Kind.OptionalValue ||
                token.TokenKind == TemplateToken.Kind.RequiredValue)
            {
                if (!string.IsNullOrWhiteSpace(valueName))
                {
                    throw CommandTemplateException.MultipleValuesAreNotSupported(template, token);
                }

                if (string.IsNullOrWhiteSpace(token.Value))
                {
                    throw CommandTemplateException.ValuesMustHaveName(template, token);
                }

                valueName = token.Value;
                required  = token.TokenKind == TemplateToken.Kind.RequiredValue;
            }
        }

        if (valueName == null)
        {
            throw CommandTemplateException.ArgumentsMustHaveValueName(template);
        }

        return(new ArgumentResult(valueName, required));
    }
Exemplo n.º 8
0
    public static OptionResult ParseOptionTemplate(string template)
    {
        var result = new OptionResult();

        foreach (var token in TemplateTokenizer.Tokenize(template))
        {
            if (token.TokenKind == TemplateToken.Kind.LongName || token.TokenKind == TemplateToken.Kind.ShortName)
            {
                if (string.IsNullOrWhiteSpace(token.Value))
                {
                    throw CommandTemplateException.OptionsMustHaveName(template, token);
                }

                if (char.IsDigit(token.Value[0]))
                {
                    throw CommandTemplateException.OptionNamesCannotStartWithDigit(template, token);
                }

                foreach (var character in token.Value)
                {
                    if (!char.IsLetterOrDigit(character) && character != '-' && character != '_')
                    {
                        throw CommandTemplateException.InvalidCharacterInOptionName(template, token, character);
                    }
                }
            }

            if (token.TokenKind == TemplateToken.Kind.LongName)
            {
                if (token.Value.Length == 1)
                {
                    throw CommandTemplateException.LongOptionMustHaveMoreThanOneCharacter(template, token);
                }

                result.LongNames.Add(token.Value);
            }

            if (token.TokenKind == TemplateToken.Kind.ShortName)
            {
                if (token.Value.Length > 1)
                {
                    throw CommandTemplateException.ShortOptionMustOnlyBeOneCharacter(template, token);
                }

                result.ShortNames.Add(token.Value);
            }

            if (token.TokenKind == TemplateToken.Kind.RequiredValue ||
                token.TokenKind == TemplateToken.Kind.OptionalValue)
            {
                if (!string.IsNullOrWhiteSpace(result.Value))
                {
                    throw CommandTemplateException.MultipleOptionValuesAreNotSupported(template, token);
                }

                foreach (var character in token.Value)
                {
                    if (!char.IsLetterOrDigit(character) &&
                        character != '=' && character != '-' && character != '_')
                    {
                        throw CommandTemplateException.InvalidCharacterInValueName(template, token, character);
                    }
                }

                result.Value           = token.Value.ToUpperInvariant();
                result.ValueIsOptional = token.TokenKind == TemplateToken.Kind.OptionalValue;
            }
        }

        if (result.LongNames.Count == 0 &&
            result.ShortNames.Count == 0)
        {
            throw CommandTemplateException.MissingLongAndShortName(template, null);
        }

        return(result);
    }
Exemplo n.º 9
0
 public TemplateCodeGen(String TemplateString, TemplateFactory TemplateFactory = null)
 {
     this.TemplateFactory = TemplateFactory;
     this.Tokens          = new TokenReader(TemplateTokenizer.Tokenize(new TokenizerStringReader(TemplateString)));
 }
Exemplo n.º 10
0
 public static OutputTemplate Parse(string input) =>
 new OutputTemplate(TemplateTokenizer.Tokenize(input, new AllNamedTokensFactory()).ToArray());