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

Parse from template #5

Closed
mazharenko opened this issue Dec 14, 2024 · 1 comment
Closed

Parse from template #5

mazharenko opened this issue Dec 14, 2024 · 1 comment

Comments

@mazharenko
Copy link
Owner

Consider the following input from day 14 of year 2024

p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3

A Superpower parser with some already implemented extensions might look like the following:

Span.EqualTo("p=")
	.IgnoreThen(Numerics.IntegerInt32)
	.ThenIgnore(Span.EqualTo(','))
	.Then(Numerics.IntegerInt32)
	.Select(x => V.Create(x.Item2, x.Item1))
	.Then(Span.EqualTo(" v=")
		.IgnoreThen(Numerics.IntegerInt32
			.ThenIgnore(Span.EqualTo(','))
			.Then(Numerics.IntegerInt32)
			.Select(x => V.Create(x.Item2, x.Item1))))
	.Lines()

Still a lot for a relatively simple input.

Should consider implementing a Parser as a template with injected Parsers, so that must be a Superpower unit easily combined with other parsers. This is expected to be possible with custom interpolated string handlers and might look like the following:

Template.Create($"p={Numerics.Integer32},{Numerics.Integer32} v={Numerics.Integer32},{Numerics.Integer32}")
	.Select(x => (V.Create(x.Item2, x.Item1), V.Create(x.Item4, x.Item3)))
	.Lines()

I'm not happy with the tuple stuff though, but some extensions should help with it.

@mazharenko
Copy link
Owner Author

Implemented for the following syntax:

Template.Create<int,int,int,int>($"p={Numerics.Integer32},{Numerics.Integer32} v={Numerics.Integer32},{Numerics.Integer32}") // returns TextParser<(int,int,int,int)>

or

Template.Create<(int,int,int,int)>($"p={Numerics.Integer32},{Numerics.Integer32} v={Numerics.Integer32},{Numerics.Integer32}") // returns TextParser<(int,int,int,int)>

or

Template.Create<int>($"p={Numerics.Integer32}") // returns TextParser<int>

I failed to figure out how I can make it infer the type from the formatted parsers values, basically because overloads cannot differ only by their return types. The type correspondence can be controlled by a dedicated Roslyn analyzer, though.

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