Quantcast

result.parseTreeRoot.getValue().toString() returns always 0

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

result.parseTreeRoot.getValue().toString() returns always 0

Marzie
Dear all,

I am totally new in Parboiled and I wrote a parser but it returns always 0. I cannot understand the reason. Can anybody please help me?


@BuildParseTree
public abstract class ParboiledParser3 extends BaseParser<Integer> {
        public abstract Rule InputLine();

        Rule Expression() {
                final Var<Character> op = new Var<Character>();
                return this.Sequence(
                                this.Optional(this.AnyOf("+-"), this.push(this.pop())),
                                this.Term(),
                                this.ZeroOrMore(this.Sequence(this.AnyOf("+-"), this.Term(), op.set(this.matchedChar()), this.push(this.operationRule(op.get(), this.pop(), this.pop())))),
                                BaseParser.EOI);
        }

        Rule Term() {
                final Var<Character> op = new Var<Character>();
                return this.Sequence(this.Number(),
                                this.Optional(this.AnyOf("hdyw"), op.set(this.matchedChar()), this.push(this.characterRule(op.get()) * this.pop())));
        }

        Rule Number() {
                return this.Sequence(this.CharRange('1', '9'),
                                this.ZeroOrMore(this.CharRange('0', '9')),
                                this.push(Integer.parseInt(this.matchOrDefault("0"))));
        }

        public int characterRule(final Character ch) {
                int result = 0;
                switch (ch) {
                case 'h':
                        result = 1;
                case 'w':
                        result = 40;
                case 'd':
                        result = 8;
                default:
                }
                return result;
        }

        public int operationRule(final Character ch, final Integer rightValue, final Integer leftValue) {
                int result = 0;
                switch (ch) {
                case '+':
                        result = rightValue + leftValue;
                case '-':
                        result = leftValue - rightValue;
                default:
                }
                return result;
        }
}
Loading...