Skip to content

Commit e8ec350

Browse files
authored
COBOL data invariants
1 parent ccb0779 commit e8ec350

File tree

7 files changed

+390
-40
lines changed

7 files changed

+390
-40
lines changed

ASTBasicTerm.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,25 @@ public Vector normaliseAntlr()
26692669
return alts;
26702670
}
26712671

2672-
public Vector cobolDataDefinitions(java.util.Map context)
2672+
public int cobolDataWidth()
2673+
{ if ("integerLiteral".equals(tag))
2674+
{ return value.length(); }
2675+
2676+
if ("X".equals(value) || "9".equals(value) ||
2677+
",".equals(value) || "A".equals(value) ||
2678+
"B".equals(value) || "0".equals(value) ||
2679+
"/".equals(value) || "+".equals(value) ||
2680+
"-".equals(value) || "$".equals(value) ||
2681+
"£".equals(value) || ".".equals(value) ||
2682+
"Z".equals(value) || "*".equals(value))
2683+
{ return 1; }
2684+
2685+
// S, P, V do not add to length.
2686+
2687+
return 0;
2688+
}
2689+
2690+
public Vector cobolDataDefinitions(java.util.Map context, Vector invs)
26732691
{ // Each immediately higher level item becomes an attribute
26742692
// of container. If composite, it also becomes a class
26752693
Vector res = new Vector();

ASTCompositeTerm.java

Lines changed: 130 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import java.util.Vector;
22
import java.io.*;
33

4+
import javax.swing.*;
5+
6+
47
/******************************
58
* Copyright (c) 2003--2022 Kevin Lano
69
* This program and the accompanying materials are made available under the
@@ -40134,7 +40137,43 @@ else if ("*".equals(suf))
4013440137
return alternatives;
4013540138
}
4013640139

40137-
public Vector cobolDataDefinitions(java.util.Map context)
40140+
public int cobolDataWidth()
40141+
{ if ("dataPictureClause".equals(tag))
40142+
{ // (PIC | PICTURE) IS? pictureString
40143+
40144+
int sze = terms.size();
40145+
ASTTerm ptrm = (ASTTerm) terms.get(sze-1);
40146+
return ptrm.cobolDataWidth();
40147+
}
40148+
40149+
if ("pictureString".equals(tag))
40150+
{ int res = 0;
40151+
for (int i = 0; i < terms.size(); i++)
40152+
{ ASTTerm tt = (ASTTerm) terms.get(i);
40153+
int wdth = tt.cobolDataWidth();
40154+
res = res + wdth;
40155+
}
40156+
return res;
40157+
}
40158+
40159+
if ("pictureCardinality".equals(tag))
40160+
{ ASTTerm val = (ASTTerm) terms.get(1);
40161+
String lit = val.literalForm();
40162+
try {
40163+
int crd = Integer.parseInt(lit);
40164+
return crd-1;
40165+
} catch (Exception _ex) { return 0; }
40166+
}
40167+
40168+
if ("pictureChars".equals(tag))
40169+
{ ASTTerm val = (ASTTerm) terms.get(0);
40170+
return val.cobolDataWidth();
40171+
}
40172+
40173+
return 0;
40174+
}
40175+
40176+
public Vector cobolDataDefinitions(java.util.Map context, Vector invs)
4013840177
{ // Each non-filler item at level > 01 becomes an attribute
4013940178
// of a relevant container. If composite (no picture),
4014040179
// it also becomes a class & container in turn.
@@ -40144,7 +40183,7 @@ public Vector cobolDataDefinitions(java.util.Map context)
4014440183
if ("compilationUnit".equals(tag))
4014540184
{ for (int i = 0; i < terms.size(); i++)
4014640185
{ ASTTerm tt = (ASTTerm) terms.get(i);
40147-
Vector ttres = tt.cobolDataDefinitions(context);
40186+
Vector ttres = tt.cobolDataDefinitions(context, invs);
4014840187
res.addAll(ttres);
4014940188
}
4015040189
return res;
@@ -40153,18 +40192,37 @@ public Vector cobolDataDefinitions(java.util.Map context)
4015340192
if ("programUnit".equals(tag))
4015440193
{ for (int i = 0; i < terms.size(); i++)
4015540194
{ ASTTerm tt = (ASTTerm) terms.get(i);
40156-
Vector ttres = tt.cobolDataDefinitions(context);
40195+
Vector ttres = tt.cobolDataDefinitions(context, invs);
4015740196
res.addAll(ttres);
4015840197
}
4015940198
return res;
4016040199
}
4016140200

40201+
if ("identificationDivision".equals(tag))
40202+
{ // (IDENTIFICATION | ID) DIVISION . programIdParagraph identificationDivisionBody*
40203+
40204+
for (int i = 0; i < terms.size(); i++)
40205+
{ ASTTerm trm = (ASTTerm) terms.get(i);
40206+
if ("programIdParagraph".equals(trm.getTag()))
40207+
{ trm.cobolDataDefinitions(context,invs); }
40208+
}
40209+
return res;
40210+
}
40211+
40212+
if ("programIdParagraph".equals(tag))
40213+
{ // PROGRAM-ID . programName _*
40214+
ASTTerm nme = (ASTTerm) terms.get(2);
40215+
String pname = nme.literalForm() + "_Class";
40216+
context.put("programName", pname);
40217+
return res;
40218+
}
40219+
4016240220
if ("dataDivision".equals(tag))
4016340221
{ // DATA DIVISION . dataDivisionSection*
4016440222

4016540223
for (int i = 3; i < terms.size(); i++)
4016640224
{ ASTTerm tt = (ASTTerm) terms.get(i);
40167-
Vector ttres = tt.cobolDataDefinitions(context);
40225+
Vector ttres = tt.cobolDataDefinitions(context, invs);
4016840226
res.addAll(ttres);
4016940227
}
4017040228
return res;
@@ -40175,7 +40233,7 @@ public Vector cobolDataDefinitions(java.util.Map context)
4017540233
// workingStorageSection | linkageSection | ...
4017640234

4017740235
ASTTerm tt = (ASTTerm) terms.get(0);
40178-
Vector ttres = tt.cobolDataDefinitions(context);
40236+
Vector ttres = tt.cobolDataDefinitions(context, invs);
4017940237
return ttres;
4018040238
}
4018140239

@@ -40184,7 +40242,7 @@ public Vector cobolDataDefinitions(java.util.Map context)
4018440242

4018540243
for (int i = 3; i < terms.size(); i++)
4018640244
{ ASTTerm tt = (ASTTerm) terms.get(i);
40187-
Vector ttres = tt.cobolDataDefinitions(context);
40245+
Vector ttres = tt.cobolDataDefinitions(context, invs);
4018840246
res.addAll(ttres);
4018940247
}
4019040248
return res;
@@ -40197,7 +40255,7 @@ public Vector cobolDataDefinitions(java.util.Map context)
4019740255
// dataDescriptionEntryExecSql
4019840256

4019940257
ASTTerm tt = (ASTTerm) terms.get(0);
40200-
Vector ttres = tt.cobolDataDefinitions(context);
40258+
Vector ttres = tt.cobolDataDefinitions(context, invs);
4020140259
return ttres;
4020240260
}
4020340261

@@ -40238,7 +40296,18 @@ public Vector cobolDataDefinitions(java.util.Map context)
4023840296
Entity container = (Entity) context.get("container");
4023940297

4024040298
if (ASTTerm.hasTag(terms,"dataPictureClause"))
40241-
{ // It is a basic item, not an entity
40299+
{ // It is a basic data item, not an entity
40300+
40301+
ASTTerm pictureClause =
40302+
ASTTerm.getTermByTag(terms,"dataPictureClause");
40303+
int wdth = 0;
40304+
if (pictureClause != null)
40305+
{ wdth = pictureClause.cobolDataWidth(); }
40306+
// JOptionPane.showMessageDialog(null, "Width of " + fieldName + " is " + wdth,
40307+
// "",
40308+
// JOptionPane.INFORMATION_MESSAGE);
40309+
40310+
4024240311
if (container == null) // no container, so top-level attribute
4024340312
{ if ("FILLER".equals(fieldName)) { }
4024440313
else
@@ -40253,8 +40322,47 @@ public Vector cobolDataDefinitions(java.util.Map context)
4025340322
else // basic attribute of some container
4025440323
{ int contLevel = container.levelNumber;
4025540324

40256-
Integer previousLevel = (Integer) context.get("previousLevel");
40325+
Integer startPosition = (Integer) context.get("startPosition");
40326+
if (startPosition != null)
40327+
{ int startPos = startPosition.intValue();
40328+
int endPos = startPos + wdth - 1;
40329+
context.put("startPosition", endPos + 1);
40330+
40331+
String cname = container.getName();
40332+
String ownername =
40333+
cname.substring(0,cname.length()-6);
40334+
String progname =
40335+
(String) context.get("programName");
40336+
JOptionPane.showMessageDialog(null, progname + ":: " + fieldName +
40337+
" = " + ownername +
40338+
".subrange(" + startPos + "," + endPos + ")",
40339+
"",
40340+
JOptionPane.INFORMATION_MESSAGE);
40341+
BasicExpression owner =
40342+
BasicExpression.newAttributeBasicExpression(
40343+
ownername,
40344+
new Type("String", null));
40345+
BasicExpression attr =
40346+
BasicExpression.newAttributeBasicExpression(
40347+
fieldName,
40348+
new Type("String", null));
40349+
Vector spars = new Vector();
40350+
spars.add(new BasicExpression(startPos));
40351+
spars.add(new BasicExpression(endPos));
40352+
BasicExpression substr =
40353+
BasicExpression.newFunctionBasicExpression(
40354+
"subrange", owner, spars);
40355+
BinaryExpression inv =
40356+
new BinaryExpression("=", attr, substr);
40357+
Constraint cons =
40358+
Constraint.getConstraint(inv);
40359+
cons.ownerName = progname;
40360+
invs.add(cons);
40361+
}
40362+
else
40363+
{ context.put("startPosition", 1 + wdth); }
4025740364

40365+
Integer previousLevel = (Integer) context.get("previousLevel");
4025840366
int prevLevel = previousLevel.intValue();
4025940367

4026040368
if (levelNumber >= prevLevel)
@@ -40300,12 +40408,20 @@ else if (levelNumber < prevLevel)
4030040408
Integer previousLevel = (Integer) context.get("previousLevel");
4030140409

4030240410
int prevLevel = -1;
40303-
if (previousLevel != null)
40304-
{ prevLevel = previousLevel.intValue(); }
40411+
if (previousLevel != null)
40412+
{ prevLevel = previousLevel.intValue(); }
4030540413

4030640414
res.add(newent);
4030740415
if (container == null) // top-level
40308-
{ res.add(att); }
40416+
{ String pname =
40417+
(String) context.get("programName");
40418+
if (pname != null)
40419+
{ Entity prog = new Entity(pname);
40420+
prog.levelNumber = -1;
40421+
newent.container = prog;
40422+
}
40423+
res.add(att);
40424+
}
4030940425
else if (levelNumber >= prevLevel)
4031040426
{ newent.container = container;
4031140427
container.addAttribute(att);
@@ -40320,7 +40436,8 @@ else if (levelNumber >= prevLevel)
4032040436
}
4032140437

4032240438
context.put("container", newent);
40323-
context.put("previousLevel", new Integer(levelNumber));
40439+
context.put("previousLevel", new Integer(levelNumber));
40440+
context.put("startPosition", new Integer(1));
4032440441
}
4032540442
}
4032640443
}

ASTSymbolTerm.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,23 @@ public Vector normaliseAntlr()
842842
public String antlrElement2cstl(Vector rulerefs, Vector conds)
843843
{ return symbol; }
844844

845-
public Vector cobolDataDefinitions(java.util.Map context)
845+
public int cobolDataWidth()
846+
{
847+
if ("X".equals(symbol) || "9".equals(symbol) ||
848+
",".equals(symbol) || "A".equals(symbol) ||
849+
"B".equals(symbol) || "0".equals(symbol) ||
850+
"/".equals(symbol) || "+".equals(symbol) ||
851+
"-".equals(symbol) || "$".equals(symbol) ||
852+
"£".equals(symbol) || ".".equals(symbol) ||
853+
"Z".equals(symbol) || "*".equals(symbol))
854+
{ return 1; }
855+
856+
// S, P, V do not add to length.
857+
858+
return 0;
859+
}
860+
861+
public Vector cobolDataDefinitions(java.util.Map context, Vector invs)
846862
{ Vector res = new Vector();
847863
return res;
848864
}

ASTTerm.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,16 @@ public static boolean hasTag(Vector terms, String tag)
10921092
return res;
10931093
}
10941094

1095+
public static ASTTerm getTermByTag(Vector terms, String tag)
1096+
{ ASTTerm res = null;
1097+
for (int i = 0; i < terms.size(); i++)
1098+
{ ASTTerm tt = (ASTTerm) terms.get(i);
1099+
if (tt.hasTag(tag))
1100+
{ return tt; }
1101+
}
1102+
return res;
1103+
}
1104+
10951105
public abstract boolean hasTag(String tagx);
10961106

10971107
public abstract boolean hasSingleTerm();
@@ -3653,8 +3663,9 @@ public static Vector randomCompositeASTTermsForTag(
36533663

36543664
public abstract Vector normaliseAntlr();
36553665

3656-
public abstract Vector cobolDataDefinitions(java.util.Map context);
3666+
public abstract Vector cobolDataDefinitions(java.util.Map context, Vector invs);
36573667

3668+
public abstract int cobolDataWidth();
36583669

36593670
public static void main(String[] args)
36603671
{ // ASTBasicTerm t = new ASTBasicTerm("OclBasicExpression", "true");

0 commit comments

Comments
 (0)