Beispiel #1
0
 private static IEnumerable<Token> ReadTokensFromString(string input)
 {
     using(var reader = new LocatedTextReaderWrapper(input)) {
         var tokenReader = new TokenReader(reader);
         return tokenReader.ReadTokens().ToList();
     }
 }
Beispiel #2
0
        public override string Execute(ExecutionContext context)
        {
            // TODO Clean up.
            var output = new StringBuilder();

            string filePath = Filename.Evaluate(context) as string;

            if(filePath == null) {
                throw new DataTypeException("Need string for filename", this);
            }

            if(Location != null && Location.FileName != null) {
                filePath = Path.GetDirectoryName(Location.FileName) + Path.DirectorySeparatorChar + filePath;
            }

            using(var inputFile = File.Open(filePath, FileMode.Open, FileAccess.Read))
            using(var reader = new LocatedTextReaderWrapper(inputFile, new Location(filePath), wrapperOwnsStream: false)) {
                foreach(var node in GetNodeReader(reader)) {
                    output.Append(node.Execute(context));
                }
            }

            if(!context.Dependencies.Contains(filePath)) {
                context.Dependencies.Add(filePath);
            }

            return output.ToString();
        }
Beispiel #3
0
        private INodeReader GetNodeReader(LocatedTextReaderWrapper textReader)
        {
            // HACK I really hate this coupling.
            if(this.parentParser == null) {
                return new Parser.Parser(textReader);
            }

            return new Parser.Parser(this.parentParser, textReader);
        }
Beispiel #4
0
        public void BadStrings()
        {
            Func<string, TestDelegate> stringTester = (input) => () => {
                using(var reader = new LocatedTextReaderWrapper(input)) {
                    Token.ReadToken(reader);
                }
            };

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

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

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

                Assert.AreEqual(token.TokenType, TokenType.String);
                Assert.AreEqual(token.Value, "\"hello \\\t\r\nworld\"");
            }
        }
Beispiel #6
0
        public void TestParameterParsing()
        {
            string input = @"#define test(a, b, c) abc";

            TreeNode.NodeBase node;

            using(var reader = new LocatedTextReaderWrapper(input)) {
                var parser = new Parser(reader);
                node = parser.ReadNode();
            }

            Assert.IsInstanceOf(typeof(TreeNode.DefineNode), node);

            TreeNode.DefineNode define = node as TreeNode.DefineNode;

            Assert.IsTrue(define.FunctionParameters.SequenceEqual(new string[] { "a", "b", "c" }));
        }
Beispiel #7
0
        public void Evaluation()
        {
            string input = "x${test - 42}.2";
            string expected = "x42.2";

            var context = new ExecutionContext();
            context.SetVariable("test", 84);

            var output = new StringBuilder();

            using(var reader = new LocatedTextReaderWrapper(input)) {
                var parser = new Parser(reader);

                foreach(var node in parser.ReadNodes()) {
                    output.Append(node.Execute(context));
                }

                Assert.AreEqual(expected, output.ToString());
            }
        }
 private static TreeNode.TokenNode ExpressionToTokenNode(string expression)
 {
     using(var reader = new LocatedTextReaderWrapper(expression, new Location())) {
         return ExpressionRewriter.Rewrite(Token.ReadTokens(reader));
     }
 }
Beispiel #9
0
        /// <summary>
        /// Parses a line containing a directive reference.
        /// </summary>
        /// <param name="parser">The parser.</param>
        /// <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>
        /// <returns>A <see cref="DirectiveInfo"/> instance containing information about the directive, or <c>null</c> if the directive does not match the expression.</returns>
        private static DirectiveInfo ParseDirectiveLine(Parser parser, string nameExpression, string line, Location location)
        {
            Regex re = new Regex("^#(?<name>" + nameExpression + ")(\\s+(?<params>.*))?$", RegexOptions.ExplicitCapture);

            var match = re.Match(line);

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

            var name = 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);

            return new DirectiveInfo {
                Parser = parser,
                Location = location,
                DirectiveName = name,
                ParametersReader = parametersReader
            };
        }
Beispiel #10
0
        public void NotVariableShorthand()
        {
            string input = "x$test.2";
            string expected = "x$test.2";

            var context = new ExecutionContext();

            var output = new StringBuilder();

            using(var reader = new LocatedTextReaderWrapper(input)) {
                var parser = new Parser(reader);

                parser.Options.AllowVariableShorthand = false;

                foreach(var node in parser.ReadNodes()) {
                    output.Append(node.Execute(context));
                }

                Assert.AreEqual(expected, output.ToString());
            }
        }
Beispiel #11
0
        private static void CheckParserOutput(string expected, string input)
        {
            var context = new ExecutionContext();

            var output = new StringBuilder();

            using(var reader = new LocatedTextReaderWrapper(input)) {
                var parser = new Parser(reader);

                foreach(var node in parser.ReadNodes()) {
                    output.Append(node.Execute(context));
                }

                Assert.AreEqual(expected, output.ToString());
            }
        }
Beispiel #12
0
        private static TokenNode ExpressionToTokenNode(string expression)
        {
            using(var reader = new LocatedTextReaderWrapper(expression, new Location())) {
                var tokenReader = new TokenReader(reader);

                return ExpressionRewriter.Rewrite(tokenReader);
            }
        }
Beispiel #13
0
 public TokenReader(LocatedTextReaderWrapper reader)
 {
     InputReader = reader;
 }
Beispiel #14
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;
        }