1
3
Fork
You've already forked dot-parser
3

Global graph attributes need to be specified inside of a graph scope #10

Closed
opened 2025年07月28日 15:31:10 +02:00 by lcnbr · 5 comments

In the dot format one can specify global graph data without wrapping it in a graph scope. Id like to be able to write:

graph { A -> subgraph { B C } 
foo=bar
}
graph { A -> subgraph { B C } 
graph[
foo=bar
]
}
In the dot format one can specify global graph data without wrapping it in a graph scope. Id like to be able to write: ```dot graph { A -> subgraph { B C } foo=bar } ``` ```dot graph { A -> subgraph { B C } graph[ foo=bar ] } ```

Both are valid dot string and both are accepted by the parser. However, there is slight difference between both of them. In the first one, the foo=bar part is an ID '=' ID statement (a stmt in dot grammar), while in the second, the graph[foo=bar] part is an attribute statement (an attr_stmt in the grammar).

You can check both ASTs with the following:

usedot_parser::*;fn main(){letgraph_1=ast::Graph::try_from("graph { foo=bar }").unwrap();letgraph_2=ast::Graph::try_from("graph { graph[foo=bar] }").unwrap();println!("{:#?}",graph_1);println!("{:#?}",graph_2);}

which outputs

Graph {
 strict: false,
 is_digraph: false,
 name: None,
 stmts: StmtList {
 stmts: [
 IDEq(
 "foo",
 "bar",
 ),
 ],
 },
}
Graph {
 strict: false,
 is_digraph: false,
 name: None,
 stmts: StmtList {
 stmts: [
 AttrStmt(
 Graph(
 AttrList {
 elems: [
 AList {
 elems: [
 (
 "foo",
 "bar",
 ),
 ],
 },
 ],
 },
 ),
 ),
 ],
 },
}

Note that, in canonical representation of graphs, the first one puts the equality in the ideqs field of the graph, while the second puts it in the attr field.

Both are valid dot string and both are accepted by the parser. However, there is slight difference between both of them. In the first one, the `foo=bar` part is an `ID '=' ID` statement (a `stmt` in dot grammar), while in the second, the `graph[foo=bar]` part is an attribute statement (an `attr_stmt` in the grammar). You can check both ASTs with the following: ```rust use dot_parser::*; fn main() { let graph_1 = ast::Graph::try_from( "graph { foo=bar }" ).unwrap(); let graph_2 = ast::Graph::try_from( "graph { graph[foo=bar] }" ).unwrap(); println!("{:#?}", graph_1); println!("{:#?}", graph_2); } ``` which outputs ``` Graph { strict: false, is_digraph: false, name: None, stmts: StmtList { stmts: [ IDEq( "foo", "bar", ), ], }, } Graph { strict: false, is_digraph: false, name: None, stmts: StmtList { stmts: [ AttrStmt( Graph( AttrList { elems: [ AList { elems: [ ( "foo", "bar", ), ], }, ], }, ), ), ], }, } ``` Note that, in canonical representation of graphs, the first one puts the equality in the `ideqs` field of the graph, while the second puts it in the `attr` field.
Author
Copy link

I see! Thanks for the explanation. Now on 0.6.0, I get an error parsing this graph:

dot_parser::ast::Graphs::try_from(r#"digraph{
 edge [overall_factor=1]
 A
 }
 "#).unwrap()

however if it is a bare stmt:

dot_parser::ast::Graphs::try_from(r#"digraph{
 overall_factor = 1
 A
 }
 "#).unwrap()

It does work. The error is

The Pair:
1
terminates early. Expected one of "ident1", "numeral", "quote", "html".

But 1 is a numeral. So this should be fine..

I see! Thanks for the explanation. Now on 0.6.0, I get an error parsing this graph: ```rust dot_parser::ast::Graphs::try_from( r#"digraph{ edge [overall_factor=1] A } "# ) .unwrap() ``` however if it is a bare stmt: ```rust dot_parser::ast::Graphs::try_from( r#"digraph{ overall_factor = 1 A } "# ) .unwrap() ``` It does work. The error is ``` The Pair: 1 terminates early. Expected one of "ident1", "numeral", "quote", "html". ``` But 1 is a numeral. So this should be fine..

Hi,
sorry I though I would have time to tackle that today, but I was extra-busy. I'll see if I can deal with it this week.

Hi, sorry I though I would have time to tackle that today, but I was extra-busy. I'll see if I can deal with it this week.

The above commit should fix the issue. Could you confirm it works on your side before I push an updated version on crates.io ?

(And sorry for the delay)

The above commit should fix the issue. Could you confirm it works on your side before I push an updated version on crates.io ? (And sorry for the delay)
Author
Copy link

It works on my side! Thanks for the update!

It works on my side! Thanks for the update!
Sign in to join this conversation.
No Branch/Tag specified
master
v0.2
v0.1
rust_ice
v0.1.3
v0.1.2
v0.1.1
v0.1.0
Labels
Clear labels
No items
No labels
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
bromind/dot-parser#10
Reference in a new issue
bromind/dot-parser
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?