reazen / relude-parse Goto Github PK
View Code? Open in Web Editor NEWMonadic parsing with Relude
License: MIT License
Monadic parsing with Relude
License: MIT License
Add a function notChar(string) that is a shortcut for anyCharNotIn([str])
failIfNone
failIfEmptyStr
I know there's a comment in the code, but I figured I'd open a more official ticket since this will probably become an issue in Relude CSV (since parsing an 8000 line CSV file doesn't seem completely out of the question).
I was looking at the MonadRec stuff in PureScript, and I think it's very similar to the trampoline technique that I was porting from Scala to Reason. I may try to do something in Relude proper, which hopefully relude-parse
will be able to benefit from.
It would be nice to have quick aliases for calling run/unParser from the ReludeParse
module.
eol
parser that handles the common occurrences of theseorEOL
function?For relude-csv, @mlms13 ran into an interesting scenario when trying to parse csv fields, which are delimited by ,
, \r\n
or \n
.
You'd normally use sepBy
for parsing the csv fields out, but for parsing the actual fields, you need a parser that will consume all the input up to ,
, \r\n
, or \n
.
Using manyUntil
doesn't quite work b/c manyUntil
consumes the delimiter, so it messes up the sepBy
.
Maybe we need something like manyUntilBacktrack
which consumes all the input and the terminator, but then backtracks to the start of the terminator?
Or another idea is a not
function that lets you negate a parse, so you could so many(not(anyOfStr([",", "\r\n", "\n"])))
. I'll have to see how negation could work like this.
We plan to have a relude-datetime
library eventually, but until then, we can add some basic date-time parsing utilities here. We can move them to a new lib later.
ReludeParse.DateTime.ISO8601.parse(...)
For date times, @mlms13 and I had an idea for type-safe format specs, which could be used for parsing and formatting datetimes:
module Format = {
type t = | Lit(string) | Year4Digit | Year2Digit | etc.
};
let parse: (list(Format.t), string) => Belt.Result.t(DateTime.t, string) = List.foldLeft(...)
let format: (list(Format.t), DateTime.t) => string = ...
E.g.
parse([Year4Digit, Lit("-"), Month2Digit, Lit("-"), Day2Digit], "1999-02-03")
We'll have to make some decisions on how best to setup the format data constructors - whether they should be very specific and unambiguous vs. parameterized with ints.
e.g.
Month2DigitZeroPad
vs.
Month(int, string) // Month(2, "0")
vs.
Month2DigitWithPad(string)
etc.
The problem with int
and string
parameters is that they make the whole thing very open-ended and prone to error.
Add some utilities for building math expression parsers. This might be more for demonstration purposes.
We should be able to use some of the implementations from https://github.com/andywhite37/haxpression2
We might eventually have a relude-uri
library, but for now, we can put a basic parser for URI/URL here.
I wrote a URI paser in scala using atto, which looked something like this - I can probably port this code to relude-parse
:
def uri: Parser[URI] = {
for {
_ <- opt(horizontalWhitespace) // strip off leading whitespace
schemeOpt <- opt(uriScheme)
authorityOpt <- opt(uriAuthority)
path <- uriPath
queryOpt <- opt(uriQuery)
fragmentOpt <- opt(uriFragment)
_ <- opt(horizontalWhitespace) // strip off trailing whitespace
_ <- endOfInput
} yield {
URI(schemeOpt, authorityOpt, path, queryOpt, fragmentOpt)
}
}
It would be nice to have some show functions for NanpPhone - I'm not sure if there are different output formats, or if there is a single canonical format, but we can support whatever makes sense.
others?
Once relude has more complete Decimal
and/or BigDecimal
types, we it would be nice to have some parsers for these data types. Since they are fairly primitive, I think those parsers can live in this repo as extras.
Not critical, but might be nice - see relude
for example
In relude-csv, I've found myself wanting to parse either until a terminator is found, or until all input is consumed. With manyUntil
's current behavior, the terminator is required or parsing will fail. I don't think this is wrong, but it's a little verbose to work around:
manyUntil(str(";") |> map(ignore) <|> eof);
Maybe something like manyUntilOpt
since the terminator isn't required to be present for the parser to succeed?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.