Skip to content

Commit 8c26b93

Browse files
EliasCalbertnetymk
authored andcommitted
Moved from Parsec to Megaparsec (parapluu#670)
* Moved from Parsec to MegaParsec * Refactoring * Add stack setup to Makefile This should automatically update the dependencies of encorec when they are changed. * Move line in Makefile * Add --system-ghc to stack calls * Rename install-deps to stack-setup * Remove old reference to "bundles" * More Makefile cleanup * Remove stack dependency for make clean
1 parent 74773cd commit 8c26b93

21 files changed

+244
-260
lines changed

Makefile

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ all: encorec
1414
typecheck:
1515
cabal build --ghc-option=-fno-code
1616

17-
encorec: dirs pony install-deps
17+
encorec: dirs pony stack-setup
1818
export ENCORE_MODULES="$(CURDIR)/modules/" && \
19-
stack --install-ghc --system-ghc install --local-bin-path $(RELEASE_DIR)
19+
stack install --system-ghc --local-bin-path $(RELEASE_DIR)
2020

21-
install-deps:
22-
stack --install-ghc --system-ghc install --dependencies-only
21+
stack-setup:
22+
stack setup --system-ghc
2323

2424
test: encorec
2525
make -C $(SRC_DIR) test
@@ -29,7 +29,7 @@ coverage: dirs pony
2929
find src -name "*.tix" -print0 | xargs -0 rm -rf;
3030
cabal clean;
3131
cabal configure --enable-tests --enable-coverage;
32-
ENCORE_BUNDLES="$(CURDIR)/bundles/" cabal build;
32+
ENCORE_MODULES="$(CURDIR)/modules/" cabal build;
3333
cp dist/build/encorec/encorec $(RELEASE_DIR);
3434
-make -C $(SRC_DIR) test;
3535
mkdir -p coverage;
@@ -42,10 +42,10 @@ coverage: dirs pony
4242
SET_DIR=$(RUNTIME_DIR)/set
4343
FUTURE_DIR=$(RUNTIME_DIR)/future
4444
ENCORE_DIR=$(RUNTIME_DIR)/encore
45-
doc: install-deps
46-
export ENCORE_BUNDLES="$(CURDIR)/bundles/" && \
45+
doc: stack-setup
46+
export ENCORE_MODULES="$(CURDIR)/modules/" && \
4747
make -C doc/encore/ && \
48-
stack haddock
48+
stack haddock --system-ghc
4949

5050
dirs: $(INC_DIR) $(LIB_DIR)
5151

@@ -108,7 +108,7 @@ pony: dirs $(PONY_INC)
108108
cp -r $(RANGE_LIB) $(LIB_DIR)
109109

110110
clean:
111-
stack clean
111+
rm -rf .stack-work/dist
112112
rm -rf dist
113113
make -C doc/encore clean
114114
make -C $(SRC_DIR) clean

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ Alternatively, run: `brew update; brew install premake`
116116

117117
If you have homebrew, you can run `brew install haskell-stack`. Otherwise,
118118
use these [installation instructions](http://docs.haskellstack.org/en/stable/README/#how-to-install).
119+
You might have to run `stack update` in order to get the latest version
120+
of all the dependencies.
119121

120122
##### Installing `pcre2`
121123

encore.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ executable encorec
3636
, hashable
3737
, hpc
3838
, mtl ==2.2.*
39-
, parsec >=3.1.7 && <3.2
39+
, megaparsec >= 5.1.2
40+
, semigroups
4041
, pretty >=1.1 && <1.2
4142
, process >=1.2 && <1.3
4243
, template-haskell

modules/standard/String.enc

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def string_from_char(c : char) : String
2727
let
2828
s = embed (embed char* end) encore_alloc(*_ctx, 2); end
2929
in
30-
new String(embed (embed char* end) *#{s} = #{c}; #{s}; end)
30+
new String(embed (embed char* end) (*#{s}) = #{c}; #{s}; end)
3131

3232
def string_from_array(arr : [char]) : String
3333
let
@@ -36,7 +36,7 @@ def string_from_array(arr : [char]) : String
3636
p = s
3737
in{
3838
for c in arr
39-
embed void *#{p}++ = #{c}; end;
39+
embed void (*#{p}++) = #{c}; end;
4040
new String(s)
4141
}
4242

src/back/CodeGen/CCodeNames.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import CCode.Main
1919
import Data.List
2020
import Data.Char
2121
import Data.String.Utils
22-
import Text.Parsec.Pos as P
2322

2423
import qualified AST.AST as A
2524

@@ -251,7 +250,7 @@ qualifiedToString ID.QName{ID.qnsource = Nothing, ID.qnlocal} = show qnlocal
251250
qualifiedToString ID.QName{ID.qnsource = Just s, ID.qnlocal} =
252251
sourceToString s ++ show qnlocal
253252

254-
sourceToString :: SourceName -> String
253+
sourceToString :: FilePath -> String
255254
sourceToString = map translateSep . filter (/='.') . dropEnc
256255
where
257256
translateSep '/' = '_'

src/back/CodeGen/Expr.hs

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import CodeGen.Type
1111
import qualified CodeGen.Context as Ctx
1212
import CodeGen.DTrace
1313

14-
import qualified Text.Parsec as Parsec
15-
import qualified Text.Parsec.String as PString
16-
1714
import CCode.Main
1815
import CCode.PrettyCCode
1916

@@ -572,7 +569,7 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
572569
| isActive && isFuture = delegateUse callTheMethodFuture "fut"
573570
| otherwise = error $ "Expr.hs: Don't know how to call target of type " ++
574571
Ty.showWithKind targetTy ++
575-
" at " ++ show (A.getPos call)
572+
" at " ++ Meta.showPos (A.getMeta call)
576573
where
577574
targetTy = A.getType target
578575
retTy = A.getType call
@@ -1305,7 +1302,7 @@ targetNullCheck ntarget target name meta op =
13051302
String op,
13061303
String (show (PP.ppExpr target)),
13071304
String (show name),
1308-
String (show (Meta.getPos meta))]
1305+
String (Meta.showPos meta)]
13091306

13101307
runtimeTypeArguments [] = return (nullVar, Skip)
13111308
runtimeTypeArguments typeArgs = do

src/front/ModuleExpander.hs

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import System.Directory(doesFileExist)
2121
import Data.Map.Strict(Map)
2222
import qualified Data.Map.Strict as Map
2323
import Data.List
24-
import Text.Parsec.Pos(SourceName, initialPos)
24+
import Text.Megaparsec(parseErrorPretty, initialPos)
2525
import Debug.Trace
2626

27-
type ProgramTable = Map SourceName Program
27+
type ProgramTable = Map FilePath Program
2828

2929
dirAndName = dirname' &&& basename
3030
where
@@ -56,7 +56,7 @@ stdLib source = [lib "String"]
5656
,ihiding = Nothing
5757
}
5858

59-
addStdLib :: SourceName -> ModuleDecl -> [ImportDecl] -> [ImportDecl]
59+
addStdLib :: FilePath -> ModuleDecl -> [ImportDecl] -> [ImportDecl]
6060
addStdLib source NoModule imports = stdLib source ++ imports
6161
addStdLib source Module{modname} imports =
6262
filter ((/= explicitNamespace [modname]) . itarget) (stdLib source) ++ imports
@@ -118,7 +118,7 @@ buildModulePath (NSExplicit ns) =
118118
then moduleName
119119
else moduleDir </> moduleName
120120

121-
findSource :: [FilePath] -> FilePath -> ImportDecl -> IO SourceName
121+
findSource :: [FilePath] -> FilePath -> ImportDecl -> IO FilePath
122122
findSource importDirs sourceDir Import{itarget} = do
123123
let modulePath = buildModulePath itarget
124124
sources = nub $
@@ -149,7 +149,7 @@ importModule importDirs preludePaths table source
149149
else raw
150150
ast <- case parseEncoreProgram source code of
151151
Right ast -> return ast
152-
Left error -> abort $ show error
152+
Left error -> abort $ parseErrorPretty error
153153
if moduledecl ast == NoModule
154154
then abort $ "No module in file " ++ source ++ ". Aborting!"
155155
else let (sourceDir, sourceName) = dirAndName source

src/front/TopLevel.hs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import System.Process
1717
import System.Posix.Directory
1818
import Data.List
1919
import Data.List.Utils(split)
20+
import qualified Data.List.NonEmpty as NE(head)
2021
import Data.Maybe
2122
import Data.String.Utils
2223
import Control.Monad
@@ -26,6 +27,8 @@ import Language.Haskell.TH -- for Template Haskell hackery
2627
import Text.Printf
2728
import qualified Text.PrettyPrint.Boxes as Box
2829
import System.FilePath (splitPath, joinPath)
30+
import Text.Megaparsec.Error(errorPos, parseErrorTextPretty)
31+
import AST.Meta(showSourcePos)
2932

3033
import Makefile
3134
import Utils
@@ -307,7 +310,10 @@ main =
307310
verbose options "== Parsing =="
308311
ast <- case parseEncoreProgram sourceName code of
309312
Right ast -> return ast
310-
Left error -> abort $ show error
313+
Left error -> do
314+
let pos = NE.head $ errorPos error
315+
abort $ showSourcePos pos ++ ":\n" ++
316+
parseErrorTextPretty error
311317

312318
verbose options "== Importing modules =="
313319
programTable <- buildProgramTable importDirs preludePaths ast

src/ir/AST/AST.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.List
1313
import Data.Map.Strict(Map)
1414
import qualified Data.Map.Strict as Map
1515
import Data.Maybe
16-
import Text.Parsec(SourcePos, SourceName)
16+
import Text.Megaparsec(SourcePos)
1717

1818
import Identifiers
1919
import Types
@@ -23,7 +23,7 @@ data FileDescriptor = Stdout | Stderr
2323
deriving (Show, Eq)
2424

2525
data Program = Program {
26-
source :: SourceName,
26+
source :: FilePath,
2727
moduledecl :: ModuleDecl,
2828
etl :: [EmbedTL],
2929
imports :: [ImportDecl],
@@ -97,7 +97,7 @@ data ImportDecl = Import {
9797
ihiding :: Maybe [Name],
9898
iselect :: Maybe [Name],
9999
ialias :: Maybe Namespace,
100-
isource :: Maybe SourceName
100+
isource :: Maybe FilePath
101101
} deriving (Show)
102102

103103
instance HasMeta ImportDecl where
@@ -170,14 +170,14 @@ data Function =
170170
funheader :: FunctionHeader,
171171
funbody :: Expr,
172172
funlocals :: [Function],
173-
funsource :: SourceName
173+
funsource :: FilePath
174174
}
175175
| MatchingFunction {
176176
funmeta :: Meta Function,
177177
matchfunheaders :: [FunctionHeader],
178178
matchfunbodies :: [Expr],
179179
funlocals :: [Function],
180-
funsource :: SourceName
180+
funsource :: FilePath
181181
} deriving (Show)
182182

183183
functionName = hname . funheader
@@ -374,7 +374,7 @@ conjunctiveTypesFromComposition _ = []
374374
-- | @translateCompositionNamespace table@ gives the included
375375
-- traits of a class (if any) the namespace specified by @table@.
376376
translateCompositionNamespace ::
377-
Map SourceName Namespace -> Maybe TraitComposition -> Maybe TraitComposition
377+
Map FilePath Namespace -> Maybe TraitComposition -> Maybe TraitComposition
378378
translateCompositionNamespace table Nothing = Nothing
379379
translateCompositionNamespace table (Just tc@TraitLeaf{tcname}) =
380380
let source = getRefSourceFile tcname
@@ -496,7 +496,7 @@ replaceHeaderTypes bindings header =
496496
p{ptype = replaceTypeVars bindings ptype}
497497

498498
translateHeaderNamespace ::
499-
Map SourceName Namespace -> FunctionHeader -> FunctionHeader
499+
Map FilePath Namespace -> FunctionHeader -> FunctionHeader
500500
translateHeaderNamespace table header =
501501
let hparams' = map (translateParamType table) (hparams header)
502502
htype' = translateTypeNamespace table (htype header)

src/ir/AST/Desugarer.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import qualified AST.Meta as Meta
66
import AST.PrettyPrinter
77
import AST.Util
88
import Types
9+
import Text.Megaparsec
910

1011
import qualified Data.List as List
1112

@@ -359,4 +360,4 @@ desugar e = e
359360
assertionFailed emeta assert =
360361
StringLiteral (cloneMeta emeta) $
361362
"Assertion failed at " ++
362-
show (Meta.getPos emeta) ++ ":\n" ++ assert
363+
Meta.showPos emeta ++ ":\n" ++ assert

src/ir/AST/Meta.hs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module AST.Meta where
22

3-
import Text.Parsec(SourcePos, sourceLine, sourceColumn)
3+
import Text.Megaparsec(unPos, SourcePos(..))
44
import Data.Maybe
5+
import Text.Printf
56

67
import Types
78

@@ -21,14 +22,17 @@ meta pos = Meta {sourcePos = pos
2122
,sugared = Nothing
2223
,metaInfo = Unspecified}
2324

24-
getPos :: Meta a -> SourcePos
25-
getPos = sourcePos
25+
showSourcePos pos =
26+
let line = unPos (sourceLine pos)
27+
col = unPos (sourceColumn pos)
28+
file = sourceName pos
29+
in printf "%s (line %d, column %d)" (show file) line col
2630

27-
getLine :: Meta a -> Int
28-
getLine = sourceLine . sourcePos
31+
showPos :: Meta a -> String
32+
showPos = showSourcePos . sourcePos
2933

30-
getCol :: Meta a -> Int
31-
getCol = sourceColumn . sourcePos
34+
getPos :: Meta a -> SourcePos
35+
getPos = sourcePos
3236

3337
setType :: Type -> Meta a -> Meta a
3438
setType newType m = m {metaType = Just newType}

src/ir/Identifiers.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ module Identifiers where
1010

1111
import Data.List
1212
import Data.Maybe
13-
import Text.Parsec.Pos as P
1413

1514
-- | An identifier of a variable, a method, a parameter, etc.
1615
newtype Name = Name String deriving (Read, Eq, Ord)
1716
instance Show Name where
1817
show (Name n) = n
1918

2019
data Namespace = NSExplicit [Name]
21-
| NSImplicit SourceName
20+
| NSImplicit FilePath
2221
deriving(Eq, Ord)
2322

2423
instance Show Namespace where
@@ -41,7 +40,7 @@ isExplicitNamespace _ = False
4140

4241
data QualifiedName =
4342
QName{qnspace :: Maybe Namespace
44-
,qnsource :: Maybe SourceName
43+
,qnsource :: Maybe FilePath
4544
,qnlocal :: Name
4645
} deriving(Eq)
4746

@@ -60,7 +59,7 @@ qName = qLocal . Name
6059
setNamespace :: Namespace -> QualifiedName -> QualifiedName
6160
setNamespace ns qname = qname{qnspace = Just ns}
6261

63-
setSourceFile :: SourceName -> QualifiedName -> QualifiedName
62+
setSourceFile :: FilePath -> QualifiedName -> QualifiedName
6463
setSourceFile source qname = qname{qnsource = Just source}
6564

6665
isLocalQName :: QualifiedName -> Bool

0 commit comments

Comments
 (0)