/// <summary> /// Parses the netlist. /// </summary> /// <param name="spiceNetlist">Netlist to parse.</param> /// <returns> /// A parsing result. /// </returns> public SpiceParserResult ParseNetlist(string spiceNetlist) { if (spiceNetlist == null) { throw new ArgumentNullException(nameof(spiceNetlist)); } var result = new SpiceParserResult { ValidationResult = new SpiceParserValidationResult() }; // Get tokens try { var tokens = TokenProviderPool.GetSpiceTokenProvider(Settings.Lexing).GetTokens(spiceNetlist); SpiceNetlist originalNetlistModel = SingleNetlistParser.Parse(tokens); // Preprocessing var preprocessedNetListModel = GetPreprocessedNetListModel(originalNetlistModel, result.ValidationResult); // Reading model var reader = new SpiceNetlistReader(Settings.Reading); ISpiceModel <Circuit, Simulation> spiceModel = reader.Read(preprocessedNetListModel); result.OriginalInputModel = originalNetlistModel; result.PreprocessedInputModel = preprocessedNetListModel; result.SpiceModel = spiceModel; result.ValidationResult.Reading.AddRange(result.SpiceModel.ValidationResult); } catch (LexerException e) { result.ValidationResult.Lexing.Add( new ValidationEntry( ValidationEntrySource.Lexer, ValidationEntryLevel.Error, e.ToString(), null)); } catch (ParseException e) { result.ValidationResult.Parsing.Add( new ValidationEntry( ValidationEntrySource.Parser, ValidationEntryLevel.Error, e.ToString(), null)); } return(result); }
private void ReadSingleLib(Statements statements, string currentDirectoryPath, Control lib) { // get full path of .lib string libPath = PathConverter.Convert(lib.Parameters.Get(0).Image); bool isAbsolutePath = Path.IsPathRooted(libPath); string libFullPath = isAbsolutePath ? libPath : Path.Combine(currentDirectoryPath, libPath); // check if file exists if (!File.Exists(libFullPath)) { Validation.Reading.Add( new ValidationEntry( ValidationEntrySource.Reader, ValidationEntryLevel.Warning, $"Netlist include at {libFullPath} could not be found", lib.LineInfo)); return; } // get lib content string libContent = FileReader.ReadAll(libFullPath); if (libContent != null) { var lexerSettings = new SpiceLexerSettings() { HasTitle = false, IsDotStatementNameCaseSensitive = LexerSettings.IsDotStatementNameCaseSensitive, }; var tokens = TokenProviderPool.GetSpiceTokenProvider(lexerSettings).GetTokens(libContent); foreach (var token in tokens) { token.FileName = libFullPath; } SpiceNetlistParser.Settings = new SingleSpiceNetlistParserSettings(lexerSettings) { IsNewlineRequired = false, IsEndRequired = false, }; SpiceNetlist includeModel = SpiceNetlistParser.Parse(tokens); var allStatements = includeModel.Statements.ToList(); if (lib.Parameters.Count == 2) { ReadSingleLibWithTwoArguments(statements, lib, allStatements); } else if (lib.Parameters.Count == 1) { ReadSingleLibWithOneArgument(statements, lib, allStatements); } } else { Validation.Reading.Add( new ValidationEntry( ValidationEntrySource.Reader, ValidationEntryLevel.Warning, $"Netlist include at {libFullPath} could not be read", lib.LineInfo)); } }
private void ReadSingleInclude(Statements statements, string currentDirectoryPath, Control include) { // get full path of .include string includePath = include.Parameters.Get(0).Image; includePath = PathConverter.Convert(includePath); bool isAbsolutePath = Path.IsPathRooted(includePath); string includeFullPath = isAbsolutePath ? includePath : Path.Combine(currentDirectoryPath, includePath); // check if file exists if (!File.Exists(includeFullPath)) { Validation.Reading.Add( new ValidationEntry( ValidationEntrySource.Reader, ValidationEntryLevel.Warning, $"Netlist include at {includeFullPath} is not found", include.LineInfo)); return; } // get include content string includeContent = FileReader.ReadAll(includeFullPath); if (includeContent != null) { var lexerSettings = new SpiceLexerSettings() { HasTitle = false, IsDotStatementNameCaseSensitive = LexerSettings.IsDotStatementNameCaseSensitive, }; var tokens = TokenProviderPool.GetSpiceTokenProvider(lexerSettings).GetTokens(includeContent); foreach (var token in tokens) { token.FileName = includeFullPath; } SpiceNetlistParser.Settings = new SingleSpiceNetlistParserSettings(lexerSettings) { IsNewlineRequired = false, IsEndRequired = false, }; SpiceNetlist includeModel = SpiceNetlistParser.Parse(tokens); // process includes of include netlist includeModel.Statements = Process(includeModel.Statements, Path.GetDirectoryName(includeFullPath)); // replace statement by the content of the include statements.Replace(include, includeModel.Statements); } else { Validation.Reading.Add( new ValidationEntry( ValidationEntrySource.Reader, ValidationEntryLevel.Warning, $"Netlist include at {includeFullPath} could not be read", include.LineInfo)); } }