Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help with certain syntax #6

Open
xunto opened this issue Feb 26, 2020 · 0 comments
Open

Need help with certain syntax #6

xunto opened this issue Feb 26, 2020 · 0 comments

Comments

@xunto
Copy link

xunto commented Feb 26, 2020

Hello! Great library, I really like it.

But I am a little bit confused about how to create parser for syntax like this:

Strength 5 and Strength 2 5 and Laying on hands 3 and Dexterity < 5 or random text

I need it to be converted into syntax tree like this:

{
  "and": {
    "Strength": "5",
    "and": {
      "Strength 2": "5",
      "and": {
        "Laying on hands": "3",
        "or": {
          "Dexterity": "< 5",
          "random text"
        }
      }
    }
  }
}

The problem if that numbers and spaces can be in (as I call it) "dn" part too ( "Strength", "Strength 2", etc).

But if I do

final dn = ~/.*/.regex();

to capture "dn", it obviously won't work as it will capture everything.

I solved it by creating:

class ParserHelper extends Parser {
    private static function find(parser:ParseObject<Dynamic>, stream:String, i:Int):SearchResult {
        var furthest:Int = i;

        for (v in i...stream.length) {
            var endResult:ParseResult<Dynamic> = parser.apply(stream, v);
            if (endResult.status) return {status: true, index: v, furthest: null};
        }

        return {status: false, index: null, furthest: furthest};
    }

    public static function takeWhileNo<A>(parser:ParseObject<A>):ParseObject<String> {
        return function(stream:String, i:Int = 0) {
            var end = ParserHelper.find(parser, stream, i);
            if (end.status) return ParseUtil.makeSuccess(end.index, stream.substring(i, end.index));

            return ParseUtil.makeFailure(end.furthest, Std.string(parser));
        };
    }
}

and did something like this:

    public static var traitValue:ParseObject<NodeDots> = DotsLevelsParser.dots.as("trait value");
    public static var traitDn:ParseObject<NodeTrait> = ParserHelper.takeWhileNo(
        Parser.whitespace().then(traitValue))
    .map(function(v) return new NodeTrait(v)).as("trait dn");

    private static var array1:Array<ParseObject<Dynamic>> = [traitDn.skip(Parser.whitespace()), traitValue];
    public static var traitStatement:ParseObject<NodeTraitRequirement> = array1.seq().map(function(values) {
        var dn:NodeTrait = cast(values[0]);
        var dots:NodeDots = cast(values[1]);
        return new NodeTraitRequirement(dn, dots);
    });

But it feels a little bit too complex. I wonder if there is a better way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant