We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Hello! Great library, I really like it.
But I am a little bit confused about how to create parser for syntax like this:
I need it to be converted into syntax tree like this:
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
to capture "dn", it obviously won't work as it will capture everything.
I solved it by creating:
and did something like this:
But it feels a little bit too complex. I wonder if there is a better way to do it.
The text was updated successfully, but these errors were encountered: