1 8 9 package net.sourceforge.chaperon.build; 10 11 import net.sourceforge.chaperon.model.Violations; 12 import net.sourceforge.chaperon.model.lexicon.Lexeme; 13 import net.sourceforge.chaperon.model.lexicon.Lexicon; 14 import net.sourceforge.chaperon.process.LexicalAutomaton; 15 import net.sourceforge.chaperon.process.PatternAutomaton; 16 17 import org.apache.commons.logging.Log; 18 19 25 public class LexicalAutomatonBuilder 26 { 27 private Lexicon lexicon = null; 28 private LexicalAutomaton automaton = null; 29 private Log log = null; 30 31 36 public LexicalAutomatonBuilder(Lexicon lexicon) 37 { 38 this(lexicon, null); 39 } 40 41 47 public LexicalAutomatonBuilder(Lexicon lexicon, Log log) 48 { 49 this.log = log; 50 51 try 52 { 53 this.lexicon = (Lexicon)lexicon.clone(); 54 } 55 catch (CloneNotSupportedException cnse) 56 { 57 throw new IllegalArgumentException ("Lexicon is nor cloneable"); 58 } 59 60 Violations violations = lexicon.validate(); 61 62 if ((violations!=null) && (violations.getViolationCount()>0)) 63 throw new IllegalArgumentException ("Lexicon is not valid: "+violations.getViolation(0)); 64 65 LexicalAutomaton automaton = new LexicalAutomaton(lexicon.getLexemeCount()); 66 67 Lexeme lexeme; 68 PatternAutomaton definition = null; 69 70 for (int i = 0; i<lexicon.getLexemeCount(); i++) 71 { 72 lexeme = lexicon.getLexeme(i); 73 automaton.setLexemeSymbol(i, (lexeme.getSymbol()!=null) ? lexeme.getSymbol().getName() : null); 74 75 definition = (new PatternAutomatonBuilder(lexeme.getDefinition())).getPatternAutomaton(); 76 if (definition!=null) 77 automaton.setLexemeDefinition(i, definition); 78 else 79 throw new IllegalArgumentException ("Couldn't create PatternAutomaton for "+ 80 lexeme.getSymbol()+" of \""+lexeme.getDefinition()+"\""); 81 } 82 83 this.automaton = automaton; 84 } 85 86 91 public LexicalAutomaton getLexicalAutomaton() 92 { 93 return automaton; 94 } 95 } 96 | Popular Tags |