Skip to content

Commit 063f178

Browse files
authored
Code cleanup (#304)
Code cleanup and some fixes
1 parent a026ada commit 063f178

30 files changed

Lines changed: 109 additions & 153 deletions

src/Parlot/Compilation/CompilationContext.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ public class CompilationContext
1212
{
1313
private int _number;
1414

15-
public CompilationContext()
16-
{
17-
}
18-
1915
/// <summary>
20-
/// Gets the expression containing the the <see cref="ParseContext"/> instance for the parser.
16+
/// Gets the expression containing the <see cref="ParseContext"/> instance for the parser.
2117
/// </summary>
2218
public ParameterExpression ParseContext { get; } = Expression.Parameter(typeof(ParseContext));
2319

@@ -27,12 +23,12 @@ public CompilationContext()
2723
public int NextNumber => _number++;
2824

2925
/// <summary>
30-
/// Gets the list of global variables to add the the final list of statements.
26+
/// Gets the list of global variables to add the final list of statements.
3127
/// </summary>
3228
public List<ParameterExpression> GlobalVariables { get; } = new();
3329

3430
/// <summary>
35-
/// Gets the list of global expressions to add the the final list of statements.
31+
/// Gets the list of global expressions to add the final list of statements.
3632
/// </summary>
3733
public List<Expression> GlobalExpressions { get; } = new();
3834

src/Parlot/Compilation/CompilationResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq.Expressions;
44

@@ -9,7 +9,7 @@ namespace Parlot.Compilation;
99
/// to parse the expected input.
1010
/// The convention is that these statements are returned in the <see cref="Body"/> property, and any variable that needs to be declared in the block
1111
/// that the <see cref="Body"/> is used in are set in the <see cref="Variables"/> list.
12-
/// The <see cref="Success"/> property represents the variable that contains the success of the statements once executed, and if <code>true</code> then
12+
/// The <see cref="Success"/> property represents the variable that contains the success of the statements once executed, and if <code>true</code> then
1313
/// the <see cref="Value"/> property contains the result.
1414
/// </summary>
1515
public class CompilationResult

src/Parlot/Compilation/CompiledParser.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ namespace Parlot.Compilation;
66
/// <summary>
77
/// Marker interface to detect a Parser has already been compiled.
88
/// </summary>
9-
public interface ICompiledParser
10-
{
11-
12-
}
9+
public interface ICompiledParser;
1310

1411
/// <summary>
1512
/// An instance of this class encapsulates the result of a compiled parser
16-
/// in order to expose is as as standard parser contract.
13+
/// in order to expose is as standard parser contract.
1714
/// </summary>
1815
/// <remarks>
1916
/// This class is used in <see cref="Parser{T}.Compile"/>.

src/Parlot/Cursor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void Advance(int count)
110110
{
111111
Current = NullChar;
112112
Offset = _textLength;
113-
_column += 1;
113+
_column++;
114114
}
115115
}
116116

src/Parlot/Fluent/Deferred.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Parlot.Fluent;
1212

1313
public sealed class Deferred<T> : Parser<T>, ICompilable, ISeekable
1414
{
15+
16+
private readonly object _lockObject = new();
1517
private Parser<T>? _parser;
1618

1719
public Parser<T>? Parser
@@ -175,7 +177,7 @@ public override string ToString()
175177
{
176178
// Handle recursion
177179

178-
lock (this)
180+
lock (_lockObject)
179181
{
180182
if (!_toString)
181183
{

src/Parlot/Fluent/Identifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override bool Parse(ParseContext context, ref ParseResult<TextSpan> resul
2727

2828
var first = context.Scanner.Cursor.Current;
2929

30-
if (Character.IsIdentifierStart(first) || _extraStart != null && _extraStart(first))
30+
if (Character.IsIdentifierStart(first) || (_extraStart != null && _extraStart(first)))
3131
{
3232
var start = context.Scanner.Cursor.Offset;
3333

src/Parlot/Fluent/LeftAssociative.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ public CompilationResult Compile(CompilationContext context)
108108
var (op, factory) = _operators[i];
109109
var opCompileResult = op.Build(context);
110110

111-
foreach (var variable in opCompileResult.Variables)
112-
{
113-
allOperatorVariables.Add(variable);
114-
}
111+
allOperatorVariables.AddRange(opCompileResult.Variables);
115112

116113
var factoryConst = Expression.Constant(factory);
117114

@@ -291,10 +288,7 @@ public CompilationResult Compile(CompilationContext context)
291288
var (op, factory) = _operators[i];
292289
var opCompileResult = op.Build(context);
293290

294-
foreach (var variable in opCompileResult.Variables)
295-
{
296-
allOperatorVariables.Add(variable);
297-
}
291+
allOperatorVariables.AddRange(opCompileResult.Variables);
298292

299293
var factoryConst = Expression.Constant(factory);
300294

src/Parlot/Fluent/NumberLiteralBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public abstract class NumberLiteralBase<T> : Parser<T>, ICompilable, ISeekable
3434

3535
public abstract bool TryParseNumber(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out T value);
3636

37-
public NumberLiteralBase(NumberOptions numberOptions = NumberOptions.Number, char decimalSeparator = NumberLiterals.DefaultDecimalSeparator, char groupSeparator = NumberLiterals.DefaultGroupSeparator, MethodInfo? tryParseMethodInfo = null)
37+
protected NumberLiteralBase(NumberOptions numberOptions = NumberOptions.Number, char decimalSeparator = NumberLiterals.DefaultDecimalSeparator, char groupSeparator = NumberLiterals.DefaultGroupSeparator, MethodInfo? tryParseMethodInfo = null)
3838
{
3939
_decimalSeparator = decimalSeparator;
4040
_groupSeparator = groupSeparator;

src/Parlot/Fluent/NumberOptions.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace Parlot.Fluent;
44

@@ -8,7 +8,7 @@ public enum NumberOptions
88
/// <summary>
99
/// Indicates that no style elements, such as leading sign, thousands
1010
/// separators, decimal separator or exponent, can be present in the parsed string.
11-
/// The string to be parsed must consist of integral decimal digits only.
11+
/// The string to be parsed must consist of integral decimal digits only.
1212
/// </summary>
1313
None = 0,
1414

@@ -18,6 +18,12 @@ public enum NumberOptions
1818
/// </summary>
1919
AllowLeadingSign = 1,
2020

21+
/// <summary>
22+
/// Indicates that the <see cref="AllowLeadingSign"/>
23+
/// style is used. This is a composite number style.
24+
/// </summary>
25+
Integer = AllowLeadingSign,
26+
2127
/// <summary>
2228
/// Indicates that the numeric string can have a decimal separator. By default it uses dot (.) as the separator.
2329
/// </summary>
@@ -29,6 +35,12 @@ public enum NumberOptions
2935
/// </summary>
3036
AllowGroupSeparators = 4,
3137

38+
/// <summary>
39+
/// Indicates that the <see cref="AllowLeadingSign"/>, <see cref="AllowDecimalSeparator"/>, <see cref="AllowGroupSeparators"/>
40+
/// styles are used. This is a composite number style.
41+
/// </summary>
42+
Number = AllowLeadingSign | AllowDecimalSeparator | AllowGroupSeparators,
43+
3244
/// <summary>
3345
/// Indicates that the numeric string can be in exponential notation. It
3446
/// allows the parsed string to contain an exponent that begins with the "E"
@@ -37,18 +49,6 @@ public enum NumberOptions
3749
/// </summary>
3850
AllowExponent = 8,
3951

40-
/// <summary>
41-
/// Indicates that the <see cref="AllowLeadingSign"/>
42-
/// style is used. This is a composite number style.
43-
/// </summary>
44-
Integer = AllowLeadingSign,
45-
46-
/// <summary>
47-
/// Indicates that the <see cref="AllowLeadingSign"/>, <see cref="AllowDecimalSeparator"/>, <see cref="AllowGroupSeparators"/>
48-
/// styles are used. This is a composite number style.
49-
/// </summary>
50-
Number = AllowLeadingSign | AllowDecimalSeparator | AllowGroupSeparators,
51-
5252
/// <summary>
5353
/// Indicates that the <see cref="AllowLeadingSign"/>, <see cref="AllowDecimalSeparator"/>, <see cref="AllowExponent"/>
5454
/// styles are used. This is a composite number style.

src/Parlot/Fluent/OneOf.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static void AddUniqueSingle(List<Parser<T>> target, Parser<T> item)
151151
lookupTable?.Remove(OtherSeekableChar);
152152
var expectedChars = string.Join(",", lookupTable?.Keys.ToArray() ?? []);
153153

154-
if (lookupTable != null && lookupTable.Count > 0)
154+
if (lookupTable?.Count > 0)
155155
{
156156
_map = new CharMap<List<Parser<T>>>(lookupTable);
157157

0 commit comments

Comments
 (0)