Skip to content

Commit

Permalink
Remove UU-parsinglib parser
Browse files Browse the repository at this point in the history
This parser is error correcting, but also slow. It's hard to maintain two parsers, and the error correcting feature was not used.
  • Loading branch information
FPtje committed Dec 28, 2023
1 parent 964753a commit 9b34bcd
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 1,165 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ Example `glualint.json` with the default options:
"prettyprint_removeRedundantParens": true,
"prettyprint_minimizeParens": false,
"prettyprint_assumeOperatorAssociativity": true,
"prettyprint_rejectInvalidCode": false,
"prettyprint_indentation": " ",

"log_format": "auto"
Expand Down Expand Up @@ -140,7 +139,6 @@ Option | Description
`prettyprint_removeRedundantParens` | Whether to remove unnecessary parentheses (e.g. `x = (1 + 2)`, `if ((1) + (2) == 3) then`)
`prettyprint_minimizeParens` | Removes parentheses which are unnecessary due to operator precedence (e.g. `(1 * 2) + 3`). This option also removes redundant parameters, regardless of whether `prettyprint_removeRedundantParens` is enabled.
`prettyprint_assumeOperatorAssociativity` | Only takes effect when `prettyprint_minimizeParens` is `true`. It decides whether parameters can be removed when the involved operators are assumed associative. Examples: `a * (b * c)`, `a and (b and c)`. This assumption is generally true, except for floating point numbers, where removing parentheses can actually change the outcome of the calculation. See [Stack Overflow](https://stackoverflow.com/a/10371890). This option is set to `true` by default, because the expectation is that this will not be problematic even if it affects your code. In a very rare case, this might cause trouble, though. You might want to consider turning this off if you have floating point code that heavily relies on evaluation order. You may also choose to leave this option on and ensure evaluation order by explicitly setting variables.
`prettyprint_rejectInvalidCode` | Whether not to pretty print when the code is syntactically invalid

### Other options

Expand Down
49 changes: 11 additions & 38 deletions app/GLuaFixer/Effects/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

module GLuaFixer.Effects.Run where

import Control.Monad (unless, when)
import Control.Monad (when)
import qualified Data.Aeson as JSON
import qualified Data.ByteString.Lazy.Char8 as BL8
import Effectful (Eff, (:>))
Expand All @@ -20,7 +20,6 @@ import GLua.AG.AST (AST)
import GLua.AG.Token (MToken)
import GLua.ASTInstances ()
import qualified GLua.PSParser as PSP
import qualified GLua.Parser as UUP
import GLuaFixer.Cli (Command (..), Options (..), OverriddenSettings, SettingsPath)
import GLuaFixer.Effects.AnalyseGlobals (analyseFile, execAnalysis, reportAnalysis)
import GLuaFixer.Effects.Cli (Cli, CliParseResult (..), parseCliOptions)
Expand Down Expand Up @@ -97,7 +96,7 @@ runOptions options =
worstExitCode exitCode <$> lint lintSettings filepath contents
(PrettyPrint, UseStdIn) -> do
(lintSettings, contents) <- getStdIn options.optsConfigFile options.optsOverridden
case prettyprint lintSettings contents of
case prettyprint lintSettings "stdin" contents of
Nothing -> pure $ ExitFailure 1
Just prettyprinted -> do
putStrStdOut prettyprinted
Expand All @@ -110,7 +109,7 @@ runOptions options =
files
$ \exitCode lintSettings filepath contents -> do
putStrLnStdOut $ "Pretty printing " <> filepath
case prettyprint lintSettings contents of
case prettyprint lintSettings filepath contents of
Nothing -> pure $ ExitFailure 1
Just prettyprinted -> do
writeFile filepath prettyprinted
Expand Down Expand Up @@ -256,16 +255,15 @@ lint lintSettings filepath contents = do
-- | Pretty print a file
prettyprint
:: LintSettings
-> FilePath
-> String
-> Maybe String
prettyprint lintSettings contents = do
if lintSettings.prettyprint_rejectInvalidCode && hasErrors
then Nothing
else Just $ Interface.prettyprint lintSettings ast
prettyprint lintSettings filepath contents =
case eAst of
Left _errors -> Nothing
Right ast -> Just $ Interface.prettyprint lintSettings ast
where
(tokens, lexErrors) = Interface.lexUU lintSettings contents
(ast, parseErrors) = Interface.parseUU tokens
hasErrors = not (null lexErrors) || not (null parseErrors)
eAst = Interface.lex lintSettings filepath contents >>= Interface.parse lintSettings filepath

-- | Test glualint itself against a file. TODO: Refactor this into a nicer command
test
Expand All @@ -277,23 +275,6 @@ test
-> Eff es ExitCode
test exitCode lintSettings filepath contents = do
putStrLnStdOut $ "Testing " <> filepath
let
(uu_lex, uu_lex_errors) = Interface.lexUU lintSettings contents
(_uu_ast, uu_parseErrs) = Interface.parseUU uu_lex

unless (null uu_lex_errors) $ do
putStrLnStdOut $
"Errors when trying to lex '"
++ filepath
++ "' with uu-parsinglib lexer!"
mapM_ (putStrLnStdOut . show) uu_lex_errors

unless (null uu_parseErrs) $ do
putStrLnStdOut $
"Errors when trying to parse '"
++ filepath
++ "' with uu-parsinglib parser!"
mapM_ (putStrLnStdOut . show) uu_parseErrs

logFormat <- getLogFormat lintSettings.log_format

Expand All @@ -305,25 +286,17 @@ test exitCode lintSettings filepath contents = do
case Interface.parse lintSettings filepath tokens of
Left msgs -> do
putStrLnStdOut $
"Errors when trying to parse '" ++ filepath ++ "' with parsec parser!"
"Errors when trying to parse '" ++ filepath
mapM_ (emitLintMessage logFormat) msgs
pure $ ExitFailure 1
Right ast -> do
let
prettyprinted = Interface.prettyprint lintSettings ast
(_uu_ast_pp, uu_parseErrs_pp) = UUP.parseGLuaFromString prettyprinted

unless (null uu_parseErrs_pp) $ do
putStrLnStdOut $
"Errors when trying to parse '"
++ filepath
++ "' with uu-parsinglib parser after pretty print!"
mapM_ (putStrLnStdOut . show) uu_parseErrs_pp

case PSP.parseGLuaFromString prettyprinted of
Left err -> do
putStrLnStdOut $
"Errors when trying to parse '" ++ filepath ++ "' with parsec parser after pretty print!"
"Errors when trying to parse '" ++ filepath ++ "' after pretty print!"

putStrLnStdOut $ show err
pure $ ExitFailure 1
Expand Down
2 changes: 0 additions & 2 deletions glualint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ library
GLua.AG.PrettyPrint
GLua.AG.Token
GLua.ASTInstances
GLua.Lexer
GLua.LineLimitParser
GLua.Parser
GLua.PSLexer
GLua.PSParser
GLua.TokenTypes
Expand Down
Loading

0 comments on commit 9b34bcd

Please sign in to comment.