Skip to content

Commit 3d69a07

Browse files
committed
Skip mechanism added
1 parent 579d8a6 commit 3d69a07

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

FexSampleSet/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,17 @@ void QuickStart() {
9696
*/
9797

9898
var fex = new FlowExpression<FexScanner>(); // Flow Expression using FexScanner.
99+
fex.DefSkip(c => c.SkipSp()); // Define the Skip operation to skip spaces.
100+
99101
string dcode = "", acode = "", number = ""; // Will record values in here.
100102

103+
101104
// Build the flow expression with 'Axiom' tnumber:
102105
var tnumber = fex.Seq(s => s
103106
.Ch('(').OnFail("( expected")
104107
.Rep(3, -1, r => r.Digit(v => dcode += v)).OnFail("3+ digit dialing code expected")
105108
.Ch(')').OnFail(") expected")
106-
.Sp()
109+
.Skip() // Perform the previously defined skip operation
107110
.Rep(3, r => r.Digit(v => acode += v)).OnFail("3 digit area code expected")
108111
.AnyCh("- ").OnFail("- or space expected")
109112
.Rep(4, r => r.Digit(v => number += v)).OnFail("4 digit number expected")

Psw.FlowExpressions/FexBuildState.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class FexBuildState<T>
1717
public FexElement<T>? HostFex;
1818
public FexElement<T>? LastFex;
1919
public FexOpr<T>? LastOpr;
20+
public Action<T>? SkipOp;
2021
public FexPreOp<T> PreOp = new FexPreOp<T>(null); // Will be added to every Op - use for e.g to skip spaces
2122
public Dictionary<string, FexRef<T>> RefSet = new Dictionary<string, FexRef<T>>();
2223

Psw.FlowExpressions/FexBuilder.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class FexPreOp<T>
2323

2424
public FexPreOp(Action<T> preOp) => _preOp = preOp;
2525

26-
public void SetAction(Action<T> preOp) => _preOp = preOp;
26+
public void SetAction(Action<T>? preOp) => _preOp = preOp;
2727

2828
public void Run(T ctx) {
2929
if (!_hasRun) _preOp?.Invoke(ctx);
@@ -115,7 +115,7 @@ public FexBuilder<T> Assert(Func<T, bool> assert, Action<T> failAction = null) {
115115
/// <summary>
116116
/// Perform an Action on the Context-T (does no affect the validity of a sequence).
117117
/// </summary>
118-
public FexBuilder<T> Act(Action<T> action) => AddFex(new FexAct<T>(action));
118+
public FexBuilder<T> Act(Action<T>? action) => AddFex(new FexAct<T>(action));
119119

120120
/// <summary>
121121
/// Perform a repeated Action on the Context-T (does no affect the validity of a sequence):<br/>
@@ -246,17 +246,34 @@ public FexBuilder<T> ActValue<V>(Action<V> valueAction) {
246246
return this;
247247
}
248248

249+
/// <summary>
250+
/// Perform the previously defined (via FlowExpression.DefSkip(...)) skip operation:<br/>
251+
/// - Typically used to skip spaces etc.<br/>
252+
/// - Runs if defined and does no affect the validity of a sequence.
253+
/// </summary>
254+
/// <returns></returns>
255+
public FexBuilder<T> Skip() => Act(_buildState.SkipOp);
256+
257+
249258
/// <summary>
250259
/// Binds a pre-operator to all subsequent operators:<br/>
251260
/// - A pre-operator executes before an operator as an action on the context:<br/>
252261
/// - Typically used to skip spaces etc. before tokens when parsing.<br/>
253262
/// - Pre-operators are efficient an only execute once while trying several lookahead operations.
254263
/// </summary>
255-
public FexBuilder<T> GlobalPreOp(Action<T> preOp) {
264+
public FexBuilder<T> GlobalPreOp(Action<T>? preOp) {
256265
_buildState.PreOp.SetAction(preOp);
257266
return this;
258267
}
259268

269+
/// <summary>
270+
/// Binds the previously defined (via FlowExpression.DefSkip(...)) skip operation as pre-operator to all subsequent operators:<br/>
271+
/// - A pre-operator executes before an operator as an action on the context:<br/>
272+
/// - Typically used to skip spaces etc. before tokens when parsing.<br/>
273+
/// - Pre-operators are efficient an only execute once while trying several lookahead operations.
274+
/// </summary>
275+
public FexBuilder<T> SkipGlobalPreOp() => GlobalPreOp(_buildState.SkipOp);
276+
260277

261278
/// <summary>
262279
/// Bind a pre-operator to the preceding operator:<br/>

Psw.FlowExpressions/FexElements.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public override FexCheckResult CheckRun(T ctx) {
6161
/// </summary>
6262
public class FexAct<T> : FexElement<T>
6363
{
64-
private readonly Action<T> _action;
64+
private readonly Action<T>? _action;
6565
private readonly FexCheckResult _checkRunReturn;
6666

67-
public FexAct(Action<T> action, FexCheckResult checkRunReturn = FexCheckResult.FailFirst) {
67+
public FexAct(Action<T>? action, FexCheckResult checkRunReturn = FexCheckResult.FailFirst) {
6868
_action = action;
6969
_checkRunReturn = checkRunReturn;
7070
IsOpt = true;
@@ -174,6 +174,7 @@ private bool _Run(T ctx, bool checkMode) {
174174
public override FexCheckResult CheckRun(T ctx) => _Run(ctx, true) ? FexCheckResult.Passed : FexCheckResult.FailFirst;
175175
}
176176

177+
177178
/// <summary>
178179
/// Construct, Run and Manage a Sequence of steps (FexElements)
179180
/// </summary>

Psw.FlowExpressions/FlowExpression.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public FlowExpression(IFexTracer tracer = null)
3535
protected FexElement<T> _Prod(FexElement<T> prod, Action<FexBuilder<T>> build)
3636
=> _fexBuildState.Production(_fexBuilder, prod, build);
3737

38+
/// <summary>
39+
/// Define the Skip action to perform for the Skip element:<br/>
40+
/// - Typically used to skip spaces etc.
41+
/// </summary>
42+
public FlowExpression<T> DefSkip(Action<T> skipAction) {
43+
_fexBuildState.SkipOp = skipAction;
44+
return this;
45+
}
46+
3847
/// <summary>
3948
/// Define a Sequence (of steps) that must complete in full to pass:<br/>
4049
/// - A step is any FexElement.

0 commit comments

Comments
 (0)