comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/Parser.frame @ 19283:08aa0372dad4

Truffle-DSL: implement new guard expression syntax.
author Christian Humer <christian.humer@gmail.com>
date Fri, 23 Jan 2015 02:55:23 +0100
parents
children
comparison
equal deleted inserted replaced
19282:ae81dd154fb6 19283:08aa0372dad4
1 /*-------------------------------------------------------------------------
2 Compiler Generator Coco/R,
3 Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
4 extended by M. Loeberbauer & A. Woess, Univ. of Linz
5 ported from C# to Java by Wolfgang Ahorner
6 with improvements by Pat Terry, Rhodes University
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22 As an exception, it is allowed to write an extension of Coco/R that is
23 used as a plugin in non-free software.
24
25 If not otherwise stated, any source code generated by Coco/R (other than
26 Coco/R itself) does not fall under the GNU General Public License.
27 ------------------------------------------------------------------------*/
28 -->begin
29 package com.oracle.truffle.dsl.processor.expression;
30
31 import java.util.*;
32 import java.io.*;
33 import java.nio.charset.*;
34
35 import com.oracle.truffle.dsl.processor.expression.DSLExpression.*;
36
37 // Checkstyle: stop
38 // @formatter:off
39 class Parser {
40 -->constants
41 static final boolean _T = true;
42 static final boolean _x = false;
43 static final int minErrDist = 2;
44
45 public Token t; // last recognized token
46 public Token la; // lookahead token
47 int errDist = minErrDist;
48
49 public final Scanner scanner;
50 public final Errors errors;
51
52 -->declarations
53 public Parser(InputStream input) {
54 this.scanner = new Scanner(input);
55 errors = new Errors();
56 }
57
58 void SynErr(int n) {
59 if (errDist >= minErrDist)
60 errors.SynErr(la.line, la.col, n);
61 errDist = 0;
62 }
63
64 public void SemErr(String msg) {
65 if (errDist >= minErrDist)
66 errors.SemErr(t.line, t.col, msg);
67 errDist = 0;
68 }
69
70 void Get() {
71 for (;;) {
72 t = la;
73 la = scanner.Scan();
74 if (la.kind <= maxT) {
75 ++errDist;
76 break;
77 }
78 -->pragmas
79 la = t;
80 }
81 }
82
83 void Expect(int n) {
84 if (la.kind == n)
85 Get();
86 else {
87 SynErr(n);
88 }
89 }
90
91 boolean StartOf(int s) {
92 return set[s][la.kind];
93 }
94
95 void ExpectWeak(int n, int follow) {
96 if (la.kind == n)
97 Get();
98 else {
99 SynErr(n);
100 while (!StartOf(follow))
101 Get();
102 }
103 }
104
105 boolean WeakSeparator(int n, int syFol, int repFol) {
106 int kind = la.kind;
107 if (kind == n) {
108 Get();
109 return true;
110 } else if (StartOf(repFol))
111 return false;
112 else {
113 SynErr(n);
114 while (!(set[syFol][kind] || set[repFol][kind] || set[0][kind])) {
115 Get();
116 kind = la.kind;
117 }
118 return StartOf(syFol);
119 }
120 }
121
122 -->productions
123
124 private DSLExpression parseImpl() {
125 la = new Token();
126 la.val = "";
127 Get();
128 DSLExpression result = -->parseRoot
129 return result;
130 }
131
132 private static final boolean[][] set = {
133 -->initialization
134 };
135
136 public static DSLExpression parse(InputStream input) {
137 Parser parser = new Parser(input);
138 DSLExpression result = parser.parseImpl();
139 if (parser.errors.errors.size() > 0) {
140 StringBuilder msg = new StringBuilder();
141 for (String error : parser.errors.errors) {
142 msg.append(error).append("\n");
143 }
144 throw new InvalidExpressionException(msg.toString());
145 }
146 return result;
147 }
148
149 public static DSLExpression parse(String s) {
150 return parse(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)));
151 }
152 } // end Parser
153
154 class Errors {
155
156 protected final List<String> errors = new ArrayList<>();
157 public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
158
159 protected void printMsg(int line, int column, String msg) {
160 StringBuffer b = new StringBuffer(errMsgFormat);
161 int pos = b.indexOf("{0}");
162 if (pos >= 0) {
163 b.delete(pos, pos + 3);
164 b.insert(pos, line);
165 }
166 pos = b.indexOf("{1}");
167 if (pos >= 0) {
168 b.delete(pos, pos + 3);
169 b.insert(pos, column);
170 }
171 pos = b.indexOf("{2}");
172 if (pos >= 0)
173 b.replace(pos, pos + 3, msg);
174 errors.add(b.toString());
175 }
176
177 public void SynErr(int line, int col, int n) {
178 String s;
179 switch (n) {-->errors
180 default:
181 s = "error " + n;
182 break;
183 }
184 printMsg(line, col, s);
185 }
186
187 public void SemErr(int line, int col, String s) {
188 printMsg(line, col, s);
189 }
190
191 public void SemErr(String s) {
192 errors.add(s);
193 }
194
195 public void Warning(int line, int col, String s) {
196 printMsg(line, col, s);
197 }
198
199 public void Warning(String s) {
200 errors.add(s);
201 }
202 } // Errors
203
204 class FatalError extends RuntimeException {
205
206 public static final long serialVersionUID = 1L;
207
208 public FatalError(String s) {
209 super(s);
210 }
211 }