Tokenize a string with escaping: Difference between revisions

→‎JS ES6: Parser combinators :: wrapped composition as a function with three arguments.
(→‎JS ES6: Added an alternative, assembled from generic parser combinators)
(→‎JS ES6: Parser combinators :: wrapped composition as a function with three arguments.)
Line 1,456:
===ES6===
 
====Hand-parsed====
As a hand-written function:
 
{{Trans|Haskell}} (Single fold version)
Line 1,503:
""</pre>
 
====Parser combinators====
OrDefining the function as a composition of generics from a parser combinator library:
 
<lang javascript>(() => {
Line 1,509 ⟶ 1,510:
 
// ------ TOKENIZATION OF A STRING WITH ESCAPES ------
 
// tokenizeWithEscapes :: Char -> Char ->
// String -> [String]
const tokenizeWithEscapes = esc =>
// A list of tokens in a given string,
// where the separator character is sep
// and any character may be escaped by
// a preceding esc character.
.flatMap(sep => compose(
concatMap(fst),
) parse(
sepBy(
takeWhileEscP(esc)(
constant(true)
)(
ne(sep)
)
)(char(sep))
));
);
 
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
parseJSON.stringify(
sepBytokenizeWithEscapes('^')('|')(
takeWhileEscP('one^|uno||three^^^^|four^^^|^cuatro|')(constant(true))(
ne('|'),
null, )2
)(char('|'));
)(
'one^|uno||three^^^^|four^^^|^cuatro|'
)
.flatMap(compose(
x => JSON.stringify(x, null, 2),
fst
));
 
// -->
// [
Line 1,533 ⟶ 1,548:
// ""
// ]
 
 
// ----------- GENERIC PARSER COMBINATORS ------------
Line 1,793 ⟶ 1,807:
).concat(...ys) : ys
)(list(xs));
 
 
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f =>
// List monad bind operator.
xs => xs.flatMap(f);
 
 
9,655

edits