Skip to content

Commit c89f3dd

Browse files
authored
Merge pull request #260 from sepehr541/parsing-numbers-fix
fix parsing numbers with radix other than 10
2 parents c70e15c + fb1a7f3 commit c89f3dd

4 files changed

Lines changed: 64 additions & 5 deletions

File tree

src/pgo/parser/TLAParser.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,23 @@ trait TLAParser extends RegexParsers {
138138
regex(raw"\\[bB][01]+".r) ^^ { str =>
139139
(
140140
TLANumber.IntValue(
141-
BigInt(str.stripPrefix("b").stripPrefix("B"), 2),
141+
BigInt(str.stripPrefix("\\").stripPrefix("b").stripPrefix("B"), 2),
142142
),
143143
TLANumber.BinarySyntax,
144144
)
145145
} |
146146
regex(raw"\\[oO][0-7]+".r) ^^ { str =>
147147
(
148148
TLANumber.IntValue(
149-
BigInt(str.stripPrefix("o").stripPrefix("O"), 8),
149+
BigInt(str.stripPrefix("\\").stripPrefix("o").stripPrefix("O"), 8),
150150
),
151151
TLANumber.OctalSyntax,
152152
)
153153
} |
154154
regex(raw"\\[hH][0-9a-fA-F]+".r) ^^ { str =>
155155
(
156156
TLANumber.IntValue(
157-
BigInt(str.stripPrefix("h").stripPrefix("H"), 16),
157+
BigInt(str.stripPrefix("\\").stripPrefix("h").stripPrefix("H"), 16),
158158
),
159159
TLANumber.HexadecimalSyntax,
160160
)

src/pgo/trans/TLARenderPass.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import pgo.model.pcal.*
99
import pgo.model.tla.*
1010
import pgo.model.Definition
1111
import scala.annotation.tailrec
12+
import pgo.model.tla.TLANumber.{
13+
DecimalSyntax,
14+
BinarySyntax,
15+
OctalSyntax,
16+
HexadecimalSyntax,
17+
}
1218

1319
object TLARenderPass:
1420
def describeQuantifierBound(qb: TLAQuantifierBound): Description =
@@ -53,11 +59,17 @@ object TLARenderPass:
5359
}""""
5460
case TLANumber(
5561
value,
56-
_, /* force decimal representation, should be correct in most cases */
62+
rep,
5763
) =>
64+
val (prefix, radix) = rep match
65+
case DecimalSyntax => ("", 10)
66+
case BinarySyntax => ("\\b", 2)
67+
case OctalSyntax => ("\\o", 8)
68+
case HexadecimalSyntax => ("\\h", 16)
69+
5870
value match {
5971
case TLANumber.IntValue(value) =>
60-
value.toString().toDescription
72+
s"$prefix${value.toString(radix)}".toDescription
6173
case TLANumber.DecimalValue(value) =>
6274
value.toString().toDescription
6375
}

test/files/tla/Numbers.tla

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---- MODULE Integers ----
2+
EXTENDS Naturals, Reals
3+
4+
Int == 1
5+
Hex == \h1
6+
Bin == \b1
7+
Oct == \o1
8+
9+
\* TODO: fix decimal
10+
\* go.parser.DefinitionLookupError:
11+
\* identifier . does not refer to a known definition at /workspaces/pgo/test/files/tla/Numbers.tla:13.13-14
12+
13+
\* Decimal == 1.1
14+
15+
====

test/pgo/TLAUnitCoverageTests.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ package pgo
22

33
import pgo.model.SourceLocation.UnderlyingFile
44
import pgo.parser.TLAParser
5+
import pgo.model.DefinitionOne
6+
import pgo.model.DefinitionComposite
7+
import pgo.model.tla.TLAOperatorDefinition
8+
import pgo.model.tla.TLANumber
9+
import pgo.model.tla.TLANumber.DecimalSyntax
10+
import pgo.model.tla.TLANumber.HexadecimalSyntax
11+
import pgo.model.tla.TLANumber.BinarySyntax
12+
import pgo.model.tla.TLANumber.OctalSyntax
13+
import pgo.model.tla.TLANumber.IntValue
514

615
final class TLAUnitCoverageTests extends munit.FunSuite:
716
def checkNames(tlaFile: os.Path, names: List[String])(using
@@ -32,3 +41,26 @@ final class TLAUnitCoverageTests extends munit.FunSuite:
3241
"Init",
3342
),
3443
)
44+
45+
test("parsing numbers"):
46+
val tlaFile = projectRoot / "test" / "files" / "tla" / "Numbers.tla"
47+
val fileContents = os.read(tlaFile)
48+
val underlyingFile = UnderlyingFile(tlaFile)
49+
val module = TLAParser.readModule(underlyingFile, fileContents)
50+
51+
val values = module.units.view
52+
.collect {
53+
case op: TLAOperatorDefinition => op
54+
}.map {
55+
case TLAOperatorDefinition(name, args, body, isLocal) => body
56+
}.collect {
57+
case num: TLANumber => num
58+
}.toList
59+
60+
val expected = List(
61+
TLANumber(IntValue(1), DecimalSyntax),
62+
TLANumber(IntValue(1), HexadecimalSyntax),
63+
TLANumber(IntValue(1), BinarySyntax),
64+
TLANumber(IntValue(1), OctalSyntax)
65+
)
66+
assertEquals(values, expected)

0 commit comments

Comments
 (0)