Allow optional globals be omitted#156
Conversation
hendrikvanantwerpen
left a comment
There was a problem hiding this comment.
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::globalspassed toFile::execute, or it fails with anExecutionError(MissingGlobalVariable("MY_GLOBAL")). This change allows the global to be omitted: