detailed parser error reporting [wip - 1 example]

This commit is contained in:
Roland Conybeare 2025-07-19 21:09:57 -05:00
commit 65a83cd77c
19 changed files with 256 additions and 84 deletions

View file

@ -29,12 +29,14 @@ namespace xo {
* 2. top-level batch
* 3. nested
*
* @text
* 1 2 3
* +--------
* def | y y y
* symbol | y n n 1: evaluate as variable
* i64 | y n n 1: evaluate as constant
*
* @endtext
**/
class exprseq_xs : public exprstate {
public:

View file

@ -189,10 +189,10 @@ namespace xo {
void illegal_input_error(const char * self_name,
const token_type & tk) const;
/** throw exception when unable to locate definition for a variable
**/
/** capture error in @p *p_psm when unable to locate definition for a variable **/
void unknown_variable_error(const char * self_name,
const token_type & tk) const;
const token_type & tk,
parserstatemachine * p_psm) const;
protected:
/** explicit subtype: identifies derived class **/

View file

@ -7,6 +7,7 @@
#include "exprstatestack.hpp"
#include "envframestack.hpp"
#include "parser_result.hpp"
#include <stdexcept>
namespace xo {
@ -210,7 +211,7 @@ namespace xo {
* @return parsed expression, if @p tk completes an expression.
* otherwise nullptr
**/
rp<Expression> include_token(const token_type & tk);
const parser_result & include_token(const token_type & tk);
/** reset to starting parsing state.
* use this after encountering an error, to avoid cascade of
@ -240,6 +241,8 @@ namespace xo {
**/
envframestack env_stack_;
/** parser result state **/
parser_result result_;
}; /*parser*/
inline std::ostream &

View file

@ -0,0 +1,65 @@
/* file parser_result.hpp
*
* author: Roland Conybeare, Jul 2025
*/
#pragma once
#include "xo/expression/Expression.hpp"
namespace xo {
namespace scm {
enum parser_result_type {
/** no result yet (incomplete expression) **/
none,
/** emit expression **/
expression,
/** emit error **/
error
};
struct parser_result {
using Expression = xo::ast::Expression;
public:
parser_result() = default;
parser_result(parser_result_type type, rp<Expression> expr, const char * src_fn, std::string errmsg);
static parser_result none();
static parser_result error(const char * error_src_function,
std::string errmsg);
static parser_result expression(rp<Expression> expr);
bool is_none() const { return result_type_ == parser_result_type::none; }
bool is_expression() const { return result_type_ == parser_result_type::expression; }
bool is_error() const { return result_type_ == parser_result_type::error; }
parser_result_type result_type() const { return result_type_; }
const rp<Expression> & result_expr() const { return result_expr_; }
const char * error_src_function() const { return error_src_function_; }
const std::string & error_description() const { return error_description_; }
public:
/** none|expression|error_description
*
* @text
* result_type | error_src_function | error_description
* -------------+--------------------+-------------------
* none | nullptr | empty
* expression | nullptr | empty
* error | non-null | non-empty
* @endtext
**/
parser_result_type result_type_ = parser_result_type::none;
/** if @ref result_state is parser_result_state::expression -> non-null result expression **/
rp<Expression> result_expr_;
/** if @ref result_state is parser_result_state::error -> non-null source function **/
const char * error_src_function_ = nullptr;
/** if @ref result_state is parser_result_state::error -> non-empty error description **/
std::string error_description_;
};
} /*namespace scm*/
} /*namespace xo*/
/* end parser_result.hpp */

View file

@ -7,6 +7,7 @@
#include "exprstate.hpp"
#include "envframestack.hpp"
#include "parser_result.hpp"
namespace xo {
namespace scm {
@ -27,10 +28,17 @@ namespace xo {
public:
parserstatemachine(exprstatestack * p_stack,
envframestack * p_env_stack,
rp<Expression> * p_emit_expr)
parser_result * p_result)
: p_stack_{p_stack},
p_env_stack_{p_env_stack},
p_emit_expr_{p_emit_expr} {}
p_result_{p_result}
{}
//const parser_result & result() const { return result_; }
//parser_result_state result_state() const { return result_state_; }
//const rp<Expression> & result_expr() const { return result_expr_; }
//const char * error_src_function() const { return error_src_function_; }
//const std::string & error_description() const { return error_description_; }
std::unique_ptr<exprstate> pop_exprstate();
exprstate & top_exprstate();
@ -57,13 +65,19 @@ namespace xo {
void on_expr_with_semicolon(bp<Expression> expr);
void on_symbol(const std::string & symbol);
// ---- parsing inputs -----
// ----- parsing inputs -----
void on_semicolon_token(const token_type & tk);
void on_operator_token(const token_type & tk);
void on_leftbrace_token(const token_type & tk);
void on_rightbrace_token(const token_type & tk);
// ----- parsing error -----
/** @p self_name location (implementation function) where error detected
**/
void on_error(const char * self_name, std::string error_description);
/** write human-readable representation on @p os **/
void print(std::ostream & os) const;
@ -72,13 +86,11 @@ namespace xo {
* generally speaking, push when to start new work for nested content;
* pop when work complete
**/
exprstatestack * p_stack_;
exprstatestack * p_stack_ = nullptr;
/** stack of environment frames, one for each enclosing lambda **/
envframestack * p_env_stack_;
/** if non-null, store next non-nested complete expressions in
* *p_emit_expr
**/
rp<Expression> * p_emit_expr_;
envframestack * p_env_stack_ = nullptr;
/** parser result object **/
parser_result * p_result_ = nullptr;
};
inline std::ostream &

View file

@ -3,6 +3,8 @@
* Author: Roland Conybeare, Jul 2025
*/
#pragma once
#include "xo/tokenizer/tokenizer_error.hpp"
namespace xo {
@ -18,7 +20,7 @@ namespace xo {
* @
**/
reader_error(const char * src_function,
const char * error_description,
std::string error_description,
const input_state_type & input_state,
size_t error_pos)
: tk_error_{src_function, error_description, input_state, error_pos}