-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow optional globals be omitted #156
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's a need for custom logic here. Setting a default value will di the trick here:
global foo? = #null
I checked and see that this feature is missing from the reference documentation. I'll look into that.
Actually, that gives me a parsing error:
Gives:
Or:
Seems globals’ defaults must be strings? Edit: seems that way: tree-sitter-graph/src/parser.rs Line 326 in 68504d7
|
Okay, I see what's going on. I have an idea for a patch. It's not too involved, so I hope I get that done in the coming days somewhere. |
Note: Was writing this comment while you posted your last, so I didn’t see you had an idea for a patch already — FWIW I’ll leave this here as well. I was looking into the global default parsing, and that could be changed to support literals with a change like this (note the tree-sitter-graph/src/parser.rs Line 326 in 68504d7
- default = Some(self.parse_string()?);
+ if self.peek()? == '#' {
+ default = Some(match self.parse_literal()? {
+ ast::Expression::TrueLiteral => Value::Boolean(true),
+ ast::Expression::FalseLiteral => Value::Boolean(false),
+ ast::Expression::NullLiteral => {
+ if quantifier == CaptureQuantifier::ZeroOrOne {
+ Value::Null
+ } else {
+ todo!("handle unexpected null")
+ }
+ }
+ _ => unreachable!(),
+ });
+ } else {
+ default = Some(self.parse_string()?.into());
+ } Where I also changed Line 59 in 68504d7
/// A global variable
#[derive(Debug, Eq, PartialEq)]
pub struct Global {
/// The name of the global variable
pub name: Identifier,
/// The quantifier of the global variable
pub quantifier: CaptureQuantifier,
/// Default value
- pub default: Option<String>,
+ pub default: Option<Value>,
pub location: Location,
} I’d be happy to close this PR and open that one? Although I kind of feel it’d be nice to allow users to omit the |
Hi, @hendrikvanantwerpen! I just noticed this PR still open — did you manage to make any progress on the idea you last said you had? |
@bm-w Thanks for reminding me. I didn't (and don't) have time to do the patch I had in might. I think your proposal in #156 (comment) is good. I would suggest to keep it simple:
|
When defining an optional global, currently the library still requires that it be set in the
ExecutionConfig::globals
passed toFile::execute
, or it fails with anExecutionError(MissingGlobalVariable("MY_GLOBAL"))
. This change allows the global to be omitted: