View | Details | Raw Unified | Return to bug 312249
Collapse All | Expand All

(-)model/org/eclipse/photran/internal/core/model/SimpleFortranModelBuilder.java (-3 / +3 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.model;
11
package org.eclipse.photran.internal.core.model;
12
12
13
import java.io.ByteArrayInputStream;
13
import java.io.Reader;
14
import java.io.InputStream;
14
import java.io.StringReader;
15
import java.util.ArrayList;
15
import java.util.ArrayList;
16
import java.util.HashMap;
16
import java.util.HashMap;
17
import java.util.Map;
17
import java.util.Map;
Lines 73-79 Link Here
73
    {
73
    {
74
        this.newElements = new HashMap();
74
        this.newElements = new HashMap();
75
        
75
        
76
        InputStream in = new ByteArrayInputStream(translationUnit.getBuffer().getContents().getBytes());
76
        Reader in = new StringReader(translationUnit.getBuffer().getContents());
77
        String filename = translationUnit.getElementName();
77
        String filename = translationUnit.getElementName();
78
        boolean wasSuccessful = true;
78
        boolean wasSuccessful = true;
79
79
(-)model/org/eclipse/photran/internal/core/model/FortranModelBuilder.java (-2 / +2 lines)
Lines 1-6 Link Here
1
package org.eclipse.photran.internal.core.model;
1
package org.eclipse.photran.internal.core.model;
2
2
3
import java.io.ByteArrayInputStream;
3
import java.io.StringReader;
4
import java.util.HashMap;
4
import java.util.HashMap;
5
import java.util.Map;
5
import java.util.Map;
6
6
Lines 74-80 Link Here
74
            SourceForm sourceForm = determineSourceForm(file);
74
            SourceForm sourceForm = determineSourceForm(file);
75
            String filename = determineFilename(file);
75
            String filename = determineFilename(file);
76
            lexer = LexerFactory.createLexer(
76
            lexer = LexerFactory.createLexer(
77
                new ByteArrayInputStream(translationUnit.getBuffer().getContents().getBytes()),
77
                new StringReader(translationUnit.getBuffer().getContents()),
78
                file,
78
                file,
79
                filename,
79
                filename,
80
                sourceForm,
80
                sourceForm,
(-)lexer/FixedFormLexerPhase1.flex (-4 / +4 lines)
Lines 34-40 Link Here
34
package org.eclipse.photran.internal.core.lexer;
34
package org.eclipse.photran.internal.core.lexer;
35
35
36
import java.util.regex.Pattern;
36
import java.util.regex.Pattern;
37
import java.io.InputStream;
37
import java.io.Reader;
38
import org.eclipse.core.resources.IFile;
38
import org.eclipse.core.resources.IFile;
39
39
40
@SuppressWarnings("unused")
40
@SuppressWarnings("unused")
Lines 58-65 Link Here
58
58
59
    private TokenFactory tokenFactory;
59
    private TokenFactory tokenFactory;
60
60
61
    public FixedFormLexerPhase1(InputStream in, FixedFormLexerPrepass _prepass, TokenFactory tokenFactory) {
61
    public FixedFormLexerPhase1(Reader in, FixedFormLexerPrepass _prepass, TokenFactory tokenFactory) {
62
        this(new LineAppendingInputStream(in));
62
        this(new LineAppendingReader(in));
63
        this.prepass=_prepass;
63
        this.prepass=_prepass;
64
        this.tokenFactory = tokenFactory;
64
        this.tokenFactory = tokenFactory;
65
    }
65
    }
Lines 188-194 Link Here
188
188
189
    private String filename = "<stdin>";
189
    private String filename = "<stdin>";
190
190
191
    public FixedFormLexerPhase1(java.io.InputStream in, FixedFormLexerPrepass _prepass, IFile file, String filename, TokenFactory tokenFactory)
191
    public FixedFormLexerPhase1(Reader in, FixedFormLexerPrepass _prepass, IFile file, String filename, TokenFactory tokenFactory)
192
    {
192
    {
193
        this(in, _prepass, tokenFactory);
193
        this(in, _prepass, tokenFactory);
194
        this.lastTokenFile = new FileOrIFile(file);
194
        this.lastTokenFile = new FileOrIFile(file);
(-)lexer/FreeFormLexerPhase1.flex (-2 / +4 lines)
Lines 27-34 Link Here
27
 */
27
 */
28
28
29
package org.eclipse.photran.internal.core.lexer;
29
package org.eclipse.photran.internal.core.lexer;
30
30
import java.io.FileNotFoundException;
31
import java.io.FileNotFoundException;
31
import java.io.IOException;
32
import java.io.IOException;
33
import java.io.Reader;
32
import org.eclipse.core.resources.IFile;
34
import org.eclipse.core.resources.IFile;
33
35
34
@SuppressWarnings("unused")
36
@SuppressWarnings("unused")
Lines 120-128 Link Here
120
    private String filename = "<stdin>";
122
    private String filename = "<stdin>";
121
    protected TokenFactory tokenFactory;
123
    protected TokenFactory tokenFactory;
122
124
123
    public FreeFormLexerPhase1(java.io.InputStream in, IFile file, String filename, TokenFactory tokenFactory, boolean accumulateWhitetext)
125
    public FreeFormLexerPhase1(Reader in, IFile file, String filename, TokenFactory tokenFactory, boolean accumulateWhitetext)
124
    {
126
    {
125
        this(new LineAppendingInputStream(in));
127
        this(new LineAppendingReader(in));
126
        this.lastTokenFile = new FileOrIFile(file);
128
        this.lastTokenFile = new FileOrIFile(file);
127
        this.filename = filename;
129
        this.filename = filename;
128
        this.tokenFactory = tokenFactory;
130
        this.tokenFactory = tokenFactory;
(-)lexer/org/eclipse/photran/internal/core/lexer/FixedFormLexerPhase1.java (-6 / +6 lines)
Lines 1-4 Link Here
1
/* The following code was generated by JFlex 1.4.1 on 3/18/10 4:23 PM */
1
/* The following code was generated by JFlex 1.4.1 on 5/11/10 8:56 PM */
2
2
3
/*******************************************************************************
3
/*******************************************************************************
4
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
4
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
Lines 36-42 Link Here
36
package org.eclipse.photran.internal.core.lexer;
36
package org.eclipse.photran.internal.core.lexer;
37
37
38
import java.util.regex.Pattern;
38
import java.util.regex.Pattern;
39
import java.io.InputStream;
39
import java.io.Reader;
40
import org.eclipse.core.resources.IFile;
40
import org.eclipse.core.resources.IFile;
41
41
42
@SuppressWarnings("unused")
42
@SuppressWarnings("unused")
Lines 44-50 Link Here
44
/**
44
/**
45
 * This class is a scanner generated by 
45
 * This class is a scanner generated by 
46
 * <a href="http://www.jflex.de/">JFlex</a> 1.4.1
46
 * <a href="http://www.jflex.de/">JFlex</a> 1.4.1
47
 * on 3/18/10 4:23 PM from the specification file
47
 * on 5/11/10 8:56 PM from the specification file
48
 * <tt>FixedFormLexerPhase1.flex</tt>
48
 * <tt>FixedFormLexerPhase1.flex</tt>
49
 */
49
 */
50
class FixedFormLexerPhase1 implements ILexer {
50
class FixedFormLexerPhase1 implements ILexer {
Lines 916-923 Link Here
916
916
917
    private TokenFactory tokenFactory;
917
    private TokenFactory tokenFactory;
918
918
919
    public FixedFormLexerPhase1(InputStream in, FixedFormLexerPrepass _prepass, TokenFactory tokenFactory) {
919
    public FixedFormLexerPhase1(Reader in, FixedFormLexerPrepass _prepass, TokenFactory tokenFactory) {
920
        this(new LineAppendingInputStream(in));
920
        this(new LineAppendingReader(in));
921
        this.prepass=_prepass;
921
        this.prepass=_prepass;
922
        this.tokenFactory = tokenFactory;
922
        this.tokenFactory = tokenFactory;
923
    }
923
    }
Lines 1046-1052 Link Here
1046
1046
1047
    private String filename = "<stdin>";
1047
    private String filename = "<stdin>";
1048
1048
1049
    public FixedFormLexerPhase1(java.io.InputStream in, FixedFormLexerPrepass _prepass, IFile file, String filename, TokenFactory tokenFactory)
1049
    public FixedFormLexerPhase1(Reader in, FixedFormLexerPrepass _prepass, IFile file, String filename, TokenFactory tokenFactory)
1050
    {
1050
    {
1051
        this(in, _prepass, tokenFactory);
1051
        this(in, _prepass, tokenFactory);
1052
        this.lastTokenFile = new FileOrIFile(file);
1052
        this.lastTokenFile = new FileOrIFile(file);
(-)lexer/org/eclipse/photran/internal/core/lexer/FixedFormLexerPhase2.java (-5 / +12 lines)
Lines 11-17 Link Here
11
package org.eclipse.photran.internal.core.lexer;
11
package org.eclipse.photran.internal.core.lexer;
12
12
13
import java.io.IOException;
13
import java.io.IOException;
14
import java.io.InputStream;
14
import java.io.Reader;
15
15
16
import org.eclipse.core.resources.IFile;
16
import org.eclipse.core.resources.IFile;
17
17
Lines 29-40 Link Here
29
29
30
    private IToken nextToken = null;
30
    private IToken nextToken = null;
31
31
32
    public FixedFormLexerPhase2(InputStream in, IFile file, String filename, TokenFactory tokenFactory)
32
    public FixedFormLexerPhase2(Reader in, IFile file, String filename, TokenFactory tokenFactory)
33
    {
33
    {
34
        in = new LineAppendingInputStream(in);
34
        final Reader input = new LineAppendingReader(in);
35
        final FixedFormLexerPrepass prepass = new FixedFormLexerPrepass(in);
35
        final FixedFormLexerPrepass prepass = new FixedFormLexerPrepass(input);
36
        InputStream prepassReader = new InputStream()
36
        Reader prepassReader = new SingleCharReader()
37
        {
37
        {
38
            @Override
38
            public int read() throws IOException
39
            public int read() throws IOException
39
            {
40
            {
40
                try
41
                try
Lines 47-52 Link Here
47
                    return -1;
48
                    return -1;
48
                }
49
                }
49
            }
50
            }
51
            
52
            @Override
53
            public void close() throws IOException
54
            {
55
                input.close();
56
            }
50
        };
57
        };
51
58
52
        FixedFormLexerPhase1 fixedLexer1 = new FixedFormLexerPhase1(prepassReader, prepass, file, filename, tokenFactory);
59
        FixedFormLexerPhase1 fixedLexer1 = new FixedFormLexerPhase1(prepassReader, prepass, file, filename, tokenFactory);
(-)lexer/org/eclipse/photran/internal/core/lexer/FixedFormLexerPrepass.java (-3 / +3 lines)
Lines 64-72 Link Here
64
		this.in = new OffsetLineReader(in);
64
		this.in = new OffsetLineReader(in);
65
	}
65
	}
66
	
66
	
67
	public FixedFormLexerPrepass(InputStream in) {
67
//	public FixedFormLexerPrepass(InputStream in) {
68
		this(new InputStreamReader(in));
68
//		this(new InputStreamReader(in));
69
	}
69
//	}
70
	
70
	
71
	public int getLine(int absCharPos) {
71
	public int getLine(int absCharPos) {
72
		if (absCharPos<0) return 0;
72
		if (absCharPos<0) return 0;
(-)lexer/org/eclipse/photran/internal/core/lexer/FreeFormLexerPhase1.java (-4 / +6 lines)
Lines 1-4 Link Here
1
/* The following code was generated by JFlex 1.4.1 on 3/5/10 10:46 AM */
1
/* The following code was generated by JFlex 1.4.1 on 5/11/10 8:56 PM */
2
2
3
/*******************************************************************************
3
/*******************************************************************************
4
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
4
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
Lines 29-36 Link Here
29
 */
29
 */
30
30
31
package org.eclipse.photran.internal.core.lexer;
31
package org.eclipse.photran.internal.core.lexer;
32
32
import java.io.FileNotFoundException;
33
import java.io.FileNotFoundException;
33
import java.io.IOException;
34
import java.io.IOException;
35
import java.io.Reader;
34
import org.eclipse.core.resources.IFile;
36
import org.eclipse.core.resources.IFile;
35
37
36
@SuppressWarnings("unused")
38
@SuppressWarnings("unused")
Lines 38-44 Link Here
38
/**
40
/**
39
 * This class is a scanner generated by 
41
 * This class is a scanner generated by 
40
 * <a href="http://www.jflex.de/">JFlex</a> 1.4.1
42
 * <a href="http://www.jflex.de/">JFlex</a> 1.4.1
41
 * on 3/5/10 10:46 AM from the specification file
43
 * on 5/11/10 8:56 PM from the specification file
42
 * <tt>FreeFormLexerPhase1.flex</tt>
44
 * <tt>FreeFormLexerPhase1.flex</tt>
43
 */
45
 */
44
public class FreeFormLexerPhase1 implements ILexer {
46
public class FreeFormLexerPhase1 implements ILexer {
Lines 2517-2525 Link Here
2517
    private String filename = "<stdin>";
2519
    private String filename = "<stdin>";
2518
    protected TokenFactory tokenFactory;
2520
    protected TokenFactory tokenFactory;
2519
2521
2520
    public FreeFormLexerPhase1(java.io.InputStream in, IFile file, String filename, TokenFactory tokenFactory, boolean accumulateWhitetext)
2522
    public FreeFormLexerPhase1(Reader in, IFile file, String filename, TokenFactory tokenFactory, boolean accumulateWhitetext)
2521
    {
2523
    {
2522
        this(new LineAppendingInputStream(in));
2524
        this(new LineAppendingReader(in));
2523
        this.lastTokenFile = new FileOrIFile(file);
2525
        this.lastTokenFile = new FileOrIFile(file);
2524
        this.filename = filename;
2526
        this.filename = filename;
2525
        this.tokenFactory = tokenFactory;
2527
        this.tokenFactory = tokenFactory;
(-)lexer/org/eclipse/photran/internal/core/lexer/LineAppendingInputStream.java (-1 / +2 lines)
Lines 36-41 Link Here
36
        this.currentInputStream = originalInputStream;
36
        this.currentInputStream = originalInputStream;
37
    }
37
    }
38
38
39
    @Override
39
    public int read() throws IOException
40
    public int read() throws IOException
40
    {
41
    {
41
        int result = currentInputStream.read();
42
        int result = currentInputStream.read();
Lines 44-50 Link Here
44
        {
45
        {
45
            currentInputStream = eolInputStream;
46
            currentInputStream = eolInputStream;
46
            result = currentInputStream.read();
47
            result = currentInputStream.read();
47
            currentInputStream.available();
48
        }
48
        }
49
        else
49
        else
50
        {
50
        {
Lines 64-69 Link Here
64
        private static final byte[] EOL = System.getProperty("line.separator").getBytes();
64
        private static final byte[] EOL = System.getProperty("line.separator").getBytes();
65
        private int eolByte = 0;
65
        private int eolByte = 0;
66
66
67
        @Override
67
        public int read() throws IOException
68
        public int read() throws IOException
68
        {
69
        {
69
            return eolByte < EOL.length ? EOL[eolByte++] : -1;
70
            return eolByte < EOL.length ? EOL[eolByte++] : -1;
(-)lexer/org/eclipse/photran/internal/core/lexer/LineAppendingReader.java (+84 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 University of Illinois at Urbana-Champaign and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     UIUC - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
12
13
import java.io.IOException;
14
import java.io.Reader;
15
16
/**
17
 * Wraps another <code>Reader</code>, and, if the wrapped stream does not end
18
 * with an end-of-line character, appends the OS-dependent end-of-line sequence to
19
 * the end of the stream.
20
 * 
21
 * @author Jeff Overbey
22
 */
23
public final class LineAppendingReader extends SingleCharReader
24
{
25
    private Reader originalReader;
26
    private Reader eolReader;
27
    private Reader currentReader;
28
    
29
    private int lastChar = -1;
30
    
31
    public LineAppendingReader(Reader streamToWrap)
32
    {
33
        this.originalReader = streamToWrap;
34
        this.eolReader = new EOLReader();
35
        
36
        this.currentReader = originalReader;
37
    }
38
39
    @Override
40
    public int read() throws IOException
41
    {
42
        int result = currentReader.read();
43
        
44
        if (result == -1 && currentReader == originalReader && !isEOL(lastChar))
45
        {
46
            currentReader = eolReader;
47
            result = currentReader.read();
48
        }
49
        else
50
        {
51
            lastChar = result;
52
        }
53
        
54
        return result;
55
    }
56
57
    private boolean isEOL(int c)
58
    {
59
        return c =='\n' || c == '\r';
60
    }
61
62
    @Override
63
    public void close() throws IOException
64
    {
65
        originalReader.close();
66
    }
67
68
    private static class EOLReader extends SingleCharReader
69
    {
70
        private static final byte[] EOL = System.getProperty("line.separator").getBytes();
71
        private int eolByte = 0;
72
73
        @Override
74
        public int read() throws IOException
75
        {
76
            return eolByte < EOL.length ? EOL[eolByte++] : -1;
77
        }
78
79
        @Override
80
        public void close() throws IOException
81
        {
82
        }
83
    }
84
}
(-)lexer/org/eclipse/photran/internal/core/lexer/LineInputStream.java (-260 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 University of Illinois at Urbana-Champaign and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     UIUC - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
12
13
import java.io.IOException;
14
import java.io.InputStream;
15
16
/**
17
 * Java's <code>BufferedReader</code> can read a line of text from another
18
 * reader, but it omits the end-of-line character(s).  This one doesn't.
19
 * Subclass if necessary.
20
 * 
21
 * @author Jeff Overbey
22
 */
23
public final class LineInputStream extends InputStream implements CharSequence
24
{
25
    protected InputStream in;
26
    
27
    protected String filename;
28
    protected int startOffset;
29
    protected int startLineNum;
30
    protected int restartLine = -1;
31
    protected int restartOffset = -1;
32
    
33
    protected char[] currentLine = new char[4096];
34
    protected int currentLineLength = -1;
35
    protected int offsetInCurrentLine = -1;
36
    protected int offsetInStream = 0;
37
    protected int lineNumInCurrentStream = 0;
38
    protected boolean atEOF = false;
39
40
    public LineInputStream(InputStream readFrom, String filename) throws IOException
41
    {
42
        this(readFrom, filename, 0, 0);
43
    }
44
45
    public LineInputStream(InputStream readFrom, String filename, int startOffset, int startLineNum) throws IOException
46
    {
47
        this.in = readFrom;
48
        this.filename = filename;
49
        this.startOffset = startOffset;
50
        this.startLineNum = startLineNum;
51
        advanceToNextLine();
52
    }
53
54
    /**
55
     * THIS IS EXPENSIVE -- The text of the line is COPIED into a <code>String</code>.
56
     * @return
57
     * @throws IOException
58
     */
59
    public String currentLine() throws IOException
60
    {
61
        ensureLineLoaded();
62
        return atEOF() ? null : String.valueOf(currentLine, offsetInCurrentLine, currentLineLength-offsetInCurrentLine);
63
    }
64
    
65
    public boolean atBOL() throws IOException
66
    {
67
        ensureLineLoaded();
68
        return offsetInCurrentLine == 0;
69
    }
70
    
71
    public boolean atEOF() throws IOException
72
    {
73
        return offsetInCurrentLine >= currentLineLength && atEOF;
74
    }
75
76
    protected void ensureLineLoaded() throws IOException
77
    {
78
        if (!atEOF && offsetInCurrentLine >= currentLineLength)
79
            advanceToNextLine();
80
    }
81
82
    protected int peek() throws IOException
83
    {
84
        return atEOF() ? -1 : currentLine[offsetInCurrentLine];
85
    }
86
87
    public void advanceToNextLine() throws IOException
88
    {
89
        int c;
90
        
91
        offsetInCurrentLine = 0;
92
        offsetInStream += Math.max(currentLineLength-offsetInCurrentLine, 0);
93
        
94
        if (atEOF) return;
95
        
96
        lineNumInCurrentStream++;
97
        
98
        if (currentLineLength < 0)
99
        {
100
            // First attempt to read a line
101
            currentLineLength = 0;
102
        }
103
        else
104
        {
105
            // Start with the extra character from the last read
106
            currentLine[0] = currentLine[currentLineLength];
107
            currentLineLength = 1;
108
        }
109
        
110
        for (c = in.read(); c != -1 && c != '\n' && c != '\r'; c = in.read())
111
            append(c);
112
        
113
        if (c == -1) append('\n'); // Always end files with a newline
114
        
115
        for ( ; c != -1 && (c == '\n' || c == '\r'); c = in.read())
116
            append(c);
117
        
118
        if (c == -1) atEOF = true;
119
120
        // To find the end of the line, we had to read an extra character; don't lose it!
121
        currentLine[currentLineLength] = (char)c;
122
    }
123
    
124
    private void append(int c)
125
    {
126
        if (currentLineLength + 1 == currentLine.length)
127
        {
128
            char[] newCurrentLine = new char[currentLine.length*2];
129
            System.arraycopy(currentLine, 0, newCurrentLine, 0, currentLine.length);
130
            currentLine = newCurrentLine;
131
        }
132
        
133
        currentLine[currentLineLength++] = (char)c;
134
    }
135
    
136
    public String getFilename()
137
    {
138
        return filename;
139
    }
140
    
141
    public int getFileOffsetOfNextRead()
142
    {
143
        return offsetInStream;
144
    }
145
    
146
    public int getFileLineOfNextRead()
147
    {
148
        return lineNumInCurrentStream;
149
    }
150
    
151
    public int getTotalOffsetOfNextRead()
152
    {
153
        return startOffset + offsetInStream;
154
    }
155
    
156
    public int getTotalLineOfNextRead()
157
    {
158
        return startLineNum + lineNumInCurrentStream;
159
    }
160
161
    public int getStartLine()
162
    {
163
        return startLineNum;
164
    }
165
166
    public int getStartOffset()
167
    {
168
        return startOffset;
169
    }
170
171
    public int getRestartLine()
172
    {
173
        return restartLine;
174
    }
175
176
    public void setRestartLine(int restartLine)
177
    {
178
        this.restartLine = restartLine;
179
    }
180
181
    public int getRestartOffset()
182
    {
183
        return restartOffset;
184
    }
185
186
    public void setRestartOffset(int restartOffset)
187
    {
188
        this.restartOffset = restartOffset;
189
    }
190
    
191
    ///////////////////////////////////////////////////////////////////////////
192
    // INPUTSTREAM IMPLEMENTATION
193
    ///////////////////////////////////////////////////////////////////////////
194
195
    public int read() throws IOException
196
    {
197
        ensureLineLoaded();
198
        int result = peek();
199
        if (result >= 0)
200
        {
201
            offsetInCurrentLine++;
202
            offsetInStream++;
203
        }
204
        return result;
205
    }
206
207
    ///////////////////////////////////////////////////////////////////////////
208
    // CHARSEQUENCE IMPLEMENTATION
209
    ///////////////////////////////////////////////////////////////////////////
210
    
211
    public char charAt(int index)
212
    {
213
        return currentLine[offsetInCurrentLine+index];
214
    }
215
216
    public int length()
217
    {
218
        return currentLineLength-offsetInCurrentLine;
219
    }
220
221
    public CharSequence subSequence(final int start, final int end)
222
    {
223
        if (start < offsetInCurrentLine || end >= length())
224
            throw new IllegalArgumentException();
225
        
226
        return new SubSequence(start, end);
227
    }
228
    
229
    private final class SubSequence implements CharSequence
230
    {
231
        private final int start;
232
        private final int end;
233
234
        private SubSequence(int start, int end)
235
        {
236
            this.start = start;
237
            this.end = end;
238
        }
239
240
        public char charAt(int index)
241
        {
242
            return currentLine[offsetInCurrentLine+start];
243
        }
244
245
        public int length()
246
        {
247
            return end-start;
248
        }
249
250
        public CharSequence subSequence(int subseqStart, int subseqEnd)
251
        {
252
            return new SubSequence(start+subseqStart, start+subseqEnd);
253
        }
254
        
255
        public String toString()
256
        {
257
            return String.valueOf(currentLine, start, end-start);
258
        }
259
    }
260
}
(-)lexer/org/eclipse/photran/internal/core/lexer/LineReader.java (+266 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 University of Illinois at Urbana-Champaign and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     UIUC - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
12
13
import java.io.IOException;
14
import java.io.Reader;
15
16
/**
17
 * Java's <code>BufferedReader</code> can read a line of text from another
18
 * reader, but it omits the end-of-line character(s).  This one doesn't.
19
 * Subclass if necessary.
20
 * 
21
 * @author Jeff Overbey
22
 */
23
public final class LineReader extends SingleCharReader implements CharSequence
24
{
25
    protected Reader in;
26
    
27
    protected String filename;
28
    protected int startOffset;
29
    protected int startLineNum;
30
    protected int restartLine = -1;
31
    protected int restartOffset = -1;
32
    
33
    protected char[] currentLine = new char[4096];
34
    protected int currentLineLength = -1;
35
    protected int offsetInCurrentLine = -1;
36
    protected int offsetInStream = 0;
37
    protected int lineNumInCurrentStream = 0;
38
    protected boolean atEOF = false;
39
40
    public LineReader(Reader readFrom, String filename) throws IOException
41
    {
42
        this(readFrom, filename, 0, 0);
43
    }
44
45
    public LineReader(Reader readFrom, String filename, int startOffset, int startLineNum) throws IOException
46
    {
47
        this.in = readFrom;
48
        this.filename = filename;
49
        this.startOffset = startOffset;
50
        this.startLineNum = startLineNum;
51
        advanceToNextLine();
52
    }
53
54
    /**
55
     * THIS IS EXPENSIVE -- The text of the line is COPIED into a <code>String</code>.
56
     * @return
57
     * @throws IOException
58
     */
59
    public String currentLine() throws IOException
60
    {
61
        ensureLineLoaded();
62
        return atEOF() ? null : String.valueOf(currentLine, offsetInCurrentLine, currentLineLength-offsetInCurrentLine);
63
    }
64
    
65
    public boolean atBOL() throws IOException
66
    {
67
        ensureLineLoaded();
68
        return offsetInCurrentLine == 0;
69
    }
70
    
71
    public boolean atEOF() throws IOException
72
    {
73
        return offsetInCurrentLine >= currentLineLength && atEOF;
74
    }
75
76
    protected void ensureLineLoaded() throws IOException
77
    {
78
        if (!atEOF && offsetInCurrentLine >= currentLineLength)
79
            advanceToNextLine();
80
    }
81
82
    protected int peek() throws IOException
83
    {
84
        return atEOF() ? -1 : currentLine[offsetInCurrentLine];
85
    }
86
87
    public void advanceToNextLine() throws IOException
88
    {
89
        int c;
90
        
91
        offsetInCurrentLine = 0;
92
        offsetInStream += Math.max(currentLineLength-offsetInCurrentLine, 0);
93
        
94
        if (atEOF) return;
95
        
96
        lineNumInCurrentStream++;
97
        
98
        if (currentLineLength < 0)
99
        {
100
            // First attempt to read a line
101
            currentLineLength = 0;
102
        }
103
        else
104
        {
105
            // Start with the extra character from the last read
106
            currentLine[0] = currentLine[currentLineLength];
107
            currentLineLength = 1;
108
        }
109
        
110
        for (c = in.read(); c != -1 && c != '\n' && c != '\r'; c = in.read())
111
            append(c);
112
        
113
        if (c == -1) append('\n'); // Always end files with a newline
114
        
115
        for ( ; c != -1 && (c == '\n' || c == '\r'); c = in.read())
116
            append(c);
117
        
118
        if (c == -1) atEOF = true;
119
120
        // To find the end of the line, we had to read an extra character; don't lose it!
121
        currentLine[currentLineLength] = (char)c;
122
    }
123
    
124
    private void append(int c)
125
    {
126
        if (currentLineLength + 1 == currentLine.length)
127
        {
128
            char[] newCurrentLine = new char[currentLine.length*2];
129
            System.arraycopy(currentLine, 0, newCurrentLine, 0, currentLine.length);
130
            currentLine = newCurrentLine;
131
        }
132
        
133
        currentLine[currentLineLength++] = (char)c;
134
    }
135
    
136
    public String getFilename()
137
    {
138
        return filename;
139
    }
140
    
141
    public int getFileOffsetOfNextRead()
142
    {
143
        return offsetInStream;
144
    }
145
    
146
    public int getFileLineOfNextRead()
147
    {
148
        return lineNumInCurrentStream;
149
    }
150
    
151
    public int getTotalOffsetOfNextRead()
152
    {
153
        return startOffset + offsetInStream;
154
    }
155
    
156
    public int getTotalLineOfNextRead()
157
    {
158
        return startLineNum + lineNumInCurrentStream;
159
    }
160
161
    public int getStartLine()
162
    {
163
        return startLineNum;
164
    }
165
166
    public int getStartOffset()
167
    {
168
        return startOffset;
169
    }
170
171
    public int getRestartLine()
172
    {
173
        return restartLine;
174
    }
175
176
    public void setRestartLine(int restartLine)
177
    {
178
        this.restartLine = restartLine;
179
    }
180
181
    public int getRestartOffset()
182
    {
183
        return restartOffset;
184
    }
185
186
    public void setRestartOffset(int restartOffset)
187
    {
188
        this.restartOffset = restartOffset;
189
    }
190
    
191
    ///////////////////////////////////////////////////////////////////////////
192
    // INPUTSTREAM IMPLEMENTATION
193
    ///////////////////////////////////////////////////////////////////////////
194
195
    public int read() throws IOException
196
    {
197
        ensureLineLoaded();
198
        int result = peek();
199
        if (result >= 0)
200
        {
201
            offsetInCurrentLine++;
202
            offsetInStream++;
203
        }
204
        return result;
205
    }
206
207
    ///////////////////////////////////////////////////////////////////////////
208
    // CHARSEQUENCE IMPLEMENTATION
209
    ///////////////////////////////////////////////////////////////////////////
210
    
211
    public char charAt(int index)
212
    {
213
        return currentLine[offsetInCurrentLine+index];
214
    }
215
216
    public int length()
217
    {
218
        return currentLineLength-offsetInCurrentLine;
219
    }
220
221
    public CharSequence subSequence(final int start, final int end)
222
    {
223
        if (start < offsetInCurrentLine || end >= length())
224
            throw new IllegalArgumentException();
225
        
226
        return new SubSequence(start, end);
227
    }
228
    
229
    private final class SubSequence implements CharSequence
230
    {
231
        private final int start;
232
        private final int end;
233
234
        private SubSequence(int start, int end)
235
        {
236
            this.start = start;
237
            this.end = end;
238
        }
239
240
        public char charAt(int index)
241
        {
242
            return currentLine[offsetInCurrentLine+start];
243
        }
244
245
        public int length()
246
        {
247
            return end-start;
248
        }
249
250
        public CharSequence subSequence(int subseqStart, int subseqEnd)
251
        {
252
            return new SubSequence(start+subseqStart, start+subseqEnd);
253
        }
254
        
255
        public String toString()
256
        {
257
            return String.valueOf(currentLine, start, end-start);
258
        }
259
    }
260
261
    @Override
262
    public void close() throws IOException
263
    {
264
        in.close();
265
    }
266
}
(-)lexer/org/eclipse/photran/internal/core/lexer/SingleCharReader.java (+66 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
12
13
import java.io.IOException;
14
import java.io.Reader;
15
16
17
/**
18
 * Copied from org.eclipse.jface.internal.text.html.SingleCharReader
19
 * <p>
20
 * Moved into this package from <code>org.eclipse.jface.internal.text.revisions</code>.</p>
21
 */
22
public abstract class SingleCharReader extends Reader {
23
24
	/**
25
	 * @see Reader#read()
26
	 */
27
	public abstract int read() throws IOException;
28
29
	/**
30
	 * @see Reader#read(char[],int,int)
31
	 */
32
	public int read(char cbuf[], int off, int len) throws IOException {
33
		int end= off + len;
34
		for (int i= off; i < end; i++) {
35
			int ch= read();
36
			if (ch == -1) {
37
				if (i == off)
38
					return -1;
39
				return i - off;
40
			}
41
			cbuf[i]= (char)ch;
42
		}
43
		return len;
44
	}
45
46
	/**
47
	 * @see Reader#ready()
48
	 */
49
    public boolean ready() throws IOException {
50
		return true;
51
	}
52
53
	/**
54
	 * Returns the readable content as string.
55
	 * @return the readable content as string
56
	 * @exception IOException in case reading fails
57
	 */
58
	public String getString() throws IOException {
59
		StringBuffer buf= new StringBuffer();
60
		int ch;
61
		while ((ch= read()) != -1) {
62
			buf.append((char)ch);
63
		}
64
		return buf.toString();
65
	}
66
}
(-)parser/org/eclipse/photran/internal/core/lexer/LexerFactory.java (-6 / +11 lines)
Lines 10-20 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
11
package org.eclipse.photran.internal.core.lexer;
12
12
13
import java.io.BufferedInputStream;
13
import java.io.BufferedReader;
14
import java.io.File;
14
import java.io.File;
15
import java.io.FileInputStream;
15
import java.io.FileReader;
16
import java.io.IOException;
16
import java.io.IOException;
17
import java.io.InputStream;
17
import java.io.InputStreamReader;
18
import java.io.Reader;
18
19
19
import org.eclipse.core.resources.IFile;
20
import org.eclipse.core.resources.IFile;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.CoreException;
Lines 29-47 Link Here
29
{
30
{
30
    private LexerFactory() {;}
31
    private LexerFactory() {;}
31
    
32
    
32
    public static IAccumulatingLexer createLexer(InputStream in, IFile file, String filename, SourceForm sourceForm, boolean accumulateWhitetext) throws IOException
33
    public static IAccumulatingLexer createLexer(Reader in, IFile file, String filename, SourceForm sourceForm, boolean accumulateWhitetext) throws IOException
33
    {
34
    {
34
        return sourceForm.createLexer(in, file, filename, accumulateWhitetext);
35
        return sourceForm.createLexer(in, file, filename, accumulateWhitetext);
35
    }
36
    }
36
    
37
    
37
    public static IAccumulatingLexer createLexer(File file, SourceForm sourceForm, boolean accumulateWhitetext) throws IOException
38
    public static IAccumulatingLexer createLexer(File file, SourceForm sourceForm, boolean accumulateWhitetext) throws IOException
38
    {
39
    {
39
        return createLexer(new BufferedInputStream(new FileInputStream(file)), null, file.getAbsolutePath(), sourceForm, accumulateWhitetext);
40
        return createLexer(new BufferedReader(new FileReader(file)), null, file.getAbsolutePath(), sourceForm, accumulateWhitetext);
40
    }
41
    }
41
    
42
    
42
    public static IAccumulatingLexer createLexer(IFile file, SourceForm sourceForm, boolean accumulateWhitetext) throws CoreException, IOException
43
    public static IAccumulatingLexer createLexer(IFile file, SourceForm sourceForm, boolean accumulateWhitetext) throws CoreException, IOException
43
    {
44
    {
44
        return createLexer(file.getContents(true), file, determineFilename(file), sourceForm, accumulateWhitetext);
45
        return createLexer(new BufferedReader(new InputStreamReader(file.getContents(true), file.getCharset())),
46
            file,
47
            determineFilename(file),
48
            sourceForm,
49
            accumulateWhitetext);
45
    }
50
    }
46
51
47
    private static String determineFilename(IFile file)
52
    private static String determineFilename(IFile file)
(-)parser/org/eclipse/photran/internal/core/lexer/SourceForm.java (-5 / +5 lines)
Lines 12-17 Link Here
12
12
13
import java.io.IOException;
13
import java.io.IOException;
14
import java.io.InputStream;
14
import java.io.InputStream;
15
import java.io.Reader;
15
16
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.core.runtime.CoreException;
Lines 21-27 Link Here
21
import org.eclipse.photran.internal.core.FortranCorePlugin;
22
import org.eclipse.photran.internal.core.FortranCorePlugin;
22
import org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include.IncludeLoaderCallback;
23
import org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include.IncludeLoaderCallback;
23
import org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include.PreprocessingFreeFormLexerPhase1;
24
import org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include.PreprocessingFreeFormLexerPhase1;
24
import org.eclipse.photran.internal.core.vpg.PhotranVPG;
25
25
26
/**
26
/**
27
 * Contains constants enumerating the various Fortran source forms (fixed form, free form, free
27
 * Contains constants enumerating the various Fortran source forms (fixed form, free form, free
Lines 63-75 Link Here
63
     *     </ol>
63
     *     </ol>
64
     * </ol>
64
     * </ol>
65
     */
65
     */
66
    public abstract IAccumulatingLexer createLexer(InputStream in, IFile file, String filename, boolean accumulateWhitetext) throws IOException;
66
    public abstract IAccumulatingLexer createLexer(Reader in, IFile file, String filename, boolean accumulateWhitetext) throws IOException;
67
    
67
    
68
    public abstract String getDescription(String filename);
68
    public abstract String getDescription(String filename);
69
    
69
    
70
    public static final SourceForm UNPREPROCESSED_FREE_FORM = new SourceForm()
70
    public static final SourceForm UNPREPROCESSED_FREE_FORM = new SourceForm()
71
    {
71
    {
72
        @Override public IAccumulatingLexer createLexer(InputStream in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
72
        @Override public IAccumulatingLexer createLexer(Reader in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
73
        {
73
        {
74
            return new LexerPhase3(new FreeFormLexerPhase2(new FreeFormLexerPhase1(in, file, filename, ASTTokenFactory.getInstance(), accumulateWhitetext)));
74
            return new LexerPhase3(new FreeFormLexerPhase2(new FreeFormLexerPhase1(in, file, filename, ASTTokenFactory.getInstance(), accumulateWhitetext)));
75
        }
75
        }
Lines 79-85 Link Here
79
    
79
    
80
    public static final SourceForm FIXED_FORM = new SourceForm()
80
    public static final SourceForm FIXED_FORM = new SourceForm()
81
    {
81
    {
82
        @Override public IAccumulatingLexer createLexer(InputStream in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
82
        @Override public IAccumulatingLexer createLexer(Reader in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
83
        {
83
        {
84
            return new LexerPhase3(new FixedFormLexerPhase2(in, file, filename, ASTTokenFactory.getInstance()));
84
            return new LexerPhase3(new FixedFormLexerPhase2(in, file, filename, ASTTokenFactory.getInstance()));
85
        }
85
        }
Lines 96-102 Link Here
96
    {
96
    {
97
        return new SourceForm()
97
        return new SourceForm()
98
        {
98
        {
99
            @Override public IAccumulatingLexer createLexer(InputStream in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
99
            @Override public IAccumulatingLexer createLexer(Reader in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
100
            {
100
            {
101
                SourceForm sf = findPreferredSourceForm(filename);
101
                SourceForm sf = findPreferredSourceForm(filename);
102
                if (sf != null)
102
                if (sf != null)
(-)parser/org/eclipse/photran/internal/core/lexer/preprocessor/fortran_include/FortranPreprocessor.java (-19 / +27 lines)
Lines 12-18 Link Here
12
12
13
import java.io.FileNotFoundException;
13
import java.io.FileNotFoundException;
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.InputStream;
15
import java.io.Reader;
16
import java.util.ArrayList;
16
import java.util.ArrayList;
17
import java.util.LinkedList;
17
import java.util.LinkedList;
18
import java.util.Stack;
18
import java.util.Stack;
Lines 21-35 Link Here
21
21
22
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.resources.IFile;
23
import org.eclipse.photran.internal.core.lexer.FreeFormLexerPhase1;
23
import org.eclipse.photran.internal.core.lexer.FreeFormLexerPhase1;
24
import org.eclipse.photran.internal.core.lexer.LineInputStream;
24
import org.eclipse.photran.internal.core.lexer.LineReader;
25
import org.eclipse.photran.internal.core.lexer.SingleCharReader;
25
26
26
/**
27
/**
27
 * An <code>InputStream</code> that recognizes and processes Fortran INCLUDE lines.
28
 * An <code>Reader</code> that recognizes and processes Fortran INCLUDE lines.
28
 * <p> 
29
 * <p> 
29
 * This class implements an {@link InputStream} the provides the contents of a
30
 * This class implements an {@link Reader} the provides the contents of a
30
 * Fortran file <i>after</i> INCLUDE lines have been processed.  Photran's
31
 * Fortran file <i>after</i> INCLUDE lines have been processed.  Photran's
31
 * machine-constructed, free-form lexer ({@link FreeFormLexerPhase1}) tokenizes
32
 * machine-constructed, free-form lexer ({@link FreeFormLexerPhase1}) tokenizes
32
 * this InputStream, oblivious to the fact that it is actually tokenizing
33
 * this Reader, oblivious to the fact that it is actually tokenizing
33
 * preprocessed text.  However, a special subclass
34
 * preprocessed text.  However, a special subclass
34
 * ({@link PreprocessingFreeFormLexerPhase1}) takes care of mapping the
35
 * ({@link PreprocessingFreeFormLexerPhase1}) takes care of mapping the
35
 * preprocessed tokens back to their images and locations in the original files.
36
 * preprocessed tokens back to their images and locations in the original files.
Lines 41-50 Link Here
41
 * 
42
 * 
42
 * @author Jeff Overbey
43
 * @author Jeff Overbey
43
 */
44
 */
44
public final class FortranPreprocessor extends InputStream
45
public final class FortranPreprocessor extends SingleCharReader
45
{
46
{
46
    /**
47
    /**
47
     * Utility class.  Originally, we simply used a <pre>Stack<LineInputStream></pre>,
48
     * Utility class.  Originally, we simply used a <pre>Stack<LineReader></pre>,
48
     * but a profile revealed that Stack#peek was consuming a large amount of time
49
     * but a profile revealed that Stack#peek was consuming a large amount of time
49
     * due to repeated invocations.  Now, the #topStream field in this class is
50
     * due to repeated invocations.  Now, the #topStream field in this class is
50
     * accessed instead, resulting in a significant performance improvement
51
     * accessed instead, resulting in a significant performance improvement
Lines 52-64 Link Here
52
     */
53
     */
53
    private static final class StreamStack
54
    private static final class StreamStack
54
    {
55
    {
55
        private Stack<LineInputStream> streamStack = new Stack<LineInputStream>();
56
        private Stack<LineReader> streamStack = new Stack<LineReader>();
56
        private LineInputStream topStream = null;
57
        private LineReader topStream = null;
57
        
58
        
58
        public void push(LineInputStream lineInputStream)
59
        public void push(LineReader lineReader)
59
        {
60
        {
60
            streamStack.push(lineInputStream);
61
            streamStack.push(lineReader);
61
            topStream = lineInputStream;
62
            topStream = lineReader;
62
        }
63
        }
63
64
64
        public int size()
65
        public int size()
Lines 93-105 Link Here
93
    private ArrayList<Integer> fileStartLines;
94
    private ArrayList<Integer> fileStartLines;
94
    private ArrayList<Integer> fileStartLineAdjustments;
95
    private ArrayList<Integer> fileStartLineAdjustments;
95
    
96
    
96
    public FortranPreprocessor(InputStream readFrom, IFile file, String filename, IncludeLoaderCallback callback) throws IOException
97
    public FortranPreprocessor(Reader readFrom, IFile file, String filename, IncludeLoaderCallback callback) throws IOException
97
    {
98
    {
98
        this.topLevelFile = file;
99
        this.topLevelFile = file;
99
        this.callback = callback;
100
        this.callback = callback;
100
        
101
        
101
        streamStack = new StreamStack();
102
        streamStack = new StreamStack();
102
        streamStack.push(new LineInputStream(readFrom, filename));
103
        streamStack.push(new LineReader(readFrom, filename));
103
        
104
        
104
        directivesInTopLevelFile = new LinkedList<FortranIncludeDirective>();
105
        directivesInTopLevelFile = new LinkedList<FortranIncludeDirective>();
105
        directivesInTopLevelFile.add(null);
106
        directivesInTopLevelFile.add(null);
Lines 167-175 Link Here
167
        throw new IllegalArgumentException();
168
        throw new IllegalArgumentException();
168
    }
169
    }
169
    
170
    
171
    @Override
170
    public int read() throws IOException
172
    public int read() throws IOException
171
    {
173
    {
172
        LineInputStream currentStream = streamStack.topStream;
174
        LineReader currentStream = streamStack.topStream;
173
        if (currentStream.atBOL()) checkForInclude();
175
        if (currentStream.atBOL()) checkForInclude();
174
        currentStream = streamStack.topStream;
176
        currentStream = streamStack.topStream;
175
        if (currentStream.atEOF()) finishInclude();
177
        if (currentStream.atEOF()) finishInclude();
Lines 192-202 Link Here
192
        Matcher m = INCLUDE_LINE.matcher(streamStack.topStream);
194
        Matcher m = INCLUDE_LINE.matcher(streamStack.topStream);
193
        if (m.matches())
195
        if (m.matches())
194
        {
196
        {
195
            LineInputStream origStream = streamStack.topStream;
197
            LineReader origStream = streamStack.topStream;
196
            String includeLine = origStream.currentLine();
198
            String includeLine = origStream.currentLine();
197
            String fileToInclude = m.group(INCLUDE_LINE_CAPTURING_GROUP_OF_FILENAME);
199
            String fileToInclude = m.group(INCLUDE_LINE_CAPTURING_GROUP_OF_FILENAME);
198
            
200
            
199
            InputStream newStream = findIncludedFile(fileToInclude);
201
            Reader newStream = findIncludedFile(fileToInclude);
200
            if (newStream != null)
202
            if (newStream != null)
201
            {
203
            {
202
                if (inTopLevelFile())
204
                if (inTopLevelFile())
Lines 208-214 Link Here
208
                origStream.setRestartOffset(offset + includeLine.length());
210
                origStream.setRestartOffset(offset + includeLine.length());
209
                origStream.setRestartLine(line + 1);
211
                origStream.setRestartLine(line + 1);
210
                
212
                
211
                streamStack.push(new LineInputStream(newStream, fileToInclude, offset, line));
213
                streamStack.push(new LineReader(newStream, fileToInclude, offset, line));
212
                
214
                
213
                fileNames.add(fileToInclude);
215
                fileNames.add(fileToInclude);
214
                fileStartOffsets.add(new Integer(offset));
216
                fileStartOffsets.add(new Integer(offset));
Lines 222-228 Link Here
222
        }
224
        }
223
    }
225
    }
224
226
225
    protected InputStream findIncludedFile(String fileToInclude) throws IOException
227
    protected Reader findIncludedFile(String fileToInclude) throws IOException
226
    {
228
    {
227
        try
229
        try
228
        {
230
        {
Lines 282-285 Link Here
282
    {
284
    {
283
        return topLevelFile;
285
        return topLevelFile;
284
    }
286
    }
287
288
    @Override
289
    public void close() throws IOException
290
    {
291
        streamStack.topStream.close();
292
    }
285
}
293
}
(-)parser/org/eclipse/photran/internal/core/lexer/preprocessor/fortran_include/IncludeLoaderCallback.java (-3 / +14 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include;
11
package org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include;
12
12
13
import java.io.BufferedReader;
13
import java.io.FileNotFoundException;
14
import java.io.FileNotFoundException;
14
import java.io.InputStream;
15
import java.io.InputStreamReader;
16
import java.io.Reader;
17
import java.io.UnsupportedEncodingException;
15
18
16
import org.eclipse.core.resources.IContainer;
19
import org.eclipse.core.resources.IContainer;
17
import org.eclipse.core.resources.IFile;
20
import org.eclipse.core.resources.IFile;
Lines 44-54 Link Here
44
     * @return <code>InputStream</code>, not null
47
     * @return <code>InputStream</code>, not null
45
     * @throws FileNotFoundException if the file cannot be found
48
     * @throws FileNotFoundException if the file cannot be found
46
     */
49
     */
47
    public InputStream getIncludedFileAsStream(String fileToInclude) throws FileNotFoundException
50
    public Reader getIncludedFileAsStream(String fileToInclude) throws FileNotFoundException
48
    {
51
    {
49
        try
52
        try
50
        {
53
        {
51
        	return getIncludedFile(fileToInclude).getContents(true);
54
        	IFile file = getIncludedFile(fileToInclude);
55
            try
56
            {
57
                return new BufferedReader(new InputStreamReader(file.getContents(true), file.getCharset()));
58
            }
59
            catch (UnsupportedEncodingException e)
60
            {
61
                return new BufferedReader(new InputStreamReader(file.getContents(true)));
62
            }
52
        }
63
        }
53
        catch (CoreException e)
64
        catch (CoreException e)
54
        {
65
        {
(-)parser/org/eclipse/photran/internal/core/lexer/preprocessor/fortran_include/PreprocessingFreeFormLexerPhase1.java (-3 / +4 lines)
Lines 12-23 Link Here
12
12
13
import java.io.IOException;
13
import java.io.IOException;
14
import java.io.InputStream;
14
import java.io.InputStream;
15
import java.io.Reader;
15
16
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IFile;
17
import org.eclipse.photran.internal.core.lexer.ASTTokenFactory;
18
import org.eclipse.photran.internal.core.lexer.ASTTokenFactory;
18
import org.eclipse.photran.internal.core.lexer.FreeFormLexerPhase1;
19
import org.eclipse.photran.internal.core.lexer.FreeFormLexerPhase1;
19
import org.eclipse.photran.internal.core.lexer.LexerException;
20
import org.eclipse.photran.internal.core.lexer.LexerException;
20
import org.eclipse.photran.internal.core.lexer.LineAppendingInputStream;
21
import org.eclipse.photran.internal.core.lexer.LineAppendingReader;
21
import org.eclipse.photran.internal.core.lexer.Token;
22
import org.eclipse.photran.internal.core.lexer.Token;
22
import org.eclipse.photran.internal.core.lexer.TokenFactory;
23
import org.eclipse.photran.internal.core.lexer.TokenFactory;
23
24
Lines 39-47 Link Here
39
    private FortranPreprocessor preprocessor;
40
    private FortranPreprocessor preprocessor;
40
    private FortranIncludeDirective lastDirective;
41
    private FortranIncludeDirective lastDirective;
41
42
42
    public PreprocessingFreeFormLexerPhase1(InputStream in, IFile file, String filename, IncludeLoaderCallback callback, boolean accumulateWhitetext) throws IOException
43
    public PreprocessingFreeFormLexerPhase1(Reader in, IFile file, String filename, IncludeLoaderCallback callback, boolean accumulateWhitetext) throws IOException
43
    {
44
    {
44
        this(new FortranPreprocessor(new LineAppendingInputStream(in), file, filename, callback), file, filename, ASTTokenFactory.getInstance(), accumulateWhitetext);
45
        this(new FortranPreprocessor(new LineAppendingReader(in), file, filename, callback), file, filename, ASTTokenFactory.getInstance(), accumulateWhitetext);
45
    }
46
    }
46
    
47
    
47
    // This would not be here if we could assign the preprocessor to a variable in the above ctor (grrr)
48
    // This would not be here if we could assign the preprocessor to a variable in the above ctor (grrr)
(-)src/org/eclipse/photran/internal/core/refactoring/infrastructure/FortranResourceRefactoring.java (-1 / +4 lines)
Lines 11-16 Link Here
11
package org.eclipse.photran.internal.core.refactoring.infrastructure;
11
package org.eclipse.photran.internal.core.refactoring.infrastructure;
12
12
13
import java.io.ByteArrayInputStream;
13
import java.io.ByteArrayInputStream;
14
import java.io.InputStreamReader;
15
import java.nio.charset.Charset;
14
import java.util.ArrayList;
16
import java.util.ArrayList;
15
import java.util.Arrays;
17
import java.util.Arrays;
16
import java.util.Collection;
18
import java.util.Collection;
Lines 271-278 Link Here
271
    {
273
    {
272
        try
274
        try
273
        {
275
        {
276
            final String charset = Charset.defaultCharset().name();
274
            IAccumulatingLexer lexer = LexerFactory.createLexer(
277
            IAccumulatingLexer lexer = LexerFactory.createLexer(
275
                new ByteArrayInputStream(string.getBytes()), null, "(none)",
278
                new InputStreamReader(new ByteArrayInputStream(string.getBytes(charset)), charset), null, "(none)",
276
                SourceForm.UNPREPROCESSED_FREE_FORM, true);
279
                SourceForm.UNPREPROCESSED_FREE_FORM, true);
277
            Parser parser = new Parser();
280
            Parser parser = new Parser();
278
281
(-)src/org/eclipse/photran/internal/core/vpg/PhotranVPGBuilder.java (-9 / +21 lines)
Lines 10-20 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.vpg;
11
package org.eclipse.photran.internal.core.vpg;
12
12
13
import java.io.BufferedReader;
13
import java.io.ByteArrayInputStream;
14
import java.io.ByteArrayInputStream;
14
import java.io.ByteArrayOutputStream;
15
import java.io.ByteArrayOutputStream;
15
import java.io.FileNotFoundException;
16
import java.io.FileNotFoundException;
16
import java.io.InputStream;
17
import java.io.InputStreamReader;
17
import java.io.PrintStream;
18
import java.io.PrintStream;
19
import java.io.Reader;
20
import java.io.UnsupportedEncodingException;
21
import java.nio.charset.Charset;
18
import java.util.List;
22
import java.util.List;
19
23
20
import org.eclipse.core.resources.IFile;
24
import org.eclipse.core.resources.IFile;
Lines 26-34 Link Here
26
import org.eclipse.photran.internal.core.SyntaxException;
30
import org.eclipse.photran.internal.core.SyntaxException;
27
import org.eclipse.photran.internal.core.analysis.binding.Binder;
31
import org.eclipse.photran.internal.core.analysis.binding.Binder;
28
import org.eclipse.photran.internal.core.analysis.binding.Definition;
32
import org.eclipse.photran.internal.core.analysis.binding.Definition;
33
import org.eclipse.photran.internal.core.analysis.binding.Definition.Visibility;
29
import org.eclipse.photran.internal.core.analysis.binding.ImplicitSpec;
34
import org.eclipse.photran.internal.core.analysis.binding.ImplicitSpec;
30
import org.eclipse.photran.internal.core.analysis.binding.ScopingNode;
35
import org.eclipse.photran.internal.core.analysis.binding.ScopingNode;
31
import org.eclipse.photran.internal.core.analysis.binding.Definition.Visibility;
32
import org.eclipse.photran.internal.core.lexer.IAccumulatingLexer;
36
import org.eclipse.photran.internal.core.lexer.IAccumulatingLexer;
33
import org.eclipse.photran.internal.core.lexer.LexerException;
37
import org.eclipse.photran.internal.core.lexer.LexerException;
34
import org.eclipse.photran.internal.core.lexer.LexerFactory;
38
import org.eclipse.photran.internal.core.lexer.LexerFactory;
Lines 213-219 Link Here
213
            sourceForm = SourceForm.preprocessedFreeForm(new IncludeLoaderCallback(file.getProject())
217
            sourceForm = SourceForm.preprocessedFreeForm(new IncludeLoaderCallback(file.getProject())
214
            {
218
            {
215
                @Override
219
                @Override
216
                public InputStream getIncludedFileAsStream(String fileToInclude) throws FileNotFoundException
220
                public Reader getIncludedFileAsStream(String fileToInclude) throws FileNotFoundException
217
                {
221
                {
218
                    // When we encounter an INCLUDE directive, set up a file dependency in the VPG
222
                    // When we encounter an INCLUDE directive, set up a file dependency in the VPG
219
223
Lines 303-313 Link Here
303
        if (ast == null)
307
        if (ast == null)
304
            throw new IllegalArgumentException(filename + " returned null AST");
308
            throw new IllegalArgumentException(filename + " returned null AST");
305
        
309
        
306
        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
310
        try
307
        ast.getRoot().printOn(new PrintStream(out), null);
311
        {
308
        ast = parse(filename, new ByteArrayInputStream(out.toByteArray()));
312
            final String charset = Charset.defaultCharset().name(); // Platform's default charset
313
            ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
314
            ast.getRoot().printOn(new PrintStream(out, false, charset), null);
315
            ast = parse(filename, new InputStreamReader(new ByteArrayInputStream(out.toByteArray()), charset));
309
316
310
        computeEdgesAndAnnotations(filename, ast);
317
            computeEdgesAndAnnotations(filename, ast);
318
        }
319
        catch (UnsupportedEncodingException e)
320
        {
321
            throw new Error(e);
322
        }
311
    }
323
    }
312
324
313
    @Override public String getSourceCodeFromAST(IFortranAST ast)
325
    @Override public String getSourceCodeFromAST(IFortranAST ast)
Lines 321-327 Link Here
321
        return parse(filename, null);
333
        return parse(filename, null);
322
    }
334
    }
323
335
324
    private IFortranAST parse(final String filename, InputStream stream)
336
    private IFortranAST parse(final String filename, Reader stream)
325
    {
337
    {
326
        if (filename == null || isVirtualFile(filename)) return null;
338
        if (filename == null || isVirtualFile(filename)) return null;
327
339
Lines 331-337 Link Here
331
            SourceForm sourceForm = determineSourceForm(filename);
343
            SourceForm sourceForm = determineSourceForm(filename);
332
            try
344
            try
333
            {
345
            {
334
                if (stream == null) stream = file.getContents(true);
346
                if (stream == null) stream = new BufferedReader(new InputStreamReader(file.getContents(true), file.getCharset()));
335
                IAccumulatingLexer lexer = LexerFactory.createLexer(stream, file, filename, sourceForm, true);
347
                IAccumulatingLexer lexer = LexerFactory.createLexer(stream, file, filename, sourceForm, true);
336
                long start = System.currentTimeMillis();
348
                long start = System.currentTimeMillis();
337
                ASTExecutableProgramNode ast = parser.parse(lexer);
349
                ASTExecutableProgramNode ast = parser.parse(lexer);
(-)src/org/eclipse/photran/internal/core/lexer/CPreprocessedSourceForm.java (-2 / +2 lines)
Lines 11-17 Link Here
11
package org.eclipse.photran.internal.core.lexer;
11
package org.eclipse.photran.internal.core.lexer;
12
12
13
import java.io.IOException;
13
import java.io.IOException;
14
import java.io.InputStream;
14
import java.io.Reader;
15
15
16
import org.eclipse.core.resources.IFile;
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include.IncludeLoaderCallback;
17
import org.eclipse.photran.internal.core.lexer.preprocessor.fortran_include.IncludeLoaderCallback;
Lines 30-36 Link Here
30
    	this.callback = callback;
30
    	this.callback = callback;
31
    }
31
    }
32
32
33
    @Override public IAccumulatingLexer createLexer(InputStream in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
33
    @Override public IAccumulatingLexer createLexer(Reader in, IFile file, String filename, boolean accumulateWhitetext) throws IOException
34
    {
34
    {
35
        return new LexerPhase3(new FreeFormLexerPhase2(new CPreprocessingFreeFormLexerPhase1(in, file, filename, callback, accumulateWhitetext)));
35
        return new LexerPhase3(new FreeFormLexerPhase2(new CPreprocessingFreeFormLexerPhase1(in, file, filename, callback, accumulateWhitetext)));
36
    }
36
    }
(-)src/org/eclipse/photran/internal/core/lexer/CPreprocessingFreeFormLexerPhase1.java (-4 / +8 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
11
package org.eclipse.photran.internal.core.lexer;
12
12
13
import java.io.BufferedReader;
13
import java.io.IOException;
14
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.InputStream;
16
import java.io.InputStreamReader;
17
import java.io.Reader;
18
import java.nio.charset.Charset;
15
import java.util.ArrayList;
19
import java.util.ArrayList;
16
20
17
import org.eclipse.core.resources.IFile;
21
import org.eclipse.core.resources.IFile;
Lines 20-26 Link Here
20
/**
24
/**
21
 * Phase 1 lexer that handles C preprocessor directives.
25
 * Phase 1 lexer that handles C preprocessor directives.
22
 * <p>
26
 * <p>
23
 * This class feeds {@link FreeFormLexerPhase1} with a {@link CPreprocessingInputStream},
27
 * This class feeds {@link FreeFormLexerPhase1} with a {@link CPreprocessingReader},
24
 * an {@link InputStream} that interprets C preprocessor directives.
28
 * an {@link InputStream} that interprets C preprocessor directives.
25
 * {@link FreeFormLexerPhase1} tokenizes the preprocessed stream.  However, this
29
 * {@link FreeFormLexerPhase1} tokenizes the preprocessed stream.  However, this
26
 * class subclasses {@link FreeFormLexerPhase1} and overrides its tokenization
30
 * class subclasses {@link FreeFormLexerPhase1} and overrides its tokenization
Lines 49-61 Link Here
49
	
53
	
50
	private Token lastReadInFile = null;
54
	private Token lastReadInFile = null;
51
55
52
    public CPreprocessingFreeFormLexerPhase1(InputStream in, IFile file, String filename, IncludeLoaderCallback callback, boolean accumulateWhitetext) throws IOException
56
    public CPreprocessingFreeFormLexerPhase1(Reader in, IFile file, String filename, IncludeLoaderCallback callback, boolean accumulateWhitetext) throws IOException
53
    {
57
    {
54
        this(new CPreprocessingInputStream(file, filename, new LineAppendingInputStream(in)), file, filename, ASTTokenFactory.getInstance(), accumulateWhitetext);
58
        this(new CPreprocessingReader(file, filename, new LineAppendingReader(in)), file, filename, ASTTokenFactory.getInstance(), accumulateWhitetext);
55
    }
59
    }
56
60
57
    // This would not be here if we could assign the preprocessor to a variable in the above ctor (grrr)
61
    // This would not be here if we could assign the preprocessor to a variable in the above ctor (grrr)
58
    private CPreprocessingFreeFormLexerPhase1(CPreprocessingInputStream cpp, IFile file, String filename, TokenFactory tokenFactory, boolean accumulateWhitetext)
62
    private CPreprocessingFreeFormLexerPhase1(CPreprocessingReader cpp, IFile file, String filename, TokenFactory tokenFactory, boolean accumulateWhitetext)
59
    {
63
    {
60
        super(cpp, file, filename, tokenFactory, accumulateWhitetext);
64
        super(cpp, file, filename, tokenFactory, accumulateWhitetext);
61
        this.producerMap = cpp.getProducerMap();
65
        this.producerMap = cpp.getProducerMap();
(-)src/org/eclipse/photran/internal/core/lexer/CPreprocessingInputStream.java (-86 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     UIUC - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
12
13
import java.io.IOException;
14
import java.io.InputStream;
15
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.photran.internal.core.preprocessor.c.CppHelper;
18
import org.eclipse.photran.internal.core.preprocessor.c.IToken;
19
import org.eclipse.photran.internal.core.preprocessor.c.OffsetLimitReachedException;
20
21
/**
22
 * An InputStream to read tokens obtained from the CPP. This class will
23
 * also make a ProducerMap when constructed that can be used to find
24
 * directives and macros.
25
 * 
26
 * @author Matthew Michelotti
27
 */
28
public class CPreprocessingInputStream extends InputStream
29
{
30
	/**ProducerMap constructed from the CPP tokens*/
31
	private ProducerMap producerMap;
32
	
33
	private IncludeMap includeMap;
34
35
	/**current CPP token being read*/
36
	private IToken curToken;
37
	/**full image of curToken*/
38
	private String curTokenImage;
39
	/**the offset in curTokenImage to be read next*/
40
	private int curTokenOffset = 0;
41
	
42
	/**
43
	 * Basic constructor. Parses the whole file in the constructor
44
	 * and turns the result into CPP tokens. A ProducerMap is made
45
	 * from these tokens. The tokens are remembered, so that they
46
	 * can be read as an input stream.
47
	 * @param filename - name of file that inputStream corresponds to
48
	 * @param inputStream - InputStream to read file data from
49
	 * @throws IOException
50
	 */
51
	public CPreprocessingInputStream(IFile file, String filename, InputStream inputStream) throws IOException
52
	{
53
		try
54
		{
55
			CppHelper cpp = new CppHelper(file, filename, inputStream);
56
			curToken = cpp.getRemainingTokens();
57
			curTokenImage = CppHelper.getFullImage(curToken);
58
			producerMap = new ProducerMap(curToken);
59
			includeMap = new IncludeMap(curToken, file);
60
		}catch(OffsetLimitReachedException e) {throw new Error(e);}
61
	}
62
	
63
	@Override public int read() throws IOException
64
	{
65
		if(curToken == null) return -1;
66
		while(curTokenOffset >= curTokenImage.length()) {
67
			curToken = curToken.getNext();
68
			if(curToken == null) return -1;
69
			curTokenImage = CppHelper.getFullImage(curToken);
70
			curTokenOffset = 0;
71
		}
72
		char result = curTokenImage.charAt(curTokenOffset);
73
		curTokenOffset++;
74
		
75
		return result;
76
	}
77
	
78
	/**@return the ProducerMap object made at construction*/
79
	public ProducerMap getProducerMap() {
80
		return producerMap;
81
	}
82
	
83
	public IncludeMap getIncludeMap() {
84
		return includeMap;
85
	}
86
}
(-)src/org/eclipse/photran/internal/core/lexer/CPreprocessingReader.java (+91 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     UIUC - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.lexer;
12
13
import java.io.IOException;
14
import java.io.Reader;
15
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.photran.internal.core.preprocessor.c.CppHelper;
18
import org.eclipse.photran.internal.core.preprocessor.c.IToken;
19
import org.eclipse.photran.internal.core.preprocessor.c.OffsetLimitReachedException;
20
21
/**
22
 * An InputStream to read tokens obtained from the CPP. This class will
23
 * also make a ProducerMap when constructed that can be used to find
24
 * directives and macros.
25
 * 
26
 * @author Matthew Michelotti
27
 */
28
public class CPreprocessingReader extends SingleCharReader
29
{
30
	/**ProducerMap constructed from the CPP tokens*/
31
	private ProducerMap producerMap;
32
	
33
	private IncludeMap includeMap;
34
35
	/**current CPP token being read*/
36
	private IToken curToken;
37
	/**full image of curToken*/
38
	private String curTokenImage;
39
	/**the offset in curTokenImage to be read next*/
40
	private int curTokenOffset = 0;
41
	
42
	/**
43
	 * Basic constructor. Parses the whole file in the constructor
44
	 * and turns the result into CPP tokens. A ProducerMap is made
45
	 * from these tokens. The tokens are remembered, so that they
46
	 * can be read as an input stream.
47
	 * @param filename - name of file that inputStream corresponds to
48
	 * @param reader - Reader to read file data from
49
	 * @throws IOException
50
	 */
51
	public CPreprocessingReader(IFile file, String filename, Reader reader) throws IOException
52
	{
53
		try
54
		{
55
			CppHelper cpp = new CppHelper(file, filename, reader);
56
			curToken = cpp.getRemainingTokens();
57
			curTokenImage = CppHelper.getFullImage(curToken);
58
			producerMap = new ProducerMap(curToken);
59
			includeMap = new IncludeMap(curToken, file);
60
		}catch(OffsetLimitReachedException e) {throw new Error(e);}
61
	}
62
	
63
	@Override public int read() throws IOException
64
	{
65
		if(curToken == null) return -1;
66
		while(curTokenOffset >= curTokenImage.length()) {
67
			curToken = curToken.getNext();
68
			if(curToken == null) return -1;
69
			curTokenImage = CppHelper.getFullImage(curToken);
70
			curTokenOffset = 0;
71
		}
72
		char result = curTokenImage.charAt(curTokenOffset);
73
		curTokenOffset++;
74
		
75
		return result;
76
	}
77
	
78
	/**@return the ProducerMap object made at construction*/
79
	public ProducerMap getProducerMap() {
80
		return producerMap;
81
	}
82
	
83
	public IncludeMap getIncludeMap() {
84
		return includeMap;
85
	}
86
87
    @Override
88
    public void close() throws IOException
89
    {
90
    }
91
}
(-)src/org/eclipse/photran/internal/core/preprocessor/c/CppHelper.java (-7 / +5 lines)
Lines 12-20 Link Here
12
12
13
import java.io.File;
13
import java.io.File;
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.InputStream;
15
import java.io.Reader;
16
import java.util.Arrays;
16
import java.util.Arrays;
17
import java.util.Map;
18
17
19
import org.eclipse.cdt.core.CCorePlugin;
18
import org.eclipse.cdt.core.CCorePlugin;
20
import org.eclipse.cdt.core.dom.parser.c.GCCScannerExtensionConfiguration;
19
import org.eclipse.cdt.core.dom.parser.c.GCCScannerExtensionConfiguration;
Lines 24-30 Link Here
24
import org.eclipse.cdt.core.parser.NullLogService;
23
import org.eclipse.cdt.core.parser.NullLogService;
25
import org.eclipse.cdt.core.parser.ParserLanguage;
24
import org.eclipse.cdt.core.parser.ParserLanguage;
26
import org.eclipse.cdt.core.parser.ScannerInfo;
25
import org.eclipse.cdt.core.parser.ScannerInfo;
27
import org.eclipse.core.resources.IFile;
28
import org.eclipse.core.resources.IProject;
26
import org.eclipse.core.resources.IProject;
29
import org.eclipse.core.resources.IResource;
27
import org.eclipse.core.resources.IResource;
30
28
Lines 64-74 Link Here
64
     * @param includeSearchPaths - file paths to search in for included files
62
     * @param includeSearchPaths - file paths to search in for included files
65
     * @throws IOException
63
     * @throws IOException
66
     */
64
     */
67
    public CppHelper(String filename, InputStream in, String[] includeSearchPaths)
65
    public CppHelper(String filename, Reader in, String[] includeSearchPaths)
68
    		throws IOException
66
    		throws IOException
69
    {
67
    {
70
    	//NOTE: it is important that the CodeReader gets an ABSOLUTE file path
68
    	//NOTE: it is important that the CodeReader gets an ABSOLUTE file path
71
        cpp = new CPreprocessor(new InputStreamCodeReader(filename, in),
69
        cpp = new CPreprocessor(new ReaderBasedCodeReader(filename, in),
72
            new ScannerInfo(null, includeSearchPaths), ParserLanguage.C,
70
            new ScannerInfo(null, includeSearchPaths), ParserLanguage.C,
73
            new NullLogService(), new GCCScannerExtensionConfiguration(),
71
            new NullLogService(), new GCCScannerExtensionConfiguration(),
74
            FileCodeReaderFactory.getInstance());
72
            FileCodeReaderFactory.getInstance());
Lines 85-91 Link Here
85
     *      Also, @param in could be different than @param file, if for example there
83
     *      Also, @param in could be different than @param file, if for example there
86
     *      are un-saved changes in the editor.
84
     *      are un-saved changes in the editor.
87
     */
85
     */
88
    public CppHelper(IResource infoProvider, String filename, InputStream in) throws IOException {
86
    public CppHelper(IResource infoProvider, String filename, Reader in) throws IOException {
89
        //NOTE: it is important that the CodeReader gets an ABSOLUTE file path
87
        //NOTE: it is important that the CodeReader gets an ABSOLUTE file path
90
        if (filename == null) 
88
        if (filename == null) 
91
            filename = "";
89
            filename = "";
Lines 104-110 Link Here
104
        }
102
        }
105
103
106
        //Map<String, String> temp = scanInfo.getDefinedSymbols();
104
        //Map<String, String> temp = scanInfo.getDefinedSymbols();
107
        cpp = new CPreprocessor(new InputStreamCodeReader(filename, in),
105
        cpp = new CPreprocessor(new ReaderBasedCodeReader(filename, in),
108
            new ScannerInfo(scanInfo.getDefinedSymbols(), scanInfo.getIncludePaths()), ParserLanguage.C, new NullLogService(), 
106
            new ScannerInfo(scanInfo.getDefinedSymbols(), scanInfo.getIncludePaths()), ParserLanguage.C, new NullLogService(), 
109
            new GCCScannerExtensionConfiguration(), FileCodeReaderFactory.getInstance());
107
            new GCCScannerExtensionConfiguration(), FileCodeReaderFactory.getInstance());
110
    }
108
    }
(-)src/org/eclipse/photran/internal/core/preprocessor/c/InputStreamCodeReader.java (-63 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    UIUC - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.core.preprocessor.c;
12
13
import java.io.IOException;
14
import java.io.InputStream;
15
16
import org.eclipse.cdt.core.parser.CodeReader;
17
18
public class InputStreamCodeReader extends CodeReader 
19
{
20
21
	public InputStreamCodeReader(String filename, InputStream stream)
22
			throws IOException 
23
	{
24
		super(filename, convertToCharBuffer(stream));
25
		
26
		// TODO Auto-generated constructor stub
27
	}
28
	
29
	public InputStreamCodeReader(String filename, String charSet, InputStream stream)
30
			throws IOException 
31
	{
32
		//We are dropping out the charSet variable because it is used internally by 
33
		// CodeReader to read from the FileStream. Since we want to read from the 
34
		// InputStream, and we do it byte-wise, we don't really care for encoding
35
		super(filename, convertToCharBuffer(stream));
36
		// TODO Auto-generated constructor stub
37
	}
38
	
39
	private static char[] convertToCharBuffer(InputStream stream)
40
	{
41
		int b = 0;
42
		StringBuffer aggregate = new StringBuffer();
43
		while(b != -1)
44
		{
45
			try 
46
			{
47
				b = stream.read();
48
			} 
49
			catch (IOException e) 
50
			{
51
				e.printStackTrace();
52
				b = -1;
53
				break;
54
			}
55
			if(b != -1)
56
			{
57
				aggregate.append((char)b);
58
			}
59
		}
60
		return aggregate.toString().toCharArray();
61
	}
62
63
}
(-)src/org/eclipse/photran/internal/core/preprocessor/c/ReaderBasedCodeReader.java (+59 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 University of Illinois at Urbana-Champaign and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    UIUC - Initial API and implementation
10
 *           (Jeff Overbey) Modified to use Reader rather than InputStream
11
 *******************************************************************************/
12
package org.eclipse.photran.internal.core.preprocessor.c;
13
14
import java.io.IOException;
15
import java.io.Reader;
16
17
import org.eclipse.cdt.core.parser.CodeReader;
18
19
public class ReaderBasedCodeReader extends CodeReader 
20
{
21
22
	public ReaderBasedCodeReader(String filename, Reader stream)
23
			throws IOException 
24
	{
25
		super(filename, convertToCharBuffer(stream));
26
	}
27
	
28
	private static char[] convertToCharBuffer(Reader stream)
29
	{
30
		int b = 0;
31
		StringBuffer aggregate = new StringBuffer();
32
		while(b != -1)
33
		{
34
			try 
35
			{
36
				b = stream.read();
37
			} 
38
			catch (IOException e) 
39
			{
40
				e.printStackTrace();
41
				b = -1;
42
				break;
43
			}
44
			if(b != -1)
45
			{
46
				aggregate.append((char)b);
47
			}
48
		}
49
		try
50
        {
51
            stream.close();
52
        }
53
        catch (IOException e)
54
        {
55
            e.printStackTrace();
56
        }
57
		return aggregate.toString().toCharArray();
58
	}
59
}
(-)src/org/eclipse/photran/internal/core/tests/preprocessor/c/CppIntegratedSourceReproductionTestCase.java (-2 / +2 lines)
Lines 15-21 Link Here
15
import java.io.FileNotFoundException;
15
import java.io.FileNotFoundException;
16
import java.io.FileReader;
16
import java.io.FileReader;
17
import java.io.IOException;
17
import java.io.IOException;
18
import java.io.InputStream;
18
import java.io.Reader;
19
19
20
import org.eclipse.photran.internal.core.lexer.CPreprocessedSourceForm;
20
import org.eclipse.photran.internal.core.lexer.CPreprocessedSourceForm;
21
import org.eclipse.photran.internal.core.lexer.SourceForm;
21
import org.eclipse.photran.internal.core.lexer.SourceForm;
Lines 35-41 Link Here
35
    {
35
    {
36
        return new CPreprocessedSourceForm(new IncludeLoaderCallback(null)
36
        return new CPreprocessedSourceForm(new IncludeLoaderCallback(null)
37
        {
37
        {
38
            @Override public InputStream getIncludedFileAsStream(String fileToInclude) throws FileNotFoundException
38
            @Override public Reader getIncludedFileAsStream(String fileToInclude) throws FileNotFoundException
39
            {
39
            {
40
                throw new FileNotFoundException("#include lines not supported in JUnit tests");
40
                throw new FileNotFoundException("#include lines not supported in JUnit tests");
41
            }
41
            }
(-)src/org/eclipse/photran/internal/tests/BaseTestCase.java (-4 / +2 lines)
Lines 10-18 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.tests;
11
package org.eclipse.photran.internal.tests;
12
12
13
import java.io.ByteArrayInputStream;
14
import java.io.IOException;
13
import java.io.IOException;
15
import java.io.InputStream;
14
import java.io.StringReader;
16
15
17
import junit.framework.TestCase;
16
import junit.framework.TestCase;
18
17
Lines 63-70 Link Here
63
    protected <T extends IBodyConstruct> T parseStmt(String stmt) throws IOException, LexerException, SyntaxException
62
    protected <T extends IBodyConstruct> T parseStmt(String stmt) throws IOException, LexerException, SyntaxException
64
    {
63
    {
65
        String program = stmt + "\nend program\n";
64
        String program = stmt + "\nend program\n";
66
        InputStream in = new ByteArrayInputStream(program.getBytes());
65
        ASTExecutableProgramNode ast = new Parser().parse(LexerFactory.createLexer(new StringReader(program), null, null, SourceForm.UNPREPROCESSED_FREE_FORM, true));
67
        ASTExecutableProgramNode ast = new Parser().parse(LexerFactory.createLexer(in, null, null, SourceForm.UNPREPROCESSED_FREE_FORM, true));
68
        assertTrue(ast != null);
66
        assertTrue(ast != null);
69
        LoopReplacer.replaceAllLoopsIn(ast);
67
        LoopReplacer.replaceAllLoopsIn(ast);
70
        return (T)((ASTMainProgramNode)ast.getProgramUnitList().get(0)).getBody().get(0);
68
        return (T)((ASTMainProgramNode)ast.getProgramUnitList().get(0)).getBody().get(0);
(-)src/org/eclipse/photran/internal/tests/analysis/types/TypeCheckerTestCase.java (-2 / +2 lines)
Lines 10-16 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.tests.analysis.types;
11
package org.eclipse.photran.internal.tests.analysis.types;
12
12
13
import java.io.ByteArrayInputStream;
13
import java.io.StringReader;
14
14
15
import junit.framework.TestCase;
15
import junit.framework.TestCase;
16
16
Lines 39-45 Link Here
39
        // a parseable program, then extract the expression from the program's AST
39
        // a parseable program, then extract the expression from the program's AST
40
40
41
        String program = "a = " + expression + "\nend";
41
        String program = "a = " + expression + "\nend";
42
        ASTExecutableProgramNode ast = new Parser().parse(LexerFactory.createLexer(new ByteArrayInputStream(program.getBytes()), null, "<literal text>", SourceForm.UNPREPROCESSED_FREE_FORM, true /*false*/));
42
        ASTExecutableProgramNode ast = new Parser().parse(LexerFactory.createLexer(new StringReader(program), null, "<literal text>", SourceForm.UNPREPROCESSED_FREE_FORM, true /*false*/));
43
        assertNotNull(ast);
43
        assertNotNull(ast);
44
        ASTMainProgramNode mainProg = (ASTMainProgramNode)ast.getProgramUnitList().get(0);
44
        ASTMainProgramNode mainProg = (ASTMainProgramNode)ast.getProgramUnitList().get(0);
45
        ASTAssignmentStmtNode assignmentStmt = (ASTAssignmentStmtNode)mainProg.getBody().get(0);
45
        ASTAssignmentStmtNode assignmentStmt = (ASTAssignmentStmtNode)mainProg.getBody().get(0);
(-)src/org/eclipse/photran/internal/tests/parser/TestOpenMPComments.java (-3 / +2 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.photran.internal.tests.parser;
11
package org.eclipse.photran.internal.tests.parser;
12
12
13
import java.io.ByteArrayInputStream;
14
import java.io.IOException;
13
import java.io.IOException;
14
import java.io.StringReader;
15
import java.util.ArrayList;
15
import java.util.ArrayList;
16
import java.util.List;
16
import java.util.List;
17
17
Lines 129-136 Link Here
129
    
129
    
130
    private ASTExecutableProgramNode parse(SourceForm sourceForm, String string) throws IOException, LexerException, SyntaxException
130
    private ASTExecutableProgramNode parse(SourceForm sourceForm, String string) throws IOException, LexerException, SyntaxException
131
    {
131
    {
132
        ByteArrayInputStream in = new ByteArrayInputStream(string.getBytes());
132
        ASTExecutableProgramNode ast = new Parser().parse(LexerFactory.createLexer(new StringReader(string), null, "<stdin>", sourceForm, true));
133
        ASTExecutableProgramNode ast = new Parser().parse(LexerFactory.createLexer(in, null, "<stdin>", sourceForm, true));
134
        assertTrue(ast != null);
133
        assertTrue(ast != null);
135
        return ast;
134
        return ast;
136
    }
135
    }
(-)src/org/eclipse/photran/internal/ui/editor_vpg/FortranEditorTasks.java (-2 / +2 lines)
Lines 1-6 Link Here
1
package org.eclipse.photran.internal.ui.editor_vpg;
1
package org.eclipse.photran.internal.ui.editor_vpg;
2
2
3
import java.io.ByteArrayInputStream;
3
import java.io.StringReader;
4
import java.util.HashSet;
4
import java.util.HashSet;
5
import java.util.Set;
5
import java.util.Set;
6
6
Lines 179-185 Link Here
179
                        String editorContents = editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();
179
                        String editorContents = editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();
180
180
181
                        SourceForm sourceForm = determineSourceForm();
181
                        SourceForm sourceForm = determineSourceForm();
182
                        IAccumulatingLexer lexer = LexerFactory.createLexer(new ByteArrayInputStream(editorContents.getBytes()),
182
                        IAccumulatingLexer lexer = LexerFactory.createLexer(new StringReader(editorContents),
183
                                                                            editor.getIFile(),
183
                                                                            editor.getIFile(),
184
                                                                            editor.getIFile().getName(),
184
                                                                            editor.getIFile().getName(),
185
                                                                            sourceForm,
185
                                                                            sourceForm,
(-)src/org/eclipse/photran/internal/ui/actions/DisplayPreprocessedSource.java (-6 / +6 lines)
Lines 13-27 Link Here
13
import java.io.BufferedOutputStream;
13
import java.io.BufferedOutputStream;
14
import java.io.File;
14
import java.io.File;
15
import java.io.FileOutputStream;
15
import java.io.FileOutputStream;
16
import java.io.InputStream;
17
import java.io.PrintStream;
16
import java.io.PrintStream;
18
import java.io.StringBufferInputStream;
17
import java.io.Reader;
18
import java.io.StringReader;
19
import java.lang.reflect.InvocationTargetException;
19
import java.lang.reflect.InvocationTargetException;
20
20
21
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.dialogs.MessageDialog;
23
import org.eclipse.jface.text.IDocument;
23
import org.eclipse.jface.text.IDocument;
24
import org.eclipse.photran.internal.core.lexer.CPreprocessingInputStream;
24
import org.eclipse.photran.internal.core.lexer.CPreprocessingReader;
25
import org.eclipse.photran.internal.ui.editor.FixedFormFortranEditor;
25
import org.eclipse.photran.internal.ui.editor.FixedFormFortranEditor;
26
import org.eclipse.photran.internal.ui.editor.FreeFormFortranEditor;
26
import org.eclipse.photran.internal.ui.editor.FreeFormFortranEditor;
27
import org.eclipse.ui.ide.IDE;
27
import org.eclipse.ui.ide.IDE;
Lines 32-38 Link Here
32
 *
32
 *
33
 * @author Jeff Overbey
33
 * @author Jeff Overbey
34
 */
34
 */
35
@SuppressWarnings({ "deprecation", "restriction" })
35
@SuppressWarnings({ "restriction" })
36
public class DisplayPreprocessedSource extends FortranEditorActionDelegate
36
public class DisplayPreprocessedSource extends FortranEditorActionDelegate
37
{
37
{
38
    public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException
38
    public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException
Lines 42-49 Link Here
42
        	boolean isFixedForm = getFortranEditor().isFixedForm();
42
        	boolean isFixedForm = getFortranEditor().isFixedForm();
43
        	
43
        	
44
            IDocument doc = getFortranEditor().getIDocument();
44
            IDocument doc = getFortranEditor().getIDocument();
45
            InputStream in = new StringBufferInputStream(doc.get());
45
            Reader in = new StringReader(doc.get());
46
            InputStream cppIn = new CPreprocessingInputStream(getFortranEditor().getIFile(), null, in);
46
            Reader cppIn = new CPreprocessingReader(getFortranEditor().getIFile(), null, in);
47
            
47
            
48
            File tempFile = File.createTempFile(
48
            File tempFile = File.createTempFile(
49
            	"tmp",
49
            	"tmp",
(-)src/org/eclipse/rephraserengine/core/util/StringUtil.java (-1 / +1 lines)
Lines 67-73 Link Here
67
    
67
    
68
    public static String read(IFile file) throws IOException, CoreException
68
    public static String read(IFile file) throws IOException, CoreException
69
    {
69
    {
70
        return read(file.getContents(true));
70
        return read(new BufferedReader(new InputStreamReader(file.getContents(true), file.getCharset())));
71
    }
71
    }
72
72
73
    public static String read(File file) throws IOException
73
    public static String read(File file) throws IOException
(-)src/org/eclipse/rephraserengine/core/vpg/refactoring/VPGRefactoring.java (-3 / +6 lines)
Lines 1-7 Link Here
1
package org.eclipse.rephraserengine.core.vpg.refactoring;
1
package org.eclipse.rephraserengine.core.vpg.refactoring;
2
2
3
import java.io.BufferedReader;
3
import java.io.IOException;
4
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.InputStreamReader;
6
import java.io.Reader;
5
7
6
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.resources.IFile;
7
import org.eclipse.core.runtime.CoreException;
9
import org.eclipse.core.runtime.CoreException;
Lines 237-245 Link Here
237
        return ast.toString();
239
        return ast.toString();
238
    }
240
    }
239
241
240
    private int getSizeOf(IFile file) throws CoreException, IOException {
242
    private int getSizeOf(IFile file) throws CoreException, IOException
243
    {
241
        int size = 0;
244
        int size = 0;
242
        InputStream in = file.getContents(true);
245
        Reader in = new BufferedReader(new InputStreamReader(file.getContents(true), file.getCharset()));
243
        while (in.read() > -1)
246
        while (in.read() > -1)
244
            size++;
247
            size++;
245
        in.close();
248
        in.close();
(-)src/org/eclipse/rephraserengine/internal/examples/text/ConvertTabsToSpacesRefactoring.java (-2 / +4 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.rephraserengine.internal.examples.text;
11
package org.eclipse.rephraserengine.internal.examples.text;
12
12
13
import java.io.BufferedInputStream;
13
import java.io.BufferedReader;
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.InputStreamReader;
16
import java.io.Reader;
15
import java.util.List;
17
import java.util.List;
16
18
17
import org.eclipse.core.resources.IFile;
19
import org.eclipse.core.resources.IFile;
Lines 127-133 Link Here
127
        TextFileChange change = new TextFileChange("Replace tabs in " + file.getName(), file);
129
        TextFileChange change = new TextFileChange("Replace tabs in " + file.getName(), file);
128
        change.setEdit(new MultiTextEdit());
130
        change.setEdit(new MultiTextEdit());
129
131
130
        BufferedInputStream in = new BufferedInputStream(file.getContents(true));
132
        Reader in = new BufferedReader(new InputStreamReader(file.getContents(true), file.getCharset()));
131
        int offset = 0;
133
        int offset = 0;
132
        int column = 0;
134
        int column = 0;
133
        int numberOfTabsReplaced = 0;
135
        int numberOfTabsReplaced = 0;
(-)src/org/eclipse/rephraserengine/internal/ui/actions/DisplayModelAction.java (-4 / +4 lines)
Lines 12-20 Link Here
12
12
13
import java.io.BufferedReader;
13
import java.io.BufferedReader;
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.io.InputStreamReader;
15
import java.io.InputStreamReader;
17
import java.io.PrintStream;
16
import java.io.PrintStream;
17
import java.io.Reader;
18
import java.util.ArrayList;
18
import java.util.ArrayList;
19
19
20
import org.eclipse.core.runtime.NullProgressMonitor;
20
import org.eclipse.core.runtime.NullProgressMonitor;
Lines 42-48 Link Here
42
                if (filename != null)
42
                if (filename != null)
43
                {
43
                {
44
                    ArrayList<Integer> lineMap = new ArrayList<Integer>();
44
                    ArrayList<Integer> lineMap = new ArrayList<Integer>();
45
                    String fileContents = readStream(lineMap, info.getFileInEditor().getContents(true));
45
                    String fileContents = readStream(lineMap,
46
                        new BufferedReader(new InputStreamReader(info.getFileInEditor().getContents(true), info.getFileInEditor().getCharset())));
46
                    ps.println(filename);
47
                    ps.println(filename);
47
                    ps.println();
48
                    ps.println();
48
                    Model model = new Model("edge model", new NullProgressMonitor(), 0, vpg, filename);
49
                    Model model = new Model("edge model", new NullProgressMonitor(), 0, vpg, filename);
Lines 60-69 Link Here
60
        }
61
        }
61
    }
62
    }
62
63
63
    protected String readStream(ArrayList<Integer> lineMap, InputStream inputStream) throws IOException
64
    protected String readStream(ArrayList<Integer> lineMap, Reader in) throws IOException
64
    {
65
    {
65
        StringBuffer sb = new StringBuffer(4096);
66
        StringBuffer sb = new StringBuffer(4096);
66
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
67
        for (int offset = 0, ch = in.read(); ch >= 0; ch = in.read())
67
        for (int offset = 0, ch = in.read(); ch >= 0; ch = in.read())
68
        {
68
        {
69
            sb.append((char)ch);
69
            sb.append((char)ch);

Return to bug 312249