GHCソースコード読みのススメ

1,462 views

Published on

Published in: Technology

GHCソースコード読みのススメ

  1. 1. -- compiler/main/HscMain.hs hscParse関数hscParse mod_summary = do dflags <- getDynFlags let src_filename = ms_hspp_file mod_summary maybe_src_buf = ms_hspp_buf mod_summary liftIO $ showPass dflags "Parser" {-# SCC "Parser" #-} do buf <- case maybe_src_buf of Just b -> return b Nothing -> liftIO $ hGetStringBuffer src_filename let loc = mkRealSrcLoc (mkFastString src_filename) 1 1 case unP parseModule (mkPState dflags buf loc) of PFailed span err ->
  2. 2. liftIO $ showPass dflags "Parser"*** Parser:
  3. 3. 詳細:http://www.kotha.net/ghcguide_ja/7.0.4/profiling.html#idp19148320
  4. 4. liftIO $ dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" $ ppr rdr_module==================== Parser =============module Queue ( Queue(..) ) whereimport Prelude hiding ( head, tail )class Queue q where {
  5. 5. hscParse :: ModSummary -> Hsc HsParsedModulehscParse mod_summary = do dflags <- getDynFlags--snip-- case unP parseModule (mkPState dflags buf loc) of PFailed span err -> liftIO $ throwOneError (mkPlainErrMsg span err) POk pst rdr_module -> do logWarningsReportErrors (getMessages pst)--snip-- return HsParsedModule { hpm_module = rdr_module, hpm_src_files = srcs2 }
  6. 6. unP parseModule (mkPState dflags buf loc)* unP: compiler/parser/Lexer.xで定義* parseModule: compiler/parser/Parser.y.ppで定義* mkPState: compiler/parser/Lexer.xで定義
  7. 7. http://www.haskell.org/alex/http://www.haskell.org/happy/
  8. 8. -- Parser.y.pp%monad { P } { >>= } { return }%lexer { lexer } { L _ ITeof }%name parseModule module%name parseStmt maybe_stmt%name parseIdentifier identifier%name parseType ctype%partial parseHeader header%tokentype { (Located Token) }http://www.haskell.org/happy/doc/html/sec-monads.html
  9. 9. module :: { Located (HsModule RdrName) } : maybedocheader module modid maybemodwarning maybeexports where body {% fileSrcSpan >>= loc -> return (L loc (HsModule (Just $3) $5 (fst $7) (snd $7) $4 $1) )} | body2 {% fileSrcSpan >>= loc -> return (L loc (HsModule Nothing Nothing (fst $1) (snd $1) Nothing Nothing )) }

×