org.graphstream.stream.file.pajek.PajekParser.jj Maven / Gradle / Ivy
options {
JDK_VERSION = "1.5";
STATIC = false;
LOOKAHEAD = 2;
IGNORE_CASE = true;
}
PARSER_BEGIN(PajekParser)
/*
* Copyright 2006 - 2012
* Julien Baudry
* Antoine Dutot
* Yoann Pigné
* Guilhelm Savin
*
* This file is part of GraphStream .
*
* GraphStream is a library whose purpose is to handle static or dynamic
* graph, create them from scratch, file or any source and display them.
*
* This program is free software distributed under the terms of two licenses, the
* CeCILL-C license that fits European law, and the GNU Lesser General Public
* License. You can use, modify and/ or redistribute the software under the terms
* of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
* URL or under the terms of the GNU LGPL as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
*/
package org.graphstream.stream.file.pajek;
import java.io.InputStream;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import org.graphstream.stream.file.FileSourcePajek;
import org.graphstream.util.parser.ParseException;
import org.graphstream.util.parser.Parser;
import org.graphstream.util.parser.SimpleCharStream;
import org.graphstream.util.parser.Token;
import org.graphstream.util.parser.TokenMgrError;
@SuppressWarnings("unused")
public class PajekParser implements Parser {
// The node count.
int nodeCount;
// If true we are parsing the vertices labels.
// False as soon as we encounter the arcs.
boolean inVertices = true;
// If true the list of edges is a edge list.
boolean edgeListMode = true;
PajekContext ctx;
public PajekParser(FileSourcePajek pajek, InputStream stream)
{
this(stream);
this.ctx = new PajekContext(pajek);
}
public PajekParser(FileSourcePajek pajek, Reader stream )
{
this(stream);
this.ctx = new PajekContext(pajek);
}
public void open() {
}
/**
* Closes the parser, closing the opened stream.
*/
public void close() throws IOException {
jj_input_stream.close();
}
}
PARSER_END(PajekParser)
/************************************************************************/
/* The lexer. */
/************************************************************************/
SKIP:
{ " "
| "\t"
}
//
// Base tokens.
//
TOKEN: { < EOL : (("\r")|("\n"))> }
TOKEN: { < #DIGIT : ["0"-"9"] > }
TOKEN: { < #HEXDIGIT : (["0"-"9","a"-"f","A"-"F"])> }
TOKEN: { < INT : ( "-" | "+" )? ( )+ > }
TOKEN: { < REAL : ( "-" | "+" )? ( )+ ( "." ()+ )?> }
TOKEN: { < STRING : (("\"" (~["\""])* "\"")|("'" (~["'"])* "'")) > }
//
// More base tokens that must appear at the end for precedence reasons.
//
TOKEN: { < NETWORK : "*network" > }
TOKEN: { < VERTICES : "*vertices" > }
TOKEN: { < ARCS : "*arcs" > }
TOKEN: { < EDGES : "*edges" > }
TOKEN: { < EDGESLIST : "*edgeslist" > }
TOKEN: { < ARCSLIST : "*arcslist" > }
TOKEN: { < MATRIX : "*matrix" > }
TOKEN: { < COMMENT : ( "%" (~["\r","\n"])* ()+ ) > }
TOKEN: { < ELLIPSE : "ellipse" > }
TOKEN: { < DIAMOND : "diamond" > }
TOKEN: { < CROSS : "cross" > }
TOKEN: { < BOX : "box" > }
TOKEN: { < TRIANGLE : "triangle" > }
TOKEN: { < EMPTY : "empty" > }
TOKEN: { < SIZE : ("s_size" | "size") > }
TOKEN: { < XFACT : "x_fact" > }
TOKEN: { < YFACT : "y_fact" > }
TOKEN: { < PHI : "phi" > }
TOKEN: { < R : "r" > }
TOKEN: { < Q : "q" > }
TOKEN: { < IC : "ic" > }
TOKEN: { < BC : "bc" > }
TOKEN: { < BW : "bw" > }
TOKEN: { < LC : "lc" > }
TOKEN: { < LA : "la" > }
TOKEN: { < LR : "lr" > }
TOKEN: { < LPHI : "lphi" > }
TOKEN: { < FOS : "fos" > }
TOKEN: { < FONT : "font" > }
TOKEN: { < C : "c" > }
TOKEN: { < P : "p" > }
TOKEN: { < W : "w" > }
TOKEN: { < S : "s" > }
TOKEN: { < A : "a" > }
TOKEN: { < AP : "ap" > }
TOKEN: { < L : "l" > }
TOKEN: { < LP : "lp" > }
TOKEN: { < H1 : "h1" > }
TOKEN: { < H2 : "h2" > }
TOKEN: { < K1 : "k1" > }
TOKEN: { < K2 : "k2" > }
TOKEN: { < A1 : "a1" > }
TOKEN: { < A2 : "a2" > }
TOKEN: { < B : "b" > }
TOKEN: { < KEY : ["A"-"Z","a"-"z","0"-"9","."](["A"-"Z","a"-"z","0"-"9","_","-",".","@","#",":"])*> }
/*****************************************************************/
/* The parser. */
/*****************************************************************/
/** Unused rule, call it to slurp in the whole file. */
void all():
{}
{
( network()
| vertices()
| edges()
|
|
)+
}
boolean next():
{
boolean ok = true;
Token k;
}
{
( network()
| vertices()
| edges()
| k= { System.err.printf("got comment %s%n", k.image); }
|
| { ok = false; }
)
{ return ok; }
}
void network():
{
Token k;
StringBuffer buf = new StringBuffer();
boolean first = true;
}
{
(
( k= { if(!first) buf.append(" "); buf.append(k.image.substring(1, k.image.length()-1)); first=false; }
| k= { if(!first) buf.append(" "); buf.append(k.image); first=false; }
)*
)
{ ctx.addGraphAttribute("ui.label", buf.toString()); }
EO()
}
// Vertices
void vertices():
{
Token n;
}
{
n= EO()
{
nodeCount = ctx.addNodes(n);
}
( vertex()
|
)*
}
void vertex():
{
Token id, k, x, y, z=null, sh;
String label=null, value;
NodeGraphics graphics = null;
}
{
id=
( ( k=validIdentifier() { label = k.image; } )
( ((x=|x=) (y=|y=) (z=|z=)? { ctx.addNodePosition(id.image, x, y, z); })
( (k= { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("shape", "circle", k); } )
| (k= { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("shape", "box", k); } )
| (k= { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("shape", "triangle", k); } )
| (k= { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("shape", "cross", k); } )
| (k= { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("shape", "diamond", k); } )
| (k= { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("shape", "circle", k); } )
)?
( (k= value=number() { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("s_size", value, k); } )
| (k= value=number() { } )
| (k= value=number() { } )
| (k= value=number() { } )
| (k= value=number() { } )
| (k= value=number() { } )
| (k= value=color() { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("ic", value, k); } )
| (k= value=color() { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("bc", value, k); } )
| (k= value=number() { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("bw", value, k); } )
| (k= value=color() { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("lc", value, k); } )
| (k= value=number() { } )
| (k= value=number() { } )
| (k= value=number() { } )
| (k= value=number() { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("fos", value, k); } )
| (k= value=keyOrString() { if(graphics==null) graphics = new NodeGraphics(); graphics.addKey("font", value, k); } )
)*
((k=validIdentifier()|k=) { System.err.printf("%d:%d: unparsed garbage in .net (%s)%n", k.beginLine, k.beginColumn, k.image); })*
)?
)?
EO()
{
if(label != null) {
if(label.startsWith("\"")) label = label.substring(1, label.length()-1);
ctx.addNodeLabel(id.image, label);
}
if(graphics != null )
ctx.addNodeGraphics(id.image, graphics);
}
}
// Edges and edge lists.
void edges():
{
}
{
( ( EO() { edgeListMode = false; inVertices = false; ctx.setDirected(true); } (edge()|)* )
| ( EO() { edgeListMode = false; inVertices = false; ctx.setDirected(false); } (edge()|)* )
| ( EO() { edgeListMode = true; inVertices = false; ctx.setDirected(true); } (edgeList()|)* )
| ( EO() { edgeListMode = true; inVertices = false; ctx.setDirected(false); } (edgeList()|)* )
| matrix()
|
)
}
void edge():
{
Token src, trg, more, weight, k, sh;
String id, value;
EdgeGraphics graphics = null;
}
{
src= trg= { id=ctx.addEdge(src.image, trg.image); }
(
(k=|k=) { ctx.addEdgeWeight(id, k); }
( ( k= value=keyOrString() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("p", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("w", value, k); } )
| ( k= value=color() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("c", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("s", value, k); } )
| ( k= (k=|k=) { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("a", k.image, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("ap", value, k); } )
| ( k= value=keyOrString() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("l", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("lp", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("lr", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("lphi", value, k); } )
| ( k= value=color() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("lc", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("la", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("fos", value, k); } )
| ( k= value=keyOrString() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("font", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("h1", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("h2", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("a1", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("a2", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("k1", value, k); } )
| ( k= value=number() { if(graphics==null) graphics = new EdgeGraphics(); graphics.addKey("k2", value, k); } )
)*
((k=validIdentifier()|k=) { System.err.printf("%d:%d: unparsed garbage in .net (%s)%n", k.beginLine, k.beginColumn, k.image); })*
)?
EO()
{
if(graphics != null)
ctx.addEdgeGraphics(id, graphics);
}
}
void edgeList():
{
Token src, trg, more;
String id;
}
{
src= trg= { id=ctx.addEdge(src.image, trg.image); }
( more= { if(edgeListMode) ctx.addEdge(src.image, more.image); } )*
EO()
}
// The awful *matrix notation.
void matrix():
{
EdgeMatrix mat = new EdgeMatrix(nodeCount);
ArrayList line;
}
{
()+
(line = matrixline(mat) { mat.addLine(line); } | )+
{ ctx.addEdges(mat); }
}
ArrayList matrixline(EdgeMatrix mat):
{
Token k;
ArrayList line = new ArrayList(nodeCount);
}
{
(k= { line.add(k.image); })+ EO()
{ return line; }
}
// Various
String keyOrString():
{
Token k;
String value;
}
{
( ( k= { value = k.image; } )
| ( k= { value = k.image.substring(1, k.image.length()-1); } )
)
{
return value;
}
}
String number():
{
Token k;
}
{
((k=) | (k=))
{ return k.image; }
}
String color():
{
String value;
Token r, g, b;
}
{
( value=keyOrString()
| ( r= g= b= { value = PajekContext.toColorValue(r, g, b); } )
| ( r= g= b= { value = PajekContext.toColorValue(r, g, b); } )
)
{ return value; }
}
void EO():
{}
{
( ()+
|
)
}
Token anyGraphicAttribute():
{
Token k;
}
{
(k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
|k=
)
{ return k; }
}
Token validIdentifier():
{
Token k=null;
}
{
( k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
| k=
)
{
return k;
}
}