public void Read()
        {
            // arrange
            var reader  = new SpiceNetlistReader(new SpiceNetlistReaderSettings(new SpiceNetlistCaseSensitivitySettings(), () => null));
            var netlist = new SpiceNetlist(string.Empty, new Statements());

            // act
            var result = reader.Read(netlist);

            // assert
            Assert.NotNull(result);
        }
Esempio n. 2
0
        /// <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 SpiceNetlist GetPreprocessedNetListModel(SpiceNetlist originalNetlistModel, SpiceParserValidationResult validationResult)
        {
            SpiceNetlist preprocessedNetListModel = (SpiceNetlist)originalNetlistModel.Clone();
            var          preprocessorContext      = GetEvaluationContext();

            foreach (var preprocessor in Preprocessors)
            {
                preprocessor.Validation = validationResult;

                if (preprocessor is IEvaluatorConsumer consumer)
                {
                    consumer.EvaluationContext = preprocessorContext;
                    consumer.CaseSettings      = Settings.Reading?.CaseSensitivity;
                }

                preprocessedNetListModel.Statements = preprocessor.Process(preprocessedNetListModel.Statements);
            }

            return(preprocessedNetListModel);
        }
        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));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Translates Netlist object mode to SpiceSharp netlist.
        /// </summary>
        /// <param name="netlist">A object model of the netlist.</param>
        /// <returns>
        /// A new SpiceSharp netlist.
        /// </returns>
        public SpiceModel <SpiceSharp.Circuit, Simulation> Read(SpiceNetlist netlist)
        {
            if (netlist == null)
            {
                throw new System.ArgumentNullException(nameof(netlist));
            }

            // Get result netlist
            var result = new SpiceModel <Circuit, Simulation>(
                new Circuit(StringComparerProvider.Get(Settings.CaseSensitivity.IsEntityNameCaseSensitive)),
                netlist.Title);

            // Get reading context
            var resultService     = new ResultService(result);
            var nodeNameGenerator = new MainCircuitNodeNameGenerator(
                new [] { "0" },
                Settings.CaseSensitivity.IsNodeNameCaseSensitive);
            var            objectNameGenerator = new ObjectNameGenerator(string.Empty);
            INameGenerator nameGenerator       = new NameGenerator(nodeNameGenerator, objectNameGenerator);
            IRandomizer    randomizer          = new Randomizer(
                Settings.CaseSensitivity.IsDistributionNameCaseSensitive,
                seed: Settings.Seed);

            IExpressionParserFactory  expressionParserFactory  = new ExpressionParserFactory(Settings.CaseSensitivity);
            IExpressionFeaturesReader expressionFeaturesReader = new ExpressionFeaturesReader(expressionParserFactory);
            IExpressionValueProvider  expressionValueProvider  = new ExpressionValueProvider(expressionParserFactory);

            EvaluationContext expressionContext = new SpiceEvaluationContext(
                string.Empty,
                Settings.EvaluatorMode,
                Settings.CaseSensitivity,
                randomizer,
                expressionParserFactory,
                expressionFeaturesReader,
                expressionValueProvider,
                nameGenerator,
                resultService);

            var simulationEvaluationContexts = new SimulationEvaluationContexts(expressionContext);
            ISimulationPreparations simulationPreparations = new SimulationPreparations(
                new EntityUpdates(Settings.CaseSensitivity.IsParameterNameCaseSensitive, simulationEvaluationContexts),
                new SimulationsUpdates(simulationEvaluationContexts));

            ICircuitEvaluator      circuitEvaluator = new CircuitEvaluator(simulationEvaluationContexts, expressionContext);
            ISpiceStatementsReader statementsReader = new SpiceStatementsReader(Settings.Mappings.Controls,
                                                                                Settings.Mappings.Models, Settings.Mappings.Components);
            IWaveformReader waveformReader = new WaveformReader(Settings.Mappings.Waveforms);

            ICircuitContext circuitContext = new CircuitContext(
                "Root circuit context",
                null,
                circuitEvaluator,
                simulationPreparations,
                resultService,
                nameGenerator,
                statementsReader,
                waveformReader,
                Settings.CaseSensitivity,
                Settings.Mappings.Exporters,
                Settings.WorkingDirectory,
                null);

            // Set initial seed
            circuitContext.Evaluator.Seed = Settings.Seed;

            // Read statements form input netlist using created context
            circuitContext.Read(netlist.Statements, Settings.Orderer);

            // Set final seed
            result.Seed = circuitContext.Evaluator.Seed;

            return(result);
        }
        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));
            }
        }