From 04e9b0d2567dfef749b586c42f765f3d8a97eee7 Mon Sep 17 00:00:00 2001 From: Ben Lippmeier Date: Wed, 2 Jan 2019 14:41:05 +1100 Subject: [PATCH] Add Eq, Ord instances for location and range, also non-IO drivers --- Changelog.md | 2 +- Readme.md | 2 +- Text/Lexer/Inchworm.hs | 20 +++++++++++++++++++- Text/Lexer/Inchworm/Char.hs | 19 ++++++++++++++++++- Text/Lexer/Inchworm/Source.hs | 4 ++-- examples/lispy/Main.hs | 2 +- examples/mlish/Main.hs | 2 +- inchworm.cabal | 2 +- 8 files changed, 44 insertions(+), 9 deletions(-) diff --git a/Changelog.md b/Changelog.md index 2ae9aea..7ea31ad 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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. diff --git a/Readme.md b/Readme.md index 0dae121..cbba307 100644 --- a/Readme.md +++ b/Readme.md @@ -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 ``` diff --git a/Text/Lexer/Inchworm.hs b/Text/Lexer/Inchworm.hs index 25b730e..1773cd4 100644 --- a/Text/Lexer/Inchworm.hs +++ b/Text/Lexer/Inchworm.hs @@ -73,6 +73,7 @@ module Text.Lexer.Inchworm , Scanner -- * Generic Scanning + , scanList , scanListIO -- ** Source Construction @@ -101,6 +102,7 @@ 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, @@ -108,8 +110,22 @@ import Text.Lexer.Inchworm.Combinator -- 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. @@ -121,3 +137,5 @@ scanListIO scanListIO loc bump input scanner = do src <- makeListSourceIO loc bump input scanSourceToList src scanner + + diff --git a/Text/Lexer/Inchworm/Char.hs b/Text/Lexer/Inchworm/Char.hs index 6085e09..e74fb75 100644 --- a/Text/Lexer/Inchworm/Char.hs +++ b/Text/Lexer/Inchworm/Char.hs @@ -4,6 +4,7 @@ module Text.Lexer.Inchworm.Char ( module Text.Lexer.Inchworm -- * Driver + , scanString , scanStringIO -- * Locations @@ -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 diff --git a/Text/Lexer/Inchworm/Source.hs b/Text/Lexer/Inchworm/Source.hs index 03112f7..355c186 100644 --- a/Text/Lexer/Inchworm/Source.hs +++ b/Text/Lexer/Inchworm/Source.hs @@ -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. @@ -215,7 +215,7 @@ data Location = Location !Int -- Line. !Int -- Column. - deriving Show + deriving (Show, Eq, Ord) {-# SPECIALIZE INLINE diff --git a/examples/lispy/Main.hs b/examples/lispy/Main.hs index 061150c..a3d2755 100644 --- a/examples/lispy/Main.hs +++ b/examples/lispy/Main.hs @@ -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 diff --git a/examples/mlish/Main.hs b/examples/mlish/Main.hs index cb23851..be23b1d 100644 --- a/examples/mlish/Main.hs +++ b/examples/mlish/Main.hs @@ -20,7 +20,7 @@ main , "'\\137'" , "\"derpo\\ntro\\BELnic\""] - result <- scanStringIO source (scanner fileName) + let result = scanString source (scanner fileName) print result diff --git a/inchworm.cabal b/inchworm.cabal index a417c58..58ccb9f 100644 --- a/inchworm.cabal +++ b/inchworm.cabal @@ -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