7 PARSER_BEGIN(DotParser)
8 package org.simantics.graphviz.internal.parser;
10 import org.simantics.graphviz.*;
12 import java.util.HashMap;
14 public class DotParser {
15 AbstractAttributeContainer defaultNode = new AbstractAttributeContainer();
16 AbstractAttributeContainer defaultEdge = new AbstractAttributeContainer();
17 Map<String,Identifiable> connectors = new HashMap<String,Identifiable>();
22 /*** Lexer *********************************************************/
25 { <WHITESPACE: " " | "\n" | "\r" | "\t" >
26 | <COMMENT1: "/*" (~["*"] | "*" ~["/"])* "*/" >
27 | <COMMENT2: "//" (~["\n"])* >
31 { "=" | "->" | "--" | ":" | "{" | "}" | "[" | "]" | ","
32 | "subgraph" | "digraph" | "graph" | "node" | "edge" | "strict" | ";"
33 | <STRING: "\"" (~["\"", "\\"] | "\\" ~[])* "\"">
34 { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); }
35 | <HTML: "<" (~["<",">"] | "<" (~["<",">","\""] | "\"" (~["\"", "\\"] | "\\" ~[])* "\"")* ">" )* ">">
36 | <IDENT: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","_",":","0"-"9"])* >
37 | <NUMBER: ("-")?("." (["0"-"9"])+ |(["0"-"9"])+ ("." (["0"-"9"])*)?) >
40 /*** Parser ********************************************************/
42 Graph document() : { Graph graph = new Graph(); }
43 { ("strict")? ("graph" | "digraph") "{" (statement(graph))* "}" <EOF> { return graph; }
46 void statement(IGraph graph) : {}
47 { { String id1, id2; }
48 id1=id() ( LOOKAHEAD(1)
49 { Node node = new Node(graph); connectors.put(id1, node); } attributes(node)
51 Edge edge = new Edge(connectors.get(id1.split(":")[0]),
52 connectors.get(id2.split(":")[0]));
55 | "graph" attributes(graph)
56 | "node" attributes(defaultNode)
57 | "edge" attributes(defaultEdge)
58 | { Subgraph subgraph; String id; }
59 "subgraph" id=id() { subgraph = new Subgraph(graph); connectors.put(id, subgraph); }
60 "{" (statement(subgraph))* "}"
63 void attributes(IAttributeContainer el) : {}
64 { ("[" attribute(el) ( (",")? attribute(el) )* "]")? ";" {}
67 void attribute(IAttributeContainer el) : { String key, value=""; }
68 { key=id() ("=" value=id())? { el.set(key, value); }
72 { <NUMBER> { return token.image; }
73 | <STRING> { return token.image; }
74 | <IDENT> { return token.image; }
75 | <HTML> { return token.image; }