001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.process;
010:
011: import java.io.Serializable;
012:
013: /**
014: * The lexical automaton holds all automata for the the lexemes of a lexicon.
015: *
016: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
017: * @version CVS $Id: LexicalAutomaton.java,v 1.3 2003/12/09 19:55:53 benedikta Exp $
018: */
019: public class LexicalAutomaton implements Serializable {
020: private int lexemecount = 0;
021:
022: // List of all symbols of the lexemes
023: private String[] symbols;
024:
025: // Pattern automata for the recognition of the lexemes
026: private PatternAutomaton[] definitions;
027:
028: /**
029: * Create a lexical automaton.
030: *
031: * @param lexemecount Count of lexemes.
032: */
033: public LexicalAutomaton(int lexemecount) {
034: this .lexemecount = lexemecount;
035: symbols = new String[lexemecount];
036: definitions = new PatternAutomaton[lexemecount];
037: }
038:
039: /**
040: * Sets the name of a terminal symbol.
041: *
042: * @param index Index of the lexeme.
043: * @param symbol Name of the symbol.
044: */
045: public void setLexemeSymbol(int index, String symbol) {
046: if ((index < 0) || (index >= lexemecount))
047: throw new IndexOutOfBoundsException();
048:
049: symbols[index] = symbol;
050: }
051:
052: /**
053: * Returns the name of a terminal symbol
054: *
055: * @param index Index of the lexeme.
056: *
057: * @return Name of the symbol.
058: */
059: public String getLexemeSymbol(int index) {
060: if ((index < 0) || (index >= lexemecount))
061: throw new IndexOutOfBoundsException();
062:
063: return symbols[index];
064: }
065:
066: /**
067: * Sets the pattern automaton for a lexeme
068: *
069: * @param index Index of the lexeme.
070: * @param definition Pattern automaton.
071: */
072: public void setLexemeDefinition(int index,
073: PatternAutomaton definition) {
074: if ((index < 0) || (index >= lexemecount))
075: throw new IndexOutOfBoundsException();
076:
077: definitions[index] = definition;
078: }
079:
080: /**
081: * Returns the pattern automaton for a lexeme.
082: *
083: * @param index Index of the lexeme.
084: *
085: * @return Pattern automaton.
086: */
087: public PatternAutomaton getLexemeDefinition(int index) {
088: if ((index < 0) || (index >= lexemecount))
089: throw new IndexOutOfBoundsException();
090:
091: return definitions[index];
092: }
093:
094: /**
095: * Return the count of lexemes.
096: *
097: * @return Count of lexemes.
098: */
099: public int getLexemeCount() {
100: return lexemecount;
101: }
102: }
|