]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/parser/DotParser.jj
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / internal / parser / DotParser.jj
1 options {\r
2   JDK_VERSION = "1.6";\r
3   STATIC = false;\r
4   IGNORE_CASE = true;\r
5 }\r
6 \r
7 PARSER_BEGIN(DotParser)\r
8 package org.simantics.graphviz.internal.parser;\r
9 \r
10 import org.simantics.graphviz.*;\r
11 import java.util.Map;\r
12 import java.util.HashMap;\r
13 \r
14 public class DotParser {\r
15         AbstractAttributeContainer defaultNode = new AbstractAttributeContainer();\r
16         AbstractAttributeContainer defaultEdge = new AbstractAttributeContainer();\r
17         Map<String,Identifiable> connectors = new HashMap<String,Identifiable>();\r
18         \r
19 }\r
20 PARSER_END(DotParser)\r
21 \r
22 /*** Lexer *********************************************************/\r
23 \r
24 SKIP:\r
25 { <WHITESPACE: " " | "\n" | "\r" | "\t" > \r
26 | <COMMENT1: "/*" (~["*"] | "*" ~["/"])* "*/" >  \r
27 | <COMMENT2: "//" (~["\n"])* >\r
28 }\r
29 \r
30 TOKEN:\r
31 { "=" | "->" | "--" | ":" | "{" | "}" | "[" | "]" | "," \r
32 | "subgraph" | "digraph" | "graph" | "node" | "edge" | "strict" | ";"\r
33 | <STRING: "\"" (~["\"", "\\"] | "\\" ~[])* "\"">\r
34     { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); }\r
35 | <HTML: "<" (~["<",">"] | "<" (~["<",">","\""] | "\"" (~["\"", "\\"] | "\\" ~[])* "\"")* ">" )* ">">\r
36 | <IDENT: ["a"-"z","A"-"Z","_"] (["a"-"z","A"-"Z","_",":","0"-"9"])* >\r
37 | <NUMBER: ("-")?("." (["0"-"9"])+ |(["0"-"9"])+ ("." (["0"-"9"])*)?) >\r
38 }\r
39 \r
40 /*** Parser ********************************************************/\r
41 \r
42 Graph document() : { Graph graph = new Graph(); }\r
43 { ("strict")? ("graph" | "digraph") "{" (statement(graph))* "}" <EOF> { return graph; }\r
44 }\r
45 \r
46 void statement(IGraph graph) : {}\r
47 { { String id1, id2; }\r
48   id1=id() ( LOOKAHEAD(1) \r
49     { Node node = new Node(graph); connectors.put(id1, node); } attributes(node)   \r
50   | "->" id2=id() { \r
51         Edge edge = new Edge(connectors.get(id1.split(":")[0]), \r
52                                                  connectors.get(id2.split(":")[0])); \r
53         } attributes(edge)\r
54   ) \r
55 | "graph" attributes(graph)\r
56 | "node" attributes(defaultNode)\r
57 | "edge" attributes(defaultEdge)\r
58 | { Subgraph subgraph; String id; } \r
59   "subgraph" id=id() { subgraph = new Subgraph(graph); connectors.put(id, subgraph); } \r
60   "{" (statement(subgraph))* "}"\r
61 }\r
62 \r
63 void attributes(IAttributeContainer el) : {}\r
64 { ("[" attribute(el) ( (",")? attribute(el) )* "]")? ";" {}\r
65 }\r
66 \r
67 void attribute(IAttributeContainer el) : { String key, value=""; }\r
68 { key=id() ("=" value=id())? { el.set(key, value); }\r
69 }\r
70 \r
71 String id() : {}\r
72 { <NUMBER> { return token.image; }\r
73 | <STRING> { return token.image; }\r
74 | <IDENT> { return token.image; }\r
75 | <HTML> { return token.image; }\r
76 }