Skip to content

Commit 04e9b0d

Browse files
committed
Add Eq, Ord instances for location and range, also non-IO drivers
1 parent 9395885 commit 04e9b0d

File tree

8 files changed

+44
-9
lines changed

8 files changed

+44
-9
lines changed

Changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## inchworm-1.1.1.1:
1+
## inchworm-1.1.1:
22

33
* Matching combinators now produce the range of source locations that matched,
44
instead of just the starting location.

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ main :: IO ()
6767
main
6868
= do let fileName = "Source.lispy"
6969
let source = "(some (Lispy like) 26 Program 93 (for you))"
70-
toks <- scanStringIO source (scanner fileName)
70+
let toks = scanString source (scanner fileName)
7171
print toks
7272
```

Text/Lexer/Inchworm.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module Text.Lexer.Inchworm
7373
, Scanner
7474

7575
-- * Generic Scanning
76+
, scanList
7677
, scanListIO
7778

7879
-- ** Source Construction
@@ -101,15 +102,30 @@ where
101102
import Text.Lexer.Inchworm.Source
102103
import Text.Lexer.Inchworm.Scanner
103104
import Text.Lexer.Inchworm.Combinator
105+
import System.IO.Unsafe
104106

105107

106108
-- | Scan a list of generic input tokens in the IO monad,
107109
-- returning the source location of the final input token,
108110
-- along with the remaining input.
109111
--
110112
-- NOTE: If you just want to scan a `String` of characters
111-
-- use @scanStringIO@ from "Text.Lexer.Inchworm.Char"
113+
-- use @scanString@ from "Text.Lexer.Inchworm.Char"
112114
--
115+
scanList
116+
:: Eq i
117+
=> loc -- ^ Starting source location.
118+
-> (i -> loc -> loc) -- ^ Function to bump the current location by one input token.
119+
-> [i] -- ^ List of input tokens.
120+
-> Scanner IO loc [i] a -- ^ Scanner to apply.
121+
-> ([a], loc, [i])
122+
123+
scanList loc bump input scanner
124+
= unsafePerformIO $ scanListIO loc bump input scanner
125+
126+
127+
-- | Implementation for `scanList`,
128+
-- that uses the IO monad to manage its state.
113129
scanListIO
114130
:: Eq i
115131
=> loc -- ^ Starting source location.
@@ -121,3 +137,5 @@ scanListIO
121137
scanListIO loc bump input scanner
122138
= do src <- makeListSourceIO loc bump input
123139
scanSourceToList src scanner
140+
141+

Text/Lexer/Inchworm/Char.hs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Text.Lexer.Inchworm.Char
44
( module Text.Lexer.Inchworm
55

66
-- * Driver
7+
, scanString
78
, scanStringIO
89

910
-- * Locations
@@ -19,13 +20,29 @@ module Text.Lexer.Inchworm.Char
1920
where
2021
import Text.Lexer.Inchworm
2122
import Text.Lexer.Inchworm.Source
23+
import System.IO.Unsafe
2224
import qualified Data.Char as Char
2325
import qualified Data.List as List
2426
import qualified Numeric as Numeric
2527

2628

2729
-- Driver ---------------------------------------------------------------------
28-
-- | Scan a string, using the IO monad to maintain internal state.
30+
-- | Scan a string.
31+
scanString
32+
:: String
33+
-> Scanner IO Location String a
34+
-> ([a], Location, String)
35+
36+
scanString str scanner
37+
= unsafePerformIO
38+
$ scanListIO
39+
(Location 0 0)
40+
bumpLocationWithChar
41+
str scanner
42+
43+
44+
-- | Implementation for `scanString`,
45+
-- that uses the IO monad to maintain internal state.
2946
scanStringIO
3047
:: String
3148
-> Scanner IO Location String a

Text/Lexer/Inchworm/Source.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ makeListSourceIO loc00 bumpLoc cs0
203203
-- | A range of locations in a source file.
204204
data Range loc
205205
= Range !loc !loc
206-
deriving Show
206+
deriving (Show, Eq, Ord)
207207

208208

209209
-- | A location in a source file.
@@ -215,7 +215,7 @@ data Location
215215
= Location
216216
!Int -- Line.
217217
!Int -- Column.
218-
deriving Show
218+
deriving (Show, Eq, Ord)
219219

220220

221221
{-# SPECIALIZE INLINE

examples/lispy/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ main :: IO ()
3535
main
3636
= do let fileName = "Source.lispy"
3737
let source = "(some (Lispy like) 26 Program 93 (for you))"
38-
toks <- scanStringIO source (scanner fileName)
38+
let toks = scanString source (scanner fileName)
3939
print toks

examples/mlish/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ main
2020
, "'\\137'"
2121
, "\"derpo\\ntro\\BELnic\""]
2222

23-
result <- scanStringIO source (scanner fileName)
23+
let result = scanString source (scanner fileName)
2424
print result
2525

2626

inchworm.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: inchworm
2-
version: 1.1.1.1
2+
version: 1.1.1.2
33
license: MIT
44
license-file: LICENSE
55
author: The Inchworm Development Team

0 commit comments

Comments
 (0)