From 8fa8516bcf8d40ea3137b62c5f984ed898c7b11c Mon Sep 17 00:00:00 2001 From: Nathan Faubion Date: Sun, 5 Sep 2021 07:06:00 -0700 Subject: [PATCH] Move operator defaults/parsing to src. (#65) --- bin/Bin/Worker.purs | 2 +- bin/Main.purs | 6 ++- script/GenerateDefaultOperatorsModule.purs | 4 +- .../PureScript/CST/Tidy}/Operators.purs | 42 ++++++++++--------- .../CST/Tidy/Operators/Defaults.purs | 2 +- test/Snapshot.purs | 4 +- test/spago.dhall | 2 +- 7 files changed, 33 insertions(+), 29 deletions(-) rename {bin/Bin => src/PureScript/CST/Tidy}/Operators.purs (80%) rename bin/DefaultOperators.purs => src/PureScript/CST/Tidy/Operators/Defaults.purs (99%) diff --git a/bin/Bin/Worker.purs b/bin/Bin/Worker.purs index f5c69cb..feeb2c0 100644 --- a/bin/Bin/Worker.purs +++ b/bin/Bin/Worker.purs @@ -4,7 +4,6 @@ import Prelude import Bin.FormatOptions (FormatOptions) import Bin.FormatOptions as FormatOptions -import Bin.Operators (parseOperatorTable) import Bin.Timing (hrtime, hrtimeDiff, toMilliseconds) import Data.Either (Either(..), either, fromRight') import Data.Lazy (Lazy) @@ -27,6 +26,7 @@ import Partial.Unsafe (unsafeCrashWith) import PureScript.CST (RecoveredParserResult(..), parseModule) import PureScript.CST.Errors (ParseError, printParseError) import PureScript.CST.Tidy (defaultFormatOptions, formatModule, toDoc) +import PureScript.CST.Tidy.Operators (parseOperatorTable) import PureScript.CST.Tidy.Precedence (PrecedenceMap, remapOperators) type WorkerConfig = diff --git a/bin/Main.purs b/bin/Main.purs index 16941c1..0022a88 100644 --- a/bin/Main.purs +++ b/bin/Main.purs @@ -6,7 +6,6 @@ import ArgParse.Basic (ArgParser) import ArgParse.Basic as Arg import Bin.FormatOptions (FormatOptions, formatOptions) import Bin.FormatOptions as FormatOptions -import Bin.Operators (getModuleName, parseOperatorTable, resolveOperatorExports) import Bin.Version (version) import Bin.Worker (WorkerData, WorkerInput, WorkerOutput, formatCommand, formatInPlaceCommand, toWorkerConfig) import Control.Monad.State (evalStateT, lift) @@ -34,7 +33,6 @@ import Data.String as String import Data.Traversable (for, traverse) import Data.Tuple (Tuple(..)) import Data.Tuple.Nested ((/\)) -import DefaultOperators (defaultOperators) import Effect (Effect) import Effect.Aff (Aff, error, launchAff_, makeAff, throwError, try) import Effect.Class (liftEffect) @@ -56,7 +54,10 @@ import Node.WorkerBees.Aff.Pool (poolTraverse) import PureScript.CST (RecoveredParserResult(..), parseModule, toRecovered) import PureScript.CST.Errors (printParseError) import PureScript.CST.ModuleGraph (ModuleSort(..), sortModules) +import PureScript.CST.Tidy.Operators (parseOperatorTable, resolveOperatorExports) +import PureScript.CST.Tidy.Operators.Defaults (defaultOperators) import PureScript.CST.Tidy.Precedence (OperatorNamespace(..), PrecedenceMap) +import PureScript.CST.Types (Module(..), ModuleHeader(..), Name(..)) data FormatMode = Check | Write @@ -389,6 +390,7 @@ generateOperatorsCommand globs = do case sortModules (_.header <<< unwrap) parsedModules of CycleDetected ms -> do + let getModuleName (Module { header: ModuleHeader { name: Name { name } } }) = name let modNames = map (unwrap <<< getModuleName) ms Console.error $ String.joinWith "\n" [ "Cycle detected in modules:" diff --git a/script/GenerateDefaultOperatorsModule.purs b/script/GenerateDefaultOperatorsModule.purs index 15ff6dc..b75849b 100644 --- a/script/GenerateDefaultOperatorsModule.purs +++ b/script/GenerateDefaultOperatorsModule.purs @@ -46,7 +46,7 @@ main = do [ "--------------------------------------------" , "-- This module is generated. DO NOT EDIT! --" , "--------------------------------------------" - , "module DefaultOperators where" + , "module PureScript.CST.Tidy.Operators.Defaults where" , "" , "defaultOperators :: Array String" , "defaultOperators =" @@ -66,7 +66,7 @@ main = do contents = Array.intercalate "\n" (header <> lines <> footer) - writeTextFile UTF8 (Path.concat [ cwdPath, "bin", "DefaultOperators.purs" ]) contents + writeTextFile UTF8 (Path.concat [ cwdPath, "src", "PureScript", "CST", "Tidy", "Operators", "Defaults.purs" ]) contents defaultSpagoDhall :: String defaultSpagoDhall = diff --git a/bin/Bin/Operators.purs b/src/PureScript/CST/Tidy/Operators.purs similarity index 80% rename from bin/Bin/Operators.purs rename to src/PureScript/CST/Tidy/Operators.purs index 5c1a429..c9ac79b 100644 --- a/bin/Bin/Operators.purs +++ b/src/PureScript/CST/Tidy/Operators.purs @@ -1,4 +1,4 @@ -module Bin.Operators where +module PureScript.CST.Tidy.Operators where import Prelude @@ -13,7 +13,7 @@ import PureScript.CST.Lexer as Lexer import PureScript.CST.Tidy.Precedence (OperatorNamespace(..), Precedence, QualifiedOperator(..), PrecedenceMap, insertOperator, lookupOperator, remapOperators) import PureScript.CST.TokenStream (TokenStep(..), TokenStream) import PureScript.CST.TokenStream as TokenStream -import PureScript.CST.Types (Declaration(..), Export(..), FixityOp(..), IntValue(..), Module(..), ModuleBody(..), ModuleHeader(..), ModuleName, Name(..), Operator(..), Separated(..), Token(..), Wrapped(..)) +import PureScript.CST.Types (Declaration(..), Export(..), FixityOp(..), IntValue(..), Module(..), ModuleBody(..), ModuleHeader(..), Name(..), Operator(..), Separated(..), Token(..), Wrapped(..)) parseOperatorTable :: Array String -> PrecedenceMap parseOperatorTable = foldr (uncurry insertOperator) Map.empty <<< Array.mapMaybe parseOperatorPrec @@ -26,18 +26,34 @@ parseOperatorPrec = Lexer.lex >>> tokenStreamToArray >>> case _ of Just $ Tuple (QualifiedOperator modName OperatorType (Operator op)) prec _ -> Nothing + where + tokenStreamToArray :: TokenStream -> Either ParseError (Array Token) + tokenStreamToArray = go [] + where + go acc = TokenStream.step >>> case _ of + TokenEOF _ _ -> + Right acc + TokenError _ err _ _ -> + Left err + TokenCons tok _ next _ -> + go (Array.snoc acc tok.value) next resolveOperatorExports :: forall e. PrecedenceMap -> Module e -> PrecedenceMap -resolveOperatorExports precMap mod@(Module { header: ModuleHeader { exports }, body: ModuleBody { decls } }) = +resolveOperatorExports + precMap + mod@ + ( Module + { header: ModuleHeader { exports, name: Name { name: modName } } + , body: ModuleBody { decls } + } + ) = case exports of Nothing -> foldl goDecl precMap decls Just (Wrapped { value: Separated { head, tail } }) -> foldl goExport precMap $ Array.cons head $ map snd tail - where - modName = - getModuleName mod + where remappedPrecMap = remapOperators precMap mod @@ -63,17 +79,3 @@ resolveOperatorExports precMap mod@(Module { header: ModuleHeader { exports }, b insertOperator (QualifiedOperator (Just modName) OperatorType op) prec pm _ -> pm - -getModuleName :: forall e. Module e -> ModuleName -getModuleName (Module { header: ModuleHeader { name: Name { name } } }) = name - -tokenStreamToArray :: TokenStream -> Either ParseError (Array Token) -tokenStreamToArray = go [] - where - go acc = TokenStream.step >>> case _ of - TokenEOF _ _ -> - Right acc - TokenError _ err _ _ -> - Left err - TokenCons tok _ next _ -> - go (Array.snoc acc tok.value) next diff --git a/bin/DefaultOperators.purs b/src/PureScript/CST/Tidy/Operators/Defaults.purs similarity index 99% rename from bin/DefaultOperators.purs rename to src/PureScript/CST/Tidy/Operators/Defaults.purs index b7dadc0..07b7ff9 100644 --- a/bin/DefaultOperators.purs +++ b/src/PureScript/CST/Tidy/Operators/Defaults.purs @@ -1,7 +1,7 @@ -------------------------------------------- -- This module is generated. DO NOT EDIT! -- -------------------------------------------- -module DefaultOperators where +module PureScript.CST.Tidy.Operators.Defaults where defaultOperators :: Array String defaultOperators = diff --git a/test/Snapshot.purs b/test/Snapshot.purs index 8a0a89f..c2a8783 100644 --- a/test/Snapshot.purs +++ b/test/Snapshot.purs @@ -2,7 +2,6 @@ module Test.Snapshot where import Prelude -import Bin.Operators (parseOperatorTable) import Control.MonadZero (guard) import Data.Array (dropEnd, mapMaybe) import Data.Array as Array @@ -20,7 +19,6 @@ import Data.String as String import Data.String.Regex as Regex import Data.Traversable (for) import Data.Tuple (Tuple(..), fst) -import DefaultOperators (defaultOperators) import Dodo (PrintOptions) import Dodo as Dodo import Effect (Effect) @@ -40,6 +38,8 @@ import PureScript.CST (RecoveredParserResult(..), parseModule) import PureScript.CST.Errors (printParseError) import PureScript.CST.Tidy (class FormatError, FormatOptions) import PureScript.CST.Tidy as Tidy +import PureScript.CST.Tidy.Operators (parseOperatorTable) +import PureScript.CST.Tidy.Operators.Defaults (defaultOperators) import PureScript.CST.Tidy.Precedence (PrecedenceMap) import PureScript.CST.Types (Module) import Test.FormatDirective (defaultFormat, directiveRegex, parseDirectivesFromModule) diff --git a/test/spago.dhall b/test/spago.dhall index cefcd9e..84df139 100644 --- a/test/spago.dhall +++ b/test/spago.dhall @@ -37,5 +37,5 @@ , "tuples" ] , packages = ../packages.dhall -, sources = [ "src/**/*.purs", "test/*.purs", "bin/Bin/*.purs", "bin/DefaultOperators.purs" ] +, sources = [ "src/**/*.purs", "test/*.purs", "bin/Bin/*.purs" ] }