Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building efficient and composable parsers with FastParse by Paul Roberts

Shannon
November 28, 2018

Building efficient and composable parsers with FastParse by Paul Roberts

Building efficient and composable parsers with FastParse

Parsers are a fundamental part of all kinds of software, and Paul will guide us through creating them the Scala-way with FastParse's parser combinators. We'll look at how FastParse provides a nice API without sacrificing performance, and he will illustrate the method for building a parser and benchmarking its performance in a live demo.

Shannon

November 28, 2018
Tweet

More Decks by Shannon

Other Decks in Technology

Transcript

  1. 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

    View full-size slide

  2. 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

    View full-size slide

  3. 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

    View full-size slide

  4. 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

    View full-size slide

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

    View full-size slide

  6. 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

    View full-size slide

  7. 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

    View full-size slide

  8. 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

    View full-size slide

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

    View full-size slide

  10. 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

    View full-size slide

  11. 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

    View full-size slide

  12. 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

    View full-size slide

  13. 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

    View full-size slide

  14. 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

    View full-size slide