KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > frontend > CupParser


1 package polyglot.frontend;
2
3 import java.io.*;
4 import polyglot.ast.*;
5 import polyglot.util.*;
6
7 /**
8  * A parser implemented with a Cup generated-parser.
9  */

10 public class CupParser implements Parser
11 {
12     java_cup.runtime.lr_parser grm;
13     Source source;
14     ErrorQueue eq;
15
16     public CupParser(java_cup.runtime.lr_parser grm, Source source, ErrorQueue eq) {
17     this.grm = grm;
18     this.source = source;
19         this.eq = eq;
20     }
21
22     public Node parse() {
23     try {
24         java_cup.runtime.Symbol sym = grm.parse();
25
26         if (sym != null && sym.value instanceof Node) {
27         SourceFile sf = (SourceFile) sym.value;
28                 return sf.source(source);
29         }
30
31         eq.enqueue(ErrorInfo.SYNTAX_ERROR, "Unable to parse " +
32         source.name() + ".");
33     }
34     catch (IOException e) {
35         eq.enqueue(ErrorInfo.IO_ERROR, e.getMessage());
36     }
37     catch (RuntimeException JavaDoc e) {
38         // Let the Compiler catch and report it.
39
throw e;
40     }
41     catch (Exception JavaDoc e) {
42         // Used by cup to indicate a non-recoverable error.
43
if (e.getMessage() != null) {
44             eq.enqueue(ErrorInfo.SYNTAX_ERROR, e.getMessage());
45         }
46     }
47
48     return null;
49     }
50
51     public String JavaDoc toString() {
52     return "CupParser(" + grm.getClass().getName() + ")";
53     }
54 }
55
Popular Tags