Questions tagged [fastparse]

FastParse is a parser-combinator library for Scala that lets you quickly and easily write recursive descent parsers in Scala.

FastParse is a parser-combinator library for Scala that lets you quickly and easily write recursive descent parsers in Scala. Features include:

Up to 1/5 the speed of a hand-written parser, 100x faster than scala-parser-combinators, comparable (though slightly slower than) Parboiled2 1/10th the size of a hand-written parser Automatic, excellent error-reporting and diagnostics. Zero allocations during a parse Compatible with both Scala-JVM and Scala.js

http://lihaoyi.github.io/fastparse/

27 questions
3
votes
1 answer

How to avoid left-recursion infinite loops in Fastparse?

I had a parser that worked well in Scala Packrat parser combinators. I would like to try something faster with the Fastparse library. However, it cannot handle left-recursion infinite loops. Is there any standard way to cope with that? sealed trait…
dawid
  • 663
  • 6
  • 12
3
votes
0 answers

How to directly retrieve the line and column number of a parsed object in Scala FastParse?

According to the docs, you can retrieve the index using Index. However, this one only returns a single integer. Do I have to manually compute for the line number and column (e.g by using Util.lineNumberLookup) or use IndexedParserInput.prettyIndex…
Melvic Ybanez
  • 1,893
  • 4
  • 19
  • 26
3
votes
1 answer

Creating AST for arithmetic expression in Scala

I would like to make an AST for arithmetic expression using fastparse from Scala. For me a arithmetic expression is like: var_name := value; // value can be an integer, or a whole expression For the moment I have this parsers: def word[_:P] =…
Adishotie
  • 31
  • 3
3
votes
2 answers

Why doesn't parser combinator backtrack?

Consider import util.parsing.combinator._ object TreeParser extends JavaTokenParsers { lazy val expr: Parser[String] = decimalNumber | sum //> expr: => TreeParser.Parser[String] lazy val…
3
votes
3 answers

How to match exactly 'n' given characters with FastParse

The FastParse parser-combinator scala library gives you the .rep(n) 'Repeat' method to allow you to create a new parser that attempts to parse the givenParser n or more times. What's the canonical way to do this if I want exactly n matches? In my…
Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
2
votes
1 answer

How to use SBT's libraryDependencyScheme key

I'm in library dependency hell right now with the following error: [error] (server / update) found version conflict(s) in library dependencies; some are suspected to be binary incompatible: [error] [error] * com.lihaoyi:geny_2.13:1.0.0…
bwbecker
  • 1,031
  • 9
  • 21
2
votes
1 answer

How to provide an explicit error/failure message in the Scala fastparse library?

I'm using Li Haoyi's FastParse library. I have several situations where I'd like to provide explicit failure messages. For example: def courseRE[p: P]: P[Regex] = P(CharIn("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.|*+[]()-^$").repX(1).!).map { re…
bwbecker
  • 1,031
  • 9
  • 21
2
votes
2 answers

Why can't I use the constructor of a case class as a function for use inside map()

Passing the tuple directly to the constructor is not accepted by the compiler as the minimal example shows: scala> case class A(a:Int, b:Int) defined class A scala> List((1, 2)).map(A) :14: error: type mismatch; found : A.type required:…
dawid
  • 663
  • 6
  • 12
2
votes
1 answer

Describe recursive grammar with type aliases

How can I describe this recursive grammar with type aliases: type FieldValue = Seq[String] :+: String :+: Int :+: Long :+: CNil type FieldLeaf = FieldValue :+: SubField :+: CNil type SubField = Seq[Field] type Field = (String, FieldLeaf) As it…
2
votes
1 answer

FastParse. How to enforce exactly once rule

I am trying to implement the following grammer using FastParse API. Expr can contain only Foo,Bar,Baz sub expressions Expr must contain atleast 1 sub expression Foo/Bar/Bar. It cannot be empty Foo/Bar/Baz can appear in any order inside…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
1
vote
0 answers

Is there a Scala parsing solution that works on string tokens rather than characters?

I have a document I would like to parse one line at a time. My tokens are entire lines: Pizza Is Great 12 14 17 red blue buckle my shoe PS. I <3 I could match the above with a grammar (pseudocode) something like: text → /.*/ int →…
Sasgorilla
  • 2,403
  • 2
  • 29
  • 56
1
vote
1 answer

How can I compose a parser to parse a quoted regex in fastparse?

What I wish to parse is any Regex that quoted with double quotes. For example, "([A-Z]+[A-Z]+[C])" What I have tried so far is the following in Scala using fastparse library: def regex[_: P]: P[Unit] = P(AnyChar.rep).log def quotedRegex[_: P]:…
iamsmkr
  • 800
  • 2
  • 10
  • 29
1
vote
0 answers

Use scala fastparse to remove enclosing escaped quotes but preserving the others

I want to use fastparse to transform the following string \"Escaped quote\"\"\" into Escaped quote\". I have the following code which almost works. def escapedQuote[_: P]: P[Unit] = P("\"") def unquotedColumn[_: P] = P(escapedQuote ~…
Rob
  • 145
  • 1
  • 13
1
vote
1 answer

Parsing indentation with FastParse

I'm trying to parse an indented language using FastParse and I'm struggling finding any resources or information on it. There is only one example I can find here that shows how to parse and calculate the sum of the integers in the tree structure.…
Michael
  • 3,411
  • 4
  • 25
  • 56
1
vote
1 answer

fail on match in scala fastparse

I have the following fastparse parser named "variable": val alphabet = 'A' to 'z' val variable: Parser[String] = P(CharsWhileIn(alphabet).!) I would like for this parser to fail on a specific word like "end", while still returning a Parser[String].
Anton
  • 1,314
  • 1
  • 12
  • 27
1
2