Skip to content

Commit

Permalink
Added the sequence parser combinator to parseq. This closes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
SKolodynski committed Jan 13, 2015
1 parent 0009cb0 commit ddc48db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 14 additions & 0 deletions libraries/qsl/parseq.q
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,20 @@ sepBy1:{[pa;sep;ps]
];
:(`s`cp`errp`errs`ast)!(s;k;0N;();res)
};

/F/ parses a list of parsers sequentially. Fails if any of the parsers fails.
/P/ lpars:LIST[FUNCTION} - a list of parsers
/R/ :LIST[ANY] - list of results returned by lpars
sequence:{[lpars;ps]
if[0N<>ps`errp;:ps];
if[0~count lpars;ps[`ast]:();:ps];
ps1:(first lpars) ps;
if[0N<>ps1`errp;:ps1];
ps2:sequence[1_lpars] ps1;
if[0N<>ps2`errp;:ps2];
ps2[`ast]:(enlist ps1`ast),ps2`ast;
:ps2
};

/F/ Parses zero or more occurrences of a pattern, seperated and ended by a separator. Always succeeds.
/P/ pa - the parser to be applied repeatedly
Expand Down
7 changes: 5 additions & 2 deletions libraries/qsl/test/parseq_test.q
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ system "l lib/qspec/qspec.q";
parseTest[{notFollowedBy[eof] pstring["abc"] x}; "abc"] mustmatch ("error at position 3";"notFollowedBy:excluded end pattern encountered");
// oneChar and empty string
(parseTest[butNot[anyChar;char")"]] "") mustmatch ("error at position 0";"unexpected end of input");
(parseTest[.par.anyChar] "") mustmatch ("error at position 0";"unexpected end of input");
(parseTest[{.par.between[.par.pstring["${"];.par.char["}"];digit] x }] "test") mustmatch ("error at position 0";"expected ${");
(parseTest[.par.anyChar] "") mustmatch ("error at position 0";"unexpected end of input");
(parseTest[{.par.between[.par.pstring["${"];.par.char["}"];digit] x }] "test") mustmatch ("error at position 0";"expected ${");
(parseTest[{.par.between[.par.pstring["${"];.par.char["}"];digit] x }] "${3}") mustmatch "3";
(parseTest[sequence(digit;pstring" is a digit")] "3 is a digit") mustmatch ("3";" is a digit");
(parseTest[sequence(char"(";pstring"abc";char")")] "(abc)") mustmatch ("(";"abc";")");

};
};

Expand Down

0 comments on commit ddc48db

Please sign in to comment.