Slide 1

Slide 1 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Building efficient and composable parsers with FastParse Paul Roberts - The Guardian 29th November 2018 Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 2

Slide 2 text

Introduction Parsers FastParse Live Demo Syntax Conclusion FastParse Created by Li Haoyi, author of: Ammonite uJson mill utest . . . and many more Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 3

Slide 3 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Parsers a parsers is just a set of rules for converting unstructured input into structured input Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 4

Slide 4 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Regular expressions Generic and flexible: excellent for giving users the power to search within your application Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 5

Slide 5 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Parser Combinator tools Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 6

Slide 6 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Why is it fast? Has a Functional API but "non-functional" internals e.g. minimal memory allocation ahead of time optimisation Comparing to Parser Combinators “FastParse’s internals do not look function at all, in fact they look really gross and ugly.” – Li Haoyi Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 7

Slide 7 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Debugging def number[_:P] = P(CharIn("0-9").rep) def coords[_:P] = P(number ~ "," ~ number) parse("10,20", coords(_)) Error without logging Failure("", 1, Extra(IndexedParserInput("10,20"), 0, ... Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 8

Slide 8 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Debugging def number[_:P] = P(CharIn("1-9").rep) def coords[_:P] = P(number ~ "," ~ number) .log // <---- added logging to the request parse("01,20", coords(_)) Shows which parsers were tried. . . +coords:1:1, cut -coords:1:1:Failure(coords:1:1 / ",":1:1 ..."01,20", cut) Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 9

Slide 9 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Live Demo Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 10

Slide 10 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Notes on the Syntax It looks scary, but it is just the same old Scala that we know already Defining a parser def parseAtSign[_: P] = P("@") Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 11

Slide 11 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Syntax Shortcut for implicit argument with a type param def parseAtSign(implicit arg0: P[_]) = P("@") Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 12

Slide 12 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Syntax Passes into the called parsers def parseAtSign(implicit arg0: ParsingRun[_]) = P("@")(arg0) P is just a handy type alias Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 13

Slide 13 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Conclusion Writing a parsers can be a better way to parse input when the format is known in advance FastParse is a really nice and quick parser library to use Paul Roberts - The Guardian Building efficient and composable parsers with FastParse

Slide 14

Slide 14 text

Introduction Parsers FastParse Live Demo Syntax Conclusion Useful links FastParse 2 blog post http://www.lihaoyi.com/post/ Fastparse2EvenFasterScalaParserCombinators.html Li Haoyi talk about FastParse https://vimeo.com/142341803 Code that I used for benchmarking https://gist.github.com/paulmr/ 379e9ddf08ee2eeab9c7b549f710f7a4 Paul Roberts - The Guardian Building efficient and composable parsers with FastParse