diff options
author | Einhard Leichtfuß <alguien@respiranto.de> | 2025-05-20 22:34:12 +0200 |
---|---|---|
committer | Einhard Leichtfuß <alguien@respiranto.de> | 2025-05-20 22:36:50 +0200 |
commit | 92f1f0486d7f39b12ebda685b0ed5ad3bf3d164a (patch) | |
tree | 73f4896489c3e26fd6fcb881b8ceaaaf9d1a3f15 | |
parent | dfd8f3bd6b12dc19cd5c4951d05b8a2ae18ceda6 (diff) |
Change relaxed expr parser implementationexperimental/relaxed-expr-parser-alt
Debatable whether better or worse.
-rw-r--r-- | src/Language/SimpleShell/Parser.hs | 6 | ||||
-rw-r--r-- | src/Language/SimpleShell/Parser/Expr.hs | 18 |
2 files changed, 9 insertions, 15 deletions
diff --git a/src/Language/SimpleShell/Parser.hs b/src/Language/SimpleShell/Parser.hs index be16c9f..d75602b 100644 --- a/src/Language/SimpleShell/Parser.hs +++ b/src/Language/SimpleShell/Parser.hs @@ -4,7 +4,6 @@ module Language.SimpleShell.Parser ( Parser , lexeme , symbol - , commentFirstChars , lookupVar , lookupFun , declareVars @@ -51,17 +50,12 @@ initContext = Context } --- Must be kept in sync with 'commentFirstChars'. sc :: Parser () sc = L.space space1 (L.skipLineComment "//") (L.skipBlockComment "/*" "*/") --- | List of all characters that may start a comment. -commentFirstChars :: [Char] -commentFirstChars = "/" - lexeme :: Parser a -> Parser a lexeme = L.lexeme sc 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. |