static void Main(string[] args) { Console.WriteLine("Start file parsing..."); using (var file = new FileStream(".\\Samples\\Can_500k_Alfa_159.txt", FileMode.Open)) using (var fileReader = new StreamReader(file)) { var fileParser = new FileParser(); var nodeReaderFactory = new NodeReaderFactory(); while (!fileReader.EndOfStream) { var line = fileReader.ReadLine(); var message = fileParser.ParseLine(line); INodeReader nodeReader = null; try { nodeReader = nodeReaderFactory.CreateNodeReader(message); } catch (NotSupportedException) { Console.WriteLine($"Line has been ignored: Identifier:{message.Identifier} not supported."); continue; } Console.WriteLine($"Reading values from {nodeReader.Name}:{nodeReader.Identifier}"); foreach (var signal in nodeReader.Signals) { Console.WriteLine(signal.ToString()); } } } Console.WriteLine("Finish file parsing..."); }
// TODO Clean up. public IfNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : base(directiveName, location) { Conditions = new List<ConditionSet>(); var condition = ExpressionRewriter.Rewrite(tokenReader); var curConditionSet = new ConditionSet(condition); Conditions.Add(curConditionSet); var childrenNodes = nodeReader.TakeWhile((node) => !IsEndDirective(node, DirectiveName)); foreach(var node in childrenNodes) { bool isConditional; var newCondition = GetConditionFromNode(node, out isConditional); if(isConditional) { if(newCondition != null && curConditionSet.Condition == null) { throw new BadDataException("Can't have an elseif node after an else node", node.Location); } curConditionSet = new ConditionSet(newCondition); Conditions.Add(curConditionSet); continue; } curConditionSet.ChildrenNodes.Add(node); } }
public LetNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : base(directiveName, location) { var startLocation = tokenReader.CurrentLocation; Token token = tokenReader.ReadToken(); if(token == null) { throw new MissingDataException("Variable name", startLocation); } if(token.TokenType != TokenType.Identifier) { throw new MissingDataException("Variable name", token.Location); } Variable = token.Value.ToString(); var shorthand = ReadShorthandNode(tokenReader); if(shorthand != null) { ChildrenNodes = new List<NodeBase> { shorthand }; } else { ChildrenNodes = new List<NodeBase>(nodeReader.TakeWhile((node) => !IsEndDirective(node, DirectiveName))); } }
public RepNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : base(directiveName, location) { Value = ExpressionRewriter.Rewrite(tokenReader); ChildrenNodes = new List<NodeBase>(nodeReader.TakeWhile((node) => !IsEndDirective(node, DirectiveName))); }
public EachNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : base(directiveName, location) { var startLocation = tokenReader.CurrentLocation; Token token = tokenReader.ReadToken(); if(token == null) { throw new MissingDataException("Variable name", startLocation); } if(token.TokenType != TokenType.Identifier) { throw new MissingDataException("Variable name", token.Location); } Variable = token.Value.ToString(); Values = ExpressionRewriter.Rewrite(tokenReader); ChildrenNodes = new List<NodeBase>(nodeReader.TakeWhile((node) => { var endDirective = node as EndDirectiveNode; if(endDirective != null && endDirective.TargetDirectiveName == DirectiveName) { return false; } return true; })); }
public ForNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : this(directiveName, location) { var parameters = ExpressionRewriter.Rewrite(tokenReader); if(!parameters.Token.IsSymbol(",")) { throw new DataTypeException("Expected comma-separated list", this); } var children = parameters.ChildrenTokenNodes; if(children.Count < 3 || children.Count > 4) { throw new MissingDataException("#for directive requires 3 to 4 parameters", location); } if(children[0].Token.TokenType != TokenType.Identifier) { throw new MissingDataException("Identifier", children[0].Location); } Variable = children[0].Token.ToString(); Start = children[1]; End = children[2]; Step = children.Count > 3 ? children[3] : null; ChildrenNodes = new List<NodeBase>(nodeReader.TakeWhile((node) => !IsEndDirective(node, DirectiveName))); }
public void Write(INodeReader nodeReader) { if (writers.ContainsKey(nodeReader?.Name)) { writers[nodeReader.Name].Write(nodeReader); } }
public IncludeNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : base(directiveName, location) { Filename = ExpressionRewriter.Rewrite(tokenReader); this.parentParser = nodeReader as Parser.Parser; }
public LocalNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : base(directiveName, location) { var startLocation = tokenReader.CurrentLocation; VariableName = tokenReader.ReadToken(); if(VariableName == null) { throw new MissingDataException("Expected a variable name", startLocation); } if(VariableName.TokenType != TokenType.Identifier) { throw new BadDataException("Expected a variable name", VariableName.Location); } }
public DefineNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : this(directiveName, location) { var startLocation = tokenReader.CurrentLocation; Token variable = tokenReader.ReadToken(); if(variable == null) { throw new MissingDataException("Variable name", startLocation); } if(variable.TokenType != TokenType.Identifier) { throw new MissingDataException("Variable name", variable.Location); } Variable = variable.ToString(); // TODO Extract method. var functionParameters = new List<string>(); FunctionParameters = functionParameters; Token token = tokenReader.ReadToken(); if(token != null && token.IsSymbol("(")) { while(token != null && !token.IsSymbol(")")) { token = tokenReader.ReadToken(); if(token.TokenType == TokenType.Identifier) { functionParameters.Add(token.Value.ToString()); } } if(token == null) { throw new MissingDataException("Closing parentheses"); } token = null; } var shorthand = ReadShorthandNode(tokenReader, token); if(shorthand != null) { ChildrenNodes = new List<NodeBase> { shorthand }; } else { ChildrenNodes = new List<NodeBase>(nodeReader.TakeWhile((node) => !IsEndDirective(node, DirectiveName))); } }
public void Write(INodeReader nodeReader) { var data = string.Join(ColumnSeparator, nodeReader.Signals.Select(s => s.ToString())); if (!string.IsNullOrWhiteSpace(data)) { var fullFileName = Path.Combine(outputDirectory, FileName); if (!File.Exists(fullFileName)) { var columnNames = string.Join(ColumnSeparator, nodeReader.Signals.Select(s => s.Name)); using (var streamWriter = File.CreateText(fullFileName)) { streamWriter.WriteLine(columnNames); } } using (var streamWriter = File.AppendText(fullFileName)) { streamWriter.WriteLine(data); } } }
public ElseNode(ITokenReader tokenReader, INodeReader nodeReader, string directiveName = null, Location location = null) : base(directiveName, location) { }
/// NOTE: order of readers sets precedence public Parser(INodeReader[] readers) { nodeReaders = readers; }
/// <summary> /// Creates an instance of a directive. /// </summary> /// <param name="nodeType">Type of the directive node.</param> /// <param name="tokenReader">The token reader.</param> /// <param name="nodeReader">The node reader.</param> /// <param name="directiveName">Name of the directive.</param> /// <param name="location">The location of the directive.</param> /// <returns>The new <see cref="DirectiveNode"/> instance.</returns> private static DirectiveNode CreateInstance(Type nodeType, ITokenReader tokenReader, INodeReader nodeReader, string directiveName, Location location) { return Activator.CreateInstance(nodeType, tokenReader, nodeReader, directiveName, location) as DirectiveNode; }