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

org.modelcc.language.syntax.SyntaxConstraints 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!
/*
 * ModelCC, distributed under ModelCC Shared Software License, www.modelcc.org
 */

package org.modelcc.language.syntax;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.modelcc.AssociativityType;
import org.modelcc.language.metamodel.LanguageModel;

/**
 * Syntactic constraints.
 * 
 * @author Luis Quesada ([email protected])
 */
public final class SyntaxConstraints implements Serializable 
{
	/**
	 * Language model
	 */
	private LanguageModel model;

    /**
     * Map of associativities.
     */
    private Map associativities;

    /**
     * Map of composition precedences.
     */
    private Map> compositionPrecedences;

    /**
     * Map of selection precedences.
     */
    private Map> selectionPrecedences;

    /**
     * Map of start precedences.
     */
    private Map> startPrecedences;

    /**
     * Constructor.
     */
    public SyntaxConstraints () 
    {
        this.model = null;
        this.associativities = new HashMap();
        this.compositionPrecedences = new HashMap>();
        this.selectionPrecedences = new HashMap>();
        this.startPrecedences = new HashMap>();
    }

    // Getters
    
    public LanguageModel getModel ()
    {
    	return model;
    }
    
    
    public Map getAssociativities() 
    {
        return Collections.unmodifiableMap(associativities);
    }

    public AssociativityType getAssociativity (Object type)
    {
    	return associativities.get(type);
    }
    
    
    public Map> getCompositionPrecedences() 
    {
        return Collections.unmodifiableMap(compositionPrecedences);
    }
    
    public Set getCompositionPrecedences (Rule r)
    {
    	return compositionPrecedences.get(r);
    }
    
    
    public Map> getSelectionPrecedences() 
    {
        return Collections.unmodifiableMap(selectionPrecedences);
    }
    
    public Set getSelectionPrecedences (Rule r)
    {
    	return selectionPrecedences.get(r);
    }
    

    public Map> getStartPrecedences() 
    {
        return Collections.unmodifiableMap(startPrecedences);
    }
    
    public Set getStartPrecedences (Rule r)
    {
    	return startPrecedences.get(r);
    }
    
    
    // Setters
    
    public void setModel (LanguageModel model)
    {
    	this.model = model;
    }
    
    
    /**
     * Set an associativity constraint for an object type.
     * @param type the object type.
     * @param as the associativity constraint.
     */
    public void setAssociativity (Object type, AssociativityType as) 
    {
        if (as==AssociativityType.UNDEFINED)
            associativities.remove(type);
        else
            associativities.put(type,as);
    }
    
    
    /**
     * Adds a composition precedence between rules.
     * @param ts1 the rule that precedes.
     * @param ts2 the rule that is preceded.
     */
    public void addCompositionPrecedences(Rule ts1,Rule ts2) {
        if (ts1 != null && ts2 != null) {
            Set set = compositionPrecedences.get(ts1);
            if (set == null) {
                set = new HashSet();
                compositionPrecedences.put(ts1,set);
            }
            set.add(ts2);
        }
    }

    /**
     * Adds a start precedence between rules.
     * @param ts1 the rule that precedes.
     * @param ts2 the rule that is preceded.
     */
    public void addStartPrecedences(Rule ts1,Rule ts2) {
        if (ts1 != null && ts2 != null) {
            Set set = startPrecedences.get(ts1);
            if (set == null) {
                set = new HashSet();
                startPrecedences.put(ts1,set);
            }
            set.add(ts2);
        }
    }

    /**
     * Removes a composition precedence between rules.
     * @param ts1 the rule that precedes.
     * @param ts2 the rule that is preceded.
     */
    public void removeCompositionPrecedences(Rule ts1,Rule ts2) {
        if (ts1 != null && ts2 != null) {
            Set set = compositionPrecedences.get(ts1);
            if (set == null) {
                return;
            }
            set.remove(ts2);
            if (set.isEmpty())
                compositionPrecedences.remove(ts1);
        }
    }


    /**
     * Adds an selection precedence between rules.
     * @param ts1 the rule that precedes.
     * @param ts2 the rule that is preceded.
     */
    public void addSelectionPrecedences(Rule ts1,Rule ts2) {
        if (ts1 != null && ts2 != null) {
            Set set = selectionPrecedences.get(ts1);
            if (set == null) {
                set = new HashSet();
                selectionPrecedences.put(ts1,set);
            }
            set.add(ts2);
        }
    }

    /**
     * Removes an selection precedence between rules.
     * @param ts1 the rule that precedes.
     * @param ts2 the rule that is preceded.
     */
    public void removeSelectionPrecedences(Rule ts1,Rule ts2) {
        if (ts1 != null && ts2 != null) {
            Set set = selectionPrecedences.get(ts1);
            if (set == null) {
                return;
            }
            set.remove(ts2);
            if (set.isEmpty())
                selectionPrecedences.remove(ts1);
        }
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy