Skip to content

Commit c2db7a1

Browse files
committed
#769 Add a placeholder for encoders to be available in AST.
1 parent e993e75 commit c2db7a1

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed

cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/ParserVisitor.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import za.co.absa.cobrix.cobol.parser.CopybookParser.CopybookAST
2323
import za.co.absa.cobrix.cobol.parser.ast.datatype._
2424
import za.co.absa.cobrix.cobol.parser.ast.{Group, Primitive}
2525
import za.co.absa.cobrix.cobol.parser.common.Constants
26-
import za.co.absa.cobrix.cobol.parser.decoders.DecoderSelector
26+
import za.co.absa.cobrix.cobol.parser.decoders.{DecoderSelector, EncoderSelector}
2727
import za.co.absa.cobrix.cobol.parser.decoders.FloatingPointFormat.FloatingPointFormat
2828
import za.co.absa.cobrix.cobol.parser.encoding.codepage.CodePage
2929
import za.co.absa.cobrix.cobol.parser.encoding._
@@ -855,8 +855,9 @@ class ParserVisitor(enc: Encoding,
855855
Map(),
856856
isDependee = false,
857857
identifier.toUpperCase() == Constants.FILLER,
858-
DecoderSelector.getDecoder(pic.value, stringTrimmingPolicy, isDisplayAlwaysString, effectiveEbcdicCodePage, effectiveAsciiCharset, isUtf16BigEndian = isUtf16BigEndian, floatingPointFormat, strictSignOverpunch = strictSignOverpunch, improvedNullDetection = improvedNullDetection, strictIntegralPrecision = strictIntegralPrecision)
859-
) (Some(parent))
858+
DecoderSelector.getDecoder(pic.value, stringTrimmingPolicy, isDisplayAlwaysString, effectiveEbcdicCodePage, effectiveAsciiCharset, isUtf16BigEndian = isUtf16BigEndian, floatingPointFormat, strictSignOverpunch = strictSignOverpunch, improvedNullDetection = improvedNullDetection, strictIntegralPrecision = strictIntegralPrecision),
859+
EncoderSelector.getEncoder(pic.value, effectiveEbcdicCodePage, effectiveAsciiCharset),
860+
)(Some(parent))
860861

861862
parent.children.append(prim)
862863

cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/ast/Primitive.scala

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,26 @@
1717
package za.co.absa.cobrix.cobol.parser.ast
1818

1919
import za.co.absa.cobrix.cobol.parser.ast.datatype.{AlphaNumeric, CobolType, Decimal, Integral}
20-
import za.co.absa.cobrix.cobol.parser.decoders.{BinaryUtils, DecoderSelector}
20+
import za.co.absa.cobrix.cobol.parser.decoders.{BinaryUtils, DecoderSelector, EncoderSelector}
2121

