Skip to content

Commit 643b427

Browse files
committed
Add two missing bits to SAWScript's Cryptol import syntax.
- allow the word "submodule" (it is currently ignored, but that will likely change soon) - allow the import name to be qualified (as in "...as M::N") Add a test for the latter part.
1 parent 951213b commit 643b427

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

intTests/test2605/Foo.cry

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Foo where
2+
3+
x : [8]
4+
x = 3

intTests/test2605/test.saw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "Foo.cry" as Moo::Boo;
2+
3+
let y = {{ Moo::Boo::x }};

intTests/test2605/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set -e
2+
3+
$SAW test.saw

saw-central/src/SAWCentral/AST.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ type LName = Located Name
120120
-- Expr Level {{{
121121

122122
data Import = Import
123-
{ iModule :: Either FilePath P.ModName
123+
{ iIsSubmodule :: Bool
124+
, iModule :: Either FilePath P.ModName
124125
, iAs :: Maybe P.ModName
125126
, iSpec :: Maybe P.ImportSpec
126127
, iPos :: Pos

saw-script/src/SAWScript/Lexer.x

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $idfirst = [$alpha \_]
4747
$idchar = [$alpha $digit $unidigit $unitick \' \_]
4848
$codechar = [$graphic $whitechar \n]
4949

50-
@reservedid = import|and|let|rec|in|do|if|then|else|as|hiding|typedef
50+
@reservedid = import|submodule|and|let|rec|in|do|if|then|else|as|hiding|typedef
5151
|JavaSetup|LLVMSetup|MIRSetup|ProofScript|TopLevel|CrucibleSetup
5252
|Int|String|Term|Type|Bool|AIG|CFG
5353
|CrucibleMethodSpec|LLVMSpec|JVMMethodSpec|JVMSpec|MIRSpec

saw-script/src/SAWScript/Parser.y

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import Control.Exception
5151

5252
%token
5353
'import' { TReserved _ "import" }
54+
'submodule' { TReserved _ "submodule" }
5455
'and' { TReserved _ "and" }
5556
'as' { TReserved _ "as" }
5657
'hiding' { TReserved _ "hiding" }
@@ -89,6 +90,7 @@ import Control.Exception
8990
'{' { TPunct _ "{" }
9091
'}' { TPunct _ "}" }
9192
':' { TPunct _ ":" }
93+
'::' { TPunct _ "::" }
9294
',' { TPunct _ "," }
9395
'.' { TPunct _ "." }
9496
'\\' { TPunct _ "\\" }
@@ -134,11 +136,12 @@ StmtSemi :: { Stmt }
134136
: fst(Stmt, opt(';')) { $1 }
135137

136138
Import :: { Import }
137-
: string mbAs mbImportSpec { Import (Left (unpack $ tokStr $1)) (fst $2) (fst $3) (maxSpan [tokPos $1, snd $2, snd $3])}
139+
: string mbAs mbImportSpec { buildImport False $1 $2 $3 }
140+
| 'submodule' string mbAs mbImportSpec { buildImport True $2 $3 $4 }
138141
-- TODO: allow imports by module name instead of path
139142

140143
mbAs :: { (Maybe P.ModName, Pos) }
141-
: 'as' name { (Just (P.packModName [tokStr $2]), maxSpan [$1, $2]) }
144+
: 'as' QName { (Just (P.packModName (fst $2)), maxSpan' $1 (snd $2)) }
142145
| {- empty -} { (Nothing, Unknown) }
143146
144147
mbImportSpec :: { (Maybe P.ImportSpec, Pos) }
@@ -271,6 +274,9 @@ Context :: { Type }
271274
FieldType :: { (Name, Type) }
272275
: name ':' Type { (tokStr $1, $3) }
273276
277+
QName :: { ([Text], Pos) }
278+
: sepBy1(name, '::') { (map tokStr $1, maxSpan (map tokPos $1)) }
279+
274280
-- Parameterized productions, most come directly from the Happy manual.
275281
fst(p, q) : p q { $1 }
276282
snd(p, q) : p q { $2 }
@@ -356,6 +362,22 @@ parseError toks = case toks of
356362
[] -> Left UnexpectedEOF
357363
t : _ -> Left (UnexpectedToken t)
358364

365+
-- | Cons up an import.
366+
buildImport ::
367+
Bool ->
368+
Token Pos ->
369+
(Maybe P.ModName, Pos) ->
370+
(Maybe P.ImportSpec, Pos) ->
371+
Import
372+
buildImport issub modName (mbAsName, asPos) (mbSpec, specPos) =
373+
Import {
374+
iIsSubmodule = issub,
375+
iModule = Left (unpack $ tokStr modName),
376+
iAs = mbAsName,
377+
iSpec = mbSpec,
378+
iPos = maxSpan [tokPos modName, asPos, specPos]
379+
}
380+
359381
-- | As seen by the parser, a "function name" is an arbitrary pattern.
360382
-- This is because we use the same syntax for function and value bindings:
361383
-- in "let (a, b) = e" the "(a, b)" can and should be an arbitrary pattern.

0 commit comments

Comments
 (0)