Java : A Parser modelling!

 

package com.z.parser;

public class ParseState {
	
	private long offset;
	private String toparse;
	public ParseState(long _offset,String _toparse) {
		this.offset=_offset;
		this.toparse=_toparse;
	}
	public String getToparse() {
		return toparse;
	}
	public long getOffset() {
		return offset;
	}
}

package com.z.parser;

public class ParseResult<T> {
	
	private T parsed;
	private ParseState parseState;
	
	public T getParsed() {
		return parsed;
	}
	public ParseState getParseState() {
		return parseState;
	}
	
}

package com.z.parser;

public interface Parser<T> {
	
	ParseResult<T> parse(ParseState parseState);

}
package com.z.parser;
public interface ParserProcessor<U,V> {
	
	Parser<V>  process(U u);

}

package com.z.parser;

public interface ChainedParser<U,V> {
	
	Parser<V> chain(Parser<U> parser1,ParserProcessor<U, V> parser2);

}

package com.z.parser;

public class ChainedParserImpl<U,V> implements ChainedParser<U, V>{

	@Override
	public Parser<V> chain(final Parser<U> parser1, 
			final ParserProcessor<U, V> parser2) {
		return new Parser<V>() {

			@Override
			public ParseResult<V> parse(ParseState parseState) {
				ParseResult<U> initResult = parser1.parse(parseState);
				return parser2.process(initResult.getParsed()).
                                           parse(initResult.getParseState()); 
			}
		};
	}

}

This need to implemented further.

Advertisements
This entry was posted in Design, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s