From f05f004356e91f110f5297bdb61827a7c4436d2f Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 28 May 2021 23:32:09 -0700 Subject: parser: provide parse-errors function. * parser.c (parse_errors): New function. * parser.h (parse_errors): Declared. * txr.1: Documented. * share/txr/stdlib/doc-syms.tl: Updated. --- parser.c | 14 ++++++++++++++ parser.h | 1 + share/txr/stdlib/doc-syms.tl | 1 + txr.1 | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/parser.c b/parser.c index 1ab2e70b..3358d7bd 100644 --- a/parser.c +++ b/parser.c @@ -1683,6 +1683,19 @@ val parser_eof(val parser) return tnil(p->eof); } +val parse_errors(val stream) +{ + val self = lit("parse-errors"); + val errors = nil; + val parser = get_parser(stream); + if (parser) { + parser_t *p = coerce(parser_t *, cobj_handle(self, parser, parser_s)); + if (p->errors) + errors = num(p->errors); + } + return errors; +} + static val circref(val n) { uw_throwf(error_s, lit("unresolved #~s# reference in object syntax"), @@ -1876,6 +1889,7 @@ void parse_init(void) reg_fun(intern(lit("get-parser"), system_package), func_n1(get_parser)); reg_fun(intern(lit("parser-errors"), system_package), func_n1(parser_errors)); reg_fun(intern(lit("parser-eof"), system_package), func_n1(parser_eof)); + reg_fun(intern(lit("parse-errors"), user_package), func_n1(parse_errors)); reg_fun(intern(lit("repl"), system_package), func_n4(repl)); reg_mac(json_s, func_n2(me_json)); } diff --git a/parser.h b/parser.h index 5955447a..3c2e2292 100644 --- a/parser.h +++ b/parser.h @@ -143,4 +143,5 @@ val ensure_parser(val stream, val name); val parser_set_lineno(val self, val stream, val lineno); val parser_errors(val parser); val parser_eof(val parser); +val parse_errors(val stream); void parse_init(void); diff --git a/share/txr/stdlib/doc-syms.tl b/share/txr/stdlib/doc-syms.tl index e92e8a4e..3f77d0d7 100644 --- a/share/txr/stdlib/doc-syms.tl +++ b/share/txr/stdlib/doc-syms.tl @@ -1330,6 +1330,7 @@ ("parenb" "N-01B1B5DF") ("parmrk" "N-02391683") ("parodd" "N-01B1B5DF") + ("parse-errors" "N-00F843D4") ("partition" "N-0142889E") ("partition*" "N-03951D7A") ("partition-by" "N-000167DF") diff --git a/txr.1 b/txr.1 index 12eae868..39afba58 100644 --- a/txr.1 +++ b/txr.1 @@ -56632,6 +56632,48 @@ it may be useful to set true in order to obtain better diagnostics. However, source location recording incurs a performance and storage penalty. +.coNP Function @ parse-errors +.synb +.mets (parse-errors << stream ) +.syne +.desc +The +.code parse-errors +function retrieves information, from a +.metn stream , +pertaining to the status of the most recent parsing operation performed +on that stream: namely, a previous call to +.codn read , +.code iread +or +.codn get-json . + +If the +.meta stream +object has not been used for parsing, or else the most recent +parsing operation did not encounter errors, then +.code parse-errors +returns +.codn nil . + +If the most recent parsing operation on +.meta stream +encountered errors, then +.code parse-errors +function returns a positive integer value indicating the error count. +Otherwise it returns +.codn nil . + +If a parse error operation encounters a syntax error before obtaining any token +from the stream, then the error count is zero and +.code parse-errors +returns +.codn nil . +Consequently, +.code parse-errors +may be used after a failed parse operation to distinguish a true +syntax error from an end-of-stream condition. + .coNP Function @ record-adapter .synb .mets (record-adapter < regex >> [ stream <> [ include-match ]]) -- cgit v1.2.3