01: package ro.infoiasi.donald.compiler.parser;
02:
03: import ro.infoiasi.donald.compiler.cfg.*;
04: import java.io.*;
05: import java.util.*;
06:
07: public class ParserSpec {
08: private String packageName;
09: private List imports;
10: private String actionCode;
11: private String parserCode;
12: private String initCode;
13: private String scanCode;
14: private CFG grammar;
15:
16: ParserSpec(String packageName, List imports, String actionCode,
17: String parserCode, String initCode, String scanCode,
18: CFG grammar) {
19: this .packageName = packageName;
20: this .imports = imports;
21: this .actionCode = actionCode;
22: this .parserCode = parserCode;
23: this .initCode = initCode;
24: this .scanCode = scanCode;
25: this .grammar = grammar;
26: }
27:
28: public static ParserSpec load(String fileName) throws IOException,
29: SpecParseException {
30:
31: FileReader fr = new FileReader(fileName);
32:
33: SpecParser p = new SpecParser(new SpecLexer(fr));
34: try {
35: return (ParserSpec) p.parse().value;
36: } catch (IOException e) {
37: throw e;
38: } catch (Exception e) {
39: if (e instanceof SpecParseException) {
40: throw (SpecParseException) e;
41: } else {
42: throw new SpecParseException(e.getMessage());
43: }
44: }
45: }
46:
47: public CFG getGrammar() {
48: return grammar;
49: }
50:
51: public String getPackageName() {
52: return packageName;
53: }
54:
55: public List getImports() {
56: return imports;
57: }
58:
59: public String getActionCode() {
60: return actionCode;
61: }
62:
63: public String getParserCode() {
64: return parserCode;
65: }
66:
67: public String getInitCode() {
68: return initCode;
69: }
70:
71: public String getScanCode() {
72: return scanCode;
73: }
74:
75: public String toString() {
76: StringBuffer sb = new StringBuffer();
77: sb.append("package " + packageName + ";\n\n");
78: Iterator it = imports.iterator();
79: while (it.hasNext()) {
80: sb.append("import " + it.next() + ";\n");
81: }
82: sb.append("\n");
83: sb.append(grammar);
84: return sb.toString();
85: }
86: }
|