Skip to content

Commit efe4b39

Browse files
author
trijezdci
committed
decoupled node type enum from node type checking, various attributes removed, typos fixed
1 parent 332baa2 commit efe4b39

11 files changed

+137
-107
lines changed

ASCII.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* M2J -- Modula-2 to Java Translator & Compiler
2+
*
3+
* Copyright (c) 2016 The Modula-2 Software Foundation
4+
*
5+
* Author & Maintainer: Benjamin Kowarsch <[email protected]>
6+
*
7+
* @synopsis
8+
*
9+
* M2J is a multi-dialect Modula-2 to Java translator and via-Java compiler.
10+
* It supports the dialects described in the 3rd and 4th editions of Niklaus
11+
* Wirth's book "Programming in Modula-2" (PIM) published by Springer Verlag,
12+
* and an extended mode with select features from the revised language by
13+
* B.Kowarsch and R.Sutcliffe "Modula-2 Revision 2010" (M2R10).
14+
*
15+
* In translator mode, M2J translates Modula-2 source to Java source files.
16+
* In compiler mode, M2J compiles Modula-2 source via Java source files
17+
* to Java .class files using the host system's resident Java compiler.
18+
*
19+
* @repository
20+
*
21+
* https://github.com/m2sf/m2j
22+
*
23+
* @file
24+
*
25+
* ASCII.java
26+
*
27+
* ASCII code constants.
28+
*
29+
* @license
30+
*
31+
* M2J is free software: you can redistribute and/or modify it under the
32+
* terms of the GNU Lesser General Public License (LGPL) either version 2.1
33+
* or at your choice version 3 as published by the Free Software Foundation.
34+
* However, you may not alter the copyright, author and license information.
35+
*
36+
* M2J is distributed in the hope that it will be useful, but WITHOUT
37+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
38+
* FITNESS FOR A PARTICULAR PURPOSE. Read the license for more details.
39+
*
40+
* You should have received a copy of the GNU Lesser General Public License
41+
* along with m2j. If not, see <https://www.gnu.org/copyleft/lesser.html>.
42+
*
43+
* NB: Components in the domain part of email addresses are in reverse order.
44+
*/
45+
46+
package org.m2sf.m2j;
47+
48+
/* ---------------------------------------------------------------------------
49+
* ASCII codes used within the compiler.
50+
* ------------------------------------------------------------------------ */
51+
52+
public class ASCII {
53+
54+
/* ASCII NUL is used as a string terminator in M2 strings */
55+
56+
public static char NUL = '\u0000';
57+
58+
/* ASCII EOT is used to signal the end of a source file */
59+
60+
public static char EOT = '\u0004';
61+
62+
/* TAB, LR and CRLF are legal within M2 source files */
63+
64+
public static char TAB = '\u0009';
65+
public static char LF = '\n';
66+
public static char CR = '\r';
67+
68+
} /* ASCII */
69+
70+
/* END OF FILE */

ProtoAstNode.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,25 @@
4545

4646
package org.m2sf.m2j;
4747

