Skip to content

Commit a863d11

Browse files
authored
Allow end users to directly instantiate Scanner using a ParserOptions instance (#348)
1 parent 0f5962f commit a863d11

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

src/Esprima/JavascriptParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public JavaScriptParser(ParserOptions options)
142142
_maxAssignmentDepth = options.MaxAssignmentDepth;
143143
_onNodeCreated = options.OnNodeCreated;
144144

145-
_scanner = new Scanner(options);
145+
_scanner = new Scanner(options.ScannerOptions);
146146

147147
_context = new Context();
148148

src/Esprima/ParserOptions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace Esprima;
55
/// <summary>
66
/// Parser options.
77
/// </summary>
8-
public record class ParserOptions : IScannerOptions
8+
public record class ParserOptions
99
{
1010
public static readonly ParserOptions Default = new();
1111

12-
internal readonly ScannerOptions _scannerOptions = new();
12+
public ScannerOptions ScannerOptions { get; } = new();
1313

1414
/// <summary>
1515
/// Gets or sets whether the tokens are included in the parsed tree, defaults to <see langword="false"/>.
@@ -19,27 +19,27 @@ public record class ParserOptions : IScannerOptions
1919
/// <summary>
2020
/// Gets or sets whether the comments are included in the parsed tree, defaults to <see langword="false"/>.
2121
/// </summary>
22-
public bool Comments { get; init; }
22+
public bool Comments { get => ScannerOptions._comments; init => ScannerOptions._comments = value; }
2323

2424
/// <summary>
2525
/// Gets or sets whether the parser is tolerant to errors, defaults to <see langword="true"/>.
2626
/// </summary>
27-
public bool Tolerant { get; init; } = true;
27+
public bool Tolerant { get => ScannerOptions._tolerant; init => ScannerOptions._tolerant = value; }
2828

2929
/// <summary>
3030
/// Gets or sets the <see cref="ErrorHandler"/> to use, defaults to <see cref="ErrorHandler.Default"/>.
3131
/// </summary>
32-
public ErrorHandler ErrorHandler { get; init; } = ErrorHandler.Default;
32+
public ErrorHandler ErrorHandler { get => ScannerOptions._errorHandler; init => ScannerOptions._errorHandler = value; }
3333

3434
/// <summary>
3535
/// Gets or sets whether the Regular Expression syntax should be converted to a .NET compatible one, defaults to <see langword="true"/>.
3636
/// </summary>
37-
public bool AdaptRegexp { get; init; } = true;
37+
public bool AdaptRegexp { get => ScannerOptions._adaptRegexp; init => ScannerOptions._adaptRegexp = value; }
3838

3939
/// <summary>
4040
/// Default timeout for created regexes, defaults to 10 seconds.
4141
/// </summary>
42-
public TimeSpan RegexTimeout { get; init; } = TimeSpan.FromSeconds(10);
42+
public TimeSpan RegexTimeout { get => ScannerOptions._regexTimeout; init => ScannerOptions._regexTimeout = value; }
4343

4444
/// <summary>
4545
/// The maximum depth of assignments allowed, defaults to 200.
@@ -57,7 +57,7 @@ public record class ParserOptions : IScannerOptions
5757
/// {
5858
/// foreach (var child in node.ChildNodes)
5959
/// {
60-
/// child.AdditionalData["Parent"] = node;
60+
/// child.AdditionalData = node;
6161
/// }
6262
/// };
6363
/// </code>

src/Esprima/Scanner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static int OctalValue(char ch)
8585
return ch - '0';
8686
}
8787

88-
internal Scanner(IScannerOptions options)
88+
internal Scanner(ScannerOptions options)
8989
{
9090
if (options == null)
9191
{

src/Esprima/ScannerOptions.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
namespace Esprima;
22

3-
internal interface IScannerOptions
4-
{
5-
bool Comments { get; }
6-
bool Tolerant { get; }
7-
ErrorHandler ErrorHandler { get; }
8-
bool AdaptRegexp { get; }
9-
TimeSpan RegexTimeout { get; }
10-
}
11-
123
/// <summary>
134
/// Scanner options.
145
/// </summary>
15-
public record class ScannerOptions : IScannerOptions
6+
public record class ScannerOptions
167
{
178
public static readonly ScannerOptions Default = new();
189

10+
internal bool _comments;
11+
internal bool _tolerant = true;
12+
internal ErrorHandler _errorHandler = ErrorHandler.Default;
13+
internal bool _adaptRegexp = true;
14+
internal TimeSpan _regexTimeout = TimeSpan.FromSeconds(10);
15+
1916
/// <summary>
2017
/// Gets or sets whether the comments are collected, defaults to <see langword="false"/>.
2118
/// </summary>
22-
public bool Comments { get; init; }
19+
public bool Comments { get => _comments; init => _comments = value; }
2320

2421
/// <summary>
2522
/// Gets or sets whether the scanner is tolerant to errors, defaults to <see langword="true"/>.
2623
/// </summary>
27-
public bool Tolerant { get; init; } = true;
24+
public bool Tolerant { get => _tolerant; init => _tolerant = value; }
2825

2926
/// <summary>
3027
/// Gets or sets the <see cref="ErrorHandler"/> to use, defaults to <see cref="ErrorHandler.Default"/>.
3128
/// </summary>
32-
public ErrorHandler ErrorHandler { get; init; } = ErrorHandler.Default;
29+
public ErrorHandler ErrorHandler { get => _errorHandler; init => _errorHandler = value; }
3330

3431
/// <summary>
3532
/// Gets or sets whether the Regular Expression syntax should be converted to a .NET compatible one, defaults to <see langword="true"/>.
3633
/// </summary>
37-
public bool AdaptRegexp { get; init; } = true;
34+
public bool AdaptRegexp { get => _adaptRegexp; init => _adaptRegexp = value; }
3835

3936
/// <summary>
4037
/// Default timeout for created regexes, defaults to 10 seconds.
4138
/// </summary>
42-
public TimeSpan RegexTimeout { get; init; } = TimeSpan.FromSeconds(10);
39+
public TimeSpan RegexTimeout { get => _regexTimeout; init => _regexTimeout = value; }
4340
}

0 commit comments

Comments
 (0)