Questions tagged [sprache]

Sprache is a lightweight library for constructing parsers directly in C# code with capabilities generally in the range between regular expressions and tools like ANTLR.

Sprache is a lightweight library for constructing parsers directly in C# code.
It doesn't compete with "industrial strength" language workbenches — it fits somewhere in between regular expressions and a full-featured toolset like ANTLR.

There's a tutorial.

(Information drawn with permission from the Sprache website.)

39 questions
7
votes
0 answers

"Sprache" monadic parser `Or` and `Many` semantics

I'm using Sprache monadic parser to parse a DSL. This is a snippet of my grammar: public static readonly Parser TerminatedStatement = from exp in Parse.Ref(() => Expression) from _ in Parse.Char(';').Token() select…
Roger Johansson
  • 22,764
  • 18
  • 97
  • 193
4
votes
0 answers

How to parse Conditional Ternary syntax (a > b ? a : b) using Sprache

I keep thinking I understand Sprache parsing the more I use it, but then I come across a new syntax that baffles me, and there are not a lot of examples online. At this time, I'm trying to allow my string to have conditional syntax (like C#) such…
Jarvis
  • 681
  • 8
  • 33
4
votes
1 answer

How to handle 'line-continuation' using parser combinators

I'm trying to write a small parser using the Sprache parser combinator library. The parser should be able to parse lines ended with a single \ as insignificant white space. Question How can I create a parser that can parse the values after the =…
Bas Bossink
  • 9,388
  • 4
  • 41
  • 53
4
votes
1 answer

Sprache: left recursion in grammar

I am developing a parser for a language similar to SQL and I have the problem of creating some of the rules of language, such as: expression IS NULL and expression IN (expression1, expression2, ...) with priority between logical and mathematical…
anpv
  • 179
  • 1
  • 13
3
votes
3 answers

How can I parse lines that can appear in any order with Sprache?

I'm using Sprache to parse a section of a file that looks like this: OneThing=Foo AnotherThing=Bar YetAnotherThing=Baz All three lines are mandatory but they can appear in any order. I have parsers for the individual lines, that look like…
Paul
  • 1,897
  • 1
  • 14
  • 28
3
votes
1 answer

Recursive expression parsing in Sprache

I am building a Sprache parser to parse expressions similar to SQL search conditions. e.g Property = 123 or Property > AnotherProperty So far both of those examples work, however I am struggling to figure out what I need to do to allow ANDing/ORing…
Simon
  • 5,373
  • 1
  • 34
  • 46
3
votes
1 answer

How can I improve Sprache parser error messaging with missing closing brace?

I am building a simple command-style grammar using Sprache. I am trying to find out if there's a way to get better error reporting when missing a closing character (e.g. ], ), }). If a closing character is missing my grammar correctly reports an…
3
votes
1 answer

“Sprache” parser `Present` semantics

I am writing a parser that conforms to System for Cross-domain Identity Management: Protocol Filtering spec. I was able to parse almost any expression with Sprache, except "pr" operator. Can't wrap my head how to make it work properly. Here is main…
Aleksei Anufriev
  • 3,206
  • 1
  • 27
  • 31
3
votes
1 answer

How can I use Sprache to parse an expression when the start of the expression is the same

I am having some trouble with my Sprache based parser. I am trying to parse a string in which there are two possible things to parse. One option is a regular variable name like x or a more complex version x[1]. The first references a simple variable…
Jack Hughes
  • 5,514
  • 4
  • 27
  • 32
3
votes
2 answers

Sprache parser and characters escaping

I haven't found an example - what to do with characters escaping. I have found a code example: static void Main(string[] args) { string text = "'test \\\' text'"; var result = Grammar.QuotedText.End().Parse(text); } public static class…
vitidev
  • 860
  • 1
  • 8
  • 19
3
votes
1 answer

Sprache parser with custom fields

I have a report server that needs to parse a string with some arguments controlling what is in the report. I am using the parser library sprache to help with this. All is working fine except for one thing I'm stuck on. I have a time filter that can…
Jack Hughes
  • 5,514
  • 4
  • 27
  • 32
2
votes
2 answers

While parsing text using Sprache, can I determine the current index within the original string?

I have Sprache set up to parse an Equation that has a number of different possible method calls in it. After it resolves the method, is there a way to determine the index values within the original string? Perhaps the Parse has a "current index"…
Jarvis
  • 681
  • 8
  • 33
2
votes
1 answer

Text query parsing in Sprache

I'm trying to write some code to match strings based on a pattern: pattern: "dog and (cat or goat)" test string: "doggoat" result: true test string: "dogfrog" result: false I'm trying to write a parser using Sprache, with most of the logic provided…
2
votes
1 answer

Getting exception while parsing file using Sprache "Parsing failure: Unexpected end of input reached; expected ="

I want to parse below file, first=The_First_Step { { value=First.Value, } } second=The_Second_Step { { another = Second_Value, more = Yet.More, } } I have written grammar as, public static NGSection…
Rahul Techie
  • 363
  • 2
  • 8
2
votes
2 answers

Using Sprache to parse Enums from identifiers?

I am starting to use Sprache to parse a domain specific language for math expressions. I know I can parse an identifier using something like this: static readonly Parser Identifier = from leading in Parse.WhiteSpace.Many() …
1
2 3