@@ -1240,7 +1240,7 @@ toDeclarationsReport source decl =
12401240 toSpaceReport source space row col
12411241
12421242 Port port_ row col ->
1243− error "TODO Port" port_ row col
1243+ toPortReport source port_ row col
12441244
12451245 DeclType declType row col ->
12461246 toDeclTypeReport source declType row col
@@ -1256,6 +1256,164 @@ toDeclarationsReport source decl =
12561256
12571257
12581258
1259+-- PORT
1260+
1261+
1262+toPortReport :: Code.Source -> Port -> Row -> Col -> Report.Report
1263+toPortReport source port_ startRow startCol =
1264+ case port_ of
1265+ PortSpace space row col ->
1266+ toSpaceReport source space row col
1267+
1268+ PortName row col ->
1269+ case Code.whatIsNext source row col of
1270+ Code.Keyword keyword ->
1271+ let
1272+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1273+ region = toKeywordRegion row col keyword
1274+ in
1275+ Report.Report "RESERVED WORD" region [] $
1276+ Code.toSnippet source surroundings (Just region)
1277+ (
1278+ D.reflow $
1279+ "I cannot handle ports with names like this:"
1280+ ,
1281+ D.reflow $
1282+ "You are trying to make a port named `" ++ keyword
1283+ ++ "` but that is a reserved word. Try using some other name?"
1284+ )
1285+
1286+ _ ->
1287+ let
1288+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1289+ region = toRegion row col
1290+ in
1291+ Report.Report "PORT PROBLEM" region [] $
1292+ Code.toSnippet source surroundings (Just region)
1293+ (
1294+ D.reflow $
1295+ "I just saw the start of a `port` declaration, but then I got stuck here:"
1296+ ,
1297+ D.stack
1298+ [ D.fillSep
1299+ ["I","was","expecting","to","see","a","name","like"
1300+ ,D.dullyellow "send","or",D.dullyellow "receive","next."
1301+ ,"Something","that","starts","with","a","lower-case","letter."
1302+ ]
1303+ , portNote
1304+ ]
1305+ )
1306+
1307+ PortColon row col ->
1308+ let
1309+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1310+ region = toRegion row col
1311+ in
1312+ Report.Report "PORT PROBLEM" region [] $
1313+ Code.toSnippet source surroundings (Just region)
1314+ (
1315+ D.reflow $
1316+ "I just saw the start of a `port` declaration, but then I got stuck here:"
1317+ ,
1318+ D.stack
1319+ [ D.reflow $
1320+ "I was expecting to see a colon next. And then a type that tells me\
1321+ \ what type of values are going to flow through."
1322+ , portNote
1323+ ]
1324+ )
1325+
1326+ PortType tipe row col ->
1327+ toTypeReport source tipe row col
1328+
1329+ PortIndentName row col ->
1330+ let
1331+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1332+ region = toRegion row col
1333+ in
1334+ Report.Report "UNFINISHED PORT" region [] $
1335+ Code.toSnippet source surroundings (Just region)
1336+ (
1337+ D.reflow $
1338+ "I just saw the start of a `port` declaration, but then I got stuck here:"
1339+ ,
1340+ D.stack
1341+ [ D.fillSep
1342+ ["I","was","expecting","to","see","a","name","like"
1343+ ,D.dullyellow "send","or",D.dullyellow "receive","next."
1344+ ,"Something","that","starts","with","a","lower-case","letter."
1345+ ]
1346+ , portNote
1347+ ]
1348+ )
1349+
1350+ PortIndentColon row col ->
1351+ let
1352+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1353+ region = toRegion row col
1354+ in
1355+ Report.Report "UNFINISHED PORT" region [] $
1356+ Code.toSnippet source surroundings (Just region)
1357+ (
1358+ D.reflow $
1359+ "I just saw the start of a `port` declaration, but then I got stuck here:"
1360+ ,
1361+ D.stack
1362+ [ D.reflow $
1363+ "I was expecting to see a colon next. And then a type that tells me\
1364+ \ what type of values are going to flow through."
1365+ , portNote
1366+ ]
1367+ )
1368+
1369+ PortIndentType row col ->
1370+ let
1371+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1372+ region = toRegion row col
1373+ in
1374+ Report.Report "UNFINISHED PORT" region [] $
1375+ Code.toSnippet source surroundings (Just region)
1376+ (
1377+ D.reflow $
1378+ "I just saw the start of a `port` declaration, but then I got stuck here:"
1379+ ,
1380+ D.stack
1381+ [ D.reflow $
1382+ "I was expecting to see a type next. Here are examples of outgoing and\
1383+ \ incoming ports for reference:"
1384+ , D.vcat
1385+ [ D.fillSep [D.cyan "port","send",":","String -> Cmd msg"]
1386+ , D.fillSep [D.cyan "port","receive",":","(String -> msg) -> Sub msg"]
1387+ ]
1388+ , D.reflow $
1389+ "The first line defines a `send` port so you can send strings out to JavaScript.\
1390+ \ Maybe you send them on a WebSocket or put them into IndexedDB. The second line\
1391+ \ defines a `receive` port so you can receive strings from JavaScript. Maybe you\
1392+ \ get receive messages when new WebSocket messages come in or when an entry in\
1393+ \ IndexedDB changes for some external reason."
1394+ ]
1395+ )
1396+
1397+
1398+portNote :: D.Doc
1399+portNote =
1400+ D.stack
1401+ [ D.toSimpleNote $
1402+ "Here are some example `port` declarations for reference:"
1403+ , D.vcat
1404+ [ D.fillSep [D.cyan "port","send",":","String -> Cmd msg"]
1405+ , D.fillSep [D.cyan "port","receive",":","(String -> msg) -> Sub msg"]
1406+ ]
1407+ , D.reflow $
1408+ "The first line defines a `send` port so you can send strings out to JavaScript.\
1409+ \ Maybe you send them on a WebSocket or put them into IndexedDB. The second line\
1410+ \ defines a `receive` port so you can receive strings from JavaScript. Maybe you\
1411+ \ get receive messages when new WebSocket messages come in or when the IndexedDB\
1412+ \ is changed for some external reason."
1413+ ]
1414+
1415+
1416+
12591417-- DECL TYPE
12601418
12611419