options { JDK_VERSION = "1.6"; STATIC = false; IGNORE_CASE = true; } PARSER_BEGIN(DotParser) package org.simantics.graphviz.internal.parser; import org.simantics.graphviz.*; import java.util.Map; import java.util.HashMap; public class DotParser { AbstractAttributeContainer defaultNode = new AbstractAttributeContainer(); AbstractAttributeContainer defaultEdge = new AbstractAttributeContainer(); Map connectors = new HashMap(); } PARSER_END(DotParser) /*** Lexer *********************************************************/ SKIP: { | | } TOKEN: { "=" | "->" | "--" | ":" | "{" | "}" | "[" | "]" | "," | "subgraph" | "digraph" | "graph" | "node" | "edge" | "strict" | ";" | { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); } | "] | "<" (~["<",">","\""] | "\"" (~["\"", "\\"] | "\\" ~[])* "\"")* ">" )* ">"> | | } /*** Parser ********************************************************/ Graph document() : { Graph graph = new Graph(); } { ("strict")? ("graph" | "digraph") "{" (statement(graph))* "}" { return graph; } } void statement(IGraph graph) : {} { { String id1, id2; } id1=id() ( LOOKAHEAD(1) { Node node = new Node(graph); connectors.put(id1, node); } attributes(node) | "->" id2=id() { Edge edge = new Edge(connectors.get(id1.split(":")[0]), connectors.get(id2.split(":")[0])); } attributes(edge) ) | "graph" attributes(graph) | "node" attributes(defaultNode) | "edge" attributes(defaultEdge) | { Subgraph subgraph; String id; } "subgraph" id=id() { subgraph = new Subgraph(graph); connectors.put(id, subgraph); } "{" (statement(subgraph))* "}" } void attributes(IAttributeContainer el) : {} { ("[" attribute(el) ( (",")? attribute(el) )* "]")? ";" {} } void attribute(IAttributeContainer el) : { String key, value=""; } { key=id() ("=" value=id())? { el.set(key, value); } } String id() : {} { { return token.image; } | { return token.image; } | { return token.image; } | { return token.image; } }