tricu

A lazy functional language based on Tree Calculus
Log | Files | Refs | README | LICENSE

parser.tri (1460B)


      1 !import 6aa33a710955e5746f322775b9078f57ba4145f6b723ff4b76a292285e919823 *
      2 !import 14282088469cc420dd014700da06998f6bb8dcf990fc885a464c0a1e1858e20d *
      3 !import 7f87549d587fa8c3be88af0ea988db3adcc3b35f1ffb48973553491b075614db as N
      4 !export parseNames
      5 !export parsePrefix
      6 
      7 -- Lowercase names separated by commas. Try "red,green", "red,,blue",
      8 -- "red," and "red green". A comma commits to another name.
      9 -- parseNames requires the entire input; parsePrefix exposes any remainder.
     10 -- Edit the parser or labels below; valid edits compile locally.
     11 -- Imports pin complete library interfaces, fetched only when compiling.
     12 nameParser = parserSatisfy (c : N.inRange c 97 122)
     13   |> parserSome
     14   |> parserLabel "lowercase name"
     15 commaParser = parserThen (parserExpect 44) parserCommit
     16 namesPrefix = parserSepBy1 nameParser commaParser
     17 namesComplete = parserBefore namesPrefix <| parserLabel "end of input" parserEof
     18 
     19 report reply = parserMatchReply
     20   (diagnostic rest consumed committed : [
     21     ["status" 0 "error"]
     22     ["expected" 0 (parserDiagnosticExpected diagnostic)]
     23     ["remaining" 0 rest]
     24     ["consumed" 2 consumed]
     25     ["committed" 2 committed]])
     26   (names rest consumed committed : [
     27     ["status" 0 "ok"]
     28     ["names" 0 (intercalate ", " names)]
     29     ["count" 1 (length names)]
     30     ["remaining" 0 rest]
     31     ["consumed" 2 consumed]
     32     ["committed" 2 committed]])
     33   reply
     34 parseNames text = namesComplete text |> report
     35 parsePrefix text = namesPrefix text |> report