~ubuntu-branches/ubuntu/wily/parboiled/wily-proposed

« back to all changes in this revision

Viewing changes to parboiled-java/src/test/java/org/parboiled/transform/TestParser.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2014-11-10 21:10:42 UTC
  • Revision ID: package-import@ubuntu.com-20141110211042-wcmfz25icr5ituj5
Tags: upstream-1.1.6
ImportĀ upstreamĀ versionĀ 1.1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009-2011 Mathias Doenitz
 
3
 *
 
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
 
7
 *
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
package org.parboiled.transform;
 
18
 
 
19
import org.parboiled.BaseParser;
 
20
import org.parboiled.Rule;
 
21
import org.parboiled.support.Var;
 
22
import org.parboiled.annotations.*;
 
23
 
 
24
import static java.lang.Integer.parseInt;
 
25
import static org.parboiled.common.StringUtils.isEmpty;
 
26
 
 
27
@SuppressWarnings({"UnusedDeclaration"})
 
28
@BuildParseTree
 
29
class TestParser extends BaseParser<Integer> {
 
30
 
 
31
    protected int integer;
 
32
    private int privateInt;
 
33
 
 
34
    public Rule RuleWithoutAction() {
 
35
        return Sequence('a', 'b');
 
36
    }
 
37
 
 
38
    @Label("harry")
 
39
    public Rule RuleWithNamedLabel() {
 
40
        return Sequence('a', 'b');
 
41
    }
 
42
 
 
43
    @SuppressNode
 
44
    public Rule RuleWithLeaf() {
 
45
        return Sequence('a', 'b');
 
46
    }
 
47
 
 
48
    public Rule RuleWithDirectImplicitAction() {
 
49
        return Sequence('a', integer == 0, 'b', 'c');
 
50
    }
 
51
 
 
52
    public Rule RuleWithIndirectImplicitAction() {
 
53
        return Sequence('a', 'b', action() || integer == 5);
 
54
    }
 
55
 
 
56
    public Rule RuleWithDirectExplicitAction() {
 
57
        return Sequence('a', ACTION(action() && integer > 0), 'b');
 
58
    }
 
59
 
 
60
    public Rule RuleWithIndirectExplicitAction() {
 
61
        return Sequence('a', 'b', ACTION(integer < 0 && action()));
 
62
    }
 
63
 
 
64
    public Rule RuleWithIndirectImplicitParamAction(int param) {
 
65
        return Sequence('a', 'b', integer == param);
 
66
    }
 
67
 
 
68
    public Rule RuleWithComplexActionSetup(int param) {
 
69
        int i = 26, j = 18;
 
70
        Var<String> string = new Var<String>("text");
 
71
        i += param;
 
72
        j -= i;
 
73
        return Sequence('a' + i, i > param + j, string, ACTION(integer + param < string.get().length() - i - j));
 
74
    }
 
75
 
 
76
    public Rule BugIn0990() {
 
77
        Var<Integer> var = new Var<Integer>();
 
78
        return FirstOf("10", "2");
 
79
    }
 
80
 
 
81
    @DontLabel
 
82
    public Rule RuleWith2Returns(int param) {
 
83
        if (param == integer) {
 
84
            return Sequence('a', ACTION(action()));
 
85
        } else {
 
86
            return EOI;
 
87
        }
 
88
    }
 
89
 
 
90
    @DontLabel
 
91
    public Rule RuleWithSwitchAndAction(int param) {
 
92
        switch (param) {
 
93
            case 0: return Sequence(EMPTY, push(1));
 
94
        }
 
95
        return null;
 
96
    }
 
97
 
 
98
    @ExplicitActionsOnly
 
99
    public Rule RuleWithExplicitActionsOnly(int param) {
 
100
        Boolean b = integer == param;
 
101
        return Sequence('a', 'b', b);
 
102
    }
 
103
 
 
104
    @Cached
 
105
    public Rule RuleWithCachedAnd2Params(String string, long aLong) {
 
106
        return Sequence(string, aLong == integer);
 
107
    }
 
108
 
 
109
    public Rule RuleWithFakeImplicitAction(int param) {
 
110
        Boolean b = integer == param;
 
111
        return Sequence('a', 'b', b);
 
112
    }
 
113
 
 
114
    public Rule NumberRule() {
 
115
        return Sequence(
 
116
                OneOrMore(CharRange('0', '9')).suppressNode(),
 
117
                push(parseInt(isEmpty(match()) ? "0" : match()))
 
118
        );
 
119
    }
 
120
 
 
121
    // actions
 
122
 
 
123
    public boolean action() {
 
124
        return true;
 
125
    }
 
126
 
 
127
}