Skip to content

Commit

Permalink
Merge pull request #136 from marcin-golebiowski/master
Browse files Browse the repository at this point in the history
Few improvements
  • Loading branch information
marcin-golebiowski authored Dec 3, 2019
2 parents 463b81a + a959f29 commit 650d0d9
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ public void SingleInclude()
Assert.Equal(14, ((BaseSimulation)netlist.Simulations[0]).Configurations.Get<BaseConfiguration>().Nodesets["OUT"]);
}

[Fact]
public void When_IncludeParentDirectory_Expect_Reference()
{
string modelFileContent = ".model 1N914 D(Is=2.52e-9 Rs=0.568 N=1.752 Cjo=4e-12 M=0.4 tt=20e-9)\n";
string modelFilePath = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "diodesx.mod");
File.WriteAllText(modelFilePath, modelFileContent);

var netlist = ParseNetlist(
"Include - Diode circuit",
"D1 OUT 0 1N914",
"V1 OUT 0 0",
".DC V1 -1 1 10e-3",
".SAVE V(OUT)",
".NODESET V(OUT)={x+1}",
".param x = 13",
$".include ../diodesx.mod",
".END");

RunDCSimulation(netlist, "V(OUT)");
Assert.Equal(14, ((BaseSimulation)netlist.Simulations[0]).Configurations.Get<BaseConfiguration>().Nodesets["OUT"]);
}

[Fact]
public void SingleIncludeSingleQuotes()
{
Expand Down
40 changes: 40 additions & 0 deletions src/SpiceSharpParser.Tests/Lexers/Spice/SpiceLexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,46 @@ public void Distribution()
Assert.True(tokens[1].SpiceTokenType == SpiceTokenType.EOF);
}

[Fact]
public void IncludeParent()
{
var tokensStr = ".INCLUDE ../lib/diodes.lib";
SpiceLexer lexer = new SpiceLexer(new SpiceLexerSettings { HasTitle = false });
var tokens = lexer.GetTokens(tokensStr).ToArray();
Assert.True(tokens.Length == 4);
Assert.True(tokens[0].SpiceTokenType == SpiceTokenType.DOT);
Assert.True(tokens[1].SpiceTokenType == SpiceTokenType.WORD);
Assert.True(tokens[2].SpiceTokenType == SpiceTokenType.WORD);
Assert.True(tokens[3].SpiceTokenType == SpiceTokenType.EOF);
}

[Fact]
public void IncludeParentInQuotes()
{
var tokensStr = ".INCLUDE \"../lib/diodes.lib\"";
SpiceLexer lexer = new SpiceLexer(new SpiceLexerSettings { HasTitle = false });
var tokens = lexer.GetTokens(tokensStr).ToArray();
Assert.True(tokens.Length == 4);
Assert.True(tokens[0].SpiceTokenType == SpiceTokenType.DOT);
Assert.True(tokens[1].SpiceTokenType == SpiceTokenType.WORD);
Assert.True(tokens[2].SpiceTokenType == SpiceTokenType.DOUBLE_QUOTED_STRING);
Assert.True(tokens[3].SpiceTokenType == SpiceTokenType.EOF);
}

[Fact]
public void IncludeCurrent()
{
var tokensStr = ".INCLUDE ./lib/diodes.lib";
SpiceLexer lexer = new SpiceLexer(new SpiceLexerSettings { HasTitle = false });
var tokens = lexer.GetTokens(tokensStr).ToArray();
Assert.True(tokens.Length == 4);
Assert.True(tokens[0].SpiceTokenType == SpiceTokenType.DOT);
Assert.True(tokens[1].SpiceTokenType == SpiceTokenType.WORD);
Assert.True(tokens[2].SpiceTokenType == SpiceTokenType.WORD);
Assert.True(tokens[3].SpiceTokenType == SpiceTokenType.EOF);
}


[Fact]
public void PercentSimpleTest()
{
Expand Down
29 changes: 24 additions & 5 deletions src/SpiceSharpParser/Lexers/LexerLineNumberProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SpiceSharpParser.Lexers
Expand All @@ -17,15 +18,33 @@ public LexerLineNumberProvider(string text)

public int GetLineForIndex(int index)
{
foreach (var t in _ranges)
// binary search over line ranges
int start = 0;
int end = _ranges.Count - 1;

while (start <= end)
{
if (t.From <= index && index <= t.To)
int middle = (end + start) / 2;

if (_ranges[middle].From <= index && index <= _ranges[middle].To)
{
return _ranges[middle].LineNumber;
}
else if (_ranges[middle].From > index)
{
end = middle - 1;
}
else if (_ranges[middle].To < index)
{
start = middle + 1;
}
else
{
return t.LineNumber;
return -1;
}
}

return _ranges.Last().LineNumber;
return -1;
}

private void Init()
Expand Down
8 changes: 8 additions & 0 deletions src/SpiceSharpParser/Lexers/Netlist/Spice/SpiceLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ private void BuildGrammar()
(SpiceLexerState state, string lexem) => LexerRuleUseDecision.Use,
ignoreCase: true));

builder.AddRegexRule(new LexerTokenRule<SpiceLexerState>(
(int)SpiceTokenType.WORD,
"A relative path",
@"(\.\.\\|\.\.\/|\.\/|\.\\)(<CHARACTER>|<SPECIAL>)*",
null,
(SpiceLexerState state, string lexem) => LexerRuleUseDecision.Use,
ignoreCase: true));

builder.AddRegexRule(
new LexerTokenRule<SpiceLexerState>(
(int)SpiceTokenType.IDENTIFIER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SpiceSharpParser.Models.Netlist.Spice.Objects.Parameters;
using System;
using System.Collections.Generic;
using SpiceSharpParser.ModelReaders.Netlist.Spice.Exceptions;

namespace SpiceSharpParser.ModelReaders.Netlist.Spice.Readers.EntityGenerators.Models
{
Expand Down Expand Up @@ -103,7 +104,7 @@ public override SpiceSharp.Components.Model Generate(string id, string type, Par
}
else
{
throw new Exception($"Unknown mosfet model level {level}");
throw new ModelNotFoundException($"Unknown mosfet model level {level}", parameters.LineNumber);
}

// Read all the parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SpiceSharpParser.Common;

namespace SpiceSharpParser.Models.Netlist.Spice.Objects
{
Expand Down Expand Up @@ -146,7 +147,7 @@ public void Replace(Statement statement, IEnumerable<Statement> statements)
}
else
{
throw new Exception("Unknown statement to replace");
throw new SpiceSharpParserException("Unknown statement to replace", statement.LineNumber);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/SpiceSharpParser/Parsers/Expression/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using SpiceSharpParser.Common;

namespace SpiceSharpParser.Parsers.Expression
{
Expand Down Expand Up @@ -148,7 +149,7 @@ private void ApplySpiceProperty(EvaluationContext context, SpicePropertyFoundEve

if (factory == null)
{
throw new Exception("Unknown spice property");
throw new SpiceSharpParserException("Unknown spice property");
}

var export = factory.CreateExport(
Expand Down

0 comments on commit 650d0d9

Please sign in to comment.