Quantcast

Ignore all whitespaces in Parboiled java

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

Ignore all whitespaces in Parboiled java

Marzie
Dear all,

is there any way to ignore all white-spaces in our expressions when we parse them. In other way, for instance, I want the parser take no difference between "5+4"    and    "    5  +   4"  ?

Thanks in advance,
Marzie
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Ignore all whitespaces in Parboiled java

tsuckow

Can you simply strip the spaces before parsing?


On Thu, Jul 14, 2016, 07:41 Marzie [via parboiled users] <[hidden email]> wrote:
Dear all,

is there any way to ignore all white-spaces in our expressions when we parse them. In other way, for instance, I want the parser take no difference between "5+4"    and    "    5  +   4"  ?

Thanks in advance,
Marzie


If you reply to this email, your message will be added to the discussion below:
http://users.parboiled.org/Ignore-all-whitespaces-in-Parboiled-java-tp4024430.html
To start a new topic under parboiled users, email [hidden email]
To unsubscribe from parboiled users, click here.
NAML
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Ignore all whitespaces in Parboiled java

Marzie
Thanks for your answer.
Yes it is possible, but I thought with some rules in Parboiled we can do it better.

Regards,
Marzie
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Ignore all whitespaces in Parboiled java

mathias
Administrator
Marzie,

are you looking for something like this:
https://github.com/sirthias/parboiled/blob/master/examples-java/src/main/java/org/parboiled/examples/calculators/CalculatorParser3.java#L121
?

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

> On 15.7.2016, at 08:27, Marzie [via parboiled users] <[hidden email]> wrote:
>
> Thanks for your answer.
> Yes it is possible, but I thought with some rules in Parboiled we can do it better.
>
> Regards,
> Marzie
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Ignore-all-whitespaces-in-Parboiled-java-tp4024430p4024432.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Ignore all whitespaces in Parboiled java

Marzie
Hi Mathias,

I have already read this. But what I understood is, it works only for the spaces at the end of string. I tried to change it to work for all white-spaces anywhere but it did not work. I removed all white-spaces first and then parse the string.

I have a column in our software and it should calculate the input data and return a value. This column shows how many hours we need for doing a task in a user story. The format for the input data can be a string like:

"2" -> return 2
"2h"-> return 2 because h stands for hours
"2h+3w"-> return 2+(3*40)=122 because w stands for week and the normal job hours for a week is 40 hours
"2w+3d+5"-> return (2*40)+(3*8)+5=109
"+2"-> return the previous value plus 2
"-2"-> return the previous value minus 2

I do appreciate if you help me to write a parser for this. What I have written is the following but it doe not work correctly:

@BuildParseTree
public abstract class ParboildeParser2 extends BaseParser<CalcNode> {
        public abstract Rule InputLine();

        Rule Expression() {
                return this.Sequence(
                                this.Optional(this.OperatorRule(this.toRule(""), this.AnyOf("+-"))),
                                this.Term(),
                                this.ZeroOrMore(
                                                this.FirstOf(
                                                                this.OperatorRule(this.Term(), this.Ch('+')),
                                                                this.OperatorRule(this.Term(), this.Ch('-'))
                                                                )),
                                BaseParser.EOI);
        }

        Rule Term() {
                final Var<Character> op = new Var<Character>();
                return this.Sequence(this.Number(), op.set(this.matchedChar()),
                                this.Optional(this.OperatorRule(this.toRule(this.pop()), this.AnyOf("ydwh"))));
        }

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

        public Rule OperatorRule(final Rule subRule, final Rule operatorRule) {
                final Var<Character> op = new Var<Character>();
                return this.Sequence(
                                subRule,
                                this.ZeroOrMore(
                                                operatorRule, op.set(this.matchedChar()),
                                                subRule,
                                                this.push(new CalcNode(op.get(), this.pop(1), this.pop()))));
        }
}


public class HoursParser {
        public void parse(final String input) {
                final ParboiledParser parser = Parboiled.createParser(ParboiledParser.class);
                final ParsingResult result = new ReportingParseRunner(parser.Expression()).run(input);
        }

        public static class CalcNode extends ImmutableBinaryTreeNode<CalcNode> {
                private double value;
                private Character operator;

                public CalcNode(final double value) {
                        super(null, null);
                        this.value = value;
                }

                public CalcNode(final Character operator, final CalcNode left, final CalcNode right) {
                        super(left, right);
                        this.operator = operator;
                }

                public double getValue() {
                        if (this.operator == null) {
                                return this.value;
                        }
                        switch (this.operator) {
                        case '+':
                                return this.left().getValue() + this.right().getValue();
                        case '-':
                                return this.left().getValue() - this.right().getValue();
                        case 'h':
                                return 1;
                        case 'd':
                                return 8;
                        case 'w':
                                return 40;
                        default:
                                throw new IllegalStateException();
                        }
                }
        }
}
Loading...