/// <summary> /// Process Includes as source. /// </summary> /// <param name="listing">The source listing containing potential ".include" directives.</param> /// <returns>Returns the new source with the included sources expanded.</returns> IEnumerable <SourceLine> ProcessIncludes(IEnumerable <SourceLine> listing) { var includedLines = new List <SourceLine>(); foreach (var line in listing) { if (line.Instruction.Equals(".include", Controller.Options.StringComparison)) { string filename = line.Operand; if (filename.EnclosedInQuotes() == false) { Controller.Log.LogEntry(line, ErrorStrings.FilenameNotSpecified); continue; } var inclistings = ConvertToSource(filename.TrimOnce('"')); includedLines.AddRange(inclistings); } else if (line.Instruction.Equals(".binclude", Controller.Options.StringComparison)) { var args = line.Operand.CommaSeparate(); var openblock = new SourceLine(); if (args.Count > 1) { if (string.IsNullOrEmpty(line.Label) == false && _symbolNameFunc(line.Label) == false) { Controller.Log.LogEntry(line, ErrorStrings.LabelNotValid, line.Label); continue; } if (line.Operand.EnclosedInQuotes() == false) { Controller.Log.LogEntry(line, ErrorStrings.None); continue; } if (FileRegistry.Add(line.Operand.TrimOnce('"')) == false) { throw new Exception(string.Format(ErrorStrings.FilePreviouslyIncluded, line.Operand)); } openblock.Label = line.Label; } else if (line.Operand.EnclosedInQuotes() == false) { Controller.Log.LogEntry(line, ErrorStrings.FilenameNotSpecified); continue; } openblock.Instruction = ConstStrings.OPEN_SCOPE; includedLines.Add(openblock); var inclistings = ConvertToSource(line.Operand.TrimOnce('"')); includedLines.AddRange(inclistings); includedLines.Add(new SourceLine()); includedLines.Last().Instruction = ConstStrings.CLOSE_SCOPE; } else { includedLines.Add(line); } } return(includedLines); }
/// <summary> /// Converts a file to a SourceLine list. /// </summary> /// <param name="file">The filename.</param> /// <returns>A <see cref="T:System.Collections.Generic.IEnumerable<DotNetAsm.SourceLine>"/> d.</returns> public IEnumerable <SourceLine> ConvertToSource(string file) { if (FileRegistry.Add(file) == false) { throw new Exception(string.Format(ErrorStrings.FilePreviouslyIncluded, file)); } if (File.Exists(file)) { Console.WriteLine("Processing input file " + file + "..."); int currentline = 1; var sourcelines = new List <SourceLine>(); using (StreamReader reader = new StreamReader(File.Open(file, FileMode.Open))) { while (reader.EndOfStream == false) { var unprocessedline = reader.ReadLine(); try { var line = new SourceLine(file, currentline, unprocessedline); line.Parse( delegate(string token) { return(Controller.IsInstruction(token) || Reserved.IsReserved(token) || (token.StartsWith(".") && Macro.IsValidMacroName(token.Substring(1))) || token == "="); }); sourcelines.Add(line); } catch (Exception ex) { Controller.Log.LogEntry(file, currentline, ex.Message); } currentline++; } sourcelines = Preprocess(sourcelines).ToList(); } return(sourcelines); } throw new FileNotFoundException(string.Format("Unable to open source file \"{0}\"", file)); }
public void Process(SourceLine line) { var openblock = new SourceLine(); if (line.Instruction.Equals(".binclude", Assembler.Options.StringComparison)) { var args = line.Operand.CommaSeparate(); if (args.Count > 1) { if (string.IsNullOrEmpty(line.Label) == false) { Assembler.Log.LogEntry(line, ErrorStrings.LabelNotValid, line.Label); Reset(); return; } else { openblock.Label = line.Label; } } else if (line.Operand.EnclosedInQuotes() == false) { Assembler.Log.LogEntry(line, ErrorStrings.FilenameNotSpecified); } openblock.Instruction = ConstStrings.OPEN_SCOPE; _includedLines.Add(openblock); } if (line.Operand.EnclosedInQuotes() == false) { Assembler.Log.LogEntry(line, ErrorStrings.None); } else { var fileName = line.Operand.Trim('"'); if (File.Exists(fileName)) { FileRegistry.Add(fileName); Console.WriteLine("Processing input file " + fileName + "..."); int currentline = 1; var sourcelines = new List <SourceLine>(); using (StreamReader reader = new StreamReader(File.Open(fileName, FileMode.Open))) { while (reader.EndOfStream == false) { var source = reader.ReadLine(); _includedLines.Add(new SourceLine(fileName, currentline, source)); currentline++; } } } else { throw new FileNotFoundException(string.Format("Unable to open source file '{0}'", fileName)); } if (openblock.Instruction.Equals(ConstStrings.OPEN_SCOPE)) { var closeBlock = new SourceLine { Instruction = ConstStrings.CLOSE_SCOPE }; _includedLines.Add(closeBlock); } } ProcessCommentBlocks(_includedLines); }