Move operator defaults/parsing to src. (#65)

This commit is contained in:
Nathan Faubion 2021-09-05 07:06:00 -07:00 committed by GitHub
parent 81d6139946
commit 8fa8516bcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 29 deletions

View File

@ -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 =

View File

@ -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:"

View File

@ -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 =

View File

@ -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

View File

@ -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 =

View File

@ -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)

View File

@ -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" ]
}