Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
456 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-} | |||
|
|||
import Data.Char | |||
import Numeric | |||
import Language.Haskell.TH | |||
import Language.Haskell.Meta.Utils | |||
|
|||
import qualified Stage2 | |||
|
|||
import Text.Peggy.Prim | |||
import Text.Peggy.Quote | |||
import Text.Peggy.CodeGen.TH | |||
import Text.Peggy.Syntax | |||
import Text.Peggy.SrcLoc | |||
|
|||
header :: String | |||
header = | |||
unlines | |||
[ "{-# Language RankNTypes #-}" | |||
, "{-# Language FlexibleContexts #-}" | |||
, "module Text.Peggy.Parser (syntax) where" | |||
, "import Control.Applicative" | |||
, "import Data.ListLike.Base hiding (head)" | |||
, "import Data.HashTable.ST.Basic" | |||
, "import Numeric" | |||
, "import Data.Char" | |||
, "import Text.Peggy.Prim" | |||
, "import Text.Peggy.Syntax" | |||
] | |||
|
|||
main :: IO () | |||
main = do | |||
res <- parseFile Stage2.syntax "peggy.peggy" | |||
case res of | |||
Left err -> error $ show err | |||
Right defs -> do | |||
code <- runQ $ genDecs defs | |||
putStrLn header | |||
putStrLn $ pp code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
# Bootstrup Instructions # | |||
|
|||
# Pre-requirement | |||
|
|||
Previous version of peggy (>= 0.1.1) required. | |||
|
|||
# Bootstrup | |||
|
|||
$ cd bootstrup | |||
$ runhaskell Bootstrup.hs > ../Text/Peggy/Parser.hs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-} | |||
|
|||
module Stage1 where | |||
|
|||
import Data.Char | |||
import Language.Haskell.TH.Quote | |||
import Numeric | |||
import Text.Peggy | |||
|
|||
genParser [] $(peggyFile "peggy.peggy") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-} | |||
|
|||
module Stage2 where | |||
|
|||
import qualified Stage1 | |||
|
|||
import Data.Char | |||
import Numeric | |||
import Language.Haskell.TH | |||
import Language.Haskell.TH.Quote | |||
import Text.Peggy | |||
|
|||
genParser [] $(runIO (parseFile Stage1.syntax "peggy.peggy") >>= \res -> case res of Left err -> error $ show err; Right syn -> dataToExpQ (const Nothing) syn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,95 @@ | |||
-- A Parser for peggy itself. | |||
|
|||
syntax :: Syntax | |||
= definition* !(skip* .) | |||
|
|||
definition ::: Definition | |||
= ident ":::" haskellType "=" expr { Definition $1 $2 (Token $3) } | |||
/ ident "::" haskellType "=" expr { Definition $1 $2 $3 } | |||
|
|||
expr :: Expr | |||
= choiceExpr | |||
|
|||
choiceExpr :: Expr | |||
= (semanticExpr, "/") { Choice $1 } | |||
|
|||
semanticExpr :: Expr | |||
= sequenceExpr "{" codeFragment "}" { Semantic $1 $2 } | |||
/ sequenceExpr | |||
|
|||
sequenceExpr :: Expr | |||
= (suffixExpr !"::" !"=")+ { Sequence $1 } | |||
|
|||
suffixExpr :: Expr | |||
= suffixExpr "*" { Many $1 } | |||
/ suffixExpr "+" { Some $1 } | |||
/ suffixExpr "?" { Optional $1 } | |||
/ prefixExpr | |||
|
|||
prefixExpr :: Expr | |||
= "&" primExpr { And $1 } | |||
/ "!" primExpr { Not $1 } | |||
/ primExpr | |||
|
|||
primExpr ::: Expr | |||
= '\"' charLit* '\"' { Terminals True True $1 } | |||
/ '\'' charLit* '\'' { Terminals False False $1 } | |||
/ '[^' range* ']' { TerminalCmp $1 } | |||
/ '[' range* ']' { TerminalSet $1 } | |||
/ "." { TerminalAny } | |||
/ ident { NonTerminal $1 } | |||
/ "(" expr "," expr ")" { SepBy $1 $2 } | |||
/ "(" expr ";" expr ")" { SepBy1 $1 $2 } | |||
/ "(" expr ")" | |||
|
|||
charLit :: Char | |||
= '\\' escChar | |||
/ ![\'\"] . | |||
|
|||
escChar :: Char | |||
= 'n' { '\n' } | |||
/ 'r' { '\r' } | |||
/ 't' { '\t' } | |||
/ '\\' { '\\' } | |||
/ '\"' { '\"' } | |||
/ '\'' { '\'' } | |||
/ 'x' hexDigit hexDigit { chr . fst . head . readHex $ [$1, $2] } | |||
|
|||
range :: CharRange | |||
= rchar '-' rchar { CharRange $1 $2 } | |||
/ rchar { CharOne $1 } | |||
|
|||
rchar :: Char | |||
= '\\' escChar | |||
/ '\\]' {']'} / '\\[' { '[' } / '\\^' { '^' } / '\\-' { '-' } | |||
/ [^\]] | |||
|
|||
haskellType :: HaskellType | |||
= [^=]+ | |||
|
|||
codeFragment :: CodeFragment | |||
= codePart* | |||
|
|||
codePart :: CodePart | |||
= argument | |||
/ (!'}' !argument .)+ { Snippet $1 } | |||
|
|||
argument :: CodePart | |||
= '$' digit+ { Argument $ read $1 } | |||
|
|||
digit :: Char = [0-9] | |||
hexDigit :: Char = [0-9a-fA-F] | |||
|
|||
ident ::: String = [a-zA-Z_] [0-9a-zA-Z_]* { $1 : $2 } | |||
|
|||
skip :: () | |||
= [ \r\n\t] { () } / comment | |||
|
|||
comment :: () | |||
= lineComment / regionComment | |||
|
|||
lineComment :: () | |||
= '--' (!'\n' .)* '\n' { () } | |||
|
|||
regionComment :: () | |||
= '{-' (regionComment / !'-}' . { () } )* '-}' { () } |