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

org.modelcc.io.java.checker.PrecedenceChecker Maven / Gradle / Ivy

Go to download

ModelCC is a model-based parser generator (a.k.a. compiler compiler) that decouples language specification from language processing, avoiding some of the problems caused by grammar-driven parser generators. ModelCC receives a conceptual model as input, along with constraints that annotate it. It is then able to create a parser for the desired textual language and the generated parser fully automates the instantiation of the language conceptual model. ModelCC also includes a built-in reference resolution mechanism that results in abstract syntax graphs, rather than mere abstract syntax trees.

The newest version!
package org.modelcc.io.java.checker;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;

import org.modelcc.io.java.JavaModelReader;
import org.modelcc.io.java.JavaModelChecker;
import org.modelcc.io.java.JavaLanguageMetadata;
import org.modelcc.io.java.JavaLanguageClass;

/**
 * Checks for cyclic precedences
 */
public class PrecedenceChecker extends JavaModelChecker 
{	
	public PrecedenceChecker (JavaModelReader reader)
	{
		super(reader);
	}
	
	@Override
	public void check(JavaLanguageMetadata metadata) 
	{
    	Set pool = new HashSet();
    	pool.addAll(metadata.getJavaElements());

    	// Token specifications preceded by any token specification in the pool.
    	Set precededs;

    	Iterator ite;
    	Iterator ite2;
    	JavaLanguageClass ts;
    	JavaLanguageClass ts2;
    	Set pset;

    	boolean found;

    	// Generate specification

    	while (!pool.isEmpty()) {

    		found = false;

    		// Update precededs list.
    		precededs = new HashSet();
    		for (ite = pool.iterator();ite.hasNext();) {
    			ts = ite.next();
    			pset = metadata.prePrecedences.get(ts);
    			if (pset != null) {
    				precededs.addAll(pset);
    			}
    		}

    		// Add new unprecededs.
    		for (ite = pool.iterator();ite.hasNext();) {
    			ts = ite.next();
    			if (!precededs.contains(ts)) {
    				ite.remove();
    				found = true;
    			}
    		}

    		if (!found) {
    			String list = new String();
    			for (ite = pool.iterator();ite.hasNext();)
    				list += " "+ite.next().getElementClass().getCanonicalName();
    			log(Level.SEVERE, "Cyclic precedence exception:{0}.", new Object[]{list.toString()});
    			for (ite = pool.iterator();ite.hasNext();) {
    				ts = ite.next();
    				pset = metadata.prePrecedences.get(ts);
    				if (pset != null) {
    					for (ite2 = pool.iterator();ite2.hasNext();) {
    						ts2 = ite2.next();
    						pset.remove(ts2);
    					}
    					if (pset.isEmpty())
    						metadata.prePrecedences.remove(ts);
    				}
    			}
    			return;
    		}
    	}
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy