KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > lexer > yacc > HeredocTerm


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby.lexer.yacc;
29
30 import org.jruby.ast.StrNode;
31 import org.jruby.parser.Tokens;
32 import org.jruby.util.ByteList;
33
34
35 public class HeredocTerm extends StrTerm {
36     private final String JavaDoc eos;
37     private final int func;
38     private final String JavaDoc lastLine;
39     
40     public HeredocTerm(String JavaDoc eos, int func, String JavaDoc lastLine) {
41         this.eos = eos;
42         this.func = func;
43         this.lastLine = lastLine;
44     }
45     
46     public int parseString(RubyYaccLexer lexer, LexerSource src) throws java.io.IOException JavaDoc {
47         char c;
48         boolean indent = (func & RubyYaccLexer.STR_FUNC_INDENT) != 0;
49         ByteList str = new ByteList();
50
51         if ((c = src.read()) == RubyYaccLexer.EOF) {
52             throw new SyntaxException(src.getPosition(), "can't find string \"" + eos + "\" anywhere before EOF");
53         }
54         if (src.wasBeginOfLine() && src.matchString(eos + '\n', indent)) {
55             src.unreadMany(lastLine);
56             return Tokens.tSTRING_END;
57         }
58
59         if ((func & RubyYaccLexer.STR_FUNC_EXPAND) == 0) {
60             /*
61              * if (c == '\n') { support.unread(c); }
62              */

63
64             // Something missing here...
65
/*
66              * int lastLineLength = here.getLastLineLength();
67              *
68              * if (lastLineLength > 0) { // It looks like I needed to append
69              * last line as well...
70              * support.unreadMany(here.getLastLineLength());
71              * str.append(support.readLine()); str.append("\n"); }
72              */

73
74             /*
75              * c was read above and should be unread before we start
76              * to fill the str buffer
77              */

78             src.unread(c);
79             do {
80                 str.append(src.readLineBytes());
81                 str.append('\n');
82
83                 if (src.peek('\0')) {
84                     throw new SyntaxException(src.getPosition(), "can't find string \"" + eos + "\" anywhere before EOF");
85                 }
86             } while (!src.matchString(eos + '\n', indent));
87         } else {
88             ByteList buffer = new ByteList();
89             if (c == '#') {
90                 switch (c = src.read()) {
91                 case '$':
92                 case '@':
93                     src.unread(c);
94                     lexer.setValue(new Token("#" + c, lexer.getPosition()));
95                     return Tokens.tSTRING_DVAR;
96                 case '{':
97                     lexer.setValue(new Token("#" + c, lexer.getPosition()));
98                     return Tokens.tSTRING_DBEG;
99                 }
100                 buffer.append('#');
101             }
102
103             src.unread(c);
104
105             do {
106                 if ((c = new StringTerm(func, '\n', '\0').parseStringIntoBuffer(src, buffer)) == RubyYaccLexer.EOF) {
107                     throw new SyntaxException(src.getPosition(), "can't find string \"" + eos + "\" anywhere before EOF");
108                 }
109                 if (c != '\n') {
110                     lexer.yaccValue = new StrNode(lexer.getPosition(), buffer);
111                     return Tokens.tSTRING_CONTENT;
112                 }
113                 buffer.append(src.read());
114                 if ((c = src.read()) == RubyYaccLexer.EOF) {
115                     throw new SyntaxException(src.getPosition(), "can't find string \"" + eos + "\" anywhere before EOF");
116                 }
117                 // We need to pushback so when whole match looks it did not
118
// lose a char during last EOF
119
src.unread(c);
120             } while (!src.matchString(eos + '\n', indent));
121             str = buffer;
122         }
123
124         src.unreadMany(lastLine);
125         lexer.setStrTerm(new StringTerm(-1, '\0', '\0'));
126         lexer.yaccValue = new StrNode(lexer.getPosition(), str);
127         return Tokens.tSTRING_CONTENT;
128     }
129 }
130
Popular Tags