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

Let’s write a parser! [SoundCloud HQ edition]

Let’s write a parser! [SoundCloud HQ edition]

Denis Defreyne

May 17, 2016
Tweet

More Decks by Denis Defreyne

Other Decks in Programming

Transcript

  1. But how do you know that I am Denis? I

    told you. I wrote it down. You’ve probably seen me before. Etc. 5
  2. 18 H e l l o 0 1 2 3

    4 char("H").apply("Hello")
  3. 18 H e l l o 0 1 2 3

    4 char("H").apply("Hello")
  4. 18 H e l l o 0 1 2 3

    4 char("H").apply("Hello")
  5. 18 H e l l o 0 1 2 3

    4 char("H").apply("Hello") Success(pos = 1)
  6. 19 A d i ó s 0 1 2 3

    4 char("H").apply("Adiós")
  7. 19 A d i ó s 0 1 2 3

    4 char("H").apply("Adiós")
  8. 19 A d i ó s 0 1 2 3

    4 char("H").apply("Adiós")
  9. Failure(pos = 0) 19 A d i ó s 0

    1 2 3 4 char("H").apply("Adiós")
  10. H e l l o 22 0 1 2 3

    4 seq(char("H"), char("e")).apply("Hello")
  11. H e l l o 22 0 1 2 3

    4 seq(char("H"), char("e")).apply("Hello")
  12. H e l l o 22 0 1 2 3

    4 seq(char("H"), char("e")).apply("Hello")
  13. H e l l o 22 0 1 2 3

    4 seq(char("H"), char("e")).apply("Hello")
  14. H e l l o 22 0 1 2 3

    4 seq(char("H"), char("e")).apply("Hello") Success(pos = 2)
  15. H e l l o 25 0 1 2 3

    4 string("Hello").apply("Hello")
  16. H e l l o 25 0 1 2 3

    4 string("Hello").apply("Hello")
  17. H e l l o 25 0 1 2 3

    4 string("Hello").apply("Hello")
  18. H e l l o 25 0 1 2 3

    4 string("Hello").apply("Hello") Success(pos = 5)
  19. H e l l o 27 0 1 2 3

    4 seq(string("Hello"), eof).apply("Hello")
  20. H e l l o 27 0 1 2 3

    4 seq(string("Hello"), eof).apply("Hello")
  21. H e l l o 27 0 1 2 3

    4 seq(string("Hello"), eof).apply("Hello")
  22. H e l l o 27 0 1 2 3

    4 seq(string("Hello"), eof).apply("Hello")
  23. H e l l o 27 0 1 2 3

    4 seq(string("Hello"), eof).apply("Hello") Success(pos = 5)
  24. 28 0 1 2 3 4 5 H e l

    l o ! seq(string("Hello"), eof).apply("Hello!")
  25. 28 0 1 2 3 4 5 H e l

    l o ! seq(string("Hello"), eof).apply("Hello!")
  26. 28 0 1 2 3 4 5 H e l

    l o ! seq(string("Hello"), eof).apply("Hello!")
  27. 28 0 1 2 3 4 5 H e l

    l o ! seq(string("Hello"), eof).apply("Hello!")
  28. 28 0 1 2 3 4 5 Failure(pos = 5)

    H e l l o ! seq(string("Hello"), eof).apply("Hello!")
  29. A d i ó s 30 0 1 2 3

    4 alt(char("H"), char("A")).apply("Adiós")
  30. A d i ó s 30 0 1 2 3

    4 alt(char("H"), char("A")).apply("Adiós")
  31. A d i ó s 30 0 1 2 3

    4 alt(char("H"), char("A")).apply("Adiós")
  32. A d i ó s 30 0 1 2 3

    4 alt(char("H"), char("A")).apply("Adiós") Success(pos = 1)
  33. a , a , b 36 0 1 2 3

    4 intersperse(char("a"), char(",")).apply("a,a,b")
  34. a , a , b 36 0 1 2 3

    4 intersperse(char("a"), char(",")).apply("a,a,b")
  35. a , a , b 36 0 1 2 3

    4 intersperse(char("a"), char(",")).apply("a,a,b")
  36. a , a , b 36 0 1 2 3

    4 intersperse(char("a"), char(",")).apply("a,a,b")
  37. a , a , b 36 0 1 2 3

    4 intersperse(char("a"), char(",")).apply("a,a,b") Success(pos = 3)
  38. 52 [ ["Horan", "Niall", 93], ["Payne", "Liam", 93], ["Tomlinson", "Louis",

    91], ["Styles", "Harry", 94], ["Malik", "Zayn", 93], ]
  39. 60

  40. 74

  41. 74 require 'd-parse'
 module JSONGrammar extend DParse::DSL
 DIGIT = char_in('0'..'9')


    NUMBER = repeat1(DIGIT)
 end
 res = Grammar::NUMBER.apply('8700')
  42. 75

  43. 76 
 expected identifier at line 1, column 36 def

    reticulate(splines, threshold, ) { ↑