2
* Copyright (C) 2009-2011 Mathias Doenitz
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package org.parboiled.examples.calculators;
19
import org.parboiled.Parboiled;
20
import org.parboiled.common.StringBuilderSink;
21
import org.parboiled.support.ParsingResult;
22
import org.testng.annotations.Test;
23
import org.parboiled.parserunners.TracingParseRunner;
25
import static org.parboiled.common.Predicates.*;
26
import static org.parboiled.errors.ErrorUtils.printParseErrors;
27
import static org.parboiled.support.Filters.rules;
28
import static org.parboiled.support.Filters.rulesBelow;
29
import static org.testng.Assert.assertEquals;
31
public class TracingParseRunnerTest {
34
public void testTracingParseRunner() {
35
CalculatorParser1 parser = Parboiled.createParser(CalculatorParser1.class);
37
StringBuilderSink log = new StringBuilderSink();
38
TracingParseRunner<Integer> runner = new TracingParseRunner<Integer>(parser.InputLine())
39
.withFilter(and(rules(parser.Number(), parser.Parens()), not(rulesBelow(parser.Digits()))))
41
ParsingResult<Integer> result = runner.run("2*(4+5");
43
assertEquals(printParseErrors(result), "" +
44
"Unexpected end of input, expected Digit, '*', '/', '+', '-' or ')' (line 1, pos 7):\n" +
48
assertEquals(log.toString(), "Starting new parsing run\n" +
49
"InputLine/Expression/Term/Factor/Number/Digits, matched, cursor at 1:2 after \"2\"\n" +
50
"..(4)../Number/Number_Action1, matched, cursor at 1:2 after \"2\"\n" +
51
"..(4)../Number, matched, cursor at 1:2 after \"2\"\n" +
52
"..(2)../Term/ZeroOrMore/FirstOf/Sequence/Factor/Number/Digits, failed, cursor at 1:3 after \"2*\"\n" +
53
"..(7)../Number, failed, cursor at 1:3 after \"2*\"\n" +
54
"..(6)../Factor/Parens/'(', matched, cursor at 1:4 after \"2*(\"\n" +
55
"..(7)../Parens/Expression/Term/Factor/Number/Digits, matched, cursor at 1:5 after \"2*(4\"\n" +
56
"..(11)../Number/Number_Action1, matched, cursor at 1:5 after \"2*(4\"\n" +
57
"..(11)../Number, matched, cursor at 1:5 after \"2*(4\"\n" +
58
"..(10)../Factor, matched, cursor at 1:5 after \"2*(4\"\n" +
59
"..(9)../Term/ZeroOrMore/FirstOf/Sequence/'*', failed, cursor at 1:5 after \"2*(4\"\n" +
60
"..(12)../Sequence, failed, cursor at 1:5 after \"2*(4\"\n" +
61
"..(11)../FirstOf/Sequence/'/', failed, cursor at 1:5 after \"2*(4\"\n" +
62
"..(12)../Sequence, failed, cursor at 1:5 after \"2*(4\"\n" +
63
"..(11)../FirstOf, failed, cursor at 1:5 after \"2*(4\"\n" +
64
"..(10)../ZeroOrMore, matched, cursor at 1:5 after \"2*(4\"\n" +
65
"..(9)../Term, matched, cursor at 1:5 after \"2*(4\"\n" +
66
"..(8)../Expression/ZeroOrMore/FirstOf/Sequence/'+', matched, cursor at 1:6 after \"2*(4+\"\n" +
67
"..(11)../Sequence/Term/Factor/Number/Digits, matched, cursor at 1:7 after \"2*(4+5\"\n" +
68
"..(14)../Number/Number_Action1, matched, cursor at 1:7 after \"2*(4+5\"\n" +
69
"..(14)../Number, matched, cursor at 1:7 after \"2*(4+5\"\n" +
70
"..(13)../Factor, matched, cursor at 1:7 after \"2*(4+5\"\n" +
71
"..(12)../Term/ZeroOrMore/FirstOf/Sequence/'*', failed, cursor at 1:7 after \"2*(4+5\"\n" +
72
"..(15)../Sequence, failed, cursor at 1:7 after \"2*(4+5\"\n" +
73
"..(14)../FirstOf/Sequence/'/', failed, cursor at 1:7 after \"2*(4+5\"\n" +
74
"..(15)../Sequence, failed, cursor at 1:7 after \"2*(4+5\"\n" +
75
"..(14)../FirstOf, failed, cursor at 1:7 after \"2*(4+5\"\n" +
76
"..(13)../ZeroOrMore, matched, cursor at 1:7 after \"2*(4+5\"\n" +
77
"..(12)../Term, matched, cursor at 1:7 after \"2*(4+5\"\n" +
78
"..(11)../Sequence/Expression_Action1, matched, cursor at 1:7 after \"2*(4+5\"\n" +
79
"..(11)../Sequence, matched, cursor at 1:7 after \"2*(4+5\"\n" +
80
"..(10)../FirstOf, matched, cursor at 1:7 after \"2*(4+5\"\n" +
81
"..(9)../ZeroOrMore/FirstOf/Sequence/'+', failed, cursor at 1:7 after \"2*(4+5\"\n" +
82
"..(11)../Sequence, failed, cursor at 1:7 after \"2*(4+5\"\n" +
83
"..(10)../FirstOf/Sequence/'-', failed, cursor at 1:7 after \"2*(4+5\"\n" +
84
"..(11)../Sequence, failed, cursor at 1:7 after \"2*(4+5\"\n" +
85
"..(10)../FirstOf, failed, cursor at 1:7 after \"2*(4+5\"\n" +
86
"..(9)../ZeroOrMore, matched, cursor at 1:7 after \"2*(4+5\"\n" +
87
"..(8)../Expression, matched, cursor at 1:7 after \"2*(4+5\"\n" +
88
"..(7)../Parens/')', failed, cursor at 1:7 after \"2*(4+5\"\n" +
89
"..(7)../Parens, failed, cursor at 1:7 after \"2*(4+5\"\n");
b'\\ No newline at end of file'