options { STATIC = false; JAVA_UNICODE_ESCAPE = true; } PARSER_BEGIN(Parser) package uml2text; import java.awt.*; import java.io.*; import java.util.*; import java.lang.Math ; import demeter.*; public class Parser { // oit is uugly. Why isn't there a Character.valueOf(String)? static char unescapifyChar(String s) { char c = s.charAt(0); if (c == '\\') { switch (s.charAt(1)) { case 'n': c = '\n'; break; case 't': c = '\t'; break; case 'b': c = '\b'; break; case 'r': c = '\r'; break; case 'f': c = '\f'; break; case '\\': c = '\\'; break; case '\'': c = '\''; break; case '\"': c = '\"'; break; default: c = (char) Integer.parseInt(s.substring(2, s.length()), 8); break; } } return c; } // Even uglier... static String unescapify(String s) { char str[] = new char[s.length()]; int i = 0, o = 0; while (i < s.length()) { char c = s.charAt(i++); if (c == '\\') { int j = i + 1; while (j < s.length() && Character.digit(s.charAt(j), 8) != -1) { j++; } c = unescapifyChar(s.substring(i-1, j)); i = j; } str[o++] = c; } return String.valueOf(str, 0, o); } } PARSER_END(Parser) UMLGraph _UMLGraph() : { UMLGraph it; UMLVertex_List _vertices; UEdge_List _edges; } { { it=new UMLGraph(); } "#Class" "#Dictionary" "#Graph" _vertices=_UMLVertex_List() { it.set_vertices(_vertices); } _edges=_UEdge_List() { it.set_edges(_edges); } { return it; } } UMLVertex_List _UMLVertex_List() : { UMLVertex_List it; Nonempty_UMLVertex_List _first; } { { it=new UMLVertex_List(); } "#Vertex" "#List" [ _first=_Nonempty_UMLVertex_List() { it.set_first(_first); } ] { return it; } } UMLVertex _UMLVertex() : { UMLVertex it; UUID _vid; UMLVertexName _vertexname; UMLCoordinates _position; IEdge_List _incoming; OEdge_List _outgoing; Decorator _vdeco; } { ( it=_UMLAltVertex() | it=_UMLConstVertex() | it=_UMLTerm() ) _vid=_UUID() { it.set_vid(_vid); } _vertexname=_UMLVertexName() { it.set_vertexname(_vertexname); } _position=_UMLCoordinates() { it.set_position(_position); } [ _incoming=_IEdge_List() { it.set_incoming(_incoming); } ] [ _outgoing=_OEdge_List() { it.set_outgoing(_outgoing); } ] [ _vdeco=_Decorator() { it.set_vdeco(_vdeco); } ] { return it; } } IEdge_List _IEdge_List() : { IEdge_List it; Nonempty_IEdge_List _first; } { { it=new IEdge_List(); } "#Incoming" [ _first=_Nonempty_IEdge_List() { it.set_first(_first); } ] { return it; } } OEdge_List _OEdge_List() : { OEdge_List it; Nonempty_OEdge_List _first; } { { it=new OEdge_List(); } "#Outgoing" [ _first=_Nonempty_OEdge_List() { it.set_first(_first); } ] { return it; } } UMLConstVertex _UMLConstVertex() : { UMLConstVertex it; } { { it=new UMLConstVertex(); } "#ConstVertex" { return it; } } UMLAltVertex _UMLAltVertex() : { UMLAltVertex it; } { { it=new UMLAltVertex(); } "#AltVertex" { return it; } } UMLTerm _UMLTerm() : { UMLTerm it; } { { it=new UMLTerm(); } "#TermVertex" { return it; } } UEdge_List _UEdge_List() : { UEdge_List it; Nonempty_UEdge_List _first; } { { it=new UEdge_List(); } "#Edge" "#List" [ _first=_Nonempty_UEdge_List() { it.set_first(_first); } ] { return it; } } UMLEdge _UMLEdge() : { UMLEdge it; UUID _eid; Decorator _edeco; UUID _fromVertex; UUID _toVertex; } { ( it=_UMLAltEdge() | it=_UMLConstEdge() ) _eid=_UUID() { it.set_eid(_eid); } [ _edeco=_Decorator() { it.set_edeco(_edeco); } ] _fromVertex=_UUID() { it.set_fromVertex(_fromVertex); } _toVertex=_UUID() { it.set_toVertex(_toVertex); } { return it; } } UMLAltEdge _UMLAltEdge() : { UMLAltEdge it; } { { it=new UMLAltEdge(); } "#AltEdge" { return it; } } UMLConstEdge _UMLConstEdge() : { UMLConstEdge it; UMLEdgeName _edgename; UMLCardinality _card; } { { it=new UMLConstEdge(); } "#ConstEdge" _edgename=_UMLEdgeName() { it.set_edgename(_edgename); } _card=_UMLCardinality() { it.set_card(_card); } { return it; } } UMLCardinality _UMLCardinality() : { UMLCardinality it; UMLLower _lower; UMLUpper _upper; } { { it=new UMLCardinality(); } _lower=_UMLLower() { it.set_lower(_lower); } [ _upper=_UMLUpper() { it.set_upper(_upper); } ] { return it; } } UMLLower _UMLLower() : { UMLLower it; Integer _l; } { { it=new UMLLower(); } _l=_Integer() { it.set_l(_l); } { return it; } } UMLUpper _UMLUpper() : { UMLUpper it; String _u; } { { it=new UMLUpper(); } "." "." _u=_String() { it.set_u(_u); } { return it; } } UUID _UUID() : { UUID it; Integer _id; } { { it=new UUID(); } _id=_Integer() { it.set_id(_id); } { return it; } } UMLEdgeName _UMLEdgeName() : { UMLEdgeName it; Ident _name; } { { it=new UMLEdgeName(); } _name=_Ident() { it.set_name(_name); } { return it; } } UMLVertexName _UMLVertexName() : { UMLVertexName it; Ident _name; } { { it=new UMLVertexName(); } _name=_Ident() { it.set_name(_name); } { return it; } } UMLCoordinates _UMLCoordinates() : { UMLCoordinates it; XX _x; YY _y; } { { it=new UMLCoordinates(); } "{" _x=_XX() { it.set_x(_x); } _y=_YY() { it.set_y(_y); } "}" { return it; } } XX _XX() : { XX it; Integer _x; } { { it=new XX(); } _x=_Integer() { it.set_x(_x); } { return it; } } YY _YY() : { YY it; Integer _y; } { { it=new YY(); } _y=_Integer() { it.set_y(_y); } { return it; } } Decorator _Decorator() : { Decorator it; TravTag _tag; Attributes_PList _attributes_plist; } { { it=new Decorator(); } _tag=_TravTag() { it.set_tag(_tag); } _attributes_plist=_Attributes_PList() { it.set_attributes_plist(_attributes_plist); } { return it; } } Attributes _Attributes() : { Attributes it; Integer _integer; } { { it=new Attributes(); } _integer=_Integer() { it.set_integer(_integer); } { return it; } } TravTag _TravTag() : { TravTag it; Integer _integer; } { { it=new TravTag(); } "travstat" _integer=_Integer() { it.set_integer(_integer); } { return it; } } IndependantTraversal _IndependantTraversal() : { IndependantTraversal it; UUID _sourceid; UUID _destid; UUID_List _bypassings; } { { it=new IndependantTraversal(); } _sourceid=_UUID() { it.set_sourceid(_sourceid); } _destid=_UUID() { it.set_destid(_destid); } _bypassings=_UUID_List() { it.set_bypassings(_bypassings); } { return it; } } UniqueIDGenerator _UniqueIDGenerator() : { UniqueIDGenerator it; } { { it=new UniqueIDGenerator(); } { return it; } } ReadVertexVisitor _ReadVertexVisitor() : { ReadVertexVisitor it; UMLGraph _thegraph; String _graphstring; Integer _lookupvid; } { { it=new ReadVertexVisitor(); } _thegraph=_UMLGraph() { it.set_thegraph(_thegraph); } _graphstring=_String() { it.set_graphstring(_graphstring); } _lookupvid=_Integer() { it.set_lookupvid(_lookupvid); } { return it; } } ReadEdgeVisitor _ReadEdgeVisitor() : { ReadEdgeVisitor it; UMLGraph _thegraph; String _graphstring; String _commonstring; Integer _lookupvid; String _fromtype; Integer _pipeswitch; } { { it=new ReadEdgeVisitor(); } _thegraph=_UMLGraph() { it.set_thegraph(_thegraph); } _graphstring=_String() { it.set_graphstring(_graphstring); } _commonstring=_String() { it.set_commonstring(_commonstring); } _lookupvid=_Integer() { it.set_lookupvid(_lookupvid); } _fromtype=_String() { it.set_fromtype(_fromtype); } _pipeswitch=_Integer() { it.set_pipeswitch(_pipeswitch); } { return it; } } Main _Main() : { Main it; } { { it=new Main(); } { return it; } } Attributes_PList _Attributes_PList() : { Attributes_PList it; Nonempty_Attributes_PList _first; } { { it=new Attributes_PList(); } "(" [ _first=_Nonempty_Attributes_PList() { it.set_first(_first); } ] ")" { return it; } } UUID_List _UUID_List() : { UUID_List it; Nonempty_UUID_List _first; } { { it=new UUID_List(); } [ _first=_Nonempty_UUID_List() { it.set_first(_first); } ] { return it; } } Nonempty_UMLVertex_List _Nonempty_UMLVertex_List() : { Nonempty_UMLVertex_List it; UMLVertex _it; Nonempty_UMLVertex_List _next; } { { it=new Nonempty_UMLVertex_List(); } _it=_UMLVertex() { it.set_it(_it); } [ _next=_Nonempty_UMLVertex_List() { it.set_next(_next); } ] { return it; } } Nonempty_IEdge_List _Nonempty_IEdge_List() : { Nonempty_IEdge_List it; UUID _it; Nonempty_IEdge_List _next; } { { it=new Nonempty_IEdge_List(); } _it=_UUID() { it.set_it(_it); } [ _next=_Nonempty_IEdge_List() { it.set_next(_next); } ] { return it; } } Nonempty_OEdge_List _Nonempty_OEdge_List() : { Nonempty_OEdge_List it; UUID _it; Nonempty_OEdge_List _next; } { { it=new Nonempty_OEdge_List(); } _it=_UUID() { it.set_it(_it); } [ _next=_Nonempty_OEdge_List() { it.set_next(_next); } ] { return it; } } Nonempty_UEdge_List _Nonempty_UEdge_List() : { Nonempty_UEdge_List it; UMLEdge _it; Nonempty_UEdge_List _next; } { { it=new Nonempty_UEdge_List(); } _it=_UMLEdge() { it.set_it(_it); } [ _next=_Nonempty_UEdge_List() { it.set_next(_next); } ] { return it; } } Nonempty_Attributes_PList _Nonempty_Attributes_PList() : { Nonempty_Attributes_PList it; Attributes _it; Nonempty_Attributes_PList _next; } { { it=new Nonempty_Attributes_PList(); } _it=_Attributes() { it.set_it(_it); } [ _next=_Nonempty_Attributes_PList() { it.set_next(_next); } ] { return it; } } Nonempty_UUID_List _Nonempty_UUID_List() : { Nonempty_UUID_List it; UUID _it; Nonempty_UUID_List _next; } { { it=new Nonempty_UUID_List(); } _it=_UUID() { it.set_it(_it); } [ _next=_Nonempty_UUID_List() { it.set_next(_next); } ] { return it; } } boolean _boolean() : { Token t; } { ( t= { return true; } | t= { return false; } ) } char _char() : { Token t; } { t= { String s = t.image; return unescapifyChar(s.substring(1, s.length()-1)); } } byte _byte() : { int i; } { i=_int() { return (byte) i; } } short _short() : { int i; } { i=_int() { return (short) i; } } int _int() : { Number num; } { num=_Number() { return num.intValue(); } } long _long() : { Number num; } { num=_Number() { return num.longValue(); } } float _float() : { Number num; } { num=_Number() { return num.floatValue(); } } double _double() : { Number num; } { num=_Number() { return num.doubleValue(); } } Boolean _Boolean() : { Token t; } { ( t= { return Boolean.TRUE; } | t= { return Boolean.FALSE; } ) } Character _Character() : { char c; } { c=_char() { return new Character(c); } } Integer _Integer() : { int i; } { i = _int() { return new Integer(i); } } Long _Long() : { long l; } { l=_long() { return new Long(l); } } Float _Float() : { float f; } { f=_float() { return new Float(f); } } Double _Double() : { double d; } { d=_double() { return new Double(d); } } Number _Number() : { Token t; String s; int radix; Number num; } { ( ( t= { s = t.image; radix = 10; } | t= { // Strip off the "0x". s = t.image.substring(2, t.image.length()); radix = 16; } | t= { s = t.image; radix = 8; } ) { switch (s.charAt(s.length()-1)) { case 'l': case 'L': num = Long.valueOf(s.substring(0, s.length()-1), radix); break; default: num = Integer.valueOf(s, radix); break; } } | t= { s = t.image; switch (s.charAt(s.length()-1)) { case 'd': case 'D': num = Double.valueOf(s.substring(0, s.length()-1)); break; case 'f': case 'F': num = Float.valueOf(s.substring(0, s.length()-1)); break; default: num = Float.valueOf(s); break; } } ) { return num; } } String _String() : { Token t; } { t= { String s = t.image; return unescapify(s.substring(1, s.length()-1)); } } StringBuffer _StringBuffer() : { String s; } { s=_String() { return new StringBuffer(s); } } Ident _Ident() : { Token t; } { t= { return new Ident(t.image); } } Text _Text() : { Token t; } { t= { String s = t.image; return new Text(s.substring(2, s.length()-2)); } } // Lexical specification (largely taken from Java.jack): IGNORE_IN_BNF : {} { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r\n")> | <"/*" (~["*"])* "*" (~["/"] (~["*"])* "*")* "/"> } TOKEN : /* LITERALS */ {} { < DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* (["l","L"])? > | < HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ (["l","L"])? > | < OCTAL_LITERAL: "0" (["0"-"7"])* (["l","L"])? > | < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])+ ()? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < CHARACTER_LITERAL: "'" ( (~["'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) ) "'" > | < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" > | < TEXT_LITERAL: "(@" (~["@"])* ( "@" ~[")"] (~["@"])* )* "@)" > | < TRUE: "true" > | < FALSE: "false" > } TOKEN : /* IDENTIFIERS */ {} { < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > }