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====
{{Trans|Haskell}} (Single fold version)
Line 1,503:
""</pre>
====Parser combinators====
<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.
concatMap(fst),▼
sepBy(
takeWhileEscP(esc)(
constant(true)
)(▼
ne(sep)
)
)(char(sep))
);
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
null,
▲ )(
▲ )
▲ .flatMap(compose(
▲ 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);
|