2222
/** An abstraction of the statements describing fields of primitive data types in the COBOL copybook
2323
*
24-
* @param level A level for the statement
25-
* @param name An identifier
26-
* @param originalName Original name of the AST element (before the conversion to the Spark-compatible name)
27-
* @param lineNumber An line number in the copybook
28-
* @param redefines A name of a field which is redefined by this one
29-
* @param occurs The number of elements in an fixed size array / minimum items in variable-sized array
30-
* @param to The maximum number of items in a variable size array
31-
* @param dependingOn A field which specifies size of the array in a record
32-
* @param parent A parent node
24+
* @param level A level for the statement
25+
* @param name An identifier
26+
* @param originalName Original name of the AST element (before the conversion to the Spark-compatible name)
27+
* @param lineNumber An line number in the copybook
28+
* @param redefines A name of a field which is redefined by this one
29+
* @param isRedefined A flag indicating if the field is redefined
30+
* @param occurs The number of elements in an fixed size array / minimum items in variable-sized array
31+
* @param to The maximum number of items in a variable size array
32+
* @param dependingOn A field which specifies size of the array in a record
33+
* @param dependingOnHandlers A map of handlers for the dependingOn field
34+
* @param isDependee A flag indicating if the field is a dependee
35+
* @param isFiller A flag indicating if the field is a filler
36+
* @param decode A decoder for the field to convert from raw data to a JVM data type
37+
* @param encode An optional encoder for the field to convert from a JVM data type to raw data
38+
* @param binaryProperties Binary properties of the field, such as size in bits, alignment, etc.
39+
* @param parent A parent node
3340
*/
3441
case class Primitive(
3542
level: Int,
@@ -46,6 +53,7 @@ case class Primitive(
4653
isDependee: Boolean = false,
4754
isFiller: Boolean = false,
4855
decode: DecoderSelector.Decoder,
56+
encode: Option[EncoderSelector.Encoder],
4957
binaryProperties: BinaryProperties = BinaryProperties(0, 0, 0)
5058
)
5159
(val parent: Option[Group] = None)

cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/asttransform/NonTerminalsAdder.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import za.co.absa.cobrix.cobol.parser.CopybookParser.CopybookAST
2020
import za.co.absa.cobrix.cobol.parser.ast.datatype.AlphaNumeric
2121
import za.co.absa.cobrix.cobol.parser.ast.{Group, Primitive, Statement}
2222
import za.co.absa.cobrix.cobol.parser.common.Constants
23-
import za.co.absa.cobrix.cobol.parser.decoders.DecoderSelector
23+
import za.co.absa.cobrix.cobol.parser.decoders.{DecoderSelector, EncoderSelector}
2424
import za.co.absa.cobrix.cobol.parser.decoders.FloatingPointFormat.FloatingPointFormat
2525
import za.co.absa.cobrix.cobol.parser.encoding.Encoding
2626
import za.co.absa.cobrix.cobol.parser.encoding.codepage.CodePage
@@ -74,13 +74,15 @@ class NonTerminalsAdder(
7474
val sz = g.binaryProperties.actualSize
7575
val dataType = AlphaNumeric(s"X($sz)", sz, enc = Some(enc))
7676
val decode = DecoderSelector.getDecoder(dataType, stringTrimmingPolicy, isDisplayAlwaysString = false, ebcdicCodePage, asciiCharset, isUtf16BigEndian, floatingPointFormat, strictSignOverpunch, improvedNullDetection)
77+
val encode = EncoderSelector.getEncoder(dataType, ebcdicCodePage, asciiCharset)
7778
val newName = getNonTerminalName(g.name, g.parent.get)
7879
newChildren.append(
7980
Primitive(
8081
g.level, newName, "", g.lineNumber,
8182
dataType,
8283
redefines = Some(g.name),
8384
decode = decode,
85+
encode = encode,
8486
binaryProperties = g.binaryProperties
8587
)(g.parent)
8688
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2018 ABSA Group Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package za.co.absa.cobrix.cobol.parser.decoders
18+
19+
import za.co.absa.cobrix.cobol.parser.ast.datatype.CobolType
20+
import za.co.absa.cobrix.cobol.parser.encoding.codepage.{CodePage, CodePageCommon}
21+
22+
import java.nio.charset.{Charset, StandardCharsets}
23+
24+
object EncoderSelector {
25+
type Encoder = Any => Array[Byte]
26+
27+
def getEncoder(dataType: CobolType,
28+
ebcdicCodePage: CodePage = new CodePageCommon,
29+
asciiCharset: Charset = StandardCharsets.US_ASCII): Option[Encoder] = {
30+
None
31+
}
32+
33+
}

cobol-parser/src/test/scala/za/co/absa/cobrix/cobol/parser/extract/BinaryExtractorSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.scalatest.funsuite.AnyFunSuite
2020
import za.co.absa.cobrix.cobol.parser.CopybookParser
2121
import za.co.absa.cobrix.cobol.parser.ast.datatype.{AlphaNumeric, CobolType}
2222
import za.co.absa.cobrix.cobol.parser.ast.{BinaryProperties, Group, Primitive}
23-
import za.co.absa.cobrix.cobol.parser.decoders.DecoderSelector
23+
import za.co.absa.cobrix.cobol.parser.decoders.{DecoderSelector, EncoderSelector}
2424
import za.co.absa.cobrix.cobol.parser.encoding.EBCDIC
2525

2626
class BinaryExtractorSpec extends AnyFunSuite {
@@ -160,7 +160,7 @@ class BinaryExtractorSpec extends AnyFunSuite {
160160
val binaryProperties: BinaryProperties = BinaryProperties(2, 10, 10)
161161

162162
val primitive: Primitive = Primitive(level, name, name, lineNumber, dataType, redefines, isRedefined,
163-
occurs, to, dependingOn, Map(), isDependee, isFiller, DecoderSelector.getDecoder(dataType), binaryProperties)(None)
163+
occurs, to, dependingOn, Map(), isDependee, isFiller, DecoderSelector.getDecoder(dataType), EncoderSelector.getEncoder(dataType), binaryProperties)(None)
164164
val result2: Any = copybook.extractPrimitiveField(primitive, bytes, startOffset)
165165
assert(result2.asInstanceOf[String] === "EXAMPLE4")
166166
}

0 commit comments

Comments
 (0)