Example #1
0
        internal static Token?TryMatch(LookaheadBuffer <char> cs)
        {
            if (cs.TryExpect('['))
            {
                return(new Token(TokenType.LBrac, new string(cs.Consume(), 1)));
            }
            else if (cs.TryExpect(']'))
            {
                return(new Token(TokenType.RBrac, new string(cs.Consume(), 1)));
            }
            else if (cs.TryExpect('='))
            {
                return(new Token(TokenType.Assign, new string(cs.Consume(), 1)));
            }
            else if (cs.TryExpect(','))
            {
                return(new Token(TokenType.Comma, new string(cs.Consume(), 1)));
            }
            else if (cs.TryExpect('.'))
            {
                return(new Token(TokenType.Dot, new string(cs.Consume(), 1)));
            }
            else if (cs.TryExpect('{'))
            {
                return(new Token(TokenType.LCurly, new string(cs.Consume(), 1)));
            }
            else if (cs.TryExpect('}'))
            {
                return(new Token(TokenType.RCurly, new string(cs.Consume(), 1)));
            }

            return(null);
        }
Example #2
0
        private static TomlString ParseLiteralString(ITomlRoot root, LookaheadBuffer <Token> tokens)
        {
            var t = tokens.Consume();

            Debug.Assert(t.type == TokenType.LiteralString);

            return(new TomlString(root, t.value, TomlString.TypeOfString.Literal));
        }
        public void LookAheadTestStartZero()
        {
            var instructions = new List<Instruction>();

            var enumerator = instructions.GetEnumerator();
            enumerator.MoveNext();
            var count = LookaheadBuffer.GetUntilEndOfRoot(enumerator).Count();
            Assert.Equal(0, count);
        }
Example #4
0
        private static TomlString ParseLiteralString(ITomlRoot root, LookaheadBuffer<Token> tokens)
        {
            var t = tokens.Consume();

            Debug.Assert(t.type == TokenType.LiteralString);

            var s = t.value.TrimNChars(1);

            return new TomlString(root, s, TomlString.TypeOfString.Literal);
        }
Example #5
0
        private static TomlInt ParseTomlInt(ITomlRoot root, LookaheadBuffer <Token> tokens)
        {
            var token = tokens.Consume();

            if (token.value.Length > 1 && token.value[0] == '0')
            {
                throw new Exception($"Failed to parse TOML int with '{token.value}' because it has  a leading '0' which is not allowed by the TOML specification.");
            }

            return(new TomlInt(root, long.Parse(token.value.Replace("_", string.Empty))));
        }
Example #6
0
        private static TomlInt ParseTomlInt(ITomlRoot root, LookaheadBuffer <Token> tokens, ParseIntDelegate parseDelegate)
        {
            var token = tokens.Consume();

            if (!parseDelegate(token.value.Replace("_", string.Empty), out long value))
            {
                throw new Exception($"Failed to parse TOML int with '{token.value}'.");
            }

            return(new TomlInt(root, value));
        }
Example #7
0
        internal static Token? TryMatch(LookaheadBuffer<char> cs)
        {
            if (cs.TryExpect('[')) { return new Token(TokenType.LBrac, new string(cs.Consume(), 1)); }
            else if (cs.TryExpect(']')) { return new Token(TokenType.RBrac, new string(cs.Consume(), 1)); }
            else if (cs.TryExpect('=')) { return new Token(TokenType.Assign, new string(cs.Consume(), 1)); }
            else if (cs.TryExpect(',')) { return new Token(TokenType.Comma, new string(cs.Consume(), 1)); }
            else if (cs.TryExpect('.')) { return new Token(TokenType.Dot, new string(cs.Consume(), 1)); }
            else if (cs.TryExpect('{')) { return new Token(TokenType.LCurly, new string(cs.Consume(), 1)); }
            else if (cs.TryExpect('}')) { return new Token(TokenType.RCurly, new string(cs.Consume(), 1)); }

            return null;
        }
        public void LookAheadTestStartMiniumLength()
        {
            var look = new List<Instruction>
            {
                X.StartObject<Style>(),
                X.EndObject()
            };

            var enumerator = look.GetEnumerator();
            enumerator.MoveNext();
            var count = LookaheadBuffer.GetUntilEndOfRoot(enumerator).Count();
            Assert.Equal(2, count);
        }
