-
Notifications
You must be signed in to change notification settings - Fork 85
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
15 changed files
with
227 additions
and
173 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
packages: | ||
packages/grammar | ||
./ |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../LICENSE |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: happy-grammar | ||
version: 1.21.0 | ||
license: BSD2 | ||
license-file: LICENSE | ||
copyright: (c) Andy Gill, Simon Marlow | ||
author: Andy Gill and Simon Marlow | ||
maintainer: Simon Marlow <marlowsd@gmail.com> | ||
bug-reports: https://github.com/simonmar/happy/issues | ||
stability: stable | ||
homepage: https://www.haskell.org/happy/ | ||
category: Development | ||
cabal-version: >= 1.10 | ||
build-type: Simple | ||
synopsis: happy's Grammar datatype | ||
|
||
Description: | ||
Happy is a parser generator for Haskell. | ||
Happy-Grammar exposes the cross-package Grammar datatype, | ||
which represents a grammar as can be parsed and processed by happy. | ||
|
||
tested-with: | ||
GHC == 7.0.4 | ||
GHC == 7.4.2 | ||
GHC == 7.6.3 | ||
GHC == 7.8.4 | ||
GHC == 7.10.3 | ||
GHC == 8.0.2 | ||
GHC == 8.2.2 | ||
GHC == 8.4.4 | ||
GHC == 8.6.5 | ||
GHC == 8.8.4 | ||
GHC == 8.10.4 | ||
GHC == 9.0.1 | ||
|
||
library | ||
hs-source-dirs: src | ||
|
||
exposed-modules: Happy.Grammar | ||
build-depends: base < 5, | ||
array | ||
|
||
default-language: Haskell98 | ||
default-extensions: CPP, MagicHash, FlexibleContexts | ||
ghc-options: -Wall | ||
other-modules: |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/----------------------------------------------------------------------------- | ||
The Grammar data type. | ||
|
||
(c) 1993-2001 Andy Gill, Simon Marlow | ||
----------------------------------------------------------------------------- | ||
|
||
> module Happy.Grammar ( | ||
> Name, | ||
> | ||
> Production(..), Grammar(..), ErrorHandlerType(..), | ||
> Priority(..), | ||
> Assoc(..), | ||
> | ||
> errorName, errorTok, startName, dummyName, firstStartTok, dummyTok, | ||
> eofName, epsilonTok | ||
> ) where | ||
|
||
> import Data.Array | ||
|
||
> type Name = Int | ||
|
||
> data ErrorHandlerType | ||
> = ErrorHandlerTypeDefault | ||
> | ErrorHandlerTypeExpList | ||
|
||
> data Production | ||
> = Production Name [Name] (String,[Int]) Priority | ||
> deriving Show | ||
|
||
> data Grammar | ||
> = Grammar { | ||
> productions :: [Production], | ||
> lookupProdNo :: Int -> Production, | ||
> lookupProdsOfName :: Name -> [Int], | ||
> token_specs :: [(Name,String)], | ||
> terminals :: [Name], | ||
> non_terminals :: [Name], | ||
> starts :: [(String,Name,Name,Bool)], | ||
> types :: Array Int (Maybe String), | ||
> token_names :: Array Int String, | ||
> first_nonterm :: Name, | ||
> first_term :: Name, | ||
> eof_term :: Name, | ||
> priorities :: [(Name,Priority)], | ||
> token_type :: String, | ||
> imported_identity :: Bool, | ||
> monad :: (Bool,String,String,String,String), | ||
> expect :: Maybe Int, | ||
> attributes :: [(String,String)], | ||
> attributetype :: String, | ||
> lexer :: Maybe (String,String), | ||
> error_handler :: Maybe String, | ||
> error_sig :: ErrorHandlerType | ||
> } | ||
|
||
> instance Show Grammar where | ||
> showsPrec _ (Grammar | ||
> { productions = p | ||
> , token_specs = t | ||
> , terminals = ts | ||
> , non_terminals = nts | ||
> , starts = sts | ||
> , types = tys | ||
> , token_names = e | ||
> , first_nonterm = fnt | ||
> , first_term = ft | ||
> , eof_term = eof | ||
> }) | ||
> = showString "productions = " . shows p | ||
> . showString "\ntoken_specs = " . shows t | ||
> . showString "\nterminals = " . shows ts | ||
> . showString "\nnonterminals = " . shows nts | ||
> . showString "\nstarts = " . shows sts | ||
> . showString "\ntypes = " . shows tys | ||
> . showString "\ntoken_names = " . shows e | ||
> . showString "\nfirst_nonterm = " . shows fnt | ||
> . showString "\nfirst_term = " . shows ft | ||
> . showString "\neof = " . shows eof | ||
> . showString "\n" | ||
|
||
> data Assoc = LeftAssoc | RightAssoc | None | ||
> deriving Show | ||
|
||
> data Priority = No | Prio Assoc Int | PrioLowest | ||
> deriving Show | ||
|
||
> instance Eq Priority where | ||
> No == No = True | ||
> Prio _ i == Prio _ j = i == j | ||
> _ == _ = False | ||
|
||
----------------------------------------------------------------------------- | ||
-- Magic name values | ||
|
||
All the tokens in the grammar are mapped onto integers, for speed. | ||
The namespace is broken up as follows: | ||
|
||
epsilon = 0 | ||
error = 1 | ||
dummy = 2 | ||
%start = 3..s | ||
non-terminals = s..n | ||
terminals = n..m | ||
%eof = m | ||
|
||
These numbers are deeply magical, change at your own risk. Several | ||
other places rely on these being arranged as they are, including | ||
ProduceCode.lhs and the various HappyTemplates. | ||
|
||
Unfortunately this means you can't tell whether a given token is a | ||
terminal or non-terminal without knowing the boundaries of the | ||
namespace, which are kept in the Grammar structure. | ||
|
||
In hindsight, this was probably a bad idea. | ||
|
||
> startName, eofName, errorName, dummyName :: String | ||
> startName = "%start" -- with a suffix, like %start_1, %start_2 etc. | ||
> eofName = "%eof" | ||
> errorName = "error" | ||
> dummyName = "%dummy" -- shouldn't occur in the grammar anywhere | ||
|
||
> firstStartTok, dummyTok, errorTok, epsilonTok :: Name | ||
> firstStartTok = 3 | ||
> dummyTok = 2 | ||
> errorTok = 1 | ||
> epsilonTok = 0 |
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
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
Oops, something went wrong.