Skip to content

Commit

Permalink
Add Eq, Ord instances for location and range, also non-IO drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
benl23x5 committed Jan 2, 2019
1 parent 9395885 commit 04e9b0d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## inchworm-1.1.1.1:
## inchworm-1.1.1:

* Matching combinators now produce the range of source locations that matched,
instead of just the starting location.
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ main :: IO ()
main
= do let fileName = "Source.lispy"
let source = "(some (Lispy like) 26 Program 93 (for you))"
toks <- scanStringIO source (scanner fileName)
let toks = scanString source (scanner fileName)
print toks
```
20 changes: 19 additions & 1 deletion Text/Lexer/Inchworm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module Text.Lexer.Inchworm
, Scanner

-- * Generic Scanning
, scanList
, scanListIO

-- ** Source Construction
Expand Down Expand Up @@ -101,15 +102,30 @@ where
import Text.Lexer.Inchworm.Source
import Text.Lexer.Inchworm.Scanner
import Text.Lexer.Inchworm.Combinator
import System.IO.Unsafe


-- | Scan a list of generic input tokens in the IO monad,
-- returning the source location of the final input token,
-- along with the remaining input.
--
-- NOTE: If you just want to scan a `String` of characters
-- use @scanStringIO@ from "Text.Lexer.Inchworm.Char"
-- use @scanString@ from "Text.Lexer.Inchworm.Char"
--
scanList
:: Eq i
=> loc -- ^ Starting source location.
-> (i -> loc -> loc) -- ^ Function to bump the current location by one input token.
-> [i] -- ^ List of input tokens.
-> Scanner IO loc [i] a -- ^ Scanner to apply.
-> ([a], loc, [i])

scanList loc bump input scanner
= unsafePerformIO $ scanListIO loc bump input scanner


-- | Implementation for `scanList`,
-- that uses the IO monad to manage its state.
scanListIO
:: Eq i
=> loc -- ^ Starting source location.
Expand All @@ -121,3 +137,5 @@ scanListIO
scanListIO loc bump input scanner
= do src <- makeListSourceIO loc bump input
scanSourceToList src scanner


19 changes: 18 additions & 1 deletion Text/Lexer/Inchworm/Char.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Text.Lexer.Inchworm.Char
( module Text.Lexer.Inchworm

-- * Driver
, scanString
, scanStringIO

-- * Locations
Expand All @@ -19,13 +20,29 @@ module Text.Lexer.Inchworm.Char
where
import Text.Lexer.Inchworm
import Text.Lexer.Inchworm.Source
import System.IO.Unsafe
import qualified Data.Char as Char
import qualified Data.List as List
import qualified Numeric as Numeric


-- Driver ---------------------------------------------------------------------
-- | Scan a string, using the IO monad to maintain internal state.
-- | Scan a string.
scanString
:: String
-> Scanner IO Location String a
-> ([a], Location, String)

scanString str scanner
= unsafePerformIO
$ scanListIO
(Location 0 0)
bumpLocationWithChar
str scanner


-- | Implementation for `scanString`,
-- that uses the IO monad to maintain internal state.
scanStringIO
:: String
-> Scanner IO Location String a
Expand Down
4 changes: 2 additions & 2 deletions Text/Lexer/Inchworm/Source.hs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ makeListSourceIO loc00 bumpLoc cs0
-- | A range of locations in a source file.
data Range loc
= Range !loc !loc
deriving Show
deriving (Show, Eq, Ord)


-- | A location in a source file.
Expand All @@ -215,7 +215,7 @@ data Location
= Location
!Int -- Line.
!Int -- Column.
deriving Show
deriving (Show, Eq, Ord)


{-# SPECIALIZE INLINE
Expand Down
2 changes: 1 addition & 1 deletion examples/lispy/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ main :: IO ()
main
= do let fileName = "Source.lispy"
let source = "(some (Lispy like) 26 Program 93 (for you))"
toks <- scanStringIO source (scanner fileName)
let toks = scanString source (scanner fileName)
print toks
2 changes: 1 addition & 1 deletion examples/mlish/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ main
, "'\\137'"
, "\"derpo\\ntro\\BELnic\""]

result <- scanStringIO source (scanner fileName)
let result = scanString source (scanner fileName)
print result


Expand Down
2 changes: 1 addition & 1 deletion inchworm.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: inchworm
version: 1.1.1.1
version: 1.1.1.2
license: MIT
license-file: LICENSE
author: The Inchworm Development Team
Expand Down

0 comments on commit 04e9b0d

Please sign in to comment.