]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/parser/DotParser.jj
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / internal / parser / DotParser.jj
1 options {
2   JDK_VERSION = "1.6";
3   STATIC = false;
4   IGNORE_CASE = true;
5 }
6
7 PARSER_BEGIN(DotParser)
8 package org.simantics.graphviz.internal.parser;
9
10 import org.simantics.graphviz.*;
11 import java.util.Map;
12 import java.util.HashMap;
13
14 public class DotParser {
15         AbstractAttributeContainer defaultNode = new AbstractAttributeContainer();
16         AbstractAttributeContainer defaultEdge = new AbstractAttributeContainer();
17         Map<String,Identifiable> connectors = new HashMap<String,Identifiable>();
18         
19 }
20 PARSER_END(DotParser)
21
22 /*** Lexer *********************************************************/
23
24 SKIP:
25 { <WHITESPACE: " " | "\n" | "\r" | "\t" > 
26 | <COMMENT1: "/*" (~["*"] | "*" ~["/"])* "*/" >  
27 | <COMMENT2: "//" (~["\n"])* >
28 }
29
30 TOKEN:
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"])*)?) >
38 }
39
40 /*** Parser ********************************************************/
41
42 Graph document() : { Graph graph = new Graph(); }
43 { ("strict")? ("graph" | "digraph") "{" (statement(graph))* "}" <EOF> { return graph; }
44 }
45
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)   
50   | "->" id2=id() { 
51         Edge edge = new Edge(connectors.get(id1.split(":")[0]), 
52                                                  connectors.get(id2.split(":")[0])); 
53         } attributes(edge)
54   ) 
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))* "}"
61 }
62
63 void attributes(IAttributeContainer el) : {}
64 { ("[" attribute(el) ( (",")? attribute(el) )* "]")? ";" {}
65 }
66
67 void attribute(IAttributeContainer el) : { String key, value=""; }
68 { key=id() ("=" value=id())? { el.set(key, value); }
69 }
70
71 String id() : {}
72 { <NUMBER> { return token.image; }
73 | <STRING> { return token.image; }
74 | <IDENT> { return token.image; }
75 | <HTML> { return token.image; }
76 }