Skip to content

Instantly share code, notes, and snippets.

@zett42
Last active May 28, 2022 21:22
Show Gist options
  • Save zett42/6dc8867cec5c2310ccf9aeb87f2ddf16 to your computer and use it in GitHub Desktop.
Save zett42/6dc8867cec5c2310ccf9aeb87f2ddf16 to your computer and use it in GitHub Desktop.
Example for a custom DSL parser in C# using ReadOnlySpan<char>
/* Copyright 2022 zett42
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*
This is a demo for a simple custom configuration file parser using ReadOnlySpan<char>.
The file format supports only dictionaries, no arrays.
The values cannot contain '<'.
Example input:
var input = @"
test-server {
store {
servers {
* {
value<>
port<>
folder<C:\windows> monitor<yes>
args<-T -H>
xrg<store>
wysargs<-t -g -b>
accept_any<yes>
pdu_length<23622>
}
test1 {
name<test1>
port<123>
root<c:\test>
monitor<yes>
}
}
nullvalue1 nullvalue2
timeout<30>
nullvalue3
}
}
nullvalue4";
*/
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
namespace zett42 {
public class ServerDataParser {
public static OrderedDictionary Parse( ReadOnlySpan<char> text ) {
var result = new OrderedDictionary();
var branch = result;
var stack = new Stack<OrderedDictionary>();
string key = "";
//...........................................................................................
// Nested functions
ReadOnlySpan<char> ParseKey( ReadOnlySpan<char> input ) {
AddUnusedKeyWithNullValue();
var keyEnd = input.IndexOfAny(" \t\r\n{<");
if( keyEnd == -1 ) { keyEnd = input.Length; }
key = input[ ..keyEnd ].ToString();
return input[ keyEnd.. ];
}
void AddUnusedKeyWithNullValue() {
// if key has not been consumed -> add with null value
if( key.Length > 0 ) {
branch[ key ] = null;
key = ""; // to indicate that it has been consumed
}
}
ReadOnlySpan<char> ParseScalar( ReadOnlySpan<char> input ) {
if( key.Length == 0 ) {
throw new Exception( "Parse error: missing key before '<'" );
}
var valueEnd = input.IndexOf('>');
if( valueEnd == -1 ) {
throw new Exception( "Parse error: missing '>'" );
}
var value = input[ 1..valueEnd ];
if( value.SequenceEqual("yes") ) {
branch[ key ] = true;
}
else if( value.SequenceEqual("no") ) {
branch[ key ] = false;
}
else if( int.TryParse( value, out int intVal ) ) {
branch[ key ] = intVal;
}
else {
branch[ key ] = value.ToString();
}
key = ""; // clear key to indicate that it has been consumed
return input[ ( valueEnd + 1 ).. ]; // Move to after the value
}
void BeginObject() {
if( key.Length == 0 ) {
throw new Exception( "Parse error: missing key before '{'" );
}
var child = new OrderedDictionary();
branch[ key ] = child;
stack.Push( branch );
branch = child;
key = ""; // clear key to indicate that it has been consumed
}
void EndObject() {
AddUnusedKeyWithNullValue();
if( stack.Count == 0 ) {
throw new Exception( "Parse error: There are more '}' than '{' braces" );
}
branch = stack.Pop();
}
//...........................................................................................
// Parser main loop
var input = text.TrimStart();
while( ! input.IsEmpty ) {
switch( input[ 0 ] ) {
case '<': {
input = ParseScalar( input );
break;
}
case '>': {
// This is an error, as '>' should have been consumed by the '<' branch.
throw new Exception( "Parse error: invalid '>'" );
}
case '{': {
BeginObject();
input = input[ 1.. ];
break;
}
case '}': {
EndObject();
input = input[ 1.. ];
break;
}
default: {
input = ParseKey( input );
break;
}
}
input = input.TrimStart();
}
AddUnusedKeyWithNullValue();
if( stack.Count > 0 ) {
throw new Exception( "Parse error: There are more '{' than '}' braces" );
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment