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

org.unlaxer.parser.elementary.LineTerminatorParser Maven / Gradle / Ivy

package org.unlaxer.parser.elementary;

import java.util.List;

import org.unlaxer.Cursor;
import org.unlaxer.Parsed;
import org.unlaxer.ParserCursor;
import org.unlaxer.TokenKind;
import org.unlaxer.TransactionElement;
import org.unlaxer.context.ParseContext;
import org.unlaxer.parser.Parser;
import org.unlaxer.parser.Parsers;
import org.unlaxer.parser.combinator.LazyChoice;

public class LineTerminatorParser extends LazyChoice{

	private static final long serialVersionUID = -325480488364751237L;
	static final String LF = new String(new byte[] {0x0a/*lf*/});
	static final String CR = new String(new byte[] {0x0d/*cr*/});
	static final String CRLF = new String(new byte[] {0x0d/*cr*/,0x0a/*lf*/});

	@Override
	public List getLazyParsers() {
		return new Parsers(
			new WordParser(CRLF),
			new WordParser(CR),
			new WordParser(LF),
			Parser.get(EndOfSourceParser.class)
		);
	}
	
	
	@Override
	public Parsed parse(ParseContext parseContext, TokenKind tokenKind, boolean invertMatch) {
		Parsed parse = super.parse(parseContext, tokenKind, invertMatch);
		TransactionElement current = parseContext.getCurrent();
		ParserCursor parserCursor = current.getParserCursor();
		Cursor cursor = parserCursor.getCursor(TokenKind.consumed);
		cursor.setLineNumber(cursor.getLineNumber()+1);
		return parse;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy