Skip to content

Commit

Permalink
[Core] Merge grids and general grids
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8000-0000 committed Apr 25, 2020
1 parent adb4ecc commit 543c7f7
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 471 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 2 additions & 20 deletions src/main/antlr/SamXParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,9 @@ spanGridElement : MUL_COLSEP attribute* optionalFlow ;

generalGridElement : gridElement | spanGridElement ;

gridHeaderRow
locals [ int columnCount = 0; ]
: attribute* ( gridElement { $ctx.columnCount ++; } )+ NEWLINE { currentHeaderLength = $ctx.columnCount; };

gridRecordRow
locals [ int columnCount = 0; ]
: metadata ( gridElement { $ctx.columnCount ++; } )+ NEWLINE
{
if (currentHeaderLength != $ctx.columnCount)
{
throw new ParseCancellationException("line " + $start.getLine() +
":" + $start.getCharPositionInLine() +
" incorrect number of columns; expected " + currentHeaderLength +
" but observed " + $ctx.columnCount);
}
};

generalGridHeaderSep : STT_HDR_SEP NEWLINE ;

generalGridRowData : metadata generalGridElement+ COLSEP ;
generalGridRowData : metadata generalGridElement+ ;

generalGridRow : (generalGridRowData | GEN_ROW_SEP) NEWLINE ;

Expand Down Expand Up @@ -246,8 +229,7 @@ block :
| STT_INCL reference=text CLOSE_PAR metadata { parseFile($reference.text); } # IncludeFile
| STT_IMAGE text CLOSE_PAR blockMetadata # InsertImage
| CODE_MARKER language=text CLOSE_PAR metadata NEWLINE+ INDENT (externalCode? NEWLINE)+ DEDENT # CodeBlock
| STT_GRID blockMetadata NEWLINE+ INDENT gridHeaderRow (gridRecordRow | NEWLINE) + DEDENT # Grid
| STT_GEN_GRID blockMetadata NEWLINE+ INDENT
| STT_GRID blockMetadata NEWLINE+ INDENT
(header=generalGridGroup? generalGridHeaderSep)?
body=generalGridGroup
(generalGridHeaderSep? footer=generalGridGroup)?
Expand Down
68 changes: 55 additions & 13 deletions src/main/java/net/signbit/samx/GridVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public String getAttributesPlain()
}
return av.toPlainString();
}

public boolean empty()
{
return flow == null && attributes.isEmpty();
}
}

static class GeneralGridRow
Expand All @@ -135,12 +140,12 @@ public int getColumnCount()
{
if (ggec.gridElement() != null)
{
GridVisitor.GridCell gc = new GridVisitor.GridCell(ggec.gridElement().attribute(), ggec.gridElement().optionalFlow());
GridCell gc = new GridCell(ggec.gridElement().attribute(), ggec.gridElement().optionalFlow());
cells.add(gc);
}
else if (ggec.spanGridElement() != null)
{
GridVisitor.GridCell gc = new GridVisitor.GridCell(ggec.spanGridElement().attribute(), ggec.spanGridElement().optionalFlow());
GridCell gc = new GridCell(ggec.spanGridElement().attribute(), ggec.spanGridElement().optionalFlow());
gc.setSpan(ggec.spanGridElement().MUL_COLSEP().getText());

for (int ii = 0; ii < gc.colSpan; ++ii)
Expand All @@ -154,20 +159,25 @@ else if (ggec.spanGridElement() != null)

static class GeneralGridGroup
{
ArrayList<GridVisitor.GeneralGridRow> rows = new ArrayList<>();
ArrayList<GeneralGridRow> rows = new ArrayList<>();

final int columnCount;

int conditionColumnWidth = 0;
int columnWidths[];
boolean isInteger[];
boolean isCurrency[];
boolean isNumeric[];

GeneralGridGroup(SamXParser.GeneralGridGroupContext gggc, SamXParserVisitor<StringBuilder> visitor)
{
HashSet<Integer> columnCount = new HashSet<>();
HashSet<Integer> columnLengths = new HashSet<>();
for (SamXParser.GeneralGridRowContext rc : gggc.generalGridRow())
{
final SamXParser.GeneralGridRowDataContext rdc = rc.generalGridRowData();
if (rdc != null)
{
final GridVisitor.GeneralGridRow ggr = new GridVisitor.GeneralGridRow(rdc);
final GeneralGridRow ggr = new GeneralGridRow(rdc);

if (visitor != null)
{
Expand All @@ -180,24 +190,35 @@ static class GeneralGridGroup
}

rows.add(ggr);
columnCount.add(ggr.getColumnCount());
columnLengths.add(ggr.getColumnCount());
}
}

if (columnCount.size() != 1)
if (columnLengths.size() != 1)
{
throw new RuntimeException("Invalid table specification: multiple table column sizes: " + columnCount.toString());
throw new RuntimeException("Invalid table specification: multiple table column sizes: " + columnLengths.toString());
}

columnWidths = new int[columnCount.iterator().next()];
int localColumnCount = columnLengths.iterator().next();
columnWidths = new int[localColumnCount];
isInteger = new boolean[localColumnCount];

if (visitor != null)
for (int ii = 0; ii < localColumnCount; ++ii)
{
for (GridVisitor.GeneralGridRow ggr : rows)
isInteger[ii] = true;
}

boolean lastColumnEmpty = true;

for (GeneralGridRow ggr : rows)
{
if (visitor != null)
{
for (int ii = 0; ii < ggr.cells.size(); ++ii)
{
final GridVisitor.GridCell gc = ggr.cells.get(ii);
final GridCell gc = ggr.cells.get(ii);

final String attributes = gc.getAttributesPlain();
final String content = gc.getContent(visitor);

int rowSpanIndicator = 0;
Expand All @@ -206,15 +227,36 @@ static class GeneralGridGroup
rowSpanIndicator = gc.rowSpan;
}

final int thisWidth = (int) Math.ceil((content.length() + rowSpanIndicator) / (double) (gc.colSpan));
final int thisWidth = (int) Math.ceil((attributes.length() + content.length() + rowSpanIndicator) / (double) (gc.colSpan));

if (columnWidths[ii] < thisWidth)
{
columnWidths[ii] = thisWidth;
}

if (!VisitorUtils.isInteger(content))
{
isInteger[ii] = false;
}
}
}

if (lastColumnEmpty)
{
final GridCell lastCell = ggr.cells.get(localColumnCount - 1);
if (! lastCell.empty())
{
lastColumnEmpty = false;
}
}
}

if (lastColumnEmpty)
{
localColumnCount--;
}

columnCount = localColumnCount;
}
}
}
Loading

0 comments on commit 543c7f7

Please sign in to comment.