Skip to content

Conversation

@exlee
Copy link

@exlee exlee commented Jan 21, 2026

  • Split run_query into run_query and run_query_impl (shared logic)
  • Add run_query_safe that returns QueryState or parser::ast::ParserError
  • Expose ParserError in lib
  • Add documentation string for ParserError and associated methods

Copy link
Contributor

@Skgland Skgland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am doing a similar thing1 as part of #3145.
You might want to have a look at what I am doing there.

Also left a two comments regarding the name choice and code duplication.

Other than that I think propagating the error instead of panic-ing is a desirable improvement.

Footnotes

  1. See https://github.com/mthom/scryer-prolog/pull/3145/changes#diff-72c8cab85361f6b46e74c79156bcd786ba5256e250bbe566b3a53e127fe2c17dR563-R591

}

/// Runs query after checking parse errors.
pub fn run_query_safe(

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about the same thing but decided to go with gut feeling. It's not a difficult change :)

Comment on lines 601 to 610
@@ -591,7 +607,10 @@ impl Machine {
let term = parser
.read_term(&op_dir, Tokens::Default)
.expect("Failed to parse query");
self.run_query_impl(term)

This comment was marked as resolved.

/// Enum for parsing errors
#[allow(dead_code)]
#[derive(Debug)]
pub enum ParserError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this already publicly reachable?1 Otherwise we might want to make this #[non_exhaustive] so that we can add new variants without that being a breaking change.

Footnotes

  1. While it was already declared pub it might not have been reachable until the pub use was added in src/machine/lib_machine/mod.rs above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know my change exposed it . Makes sense to add #[non_exhaustive], I'll add it to the PR.

Copy link
Contributor

@Skgland Skgland Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mh, looking at the error variants some more a lot of them have two usize that appear to be a line column number pair.

Maybe it would be good to introduce a Span type e.g.

struct Span {
 line: usize,
 column: usize
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though span would be a range of locations and we only have one, so it should be rather Location instead of Span.

/// Unexpected character found.
UnexpectedChar(char, usize, usize),
// UnexpectedEOF,
/// UTF8 error found.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These additional comments seem to mostly repeat the types? I would suggest leaving this out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the missing_docs lint is set to deny so either this documentation or an explicit override i.e. #[allow(missing_docs)] on the enum is required now that this type is reachable.

#![deny(missing_docs)]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK, thank you, I was already wondering why this is suddenly part of the PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree they're superfluous but public enums seems to need to have documentation and I'd rather put them in than mess with the linter requirements.

@exlee
Copy link
Author

exlee commented Jan 23, 2026

Just to check in, is there anything that should still be done on this PR?

From what I understand there are comments, but I'm not sure if they should be implemented or not.

@Skgland
Copy link
Contributor

Skgland commented Jan 23, 2026

Just to check in, is there anything that should still be done on this PR?

From what I understand there are comments, but I'm not sure if they should be implemented or not.

I think leaving the documentation as-is is fine as it can be changed at any point.
The more I look at the ParseError type the more I want to completely overhaul it first (i.e. before this PR makes it part of the public API, sorry).

The following isn't really directed at this PR but more a collection of my thought of what I think would be good to change about the ParseError type

  • use of tuple variants, struct variant would allow giving the fields a meaningful name

  • all but three variants contain a location

    • makes me think maybe this should be changed to
      struct ParseError{
          location: Option<Location>, 
          kind: ParseErrorKind
      }
      with ParseErrorKind being the current ParseError enum but with the location moved into the struct.
    • why don't we have a location for InvalidSingleQuotedCharacter or LexicalError?
  • variant LexicalError is exposing a type of a dependency which is often not a good idea

    • am I remembering correctly that lexical was the library we use for integer/float parsing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants