Skip to content

Commit

Permalink
Write documentation about syntax checking with Farkle.
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-tsirpanis committed Mar 20, 2020
1 parent 6ff7668 commit fc8773a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
45 changes: 45 additions & 0 deletions docsrc/content/advanced-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,49 @@ RuntimeFarkle<object> AdderRuntime = Adder.BuildUntyped();

`buildUntyped` creates a `RuntimeFarkle` that does not return anything meaningful, and succeeds if the input text is valid.

## Syntax checking

It is sometimes useful to just check if a string is syntactically valid, instead of giving it a meaning by returning an object out of it.

This is what the untyped API does, and we can call `buildUntyped` on a typed designtime Farkle to achieve the same.

Because building a designtime Farkle is expensive, if we already have a runtime Farkle, we can create a new one with the same grammar, but with a post-processor that does nothing. This post-processor is called a _syntax checker_. We can change the post-processor of a runtime Farkle this way:

__F#:__
``` fsharp
open Farkle.PostProcessor
let designtime: DesigntimeFarkle<int> = foo()
let runtime: RuntimeFarkle<int> = RuntimeFarkle.build designtime
// syntaxChecker is of type RuntimeFarkle<unit>.
let syntaxCheck = RuntimeFarkle.changePostProcessor PostProcessor.syntaxCheck runtime
```

__C#:__
``` csharp
DesigntimeFarkle<int> Designtime = Foo();

RuntimeFarkle<int> Runtime = Designtime.Build();

RuntimeFarkle<object> SyntaxCheck = Runtime.SyntaxCheck();
// or
using Farkle.PostProcessor.CSharp;
RuntimeFarkle<object> SyntaxCheck = Runtime.ChangePostProcessor(PostProcessor.SyntaxChecker);
```

> The namespaces for the post-processor APIs are a mess and will be streamlined in a next release.
Changing the post-processor is extremely cheap; no new grammar objects are created, and the syntax-checking post-processor is the same.

> Actually, the post-processor used in both the F# and the C# example is the same object too. Post-processors are [covariant][covariance] like designtime Farkles, because they are interfaces. Runtime Farkles on the other hand are classes and therefore not variant at all.
---

Farkle has more APIs for various little features that would make this document too lengthy. Fortunately, [they are well-documented in this site](reference.html), as well as while you code.

So, I hope you enjoyed this little guide. If you did, don't forget to give Farkle a try, and maybe you feel especially untyped today, and want to hit the star button as well. I hope that all of you have a wonderful day, and to see you soon. Goodbye!

[issue-7917]: https://github.com/dotnet/fsharp/issues/7917
[covariance]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/
8 changes: 6 additions & 2 deletions docsrc/content/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ The following table highlights the differences between the F# and C# designtime

### Customizing designtime Farkles

To customize things like the case-sensitivity of designtime Farkles, there are some extension methods for designtime Farkles.
To customize things like the case-sensitivity of designtime Farkles, there are some extension methods for them that reside in the `Farkle` namespace.

## Parsing

To parse text, there are some extension methods for runtime Farkles.
To parse text, there are some extension methods for runtime Farkles that reside in the `Farkle` namespace.

---

So, I hope you enjoyed this little guide. If you did, don't forget to give Farkle a try, and maybe you feel especially sharp today, and want to hit the star button as well. I hope that all of you have a wonderful day, and to see you soon. Goodbye!

[fsharp]: quickstart.html
[regex]: reference/farkle-builder-regex.html
2 changes: 2 additions & 0 deletions docsrc/content/quickstart.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ To be able to use a designtime Farkle, we will first feed it to a component call
30: I guess you can't read this line.
> The original idea was to name the designtime Farkles simply _Farkles_, but having a type named `Farkle.Farkle` would create lots of confusion and compile errors.
## Designing our grammar
We want to design a grammar that represents a mathematical expressions on floating-point numbers. The supported operations will be addition, subtraction, multiplication, division, and unary negation. The operator precedence has to be honored, as well as parentheses.
Expand Down

0 comments on commit fc8773a

Please sign in to comment.