All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graphstream.stream.file.gml.GMLParser.jj Maven / Gradle / Ivy

options { JDK_VERSION = "1.5"; STATIC = false; IGNORE_CASE = true; }

PARSER_BEGIN(GMLParser)
/*
 * 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.gml;

import java.io.InputStream;
import java.io.IOException;
import java.io.Reader;

import org.graphstream.stream.file.FileSourceGML;

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 GMLParser implements Parser {
	boolean inGraph = false;
	GMLContext ctx;
	boolean step;
  
	public GMLParser(FileSourceGML gml, InputStream stream)
	{
		this(stream);
		this.ctx = new GMLContext(gml);
	}
	
	public GMLParser(FileSourceGML gml, Reader stream )
	{
		this(stream);
		this.ctx = new GMLContext(gml);
	}
	
	public boolean isInGraph()
	{
		return inGraph; 
	}
	
	public void open() throws IOException, ParseException {
	}
	
	public boolean next() throws IOException, ParseException {
		KeyValues kv = null;
		kv = nextEvents();
		ctx.handleKeyValues(kv);
		
		return (kv != null);
	}
	
	public boolean step() throws IOException, ParseException {
		KeyValues kv = null;
		step = false;
		
		while((kv = nextEvents()) != null && !step)
			ctx.handleKeyValues(kv);
			
		if (kv != null)
			ctx.setNextStep(kv);
		
		return (kv != null);
	}
	
	/**
	 * Closes the parser, closing the opened stream.
	 */
	public void close() throws IOException {
		jj_input_stream.close();
	}
}
PARSER_END(GMLParser)

/************************************************************************/
/* The lexer.                                                           */
/************************************************************************/

SKIP :
{ 	" "
|	"\r"
|	"\t"
|	"\n"
}
//
// Base tokens.
//
TOKEN: { < #EOL              : (("\r")|("\n"))> }
TOKEN: { < #DIGIT            : ["0"-"9"] > }
TOKEN: { < #HEXDIGIT         : (["0"-"9","a"-"f","A"-"F"])> }

TOKEN: { < LSQBR             : "[" > }
TOKEN: { < RSQBR             : "]" > }
TOKEN: { < REAL              : ( "-" | "+" )? (  )+ ( "." ()+ )?> } 
TOKEN: { < STRING            : (("\"" (~["\""])* "\"")|("'" (~["'"])* "'")) > }

//
// More base tokens that must appear at the end for precedence reasons.
//
TOKEN: { < GRAPH     : "graph" > }
TOKEN: { < DIGRAPH   : "digraph" > }
TOKEN: { < KEY       : (["-","+","a"-"z","A"-"Z"])(["a"-"z","A"-"Z","0"-"9","_","-","."])* > }
TOKEN: { < COMMENT   : ("#"((~["\r","\n"])*)) > }


/*****************************************************************/
/* The parser.                                                   */
/*****************************************************************/

/** Unused rule, call it to slurp in the whole file. */
void start():
{}
{
	list()
}

void all() throws IOException:
{
	KeyValues values = new KeyValues();
	String key;
}
{
	( graphStart()         { ctx.setIsInGraph(true); ctx.setDirected(false); }
	| diGraphStart()       { ctx.setIsInGraph(true); ctx.setDirected(true); }
	)
	
	( key=keyValue(values) { values.key = key; ctx.handleKeyValues(values); values.clear(); } )*

	graphEnd()           { values.key = null; inGraph = false; }
	
	
}

void graphStart(): {} {   }
void diGraphStart(): {} {   }
void graphEnd(): {} {  }

/** The top-level method to be called by the file source.
  * Returns a set of top-level key values or null if the
  * end of the file was reached.
  *
  * Top-level key values are nodes and edges as well as
  * all key-values defined before and after the graph.  
  */
KeyValues nextEvents():
{
	KeyValues values = new KeyValues();
	String key;
}
{
	( graphStart()         { values.key = null; ctx.setIsInGraph(true); ctx.setDirected(false); }
	| diGraphStart()       { values.key = null; ctx.setIsInGraph(true); ctx.setDirected(true); }
	| graphEnd()           { values.key = null; ctx.setIsInGraph(false); }
	| key=keyValue(values) { values.key = key; }
	|			       { values = null; }
	)
	{ return values; }
}

/** A list of key values, all values are
  * stored in a KeyValues object.
  */
KeyValues list():
{
	KeyValues values = new KeyValues();
}
{
  	(keyValue(values))*
  	{ return values; }
}

/** A set of key and value, the value can recursively be a list of key-values.
  * Only the key-value list "graph [ ... ]" is not parsed by this rule, and
  * parsed by another rules, so that the nextEvent() rule can be called
  * repeatedly. 
  */
String keyValue(KeyValues values):
{
	Token k;
	String key;
	Object v;
	boolean isGraph = false;
}
{
	( ()*
  	  ( k=    { key=k.image; if (key.equalsIgnoreCase("step")) step = true; }
  	  | k= { key=k.image.substring(1,k.image.length()-2); }
  	  )
	  v=value(key) { values.put(key, v); values.line = k.beginLine; values.column = k.beginColumn; }
	)
	{ return key; }
/*
	( ()*
  	  ( k=    { key = k.image; }
  	  | k= { key = k.image.substring(1, k.image.length()-2); }
  	  )
	  v=value(key) { values.put(key, v); values.line = k.beginLine; values.column = k.beginColumn; }
	)
	{ return key; }
*/
}

/** A value for a key, either a number, a string or
  * a recursive list of key-values.
  */
Object value(String key):
{
	Token t;
	Object val;
	KeyValues kv;
}
{
	( t=   { if (t.image.indexOf('.') < 0) val = Integer.valueOf(t.image); else val = Double.valueOf(t.image); }
	| t= { val = t.image.substring(1, t.image.length()-1); }
	| t=    { val = t.image; }
	|   kv = list() { val = kv; } 
	) 
	{ return val; }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy