From 92f1f0486d7f39b12ebda685b0ed5ad3bf3d164a Mon Sep 17 00:00:00 2001 From: Einhard Leichtfuß Date: Tue, 20 May 2025 22:34:12 +0200 Subject: Change relaxed expr parser implementation Debatable whether better or worse. --- src/Language/SimpleShell/Parser/Expr.hs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/Language/SimpleShell/Parser') diff --git a/src/Language/SimpleShell/Parser/Expr.hs b/src/Language/SimpleShell/Parser/Expr.hs index cabf522..0f2f6c9 100644 --- a/src/Language/SimpleShell/Parser/Expr.hs +++ b/src/Language/SimpleShell/Parser/Expr.hs @@ -15,7 +15,6 @@ import Language.SimpleShell.Parser ( Parser , lexeme , symbol - , commentFirstChars , lookupVar , lookupFun ) @@ -32,7 +31,7 @@ import Control.Monad (void) import Control.Monad.Combinators (manyTill) import Data.Foldable (asum) import Data.Text (Text) -import Text.Megaparsec (takeWhile1P, oneOf, many) +import Text.Megaparsec (anySingleBut, many) import Text.Megaparsec.Char (char) import qualified Text.Megaparsec.Char.Lexer as L (charLiteral, decimal) @@ -67,16 +66,17 @@ strongTermP_ -- * parentheses -- * string literals -- * comments + -- - Thus, `takeWhile1P` is dangerous. + -- * This is hardly the most efficient implementation. + -- * A more efficient implementation would use `takeWhile1P` to parse + -- large chunks of input, but--as noted above--that is dangerous; + -- i.e., non-trivial (albeit not really hard) to do correctly. tok :: Parser () tok - = void strLitP - <|> symbol "(" *> void (many tok) <* symbol ")" - <|> void (lexeme $ takeWhile1P Nothing isBoring) - <|> void (lexeme $ oneOf commentFirstChars) - where - isBoring :: Char -> Bool - isBoring = not . (`elem` "()\"" ++ commentFirstChars) + = strongTermP_ + <|> void nameP -- function names; for efficiency only + <|> void (lexeme $ anySingleBut ')') -- operators (i.a.; catchall) -- | Parse "strong" term with fixed type. -- cgit v1.2.3