Parser in Curry (fl-parser 3.0.0)

Vanessa McHale
2024-04-24T03:34:35Z

parse.curry

| -- cypm add fl-parser
| -- ported from: https://www-ps.informatik.uni-kiel.de/~cpm/DOC/fl-parser-3.0.0/Parser_curry.html
| import Parser
|
| num = Parser.some digit l >>> numeric_value l
| where l free
| numeric_value ds = foldl1 (\acc x -> 10*acc+x) (map (\c -> ord c-ord '0') ds)
|
| expr = term m Parser.<*> terminal '+' Parser.<*> expr n >>> m+n
| <||> term m Parser.<*> terminal '-' Parser.<*> expr n >>> m-n
| <||> term
| where m,n free
|
| term = factor m Parser.<*> terminal '*' Parser.<*> term n >>> m*n
| <||> factor m Parser.<*> terminal '/' Parser.<*> term n >>> div m n
| <||> num
| where m,n free
|
| factor = terminal '(' Parser.<*> expr e Parser.<*> terminal ')' >>> e
| <||> num
| where e free
|
| digit = satisfy isDigit
|
| -- expr n "(10+5*2)/4" =:= [] where n free