public ParseRuleBranch <TToken> Rule(Action <ParseContext <TToken> > factory) { if (Asserts.Any(a => a.Error != null)) { return(new ParseRuleBranch <TToken>(this)); } var currentOffset = Asserts.Aggregate(0, (agg, a) => agg + a.Length); var assert = new AssertResultBranch <TToken> { Index = Asserts.Count, BranchName = BranchName }; Asserts.Add(assert); var body = factory.Method.ToString(); body = body.Substring(5, body.IndexOf('(') - 5); _stack111.Push(body); Console.WriteLine(_stack111.Aggregate(string.Empty, (agg, b) => b + " -> " + agg)); try { var localContext = Context.Branch(currentOffset); factory(localContext); var branch = localContext.SuccessBranch ?? localContext.FailedBranch; if (branch != null) { assert.Branch = branch; } else { assert.Error = new ParserException("There is no sequence checks", BranchName); } } catch (Exception e) { assert.Error = new ParserException(null, BranchName, e); } _stack111.Pop(); Console.WriteLine("<-" + (assert.Error == null ? "Success" : "Error")); //Console.WriteLine(_stack111.Aggregate(string.Empty, (agg, b) => b + " -> " + agg)); return(new ParseRuleBranch <TToken>(this, assert.Branch.Environment, assert.Error != null)); }
public ParseTokenBranch <TToken> Token(string token) { if (Asserts.Any(a => a.Error != null)) { return(new ParseTokenBranch <TToken>(this)); } var currentOffset = Asserts.Aggregate(0, (agg, a) => agg + a.Length); var aheadToken = Context.GetToken(currentOffset); var assert = new AssertResultToken <TToken> { Index = Asserts.Count, Token = aheadToken, BranchName = BranchName }; Asserts.Add(assert); try { if (aheadToken == null) { assert.Error = new ParserException("Unexpected end of sequence.", BranchName); } else { var expectedToken = Context.TokenTable[token]; if (expectedToken?.Equals(aheadToken) != true) { assert.Error = new ParserException($"Unexpected token '{aheadToken}'. " + $"'{expectedToken?.ToString() ?? "unknown"}' token expected.", BranchName); } } } catch (Exception e) { assert.Error = new ParserException(null, BranchName, e); } return(new ParseTokenBranch <TToken>(this, aheadToken, assert.Error != null)); }
public void Commit(Action <ParseEnvironment> action) { if (Asserts.Any(a => a.Error != null)) { return; } var assert = new AssertResultEmpty { Index = Asserts.Count }; Asserts.Add(assert); try { action?.Invoke(Environment); } catch (Exception e) { assert.Error = new ParserException(null, BranchName, e); } }