/*
 * Copyright (c) 2008, Heng Yuan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Heng Yuan nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Heng Yuan ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Heng Yuan BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.yuanheng.cookcc.doc;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.StringTokenizer;

/**
 * @author Heng Yuan
 * @version $Id$
 */
public class RuleDoc
{
	private final LexerDoc m_lexer;
	private int m_lineNumber;

	private final LinkedList m_patterns = new LinkedList ();

	private String m_action = "";
	private HashSet m_states = new HashSet ();

	public RuleDoc (LexerDoc lexer)
	{
		m_lexer = lexer;
	}

	/**
	 * Obtain the line number information of the pattern.  It may not be available.
	 * @return	the line number information of the pattern.
	 */
	public int getLineNumber ()
	{
		return m_lineNumber;
	}

	public void setLineNumber (int lineNumber)
	{
		m_lineNumber = lineNumber;
	}

	public void addPattern (String pattern)
	{
		m_patterns.add (pattern);
	}

	public String[] getPatterns ()
	{
		return (String[])m_patterns.toArray (new String[m_patterns.size ()]);
	}

	public void setAction (String action)
	{
		if (action == null)
			action = "";
		m_action = action;
	}

	public String getAction ()
	{
		return m_action;
	}

	/**
	 * Add a comma separated list of states that this pattern applies to.
	 *
	 * @param	states
	 *			a comma separated list of states
	 */
	public void addStates (String states)
	{
		StringTokenizer tokenizer = new StringTokenizer (states, ",");
		while (tokenizer.hasMoreTokens ())
		{
			String tok = tokenizer.nextToken ().trim ();
			LexerStateDoc lexerState = m_lexer.getLexerState (tok);
			if (!m_states.contains (lexerState))
			{
				m_states.add (lexerState);
				lexerState.addRule (this);
			}
		}
	}

	public LexerStateDoc[] getStates ()
	{
		return (LexerStateDoc[])m_states.toArray (new LexerStateDoc[m_states.size ()]);
	}
}

@@ -1769,31 +1769,95 @@
 ate 
-final LexerDoc m_lexer;
+static int s_count = 0;%0D%0A%0D%0A%09private final LexerDoc m_lexer;%0D%0A%09private final int m_id;%0D%0A
 %0D%0A%09p
@@ -2058,24 +2058,24 @@
 lexer)%0D%0A%09%7B%0D%0A
-
 %09%09m_lexer = 
@@ -2078,24 +2078,283 @@
 r = lexer;%0D%0A
+%09%09m_id = s_count++;%0D%0A%09%7D%0D%0A%0D%0A%09/**%0D%0A%09 * The order of the rule is important, so it is necessary to have a sequential id%0D%0A%09 * that determines the order of the rules.%0D%0A%09 *%0D%0A%09 * @return%09the sequential id of the rule.%0D%0A%09 */%0D%0A%09public int getId ()%0D%0A%09%7B%0D%0A%09%09return m_id;%0D%0A
 %09%7D%0D%0A%0D%0A%09/**%0D%0A

add a sequential id to the rule.