tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

parser.tri (1453B)


      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 = parserLabel "lowercase name" (parserSome (parserSatisfy (c : N.inRange c 97 122)))
     13 commaParser = parserThen (parserExpect 44) parserCommit
     14 namesPrefix = parserSepBy1 nameParser commaParser
     15 namesComplete = parserBefore namesPrefix (parserLabel "end of input" parserEof)
     16 
     17 report reply = (parserMatchReply
     18   (diagnostic rest consumed committed : [
     19     ["status" 0 "error"]
     20     ["expected" 0 (parserDiagnosticExpected diagnostic)]
     21     ["remaining" 0 rest]
     22     ["consumed" 2 consumed]
     23     ["committed" 2 committed]])
     24   (names rest consumed committed : [
     25     ["status" 0 "ok"]
     26     ["names" 0 (intercalate ", " names)]
     27     ["count" 1 (length names)]
     28     ["remaining" 0 rest]
     29     ["consumed" 2 consumed]
     30     ["committed" 2 committed]])
     31   reply)
     32 parseNames text = report (namesComplete text)
     33 parsePrefix text = report (namesPrefix text)