comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.frame @ 13761:7c418666c6c9

Refactoring and cleanup of Simple Language (more to come soon)
author Christian Wimmer <christian.wimmer@oracle.com>
date Fri, 24 Jan 2014 18:16:24 -0800
parents 71991b7a0f14
children b16ec83edc73
comparison
equal deleted inserted replaced
13760:a12017c18d5d 13761:7c418666c6c9
28 -->begin 28 -->begin
29 package com.oracle.truffle.sl.parser; 29 package com.oracle.truffle.sl.parser;
30 30
31 import java.util.*; 31 import java.util.*;
32 32
33 import com.oracle.truffle.api.*;
33 import com.oracle.truffle.sl.*; 34 import com.oracle.truffle.sl.*;
34 import com.oracle.truffle.sl.nodes.*; 35 import com.oracle.truffle.sl.nodes.*;
36 import com.oracle.truffle.sl.runtime.*;
35 37
36 // Checkstyle: stop 38 // Checkstyle: stop
37 // @formatter:off 39 // @formatter:off
38 public class Parser { 40 public class Parser {
39 -->constants 41 -->constants
47 49
48 public final Scanner scanner; 50 public final Scanner scanner;
49 public final Errors errors; 51 public final Errors errors;
50 private final SLNodeFactory factory; 52 private final SLNodeFactory factory;
51 -->declarations 53 -->declarations
52 public Parser(Scanner scanner, SLNodeFactory factory) { 54 public Parser(SLContext context, Source source) {
53 this.scanner = scanner; 55 this.scanner = new Scanner(source.getInputStream());
54 this.factory = factory; 56 this.factory = new SLNodeFactory(context, source, this);
55 errors = new Errors(); 57 errors = new Errors();
56 } 58 }
57 59
58 void SynErr(int n) { 60 void SynErr(int n) {
59 if (errDist >= minErrDist) 61 if (errDist >= minErrDist)
130 132
131 private static final boolean[][] set = { 133 private static final boolean[][] set = {
132 -->initialization 134 -->initialization
133 }; 135 };
134 136
135 public String ParseErrors() { 137 public static void parseSL(SLContext context, Source source) {
136 java.io.PrintStream oldStream = System.out; 138 Parser parser = new Parser(context, source);
137 139 parser.Parse();
138 java.io.OutputStream out = new java.io.ByteArrayOutputStream(); 140 if (parser.errors.errors.size() > 0) {
139 java.io.PrintStream newStream = new java.io.PrintStream(out); 141 StringBuilder msg = new StringBuilder("Error(s) parsing script:\n");
140 142 for (String error : parser.errors.errors) {
141 errors.errorStream = newStream; 143 msg.append(error).append("\n");
142 144 }
143 Parse(); 145 throw new SLException(msg.toString());
144 146 }
145 String errorStream = out.toString();
146 errors.errorStream = oldStream;
147
148 return errorStream;
149
150 } 147 }
151 } // end Parser 148 } // end Parser
152 149
153 class Errors { 150 class Errors {
154 151
155 public int count = 0; // number of errors detected 152 protected final List<String> errors = new ArrayList<>();
156 public java.io.PrintStream errorStream = System.out; // error messages go to this stream
157 public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text 153 public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
158 154
159 protected void printMsg(int line, int column, String msg) { 155 protected void printMsg(int line, int column, String msg) {
160 StringBuffer b = new StringBuffer(errMsgFormat); 156 StringBuffer b = new StringBuffer(errMsgFormat);
161 int pos = b.indexOf("{0}"); 157 int pos = b.indexOf("{0}");
169 b.insert(pos, column); 165 b.insert(pos, column);
170 } 166 }
171 pos = b.indexOf("{2}"); 167 pos = b.indexOf("{2}");
172 if (pos >= 0) 168 if (pos >= 0)
173 b.replace(pos, pos + 3, msg); 169 b.replace(pos, pos + 3, msg);
174 errorStream.println(b.toString()); 170 errors.add(b.toString());
175 } 171 }
176 172
177 public void SynErr(int line, int col, int n) { 173 public void SynErr(int line, int col, int n) {
178 String s; 174 String s;
179 switch (n) {-->errors 175 switch (n) {-->errors
180 default: 176 default:
181 s = "error " + n; 177 s = "error " + n;
182 break; 178 break;
183 } 179 }
184 printMsg(line, col, s); 180 printMsg(line, col, s);
185 count++;
186 } 181 }
187 182
188 public void SemErr(int line, int col, String s) { 183 public void SemErr(int line, int col, String s) {
189 printMsg(line, col, s); 184 printMsg(line, col, s);
190 count++;
191 } 185 }
192 186
193 public void SemErr(String s) { 187 public void SemErr(String s) {
194 errorStream.println(s); 188 errors.add(s);
195 count++;
196 } 189 }
197 190
198 public void Warning(int line, int col, String s) { 191 public void Warning(int line, int col, String s) {
199 printMsg(line, col, s); 192 printMsg(line, col, s);
200 } 193 }
201 194
202 public void Warning(String s) { 195 public void Warning(String s) {
203 errorStream.println(s); 196 errors.add(s);
204 } 197 }
205 } // Errors 198 } // Errors
206 199
207 class FatalError extends RuntimeException { 200 class FatalError extends RuntimeException {
208 201