Code Monkey home page Code Monkey logo

interpolatedparser's Introduction

InterpolatedParser

DO NOT USE THIS LIBRARY IN PRODUCTION
The way InterpolatedParser works is by using unsafe code that is very volatile. Do not use this outside small short lived executions to test things or parse simple strings. It's meant more of a weird language showcase than an actual product.

InterpolatedParser is a nuget library enabling string interpolation, but in reverse.

Example code:

using InterpolatedParsing;

int x = 0;

string input = "x is 69";

InterpolatedParser.Parse($"x is {x}", input);

Console.WriteLine(x); // Prints 69.

Usage

Limitations

Make sure any variable you parse into is a variable living on the stack. The reason why is explained below.

Supported types

InterpolatedParser supports anything that implements IParseable, which includes many common types in .NET. This also means you can use your own types by having them implement IParseable.

Collections

The parser supports Lists and Arrays. A separator is provided as a format string. Format strings don't allow trailing whitespace, so if you need that enclose the format string in single quotes.

using InterpolatedParsing;

List<int> numbers = null!;

InterpolatedParser.Parse(
	$"Winning numbers are: {x:,}",
	"Winning numbers are: 5,10,15,25);

List<string> beans = null!;
InterpolatedParser.Parse(
	$"Bean list: {x:', '}", // Add single quotes to support whitespace
	"Bean list: "black", "coffee", "green");

This is cursed, how does it do that?

C# 10 added support for writing custom interpolated string handlers. At compile time they translate interpolated strings into a series of calls to magic methods: AppendLiteral for literal strings, and AppendFormatted to the parameters of the string.

var str = $"Hello {123}!";

// Becomes this code on compile time:

var handler = new DefaultInterpolatedStringHandler(7, 4);
handler.AppendLiteral("Hello ");
handler.AppendFormatted(123);
handler.AppendLiteral("!");
var str = handler.ToStringAndClear();

That's all good and normally doesn't enable the shenanigans we need. However we can abuse the in parameter modifier. The in parameter can be implicit, so the generated calls to AppendFormatted allow it. This means we're now passing down a read only reference to the value when we call AppendFormatted. This is where things become really cursed, using unsafe code it's casted down to a void* so we can keep a reference to it.

    public readonly void AppendFormatted(in int value) {
        void* pointer = Unsafe.AsPointer(ref Unsafe.AsRef(in value));

As you might imagine this is very unsafe and volatile. Because the reference is needed between method calls, the pointer can also not be pinned so it can be moved around by garbage collection. That's why it's very important to only use this for stack variables.

interpolatedparser's People

Contributors

antonbergaker avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.