Skip to content

Commit 8005665

Browse files
committed
Fixed problem where IJ4JLogger wasn't being passed correctly among objects. Fixed problem where tokens weren't being created if the initial token was text (which will always be the case if the program's path is passed). Enhanced error messages when parsing problems encountered.
1 parent 28fc21f commit 8005665

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

Binder/J4JCommandLineSource.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,23 @@ public class J4JCommandLineSource : IConfigurationSource
1111

1212
public J4JCommandLineSource(
1313
IOptionCollection options,
14-
string cmdLine,
15-
Func<IJ4JLogger>? loggerFactory = null
14+
string cmdLine
1615
)
1716
{
1817
Options = options;
1918
_cmdLine = cmdLine;
2019

21-
var tokenizer = new Tokenizer( options.CommandLineStyle, options.MasterText.TextComparison, loggerFactory );
22-
var parsingTable = new ParsingTable( options, loggerFactory );
20+
var tokenizer = new Tokenizer( options.CommandLineStyle, options.MasterText.TextComparison, options.LoggerFactory );
21+
var parsingTable = new ParsingTable( options, options.LoggerFactory );
2322

24-
_parser = new Parser( options, tokenizer, parsingTable, loggerFactory?.Invoke() );
23+
_parser = new Parser( options, tokenizer, parsingTable, options.LoggerFactory?.Invoke() );
2524
}
2625

2726
public J4JCommandLineSource(
2827
IOptionCollection options,
29-
string[] args,
30-
Func<IJ4JLogger>? loggerFactory
28+
string[] args
3129
)
32-
: this( options, string.Join( " ", args ), loggerFactory )
30+
: this( options, string.Join( " ", args ) )
3331
{
3432
}
3533

Binder/grammar/Parser.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ public Parser(
1212
CommandLineStyle style = CommandLineStyle.Windows,
1313
Func<IJ4JLogger>? loggerFactory = null
1414
)
15-
: this( new OptionCollection( style ), loggerFactory )
15+
: this( new OptionCollection( style, loggerFactory ) )
1616
{
1717
}
1818

19-
public Parser(
20-
IOptionCollection options,
21-
Func<IJ4JLogger>? loggerFactory = null
22-
)
19+
public Parser( IOptionCollection options )
2320
: this(
2421
options,
25-
new Tokenizer( options.CommandLineStyle, options.MasterText.TextComparison, loggerFactory ),
26-
new ParsingTable( options, loggerFactory ),
27-
(loggerFactory == null ? options.LoggerFactory : loggerFactory)?.Invoke() )
22+
new Tokenizer( options.CommandLineStyle, options.MasterText.TextComparison, options.LoggerFactory ),
23+
new ParsingTable( options, options.LoggerFactory ),
24+
options.LoggerFactory?.Invoke() )
2825
{
2926
}
3027

@@ -56,9 +53,7 @@ public bool Parse( string cmdLine )
5653

5754
var allOkay = true;
5855

59-
var tokens = _tokenizer.Tokenize( cmdLine );
60-
foreach( var token in tokens )
61-
//foreach( var token in _tokenizer.Tokenize( cmdLine ) )
56+
foreach( var token in _tokenizer.Tokenize(cmdLine))
6257
{
6358
allOkay &= ParsingTable[ prevToken.Type, token.Type ]!( prevToken, token, token.Text );
6459

Binder/grammar/TokenEntry.TokenEntries.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public bool Create( Token prevToken, Token curToken, params string[] args )
3030

3131
public bool TerminateWithPrejuidice( Token prevToken, Token curToken, params string[] args )
3232
{
33-
_logger?.Error( "{0} ('{1}') follows {2} ('{3}'), which is not allowed",
34-
new object[] { curToken.Type, curToken.Text, prevToken.Type, prevToken.Text } );
33+
_logger?.Error( "{0} ('{1}') => {2} ('{3}'): {4}",
34+
new object[] { curToken.Type, curToken.Text, prevToken.Type, prevToken.Text, "invalid token sequence" } );
3535

3636
Current = null;
3737

@@ -42,13 +42,26 @@ public bool Commit( Token prevToken, Token curToken, params string[] args )
4242
{
4343
if( Current == null )
4444
{
45-
_logger?.Error( "Attempted to commit undefined TokenEntry" );
45+
// if we're not yet building an entry but we received a KeyPrefix token we must be
46+
// about to start building one
47+
if( curToken.Type == TokenType.KeyPrefix )
48+
return Create( prevToken, curToken );
49+
50+
_logger?.Error("{0} ('{1}') => {2} ('{3}'): {4}",
51+
new object[] { curToken.Type, curToken.Text, prevToken.Type, prevToken.Text, "undefined TokenEntry" });
52+
4653
return false;
4754
}
4855

4956
if( Current.Option == null )
5057
{
51-
_logger?.Error<string>( "Found entry with unexpected key '{0}'", Current.Key ?? string.Empty );
58+
_logger?.Error( "{0} ('{1}') => {2} ('{3}'): {4}",
59+
new object[]
60+
{
61+
curToken.Type, curToken.Text, prevToken.Type, prevToken.Text,
62+
$"unexpected key '{Current.Key}'"
63+
} );
64+
5265
Options.UnknownKeys.Add( Current );
5366

5467
// create a new TokenEntry
@@ -78,11 +91,12 @@ public bool ProcessText( Token prevToken, Token curToken, params string[] args )
7891
{
7992
if( args.Length != 1 )
8093
{
81-
_logger?.Error<int, TokenType, string>(
82-
"Invalid number of text arguments ({0}) process {1} ('{2}')",
83-
args.Length,
84-
curToken.Type,
85-
curToken.Text );
94+
_logger?.Error("{0} ('{1}') => {2} ('{3}'): {4}",
95+
new object[]
96+
{
97+
curToken.Type, curToken.Text, prevToken.Type, prevToken.Text,
98+
$"invalid number of text arguments '{args.Length}'"
99+
});
86100

87101
Current = null;
88102

0 commit comments

Comments
 (0)