Source Code Cross Referenced for SExpParser.java in  » IDE » DrJava » edu » rice » cs » util » sexp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » DrJava » edu.rice.cs.util.sexp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*BEGIN_COPYRIGHT_BLOCK
002:         *
003:         * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004:         * All rights reserved.
005:         * 
006:         * Redistribution and use in source and binary forms, with or without
007:         * modification, are permitted provided that the following conditions are met:
008:         *    * Redistributions of source code must retain the above copyright
009:         *      notice, this list of conditions and the following disclaimer.
010:         *    * Redistributions in binary form must reproduce the above copyright
011:         *      notice, this list of conditions and the following disclaimer in the
012:         *      documentation and/or other materials provided with the distribution.
013:         *    * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014:         *      names of its contributors may be used to endorse or promote products
015:         *      derived from this software without specific prior written permission.
016:         * 
017:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018:         * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019:         * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020:         * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024:         * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028:         *
029:         * This software is Open Source Initiative approved Open Source Software.
030:         * Open Source Initative Approved is a trademark of the Open Source Initiative.
031:         * 
032:         * This file is part of DrJava.  Download the current version of this project
033:         * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034:         * 
035:         * END_COPYRIGHT_BLOCK*/
036:
037:        package edu.rice.cs.util.sexp;
038:
039:        import java.io.File;
040:        import java.io.Reader;
041:        import java.io.FileReader;
042:        import java.io.StringReader;
043:        import java.io.IOException;
044:        import java.util.List;
045:        import java.util.ArrayList;
046:        import java.util.LinkedList;
047:
048:        /**
049:         * This parser is not meant to be instantiated.  It has
050:         * static methods that do all the work for you.  These
051:         * parse methods take in the data that is to be parsed
052:         * and simply returns an s-expression abstract syntax.
053:         * @author Jonathan Lugo, PLT Group
054:         */
055:        public class SExpParser {
056:
057:            public static List<SEList> parse(File f) throws SExpParseException,
058:                    IOException {
059:                return parse(new FileReader(f));
060:            }
061:
062:            public static List<SEList> parse(String s)
063:                    throws SExpParseException {
064:                return parse(new StringReader(s));
065:            }
066:
067:            public static List<SEList> parse(Reader r)
068:                    throws SExpParseException {
069:                try {
070:                    return new ParseHelper(r).parseMultiple();
071:                } catch (LexingException e) {
072:                    throw new SExpParseException(e.getMessage());
073:                } catch (PrivateParseException e) {
074:                    throw new SExpParseException(e.getMessage());
075:                }
076:            }
077:
078:            /**
079:             * A new helper is instantiated for each time
080:             * the user wants to parse data.  This is not
081:             * reused.  The instances of the ParseHelpers are
082:             * handled solely in the outer class SExpParser.
083:             */
084:            private static class ParseHelper {
085:
086:                private Lexer _lex;
087:
088:                public ParseHelper(Reader r) {
089:                    _lex = new Lexer(r);
090:                }
091:
092:                /**
093:                 * Parse a forest of top-level s-expressions from {@link #parseTopLevelExp()}.
094:                 * @see #parseTopLevelExp()
095:                 */
096:                public List<SEList> parseMultiple() {
097:                    ArrayList<SEList> l = new ArrayList<SEList>();
098:                    SEList exp;
099:                    while ((exp = parseTopLevelExp()) != null) {
100:                        l.add(exp);
101:                    }
102:                    return l;
103:                }
104:
105:                /**
106:                 * A top-level s-expression is simply a non-empty list.  Our s-expression files
107:                 * can be a forest of several trees, but the Atomic values are not allowed
108:                 * at the top level, only lists.
109:                 * @return the top-level list s-expression
110:                 */
111:                public SEList parseTopLevelExp() {
112:                    Tokens.SExpToken t = _lex.readToken();
113:                    if (t == Tokens.LeftParenToken.ONLY) {
114:                        return parseList();
115:                    } else if (t == null) {
116:                        return null;
117:                    } else {
118:                        throw new PrivateParseException(
119:                                "A top-level s-expression must be a list. "
120:                                        + "Invalid start of list: " + t);
121:                    }
122:                }
123:
124:                /**
125:                 * Parses the next s-expression in the lexer's buffer.
126:                 * This may be either a cons or an atom
127:                 * @return the next s-expression in the read buffer.
128:                 */
129:                public SExp parseExp() {
130:                    Tokens.SExpToken t = _lex.readToken();
131:                    assertNotEOF(t);
132:                    if (t == Tokens.LeftParenToken.ONLY) {
133:                        return parseList();
134:                    } else {
135:                        return parseAtom(t);
136:                    }
137:                }
138:
139:                /**
140:                 * The left paren has already been read. This starts
141:                 * building up the recursive list structure
142:                 * @return the parsed recursive s-expression list
143:                 */
144:                private SEList parseList() {
145:                    LinkedList<SExp> list = new LinkedList<SExp>();
146:                    Tokens.SExpToken t = _lex.peek();
147:                    assertNotEOF(t);
148:
149:                    while (t != Tokens.RightParenToken.ONLY) {
150:                        list.addFirst(parseExp());
151:                        t = _lex.peek();
152:                    }
153:
154:                    // t has to be a Tokens.RightParenToken at this point.
155:                    // simply eat the token
156:                    _lex.readToken();
157:
158:                    // Compile the cons structure from the list of exps
159:                    SEList cons = Empty.ONLY;
160:                    for (SExp exp : list) {
161:                        cons = new Cons(exp, cons);
162:                    }
163:                    return cons;
164:                }
165:
166:                /**
167:                 * Parses an atom.  The token was already read and
168:                 * found not to start a list, this method interprets
169:                 * what is given.  This method chooses which type of
170:                 * atom the token represents and creates the atom.
171:                 * @param t the token to interpret
172:                 * @return the correct corresponding atom
173:                 */
174:                private Atom parseAtom(Tokens.SExpToken t) {
175:                    if (t instanceof  Tokens.BooleanToken) {
176:                        if (((Tokens.BooleanToken) t).getValue())
177:                            return BoolAtom.TRUE;
178:                        else
179:                            return BoolAtom.FALSE;
180:                    } else if (t instanceof  Tokens.NumberToken) {
181:                        return new NumberAtom(((Tokens.NumberToken) t)
182:                                .getValue());
183:                    } else if (t instanceof  Tokens.QuotedTextToken) {
184:                        return new QuotedTextAtom(t.getText());
185:                    } else {
186:                        return new TextAtom(t.getText());
187:                    }
188:                }
189:
190:                /**
191:                 * Throws the EOF exception if the given token is the end of file
192:                 * @param t the token to check
193:                 */
194:                private void assertNotEOF(Tokens.SExpToken t) {
195:                    if (t == null) {
196:                        throw new PrivateParseException(
197:                                "Unexpected <EOF> at line " + _lex.lineno());
198:                    }
199:                }
200:            }
201:
202:            /**
203:             * This runtime exception makes it easier to write the parser since
204:             * the methods of the helper class won't need to constantly declare
205:             * the SExpParseException to be thrown.
206:             */
207:            private static class PrivateParseException extends RuntimeException {
208:                /**
209:                 * Creates a runtime exception with the message that is desired for
210:                 * the eventual checked exception
211:                 * @param msg the message to display
212:                 */
213:                public PrivateParseException(String msg) {
214:                    super(msg);
215:                }
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.