0

I'm getting an error in parsing a .dat file. This is the error I am getting:

shapes.dat:1: parse error, expected '(' or '[' or '#' or '{'.

The way I am reading the file:

ShapeApp >> readShapesFromFile: fileName [
        | fileStream lines vec |
        vec := OrderedCollection new.
        [fileStream := FileStream open: fileName mode: #read]
            " on: FileDoesNotExistException "
            do: [:ex | Transcript show: 'Error: File does not exist'; cr. ^vec].

            lines := fileStream lines.
            lines do: [:line | 
               
                | tokens name shapeType radius height length width smallRadius bigRadius |
                tokens := line findTokens: ' '.
                name := tokens first.
                shapeType := tokens second.
                
                shapeType caseOf: {
                    ['sphere'] -> [
                        radius := tokens third asNumber.
                        vec add: (Sphere newWithName: name radius: radius)
                    ].
                    ['cylinder'] -> [
                        radius := tokens third asNumber.
                        height := tokens fourth asNumber.
                        vec add: (Cylinder newWithName: name radius: radius height: height)
                    ].
                    ['torus'] -> [
                        smallRadius := tokens third asNumber.
                        bigRadius := tokens fourth asNumber.
                        vec add: (Torus newWithName: name smallRadius: smallRadius bigRadius: bigRadius)
                    ].
                    ['box'] -> [
                        length := tokens third asNumber.
                        width := tokens fourth asNumber.
                        height := tokens fifth asNumber.
                        vec add: (Box newWithName: name length: length width: width height: height)
                    ]
                } otherwise: [
                    Transcript show: 'Error: Unknown shape type'; cr.
                ]
            ].

        fileStream close.
        ^vec
    ]

And the way I am using that function above:

ShapeApp >> printAllShapes [
        | shapes |
        shapes := self readShapesFromFile: 'shapes.dat'.
        self printShapes: shapes
    ]

And this is the print:

ShapeApp >> printAllShapes [
        | shapes |
        shapes := self readShapesFromFile: 'shapes.dat'.
        self printShapes: shapes
    ]

This is how the the .dat file, shapes.dat looks like, and it seems normal to me:

Cube#1 box 1 1 1
Cube#2 box 2 2 2
Donut#1 torus 1 1
Cyl#1 cylinder 1 1
Case#1 box 2 4 6
Case#2 box 10.5 21 10.5
UnitSphere sphere 1
LargeSphere sphere 100
Donut#2 torus 3 7
Cyl#2 cylinder 1 2

I tried many different ways, but the error still persists, and it is giving me the parsing error.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Have you tried uncommenting `on: FileDoesNotExistException`, i.e., removing the double quotes? Otherwise the code is sending the message `do:` to a block. – Leandro Caniglia Apr 06 '23 at 11:55
  • I don't need to do that, since it loads the file when I am running it like this: shape.st torus.st box.st sphere.st cylinder.st -f main.st shapes.dat – anirudh raghavan Apr 06 '23 at 20:25

0 Answers0