haskell / alex

A lexical analyser generator for Haskell
https://hackage.haskell.org/package/alex
BSD 3-Clause "New" or "Revised" License
298 stars 82 forks source link

alex-3.2.0 fails to generate correct parser for ghc-HEAD #98

Closed trofi closed 8 years ago

trofi commented 8 years ago

https://github.com/simonmar/alex/commit/00431304aa18ada0a12aab43443023a86521f353 added explicit type signature for alex_accept:

--- a/src/Output.hs
+++ b/src/Output.hs
@@ -63,43 +66,89 @@ outputDFA target _ _ typesig dfa
-    outputAccept
-        = case typesig of
-          Nothing ->
-              -- No type signature: we don't know what the type of the actions is.
-              -- str accept_nm . str " :: Array Int (Accept Code)\n"
-              str accept_nm . str " = listArray (0::Int," . shows n_states
-            . str ") [" . interleave_shows (char ',') (map outputAccs accept)
+    outputAccept =
+        str accept_nm . str " :: Array Int (AlexAcc ())\n"
+      . str accept_nm . str " = listArray (0::Int," . shows n_states . str ") ["
+      . interleave_shows (char ',') (snd (mapAccumR outputAccs 0 accept))
+      . str "]\n"

and GHC relies it to be user-defined:

compiler/parser/Lexer.x:2658:21: error:
    • Couldn't match type ‘Word64’ with ‘()’
      Expected type: ()
        Actual type: ExtsBitmap
    • In the first argument of ‘alexScanUser’, namely ‘exts’
      In the expression: alexScanUser exts inp sc
      In a stmt of a 'do' block:
        case alexScanUser exts inp sc of {
          AlexEOF
            -> do { let ...;
                    setLastToken span 0;
                    .... }
          AlexError (AI loc2 buf)
            -> reportLexError loc1 loc2 buf "lexical error"
          AlexSkip inp2 _
            -> do { setInput inp2;
                    lexToken }
          AlexToken inp2@(AI end buf2) _ t
            -> do { setInput inp2;
                    let ...;
                    .... } }

compiler/stage1/build/Lexer.hs:116:15: error:
    • Couldn't match type ‘Word64’ with ‘()’
      Expected type: Array Int (AlexAcc ())
        Actual type: Array Int (AlexAcc ExtsBitmap)
    • In the expression:
        listArray
          (0 :: Int, 207)
          [AlexAccNone, AlexAcc 167, AlexAcc 166, AlexAcc 165, ....]
      In an equation for ‘alex_accept’:
          alex_accept
            = listArray
                (0 :: Int, 207) [AlexAccNone, AlexAcc 167, AlexAcc 166, ....]

alex-3.2.0 generates the following alex_accept:

alex_accept :: Array Int (AlexAcc ())
alex_accept = listArray ...

Removing explicit type signature makes lexer build fine.

Adding @emc2 for visibility.

erikd commented 8 years ago

@trofi Is is possible to narrow down exactly what in that file is causing problems? I'm thinking that such a very much reduced example should end up in the tests.

trofi commented 8 years ago

I expect any lexer that uses alexScanUser style will break (alex has no tests/ or examples/ for alexScanUser).

Will try to write example (and a fix) today.

trofi commented 8 years ago

Added lexer test: https://github.com/simonmar/alex/pull/99 It triggered the same build failure.