Think.Parser.Text 1.0.7

dotnet add package Think.Parser.Text --version 1.0.7
NuGet\Install-Package Think.Parser.Text -Version 1.0.7
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Think.Parser.Text" Version="1.0.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Think.Parser.Text --version 1.0.7
#r "nuget: Think.Parser.Text, 1.0.7"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Think.Parser.Text as a Cake Addin
#addin nuget:?package=Think.Parser.Text&version=1.0.7

// Install Think.Parser.Text as a Cake Tool
#tool nuget:?package=Think.Parser.Text&version=1.0.7

Introduction

The think parser library provides the ability to generically parse text using an event callback model. To help enable this functionality four classes are exposed:

  1. TextParser
  2. CommandLineParser
  3. CommandLineInterpreter
  4. SequenceParser

TextParser

This is a generic parser that can be used to parse any block in whatever manner the user chooses to interpret the characters or words of the text in. This is the internal parser that all other parsers in this library use (which highlights it's flexibility).

The following snippet shows how some of the processing for the sequence diagram parser is used:

//process symbol patterns
_parser["->>"].Found += context => ProcessSignal(context, SignalTypeCode.Request);
_parser["-->>"].Found += context => ProcessSignal(context, SignalTypeCode.Response);
_parser["-x"].Found += context => ProcessSignal(context, SignalTypeCode.AsyncRequest);
_parser["--x"].Found += context => ProcessSignal(context, SignalTypeCode.AsyncResponse);
_parser["%%"].Found += context => ProcessComment(context);

_parser.Pattern((pattern, iterator) =>
{
    iterator.InitializePreview();
    if (iterator.MoveNextPreview())
    {
        if (pattern.ToLower() == "->" && iterator.CurrentPreview != '>')
        {
            return true;
        }
    }
    return false;
 }, "-> where the very next character is not '>'").Found += context => ProcessSignal(context, SignalTypeCode.Association);

CommandLineParser

The CommandLineParser class can be used to parse any command string or string array (for instance args array in console applications) into an object that can be traversed to pull the values that were inputed by the user out.

The following code illustrates the use of if this class.

static bool SwitchMethod(string command_line)
{
    var run = true;
    var parse_result = CommandLineParser.Parse(command_line);
    if (parse_result.HasTokens)
    {
        switch (parse_result.First.Text.ToLower())
        {
            case "end":
                run = false;
                Console.WriteLine("Done");
                break;
            case "test" when parse_result.ArgsAre("test", "|hello|hi|ola"):
                Console.WriteLine("done");
                break;
            case "parse" when parse_result.ArgsAre("parse", "test", "one"):
                {
                    Console.WriteLine("processed [test one]");
                }
                break;
            case "parse" when parse_result.ArgsAre("parse", "*", "test"):
                {
                    var argument = parse_result.Fetch("test").Node.Next.Value.Text;
                    Console.WriteLine($"processed [test {argument}]");
                }
                break;
            case "parse" when parse_result.ArgsAre("parse", "seq,*", "*", "1,2,3"):
                {
                    Console.WriteLine("processing 2...");
                    SequenceDiagramTesting();
                }
                break;
            default:
                Console.WriteLine("This command cannot be processed");
                break;
        }
    }
    return run;
}

CommandLineInterpreter

This library also contains a CommandLineInterpreter class that can be used to more easily produce a REPL loop. Using this class simplifies the traversal process and also provides for automatic command output when the input command by the user cannot be processed. The following illustrates:

static void Main(string[] args)
{
    bool run = true;
    CommandInterpreter cli = new CommandInterpreter();
    cli.NotInterpreted+=(all_options) => Console.WriteLine($"Command not found{all_options}");
    cli["say", "hello", "word"]
        .SetLabel("Simple hello world").Matched+= (result) => Console.WriteLine("Hello there");
            
    cli["say", "hi|ola|kedu"]
        .SetLabel("With optional parameters").Matched += result =>
        {
            var what_was_said = result.Fetch("say").Node.Next.Value.Text;
            Console.WriteLine($"{what_was_said} there!");
        };

    cli["say", "*", "world"]
        .SetLabel("With variables").Matched += (result) =>
        {
            var what_was_said = result.Fetch("say").Node.Next.Value.Text;
            Console.WriteLine($"Random \"{what_was_said}\" there!");
        };

    cli["say", "-name,*", "-age,*", "-gender,male|female"]
        .SetLabel("Combination of all possibilities").Matched += (result) =>
        {
            var name = result.Fetch("-name").Node.Next.Value.Text;
            var age = result.Fetch("-age").Node.Next.Value.Text;
            var gender = result.Fetch("-gender").Node.Next.Value.Text;
            Console.WriteLine($"Hello {name}, you are a {age} year old {gender}");
        };

    while (run)
    {
        Console.Write("Enter Command > ");
        var command_line = Console.ReadLine();
        cli.Interpret(command_line);
    }
}
}

The following code, when run in a command line will produce output similar to the following:

Enter Command > say -age 12 -name edwuardo -gender female
Hello edwuardo, you are a 12 year old female
Enter Command > say -age 12 -name edwuardo -gender femalefff
Command not found

Valid Options
=====================================
Option -  With optional parameters : [say] [hi|ola|kedu]
Option -  Simple hello world : [say] [hello] [word]
Option -  With variables : [say] [*] [world]
Option -  Combination of all possibilities : [say] [-name *] [-age *] [-gender male|female]

Note that teh commands specified during definition are all itemized with labels and printed out when invalid commands are entered

SequenceParser

Finally, this library provides the ability to parse mermaid based sequence diagram text using the SequenceParser class. This class can parse all compatible sequence diagram syntax (from mermain). The follwing sequence diagrams illustrates all the possible syntax that cann be parsed via this class.

%% basic diagram
Alice->>John: Hello John, how are you?
John-->>Alice: Great!

%% participants example
participant John2
participant Alice2
Alice2->>John2: Hello John2, how are you?
John2-->>Alice2: Great2!

%% using aliases
participant A as Alice3
participant J as John3
A->>J: Normal Request
J->>A: Normal Response

%% other message types
A->J: association
J-->A: dependency
A-xJ: async request
J--xA: async response

%% activation examples
Alice->>John: Hello John, how are you?
activate John
John-->>Alice: Great!
deactivate John

%% stacked activation
Alice->>+John: Hello John, how are you?
Alice->>+John: John, can you hear me?
John-->>-Alice: Hi Alice, I can hear you!
John-->>-Alice: I feel great!

%% Loops
Alice->John: Hello John, how are you?
loop Every minute
    John-->Alice: Great!
end

%% alt scenarios
Alice->>Bob: Hello Bob, how are you?
alt is sick
    Bob->>Alice: Not so good :(
else is well
    Bob->>Alice: Feeling fresh like a daisy
end
opt Extra response
    Bob->>Alice: Thanks for asking
end


%% par scenarios
Alice->>Bob: Start employment process
par procure laptop
    Bob->>Alice: buy laptop
    Bob->>Employee: send laptop
and setup id
    Bob->>Joe: create id
    Bob->>Employee: here is your id
and building access
    Bob->>Alice: Create building access
and onboard payroll
    Bob->>Alice: Get them setup in the system
end

Sequence diagrams can be traversed as follows:

//sequance parsing
var result = SequenceParser.ParseFromFile("../../../fullsyntax.md");
            
//Iterates through participants
foreach (var participant in result.Participants)
{
    //iterates through the signals the participant is involved in (either as a sender or a recipient)
    foreach (var signal in participant.Signals)
    {
                    
    }
}

//iterates through the messages in the steps of the sequence diagram
foreach (var message in result.Sequence.Children)
{
    switch (message)
    {
        case AltStep alt:
            break;
        case LabelledStep step:
            break;

            ...
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.7 263 1/11/2022
1.0.6 409 1/11/2022
1.0.5 417 1/11/2022
1.0.4 324 1/28/2021
1.0.3 297 1/28/2021
1.0.2 266 1/27/2021
1.0.1 332 1/24/2021
1.0.0 327 1/24/2021