Think.Parser.Text
1.0.3
dotnet add package Think.Parser.Text --version 1.0.3
NuGet\Install-Package Think.Parser.Text -Version 1.0.3
<PackageReference Include="Think.Parser.Text" Version="1.0.3" />
paket add Think.Parser.Text --version 1.0.3
#r "nuget: Think.Parser.Text, 1.0.3"
// Install Think.Parser.Text as a Cake Addin #addin nuget:?package=Think.Parser.Text&version=1.0.3 // Install Think.Parser.Text as a Cake Tool #tool nuget:?package=Think.Parser.Text&version=1.0.3
Introduction
The think parser library provides the ability to generically parse text using an event callback model. It exposes three classes
for doing this.
The CommandLineParser class can be used to parse any command string 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 the CommandLineParser 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;
}
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
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 | Versions 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. |
-
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 |
---|