Example #9
0
        private static TomlFloat ParseTomlFloat(ITomlRoot root, LookaheadBuffer <Token> tokens)
        {
            var floatToken = tokens.Consume();

            var check = floatToken.value;
            int startToCheckForZeros = check[0] == '+' || check[0] == '-' ? 1 : 0;

            if (check[startToCheckForZeros] == '0' && check[startToCheckForZeros + 1] != '.')
            {
                throw new Exception($"Failed to parse TOML float with '{floatToken.value}' because it has  a leading '0' which is not allowed by the TOML specification.");
            }

            return(new TomlFloat(root, double.Parse(floatToken.value.Replace("_", string.Empty), CultureInfo.InvariantCulture)));
        }
Example #10
0
        public static Token? TryMatch(LookaheadBuffer<char> chars)
        {
            if (!chars.TryExpect('#')) { return null; }
            else
            {
                chars.Consume();
                StringBuilder sb = new StringBuilder(64);
                while (!chars.End && !chars.TryExpect('\r') && !chars.TryExpect('\n'))
                {
                    sb.Append(chars.Consume());
                }

                return new Token(TokenType.Comment, sb.ToString());
            }
        }
Example #11
0
        private static TomlString ParseStringValue(ITomlRoot root, LookaheadBuffer <Token> tokens)
        {
            var t = tokens.Consume();

            Debug.Assert(t.type == TokenType.String);

            if (t.value.Contains("\n"))
            {
                throw Parser.CreateParseError(t, $"String '{t.value}' is invalid because it contains newlines.");
            }

            var s = t.value.Unescape(t);

            return(new TomlString(root, s, TomlString.TypeOfString.Normal));
        }
Example #12
0
        public void LookAheadTestWithGetObjectAndCollection()
        {
            var look = resources.ComboBoxUnsorted;
            var expectedCount = look.Count;

            for (var t = 0; t < 4; t++)
            {
                look.Add(new Instruction(InstructionType.Value, "Noise"));
            }

            var enumerator = look.GetEnumerator();
            enumerator.MoveNext();
            var count = LookaheadBuffer.GetUntilEndOfRoot(enumerator).Count();

            Assert.Equal(expectedCount, count);
        }
Example #13
0
        public static Token?TryMatch(LookaheadBuffer <char> chars)
        {
            if (!chars.TryExpect('#'))
            {
                return(null);
            }
            else
            {
                chars.Consume();
                StringBuilder sb = new StringBuilder(Constants.MatcherBufferSize);
                while (!chars.End && !chars.TryExpect('\r') && !chars.TryExpect('\n'))
                {
                    sb.Append(chars.Consume());
                }

                return(new Token(TokenType.Comment, sb.ToString()));
            }
        }
Example #14
0
        public void LookAheadTest()
        {
            var look = new List<Instruction>
            {
                X.StartObject<Style>(),
                X.StartMember<Setter>(c => c.Value),
                X.Value("Value"),
                X.EndMember(),
                X.StartMember<Setter>(c => c.Property),
                X.Value("Property"),
                X.EndMember(),
                X.EndObject(),
            };

            var enumerator = look.GetEnumerator();
            enumerator.MoveNext();
            var count = LookaheadBuffer.GetUntilEndOfRoot(enumerator).Count();
            Assert.Equal(8, count);
        }
Example #15
0
        private static TomlString ParseMultilineLiteralString(ITomlRoot root, LookaheadBuffer <Token> tokens)
        {
            var t = tokens.Consume();

            Debug.Assert(t.type == TokenType.MultilineLiteralString);

            string s = t.value;

            // Trim newline following the """ tag immediately
            if (s.Length > 0 && s[0] == '\r')
            {
                s = s.Substring(1);
            }
            if (s.Length > 0 && s[0] == '\n')
            {
                s = s.Substring(1);
            }

            return(new TomlString(root, t.value, TomlString.TypeOfString.MultilineLiteral));
        }
Example #16
0
        public void LookAheadTest10()
        {
            var look = new List<Instruction>
            {
                X.StartObject<Setter>(),
                X.StartObject<Setter>(),
                X.StartObject<Setter>(),
                X.StartObject<Setter>(),
                X.StartObject<Setter>(),
                X.EndObject(),
                X.EndObject(),
                X.EndObject(),
                X.EndObject(),
                X.EndObject(),
            };

            var enumerator = look.GetEnumerator();
            enumerator.MoveNext();
            var count = LookaheadBuffer.GetUntilEndOfRoot(enumerator).Count();
            Assert.Equal(10, count);
        }
Example #17
0
        private static TomlString ParseMultilineString(ITomlRoot root, LookaheadBuffer <Token> tokens)
        {
            var t = tokens.Consume();

            Debug.Assert(t.type == TokenType.MultilineString);

            var s = t.value;

            // Trim newline following the """ tag immediate
            if (s.Length > 0 && s[0] == '\r')
            {
                s = s.Substring(1);
            }
            if (s.Length > 0 && s[0] == '\n')
            {
                s = s.Substring(1);
            }

            s = ReplaceDelimeterBackslash(s);

            return(new TomlString(root, s, TomlString.TypeOfString.Multiline));
        }
Example #18
0
        private static TomlFloat ParseTomlFloat(ITomlRoot root, LookaheadBuffer <Token> tokens)
        {
            var floatToken = tokens.Consume();

            switch (floatToken.value)
            {
            case "nan":
            case "+nan": return(new TomlFloat(root, double.NaN));

            case "-nan": return(new TomlFloat(root, -double.NaN));

            case "inf":
            case "+inf": return(new TomlFloat(root, double.PositiveInfinity));

            case "-inf": return(new TomlFloat(root, double.NegativeInfinity));

            default:
                return(new TomlFloat(
                           root,
                           double.Parse(floatToken.value.Replace("_", string.Empty), CultureInfo.InvariantCulture)));
            }
        }
Example #19
0
        private static TomlInt ParseTomlInt(ITomlRoot root, LookaheadBuffer<Token> tokens)
        {
            var token = tokens.Consume();

            if (token.value.Length > 1 && token.value[0] == '0')
            {
                throw new Exception($"Failed to parse TOML int with '{token.value}' because it has  a leading '0' which is not allowed by the TOML specification.");
            }

            return new TomlInt(root, long.Parse(token.value.Replace("_", string.Empty)));
        }
Example #20
0
        private static TomlString ParseStringValue(ITomlRoot root, LookaheadBuffer<Token> tokens)
        {
            var t = tokens.Consume();

            Debug.Assert(t.type == TokenType.String);

            if (t.value.Contains("\n"))
            {
                throw Parser.CreateParseError(t, $"String '{t.value}' is invalid because it contains newlines.");
            }

            var s = t.value.TrimNChars(1).Unescape(t);

            return new TomlString(root, s, TomlString.TypeOfString.Normal);
        }
Example #21
0
        private static TomlFloat ParseTomlFloat(ITomlRoot root, LookaheadBuffer<Token> tokens)
        {
            var floatToken = tokens.Consume();

            var check = floatToken.value;
            int startToCheckForZeros = check[0] == '+' || check[0] == '-' ? 1 : 0;

            if (check[startToCheckForZeros] == '0' && check[startToCheckForZeros + 1] != '.')
            {
                throw new Exception($"Failed to parse TOML float with '{floatToken.value}' because it has  a leading '0' which is not allowed by the TOML specification.");
            }

            return new TomlFloat(root, double.Parse(floatToken.value.Replace("_", string.Empty), CultureInfo.InvariantCulture));
        }
Example #22
0
        private static TomlString ParseMultilineLiteralString(ITomlRoot root, LookaheadBuffer<Token> tokens)
        {
            var t = tokens.Consume();
            Debug.Assert(t.type == TokenType.MultilineLiteralString);

            var s = t.value.TrimNChars(3);

            // Trim newline following the """ tag immediate
            if (s.Length > 0 && s[0] == '\r') { s = s.Substring(1); }
            if (s.Length > 0 && s[0] == '\n') { s = s.Substring(1); }

            return new TomlString(root, s, TomlString.TypeOfString.MultilineLiteral);
        }