001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
015: * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
016: * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
017: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
018: *
019: * Alternatively, the contents of this file may be used under the terms of
020: * either of the GNU General Public License Version 2 or later (the "GPL"),
021: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
022: * in which case the provisions of the GPL or the LGPL are applicable instead
023: * of those above. If you wish to allow use of your version of this file only
024: * under the terms of either the GPL or the LGPL, and not to allow others to
025: * use your version of this file under the terms of the CPL, indicate your
026: * decision by deleting the provisions above and replace them with the notice
027: * and other provisions required by the GPL or the LGPL. If you do not delete
028: * the provisions above, a recipient may use your version of this file under
029: * the terms of any one of the CPL, the GPL or the LGPL.
030: ***** END LICENSE BLOCK *****/package org.jruby.parser;
031:
032: import java.io.Reader;
033: import java.io.StringReader;
034:
035: import org.jruby.Ruby;
036: import org.jruby.RubyFile;
037: import org.jruby.ast.Node;
038: import org.jruby.lexer.yacc.LexerSource;
039: import org.jruby.lexer.yacc.SyntaxException;
040: import org.jruby.runtime.DynamicScope;
041: import org.jruby.runtime.ThreadContext;
042: import org.jruby.util.collections.SinglyLinkedList;
043:
044: /**
045: * Serves as a simple facade for all the parsing magic.
046: */
047: public class Parser {
048: private final Ruby runtime;
049:
050: public Parser(Ruby runtime) {
051: this .runtime = runtime;
052: }
053:
054: public Node parse(String file, String content,
055: DynamicScope blockScope, int lineNumber,
056: boolean extraPositionInformation) {
057: return parse(file, new StringReader(content), blockScope,
058: lineNumber, extraPositionInformation);
059: }
060:
061: public Node parse(String file, String content,
062: DynamicScope blockScope, int lineNumber) {
063: return parse(file, new StringReader(content), blockScope,
064: lineNumber, false);
065: }
066:
067: public Node parse(String file, Reader content,
068: DynamicScope blockScope, int lineNumber) {
069: return parse(file, content, blockScope, lineNumber, false);
070: }
071:
072: public Node parse(String file, Reader content,
073: DynamicScope blockScope, int lineNumber,
074: boolean extraPositionInformation) {
075: RubyParserConfiguration configuration = new RubyParserConfiguration();
076: SinglyLinkedList cref = runtime.getObject().getCRef();
077: ThreadContext tc = runtime.getCurrentContext();
078:
079: // We only need to pass in current scope if we are evaluating as a block (which
080: // is only done for evals). We need to pass this in so that we can appropriately scope
081: // down to captured scopes when we are parsing.
082: if (blockScope != null) {
083: configuration.parseAsBlock(blockScope);
084: }
085:
086: DefaultRubyParser parser = null;
087: RubyParserResult result = null;
088: try {
089: parser = RubyParserPool.getInstance().borrowParser();
090: parser.setWarnings(runtime.getWarnings());
091: tc.setCRef(cref);
092: LexerSource lexerSource = LexerSource.getSource(file,
093: content, lineNumber, extraPositionInformation);
094: result = parser.parse(configuration, lexerSource);
095: if (result.isEndSeen()) {
096: org.jruby.runtime.builtin.IRubyObject verbose = runtime
097: .getVerbose();
098: runtime.setVerbose(runtime.getNil());
099: runtime.defineGlobalConstant("DATA", new RubyFile(
100: runtime, file, content));
101: runtime.setVerbose(verbose);
102: result.setEndSeen(false);
103: }
104: } catch (SyntaxException e) {
105: StringBuffer buffer = new StringBuffer(100);
106: buffer.append(e.getPosition().getFile()).append(':');
107: buffer.append(e.getPosition().getEndLine()).append(": ");
108: buffer.append(e.getMessage());
109: throw runtime.newSyntaxError(buffer.toString());
110: } finally {
111: RubyParserPool.getInstance().returnParser(parser);
112: tc.unsetCRef();
113: }
114:
115: // If variables were added then we may need to grow the dynamic scope to match the static
116: // one.
117: // FIXME: Make this so we only need to check this for blockScope != null. We cannot
118: // currently since we create the DynamicScope for a LocalStaticScope before parse begins.
119: // Refactoring should make this fixable.
120: if (result.getScope() != null) {
121: result.getScope().growIfNeeded();
122: }
123:
124: // FIXME: We should move this into ParserSupport.addRootNode since actual parser should do this.
125: result.addAppendBeginNodes();
126: return result.getAST();
127: }
128: }
|