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

com.github.uscexp.grappa.extension.parser.Parser Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014 - 2016 by haui - all rights reserved
 */
package com.github.uscexp.grappa.extension.parser;

import com.github.fge.grappa.exceptions.InvalidGrammarException;
import com.github.fge.grappa.parsers.BaseParser;
import com.github.fge.grappa.rules.Rule;
import com.github.fge.grappa.run.ListeningParseRunner;
import com.github.uscexp.grappa.extension.nodes.AstTreeNode;
import com.github.uscexp.grappa.extension.nodes.treeconstruction.AstTreeNodeBuilder;

/**
 * @author haui
 *
 */
public class Parser {
	
	private Parser() {}
	
	public static  AstTreeNode parseInput(Class> parserClass, Rule rule, String input) {
		return parseInput(parserClass, rule, input, false);
	}
	
	public static  AstTreeNode parseInput(Class> parserClass, Rule rule, String input, boolean removeAstNopTreeNodes) {
		final AstTreeNodeBuilder treeNodeBuilder = new AstTreeNodeBuilder(parserClass, removeAstNopTreeNodes);
		
		final ListeningParseRunner parseRunner = new ListeningParseRunner<>(rule);
		
		parseRunner.registerListener(treeNodeBuilder);

		parseRunner.run(input);
		
		if(treeNodeBuilder.getRootNode() == null) {
			String errors = treeNodeBuilder.getParsingErrors();
			String errorMessage = String.format("Error parsing input: %s", errors);
			throw new InvalidGrammarException(errorMessage);
		}
		
		return treeNodeBuilder.getRootNode();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy