Example #1
0
 private static IEnumerable<Token> ReadTokensFromString(string input)
 {
     using(var reader = new LocatedTextReaderWrapper(input)) {
         var tokenReader = new TokenReader(reader);
         return tokenReader.ReadTokens().ToList();
     }
 }
Example #2
0
        public void BadStrings()
        {
            Func<string, TestDelegate> stringTester = (input) => () => {
                using(var reader = new LocatedTextReaderWrapper(input)) {
                    var tokenReader = new TokenReader(reader);
                    tokenReader.ReadToken();
                }
            };

            Assert.Throws<MissingDataException>(stringTester("\""));
            Assert.Throws<MissingDataException>(stringTester("\"\\"));
            Assert.Throws<BadDataException>(stringTester("\"\\?\""));
        }
Example #3
0
        public void Strings()
        {
            string input = "\"\\\"hello \\\\\\t\\r\\nworld\\\"\"";

            using(var reader = new LocatedTextReaderWrapper(input)) {
                var tokenReader = new TokenReader(reader);
                var token = tokenReader.ReadToken();

                Assert.AreEqual(-1, reader.Peek());    // EOF

                Assert.AreEqual(token.TokenType, TokenType.String);
                Assert.AreEqual(token.Value, "\"hello \\\t\r\nworld\"");
            }
        }
Example #4
0
        private static TokenNode ExpressionToTokenNode(string expression)
        {
            using(var reader = new LocatedTextReaderWrapper(expression, new Location())) {
                var tokenReader = new TokenReader(reader);

                return ExpressionRewriter.Rewrite(tokenReader);
            }
        }
Example #5
0
        /// <summary>
        /// Parses a line containing a directive reference.
        /// </summary>
        /// <param name="nameExpression">The regular expression to match the name of the directive.</param>
        /// <param name="line">The line containing the directive.</param>
        /// <param name="location">The location of the directive.</param>
        /// <param name="parameterReader">The reader of the parsed directive parameters.</param>
        /// <param name="directiveName">The parsed name of the directive.</param>
        /// <returns>True if the directive was successfully parsed; otherwise, false.</returns>
        private static bool ParseDirectiveLine(string nameExpression, string line, Location location, out ITokenReader parameterReader, out string directiveName)
        {
            parameterReader = null;
            directiveName = null;

            Regex re = new Regex("^#(?<name>" + nameExpression + ")(\\s+(?<params>.*))?$", RegexOptions.ExplicitCapture);

            var match = re.Match(line);

            if(!match.Success) {
                return false;
            }

            directiveName = match.Groups["name"].Value;

            var parametersText = match.Groups["params"].Value;
            var parametersLocation = location.Clone();
            parametersLocation.AdvanceString(line.Substring(0, match.Groups["params"].Index));
            var parametersReader = new LocatedTextReaderWrapper(parametersText, parametersLocation);

            parameterReader = new TokenReader(parametersReader);

            return true;
        }