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

org.modelcc.language.syntax.Rule 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.ArrayList;
import java.util.List;

/**
 * Context-free grammar rule.
 * 
 * @author Luis Quesada ([email protected]) & Fernando Berzal ([email protected])
 */
public final class Rule implements Serializable 
{
	/**
	 * Rule type.
	 */
	public enum Type { UNDEFINED, COMPOSITION, SELECTION, REPETITION, COPY, REFERENCE };
	
    /**
     * User data.
     */
    private Object userData;

    /**
     * Left hand side element.
     */
    private RuleSymbol left;

    /**
     * Right hand side elements.
     */
    private List right;

    
    /**
     * Rule type
     */
    private Type type;
    
    
    /**
     * Collection element
     */
    private RuleSymbol element;
    
    /**
     * Builder.
     */
    private SymbolBuilder builder;

    /**
     * Post builder.
     */
    private SymbolBuilder postBuilder;

    private static final SymbolBuilder DEFAULT_SYMBOL_BUILDER = new SymbolBuilder(); 
    
    /**
     * Constructor.
     */
    public Rule (RuleSymbol left) 
    {
    	this.left = left;
    	this.right = new ArrayList();
    	this.userData = null;
    	this.type = Type.UNDEFINED;
        this.postBuilder = DEFAULT_SYMBOL_BUILDER;    	
    }
    
    /**
     * Copy constructor
     * @param r Original rule
     */
    public Rule (Rule r)
    {
    	this.left = r.left;
    	this.right = new ArrayList(r.right);
    	this.builder = r.builder;
    	this.postBuilder = r.postBuilder;
    	this.userData = r.userData;
    	this.type = r.type;
    }

    // Getters & setters
    
    public RuleSymbol getLeft() 
    {
        return left;
    }

    public void setLeft(RuleSymbol left) 
    {
        this.left = left;
    }

    public List getRight() 
    {
        return right;
    }

    public void add (RuleSymbol element)
    {
    	right.add(element);
    }
    
    public Type getType ()
    {
    	return type;
    }
    
    public void setType (Type type)
    {
    	this.type = type;
    }
    
    public RuleSymbol getElement ()
    {
    	return element;
    }
    
    public void setElement (RuleSymbol element)
    {
    	this.element = element;
    }
    
    protected SymbolBuilder getBuilder() 
    {
        return builder;
    }

    public void setBuilder(SymbolBuilder builder) 
    {
        this.builder = builder;
    }

    public SymbolBuilder getPostBuilder() 
    {
        return postBuilder;
    }

    public void setPostBuilder(SymbolBuilder postBuilder) 
    {
        this.postBuilder = postBuilder;
        if (this.postBuilder == null)
            this.postBuilder = DEFAULT_SYMBOL_BUILDER;
    }
    
    public Object getUserData() 
    {
        return userData;
    }

    public void setUserData(Object userData) 
    {
        this.userData = userData;
    }
    
    // toString

    @Override
    public String toString() {
        String r = "";
        
        if (left!=null)
        	r += left.getType();
        else
        	r += "";
        
        r += " ::= ";
        
        if (right!=null) {

        	for (int i=0; i
            


© 2015 - 2024 Weber Informatics LLC | Privacy Policy