48-
4948
interface ProtoAstNode {
5049

50+
/* ---------------------------------------------------------------------------
51+
* AstNode status codes
52+
* ------------------------------------------------------------------------ */
53+
54+
public enum Status {
55+
SUCCESS,
56+
TODO
57+
} /* Status */
58+
59+
5160
/* ---------------------------------------------------------------------------
5261
* method emptyNode()
5362
* ---------------------------------------------------------------------------
5463
* Returns the empty node singleton.
5564
* ------------------------------------------------------------------------ */
5665

57-
public static final Result<ProtoAstNode, Status> emptyNode ();
66+
public Result<ProtoAstNode, Status> emptyNode ();
5867

5968

6069
/* ---------------------------------------------------------------------------
@@ -139,7 +148,7 @@ interface ProtoAstNode {
139148
* the given index is stored in node.
140149
* ------------------------------------------------------------------------ */
141150

142-
public Result<ProtoAstNode, status>
151+
public Result<ProtoAstNode, Status>
143152
subnodeForIndex(ProtoAstNode node, int index);
144153

145154

ProtoAstNodeType.java

+3-76
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,14 @@
4545

4646
package org.m2sf.m2j;
4747

48-
49-
/* Face palm alert: Java doesn't allow interfaces on enums -- use abstract. */
50-
51-
abstract public enum ProtoAstNodeType {
52-
5348
/* ---------------------------------------------------------------------------
5449
* type AstNodeType
5550
* ---------------------------------------------------------------------------
5651
* Enumerated values representing AST node types.
5752
* ------------------------------------------------------------------------ */
5853

54+
public enum ProtoAstNodeType {
55+
5956
/* Empty Node Type */
6057

6158
AST_EMPTY,
@@ -201,6 +198,7 @@ abstract public enum ProtoAstNodeType {
201198

202199
AST_INVALID; /* for use as failure indicator */
203200

201+
} /* ProtoAstNodeType */
204202

205203
/* ---------------------------------------------------------------------------
206204
* AST node type groupings.
@@ -236,75 +234,4 @@ abstract public enum ProtoAstNodeType {
236234
* last literal node type : AST_QUOTEDVAL
237235
* ------------------------------------------------------------------------ */
238236

239-
240-
/* ---------------------------------------------------------------------------
241-
* method isValid(nodeType)
242-
* ---------------------------------------------------------------------------
243-
* Returns true if nodeType is a valid node type, otherwise false.
244-
* ------------------------------------------------------------------------ */
245-
246-
abstract public boolean isValid(ProtoAstNodeType nodeType);
247-
248-
249-
/* ---------------------------------------------------------------------------
250-
* method isNonterminalType(nodeType)
251-
* ---------------------------------------------------------------------------
252-
* Returns true if nodeType is a nonterminal node type, otherwise false.
253-
* ------------------------------------------------------------------------ */
254-
255-
abstract public boolean isNonterminalType (ProtoAstNodeType nodeType);
256-
257-
258-
/* ---------------------------------------------------------------------------
259-
* method isTerminalType(nodeType)
260-
* ---------------------------------------------------------------------------
261-
* Returns true if nodeType is a terminal node type, otherwise false.
262-
* ------------------------------------------------------------------------ */
263-
264-
abstract public boolean isTerminalType (ProtoAstNodeType nodeType);
265-
266-
267-
/* ---------------------------------------------------------------------------
268-
* method isListType(nodeType)
269-
* ---------------------------------------------------------------------------
270-
* Returns true if nodeType is a list node type, otherwise false.
271-
* ------------------------------------------------------------------------ */
272-
273-
abstract public boolean isListType (ProtoAstNodeType nodeType);
274-
275-
276-
/* ---------------------------------------------------------------------------
277-
* method isLegalSubnodeCount(nodeType, subnodeCount)
278-
* ---------------------------------------------------------------------------
279-
* Returns true if the given subnode count is a legal value for the given
280-
* node type, otherwise false.
281-
* ------------------------------------------------------------------------ */
282-
283-
abstract public boolean isLegalSubnodeCount
284-
(ProtoAstNodeType nodeType, int SubnodeCount);
285-
286-
287-
/* ---------------------------------------------------------------------------
288-
* method isLegalSubnodeType(inNodeType, subnodeType, index)
289-
* ---------------------------------------------------------------------------
290-
* Returns true if the given subnode type is a legal node type for the given
291-
* index in a node of the given subnode type, otherwise false.
292-
* ------------------------------------------------------------------------ */
293-
294-
abstract public boolean isLegalSubnodeType
295-
(ProtoAstNodeType inNodeType, ProtoAstNodeType subnodeType, int index);
296-
297-
298-
/* ---------------------------------------------------------------------------
299-
* method nameForNodeType(nodeType)
300-
* ---------------------------------------------------------------------------
301-
* Returns a string with a human readable name for nodeType or null if the
302-
* given node type is invalid.
303-
* ------------------------------------------------------------------------ */
304-
305-
abstract public String nameForNodeType (ProtoAstNodeType nodeType);
306-
307-
308-
} /* ProtoNodeType */
309-
310237
/* END OF FILE */

ProtoAstWriter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ interface ProtoAstWriter {
5555
* of characters written and a status code.
5656
* ------------------------------------------------------------------------ */
5757

58-
public static Result<int /* chars written */, IOStatus>
58+
public Result<Number /* chars written */, IOStatus>
5959
WriteAst (String path, ProtoAstNode ast);
6060

6161

ProtoDiagnostics.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
package org.m2sf.m2j;
4747

48-
import java.util.enumset;
48+
import java.util.EnumSet;
4949

5050
interface ProtoDiagnostics {
5151

@@ -203,9 +203,9 @@ public enum Code {
203203

204204
public void emitSyntaxErrorWithToken
205205
(int line, int column,
206-
ProtoTerminal.Token unexpectedToken,
206+
ProtoTerminals.Token unexpectedToken,
207207
String offendingLexeme,
208-
ProtoTerminal.Token expectedToken);
208+
ProtoTerminals.Token expectedToken);
209209

210210

211211
/* ---------------------------------------------------------------------------
@@ -218,9 +218,9 @@ public enum Code {
218218

219219
public void emitSyntaxErrorWithSet
220220
(int line, int column,
221-
ProtoTerminal.Token unexpectedToken,
221+
ProtoTerminals.Token unexpectedToken,
222222
String offendingLexeme,
223-
EnumSet<ProtoTerminal.Token> expectedTokenSet);
223+
EnumSet<ProtoTerminals.Token> expectedTokenSet);
224224

225225

226226
/* ---------------------------------------------------------------------------

ProtoDotWriter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ interface ProtoDotWriter {
5555
* of characters written and a status code.
5656
* ------------------------------------------------------------------------ */
5757

58-
public static Result<int /* chars written */, IOStatus>
58+
public Result<Number /* chars written */, IOStatus>
5959
WriteDot (String path, ProtoAstNode ast);
6060

6161

ProtoInfile.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ interface ProtoInfile {
5151
* File size, line and column counter limits
5252
* ------------------------------------------------------------------------ */
5353

54-
public static char EOT = '\u04'; /* indicating EOF */
55-
5654
public static int INFILE_MAX_SIZE = 260000; /* chars */
5755

5856
public static int INFILE_MAX_LINES = 64000; /* lines */
@@ -61,7 +59,7 @@ interface ProtoInfile {
6159

6260

6361
/* ---------------------------------------------------------------------------
64-
* Status codes
62+
* Infile status codes
6563
* ------------------------------------------------------------------------ */
6664

6765
public enum Status {
@@ -192,13 +190,13 @@ public enum Status {
192190
* Consumes the current lookahead character, advancing the current reading
193191
* position, updating line and column counter and returns the character code
194192
* of the new lookahead character that follows the consumed character.
195-
* Returns EOT if the lookahead character lies beyond the end of infile.
193+
* Returns ASCII.EOT if the lookahead character lies beyond the end of infile.
196194
*
197195
* pre-conditions:
198196
* o infile must be open
199197
*
200198
* post-conditions:
201-
* o character code of lookahead character or EOT is returned
199+
* o character code of lookahead character or ASCII.EOT is returned
202200
* o current reading position and line and column counters are updated
203201
* o file status is set to INFILE_STATUS_SUCCESS
204202
*
@@ -213,7 +211,7 @@ public enum Status {
213211
* method nextChar()
214212
* ---------------------------------------------------------------------------
215213
* Reads the lookahead character from infile without advancing the current
216-
* reading position and returns its character code. Returns EOT if the
214+
* reading position and returns its character code. Returns ASCII.EOT if the
217215
* lookahead character lies beyond the end of infile.
218216
*
219217
* pre-conditions:
@@ -235,14 +233,14 @@ public enum Status {
235233
* method la2Char()
236234
* ---------------------------------------------------------------------------
237235
* Reads the second lookahead character from infile without advancing the
238-
* current reading position and returns its character code. Returns EOT
236+
* current reading position and returns its character code. Returns ASCII.EOT
239237
* if the second lookahead character lies beyond the end of infile.
240238
*
241239
* pre-conditions:
242240
* o infile must be open
243241
*
244242
* post-conditions:
245-
* o character code of second lookahead character or EOT is returned
243+
* o character code of second lookahead character or ASCII.EOT is returned
246244
* o current reading position and line and column counters are NOT updated
247245
* o file status is set to INFILE_STATUS_SUCCESS
248246
*
@@ -275,7 +273,8 @@ public enum Status {
275273
* method eof()
276274
* ---------------------------------------------------------------------------
277275
* Returns true if the current reading position of infile lies beyond the end
278-
* of the associated file, returns false otherwise.
276+
* of the associated file, returns false otherwise. This method should be
277+
* called whenever ASCII.EOT is read to ascertain that EOF has been reached.
279278
* ------------------------------------------------------------------------ */
280279

281280
public boolean eof ();

ProtoLexer.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ interface ProtoLexer {
5656
public static int COMMENT_NESTING_LIMIT = 100;
5757

5858

59+
/* ---------------------------------------------------------------------------
60+
* Lexer status codes
61+
* ------------------------------------------------------------------------ */
62+
63+
public enum Status {
64+
SUCCESS,
65+
FILE_NOT_FOUND,
66+
FILE_ACCESS_DENIED,
67+
NOT_INITIALIZED,
68+
TODO
69+
} /* Status */
70+
71+
5972
/* ---------------------------------------------------------------------------
6073
* constructor newLexer(filename)
6174
* ---------------------------------------------------------------------------
@@ -101,7 +114,7 @@ interface ProtoLexer {
101114
* and status is set to NOT_INITIALIZED
102115
* ----------------------------------------------------------------------- */
103116

104-
public Token readSym();
117+
public ProtoTerminals.Token readSym();
105118

106119

107120
/* --------------------------------------------------------------------------
@@ -125,7 +138,7 @@ interface ProtoLexer {
125138
* and status is set to NOT_INITIALIZED
126139
* ----------------------------------------------------------------------- */
127140

128-
public Token nextSym ();
141+
public ProtoTerminals.Token nextSym ();
129142

130143

131144
/* --------------------------------------------------------------------------
@@ -135,7 +148,7 @@ interface ProtoLexer {
135148
* symbol.
136149
* ----------------------------------------------------------------------- */
137150

138-
public Token consumeSym ();
151+
public ProtoTerminals.Token consumeSym ();
139152

140153

141154
/* --------------------------------------------------------------------------

ProtoNonTerminals.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
package org.m2sf.m2j;
4747

48-
import java.util.enumset;
48+
import java.util.EnumSet;
4949

5050

5151
interface ProtoNonTerminals {

0 commit comments

Comments
 (0)