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.transform;
19
import org.parboiled.BaseParser;
20
import org.parboiled.Rule;
21
import org.parboiled.support.Var;
22
import org.parboiled.annotations.*;
24
import static java.lang.Integer.parseInt;
25
import static org.parboiled.common.StringUtils.isEmpty;
27
@SuppressWarnings({"UnusedDeclaration"})
29
class TestParser extends BaseParser<Integer> {
31
protected int integer;
32
private int privateInt;
34
public Rule RuleWithoutAction() {
35
return Sequence('a', 'b');
39
public Rule RuleWithNamedLabel() {
40
return Sequence('a', 'b');
44
public Rule RuleWithLeaf() {
45
return Sequence('a', 'b');
48
public Rule RuleWithDirectImplicitAction() {
49
return Sequence('a', integer == 0, 'b', 'c');
52
public Rule RuleWithIndirectImplicitAction() {
53
return Sequence('a', 'b', action() || integer == 5);
56
public Rule RuleWithDirectExplicitAction() {
57
return Sequence('a', ACTION(action() && integer > 0), 'b');
60
public Rule RuleWithIndirectExplicitAction() {
61
return Sequence('a', 'b', ACTION(integer < 0 && action()));
64
public Rule RuleWithIndirectImplicitParamAction(int param) {
65
return Sequence('a', 'b', integer == param);
68
public Rule RuleWithComplexActionSetup(int param) {
70
Var<String> string = new Var<String>("text");
73
return Sequence('a' + i, i > param + j, string, ACTION(integer + param < string.get().length() - i - j));
76
public Rule BugIn0990() {
77
Var<Integer> var = new Var<Integer>();
78
return FirstOf("10", "2");
82
public Rule RuleWith2Returns(int param) {
83
if (param == integer) {
84
return Sequence('a', ACTION(action()));
91
public Rule RuleWithSwitchAndAction(int param) {
93
case 0: return Sequence(EMPTY, push(1));
99
public Rule RuleWithExplicitActionsOnly(int param) {
100
Boolean b = integer == param;
101
return Sequence('a', 'b', b);
105
public Rule RuleWithCachedAnd2Params(String string, long aLong) {
106
return Sequence(string, aLong == integer);
109
public Rule RuleWithFakeImplicitAction(int param) {
110
Boolean b = integer == param;
111
return Sequence('a', 'b', b);
114
public Rule NumberRule() {
116
OneOrMore(CharRange('0', '9')).suppressNode(),
117
push(parseInt(isEmpty(match()) ? "0" : match()))
123
public boolean action() {