private static IEnumerable<Token> ReadTokensFromString(string input) { using(var reader = new LocatedTextReaderWrapper(input)) { var tokenReader = new TokenReader(reader); return tokenReader.ReadTokens().ToList(); } }
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(); }
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); }
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("\"\\?\"")); }
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\""); } }
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" })); }
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)); } }
/// <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 }; }
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()); } }
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()); } }
private static TokenNode ExpressionToTokenNode(string expression) { using(var reader = new LocatedTextReaderWrapper(expression, new Location())) { var tokenReader = new TokenReader(reader); return ExpressionRewriter.Rewrite(tokenReader); } }
public TokenReader(LocatedTextReaderWrapper reader) { InputReader = reader; }
/// <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; }