BNFC-2.9.3: A compiler front-end generator.
Safe HaskellNone
LanguageHaskell2010

BNFC.CF

Synopsis

Documentation

type CF = CFG RFun Source #

A context free grammar consists of a set of rules and some extended information (e.g. pragmas, literals, symbols, keywords).

type Rule = Rul RFun Source #

A rule consists of a function name, a main category and a sequence of terminals and non-terminals.

  function_name . Main_Cat ::= sequence

data Rul function Source #

Polymorphic rule type.

Constructors

Rule 

Fields

  • funRule :: function

    The function (semantic action) of a rule. In order to be able to generate data types this must be a constructor (or an identity function).

  • valRCat :: RCat

    The value category, i.e., the defined non-terminal.

  • rhsRule :: SentForm

    The sentential form, i.e., the list of (non)terminals in the right-hand-side of a rule.

  • internal :: InternalRule

    Is this an "internal" rule only for the AST and printing, not for parsing?

Instances

Instances details
Functor Rul Source # 
Instance details

Defined in BNFC.CF

Methods

fmap :: (a -> b) -> Rul a -> Rul b #

(<$) :: a -> Rul b -> Rul a #

Eq function => Eq (Rul function) Source # 
Instance details

Defined in BNFC.CF

Methods

(==) :: Rul function -> Rul function -> Bool #

(/=) :: Rul function -> Rul function -> Bool #

Pretty function => Pretty (Rul function) Source # 
Instance details

Defined in BNFC.CF

Methods

pretty :: Rul function -> Doc Source #

prettyPrec :: Int -> Rul function -> Doc Source #

IsFun a => IsFun (Rul a) Source # 
Instance details

Defined in BNFC.CF

data InternalRule Source #

Constructors

Internal

internal rule (only for AST & printer)

Parsable

ordinary rule (also for parser)

Instances

Instances details
Eq InternalRule Source # 
Instance details

Defined in BNFC.CF

type SentForm = [Either Cat String] Source #

A sentential form is a sequence of non-terminals or terminals.

data CFG function Source #

Type of context-free grammars (GFG).

Constructors

CFG 

Fields

Instances

Instances details
Functor CFG Source # 
Instance details

Defined in BNFC.CF

Methods

fmap :: (a -> b) -> CFG a -> CFG b #

(<$) :: a -> CFG b -> CFG a #

type Signature = Map String (WithPosition Type) Source #

Types of the rule labels, together with the position of the rule label.

data Base Source #

Type of a non-terminal.

Constructors

BaseT String 
ListT Base 

Instances

Instances details
Eq Base Source # 
Instance details

Defined in BNFC.CF

Methods

(==) :: Base -> Base -> Bool #

(/=) :: Base -> Base -> Bool #

Ord Base Source # 
Instance details

Defined in BNFC.CF

Methods

compare :: Base -> Base -> Ordering #

(<) :: Base -> Base -> Bool #

(<=) :: Base -> Base -> Bool #

(>) :: Base -> Base -> Bool #

(>=) :: Base -> Base -> Bool #

max :: Base -> Base -> Base #

min :: Base -> Base -> Base #

Show Base Source # 
Instance details

Defined in BNFC.CF

Methods

showsPrec :: Int -> Base -> ShowS #

show :: Base -> String #

showList :: [Base] -> ShowS #

data Type Source #

Type of a rule label.

Constructors

FunT [Base] Base 

Instances

Instances details
Eq Type Source # 
Instance details

Defined in BNFC.CF

Methods

(==) :: Type -> Type -> Bool #

(/=) :: Type -> Type -> Bool #

Ord Type Source # 
Instance details

Defined in BNFC.CF

Methods

compare :: Type -> Type -> Ordering #

(<) :: Type -> Type -> Bool #

(<=) :: Type -> Type -> Bool #

(>) :: Type -> Type -> Bool #

(>=) :: Type -> Type -> Bool #

max :: Type -> Type -> Type #

min :: Type -> Type -> Type #

Show Type Source # 
Instance details

Defined in BNFC.CF

Methods

showsPrec :: Int -> Type -> ShowS #

show :: Type -> String #

showList :: [Type] -> ShowS #

dummyBase :: Base Source #

Placeholder for a type.

dummyType :: Type Source #

Placeholder for a function type.

data Exp' f Source #

Expressions for function definitions.

Constructors

App f Type [Exp' f]

(Possibly defined) label applied to expressions. The function Type is inferred by the type checker.

Var String

Function parameter.

LitInt Integer 
LitDouble Double 
LitChar Char 
LitString String 

Instances

Instances details
Eq f => Eq (Exp' f) Source # 
Instance details

Defined in BNFC.CF

Methods

(==) :: Exp' f -> Exp' f -> Bool #

(/=) :: Exp' f -> Exp' f -> Bool #

(IsFun f, Pretty f) => Pretty (Exp' f) Source # 
Instance details

Defined in BNFC.CF

Methods

pretty :: Exp' f -> Doc Source #

prettyPrec :: Int -> Exp' f -> Doc Source #

data Pragma Source #

Pragmas.

Constructors

CommentS String

for single line comments

CommentM (String, String)

for multiple-line comments.

TokenReg RString Bool Reg

for tokens

EntryPoints [RCat] 
Layout LayoutKeyWords 
LayoutStop [KeyWord] 
LayoutTop Symbol

Separator for top-level layout.

FunDef Define 

data Define Source #

Constructors

Define 

Fields

type Telescope = [(String, Base)] Source #

Function arguments with type.

isFunDef :: Pragma -> Either Pragma Define Source #

For use with partitionEithers.

definitions :: CFG f -> [Define] Source #

All define pragmas of the grammar.

data Delimiters Source #

List delimiters.

Constructors

Delimiters 

Fields

Instances

Instances details
Show Delimiters Source # 
Instance details

Defined in BNFC.CF

tokenPragmas :: CFG f -> [(TokenCat, Reg)] Source #

User-defined regular expression tokens

tokenNames :: CFG f -> [String] Source #

The names of all user-defined tokens.

type Literal = String Source #

Literal: builtin-token types Char, String, Ident, Integer, Double.

data Position Source #

Source positions.

Constructors

NoPosition 
Position 

Fields

Instances

Instances details
Eq Position Source # 
Instance details

Defined in BNFC.CF

Ord Position Source # 
Instance details

Defined in BNFC.CF

Show Position Source # 
Instance details

Defined in BNFC.CF

data WithPosition a Source #

Constructors

WithPosition 

Fields

Instances

Instances details
Functor WithPosition Source # 
Instance details

Defined in BNFC.CF

Methods

fmap :: (a -> b) -> WithPosition a -> WithPosition b #

(<$) :: a -> WithPosition b -> WithPosition a #

IsString RFun Source # 
Instance details

Defined in BNFC.CF

Methods

fromString :: String -> RFun #

Foldable WithPosition Source # 
Instance details

Defined in BNFC.CF

Methods

fold :: Monoid m => WithPosition m -> m #

foldMap :: Monoid m => (a -> m) -> WithPosition a -> m #

foldMap' :: Monoid m => (a -> m) -> WithPosition a -> m #

foldr :: (a -> b -> b) -> b -> WithPosition a -> b #

foldr' :: (a -> b -> b) -> b -> WithPosition a -> b #

foldl :: (b -> a -> b) -> b -> WithPosition a -> b #

foldl' :: (b -> a -> b) -> b -> WithPosition a -> b #

foldr1 :: (a -> a -> a) -> WithPosition a -> a #

foldl1 :: (a -> a -> a) -> WithPosition a -> a #

toList :: WithPosition a -> [a] #

null :: WithPosition a -> Bool #

length :: WithPosition a -> Int #

elem :: Eq a => a -> WithPosition a -> Bool #

maximum :: Ord a => WithPosition a -> a #

minimum :: Ord a => WithPosition a -> a #

sum :: Num a => WithPosition a -> a #

product :: Num a => WithPosition a -> a #

Traversable WithPosition Source # 
Instance details

Defined in BNFC.CF

Methods

traverse :: Applicative f => (a -> f b) -> WithPosition a -> f (WithPosition b) #

sequenceA :: Applicative f => WithPosition (f a) -> f (WithPosition a) #

mapM :: Monad m => (a -> m b) -> WithPosition a -> m (WithPosition b) #

sequence :: Monad m => WithPosition (m a) -> m (WithPosition a) #

Eq a => Eq (WithPosition a) Source #

Ignore position in equality and ordering.

Instance details

Defined in BNFC.CF

Ord a => Ord (WithPosition a) Source # 
Instance details

Defined in BNFC.CF

Show a => Show (WithPosition a) Source # 
Instance details

Defined in BNFC.CF

Pretty a => Pretty (WithPosition a) Source # 
Instance details

Defined in BNFC.CF

IsFun a => IsFun (WithPosition a) Source # 
Instance details

Defined in BNFC.CF

type RString = WithPosition String Source #

A "ranged string" (terminology from Agda code base).

blendInPosition :: RString -> String Source #

Prefix string with pretty-printed position information.

valCat :: Rul fun -> Cat Source #

data Cat Source #

Categories are the non-terminals of the grammar.

Constructors

Cat String

Ordinary non-terminal.

TokenCat TokenCat

Token types (like Ident, Integer, ..., user-defined).

ListCat Cat

List non-terminals, e.g., [Ident], [Exp], [Exp1].

CoercCat String Integer

E.g. Exp1, Exp2.

Instances

Instances details
Eq Cat Source # 
Instance details

Defined in BNFC.CF

Methods

(==) :: Cat -> Cat -> Bool #

(/=) :: Cat -> Cat -> Bool #

Ord Cat Source # 
Instance details

Defined in BNFC.CF

Methods

compare :: Cat -> Cat -> Ordering #

(<) :: Cat -> Cat -> Bool #

(<=) :: Cat -> Cat -> Bool #

(>) :: Cat -> Cat -> Bool #

(>=) :: Cat -> Cat -> Bool #

max :: Cat -> Cat -> Cat #

min :: Cat -> Cat -> Cat #

Show Cat Source #

This instance is for the Hspec test suite.

Instance details

Defined in BNFC.CF

Methods

showsPrec :: Int -> Cat -> ShowS #

show :: Cat -> String #

showList :: [Cat] -> ShowS #

Pretty Cat Source # 
Instance details

Defined in BNFC.CF

Methods

pretty :: Cat -> Doc Source #

prettyPrec :: Int -> Cat -> Doc Source #

catToStr :: Cat -> String Source #

Render category symbols as strings

strToCat :: String -> Cat Source #

Reads a string into a category. This should only need to handle the case of simple categories (with or without coercion) since list categories are parsed in the grammar already. To be on the safe side here, we still call the parser function that parses categries.

baseTokenCatNames :: [TokenCat] Source #

Token categories corresponding to base types.

isDataCat :: Cat -> Bool Source #

Does the category correspond to a data type?

sameCat :: Cat -> Cat -> Bool Source #

Categories C1, C2,... (one digit at the end) are variants of C. This function returns true if two category are variants of the same abstract category. E.g.

>>> sameCat (Cat "Abc") (CoercCat "Abc" 44)
True

normCat :: Cat -> Cat Source #

Removes precedence information. C1 => C, [C2] => [C]

identCat :: Cat -> String Source #

When given a list Cat, i.e. '[C]', it removes the square brackets, and adds the prefix List, i.e. ListC. (for Happy and Latex)

catOfType :: [TokenCat] -> Base -> Cat Source #

Reconstruct (non-coercion) category from a type, given a list of what should be the token categories.

baseCat :: Cat -> Either BaseCat TokenCat Source #

Get the underlying category identifier.

catOfList :: Cat -> Cat Source #

Unwraps the list constructor from the category name. E.g. [C1] => C1.

type Fun = String Source #

Fun is the function name of a rule.

class IsFun a where Source #

Minimal complete definition

funName

Methods

funName :: a -> String Source #

isNilFun Source #

Arguments

:: a 
-> Bool

Is this the constructor for empty lists?

isOneFun Source #

Arguments

:: a 
-> Bool

Is this the constructor for singleton lists?

isConsFun Source #

Arguments

:: a 
-> Bool

Is this the list constructor?

isConcatFun Source #

Arguments

:: a 
-> Bool

Is this list concatenation?

isCoercion :: a -> Bool Source #

Is this function just a coercion? (I.e. the identity)

Instances

Instances details
IsFun String Source # 
Instance details

Defined in BNFC.CF

IsFun a => IsFun (WithPosition a) Source # 
Instance details

Defined in BNFC.CF

IsFun a => IsFun (Rul a) Source # 
Instance details

Defined in BNFC.CF

IsFun a => IsFun (k, a) Source # 
Instance details

Defined in BNFC.CF

Methods

funName :: (k, a) -> String Source #

isNilFun :: (k, a) -> Bool Source #

isOneFun :: (k, a) -> Bool Source #

isConsFun :: (k, a) -> Bool Source #

isConcatFun :: (k, a) -> Bool Source #

isCoercion :: (k, a) -> Bool Source #

isNilCons :: IsFun a => a -> Bool Source #

type Data = (Cat, [(String, [Cat])]) Source #

The abstract syntax of a grammar.

firstEntry :: CF -> Cat Source #

firstEntry returns the first of the entrypoints, or (if none), the first parsable Category appearing in the grammar.

allNames :: CF -> [RString] Source #

Constructors and categories.

filterNonUnique :: Ord a => [a] -> [a] Source #

Get all elements with more than one occurrence.

commentPragmas :: [Pragma] -> [Pragma] Source #

Extract the comment pragmas.

lookupRule :: Eq f => f -> [Rul f] -> Maybe (Cat, SentForm) Source #

rulesForCat :: CF -> Cat -> [Rule] Source #

Returns all parseable rules that construct the given Cat. Whitespace separators have been removed.

mapRhs :: (SentForm -> SentForm) -> Rul f -> Rul f Source #

Modify the rhsRule part of a Rule.

rulesForNormalizedCat :: CF -> Cat -> [Rule] Source #

Like rulesForCat but for normalized value categories. I.e., `rulesForCat (Cat Exp)` will return rules for category Exp but also Exp1, Exp2... in case of coercion

rulesForCat' :: CF -> Cat -> [Rule] Source #

As rulesForCat, but this version doesn't exclude internal rules.

allCats :: (InternalRule -> Bool) -> CFG f -> [Cat] Source #

Get all categories of a grammar matching the filter. (No Cat w/o production returned; no duplicates.)

reallyAllCats :: CFG f -> [Cat] Source #

Get all categories of a grammar. (No Cat w/o production returned; no duplicates.)

allCatsIdNorm :: CF -> [RString] Source #

Gets all normalized identified Categories

allCatsNorm :: CF -> [Cat] Source #

Get all normalized Cat

allParserCatsNorm :: CFG f -> [Cat] Source #

Get all normalized Cat

isUsedCat :: CFG f -> Cat -> Bool Source #

Is the category is used on an rhs? Includes internal rules.

ruleGroups :: CF -> [(Cat, [Rule])] Source #

Group all parsable categories with their rules. Deletes whitespace separators, as they will not become part of the parsing rules.

ruleGroupsInternals :: CF -> [(Cat, [Rule])] Source #

Group all categories with their rules including internal rules.

literals :: CFG f -> [TokenCat] Source #

Get all literals of a grammar. (e.g. String, Double)

reservedWords :: CFG f -> [String] Source #

Get the keywords of a grammar.

cfTokens :: CFG f -> [(String, Int)] Source #

Canonical, numbered list of symbols and reserved words. (These do not end up in the AST.)

comments :: CF -> ([(String, String)], [String]) Source #

Comments can be defined by the comment pragma

numberOfBlockCommentForms :: CF -> Int Source #

Number of block comment forms defined in the grammar file.

hasIdent :: CFG f -> Bool Source #

Whether the grammar uses the predefined Ident type.

specialCats :: CF -> [TokenCat] Source #

Categories corresponding to tokens. These end up in the AST. (unlike tokens returned by cfTokens)

abstract syntax trees: data type definitions

getAbstractSyntax :: CF -> [Data] Source #

Return the abstract syntax of the grammar. All categories are normalized, so a rule like: EAdd . Exp2 ::= Exp2 "+" Exp3 ; Will give the following signature: EAdd : Exp -> Exp -> Exp

cf2data' :: (Cat -> Bool) -> CF -> [Data] Source #

All the functions below implement the idea of getting the abstract syntax of the grammar with some variation but they seem to do a poor job at handling corner cases involving coercions. Use getAbstractSyntax instead if possible.

sigLookup :: IsFun a => a -> CF -> Maybe (WithPosition Type) Source #

Get the type of a rule label.

isParsable :: Rul f -> Bool Source #

Checks if the rule is parsable.

hasSingletonRule :: [Rule] -> Maybe Rule Source #

Gets the singleton rule out of the rules for a list.

sortRulesByPrecedence :: [Rule] -> [(Integer, Rule)] Source #

Sort rules by descending precedence.

isEmptyListCat :: CF -> Cat -> Bool Source #

Is the given category a list category parsing also empty lists?

precCat :: Cat -> Integer Source #

Returns the precedence of a category symbol. E.g. >>> precCat (CoercCat Abc 4) 4

hasIdentLikeTokens :: CFG g -> Bool Source #

Defines or uses the grammar token types like Ident? Excludes position tokens.

hasTextualTokens :: CFG g -> Bool Source #

Defines or uses the grammar token types or Ident?

hasPositionTokens :: CFG g -> Bool Source #

Is there a position token declaration in the grammar?

isPositionCat :: CFG f -> TokenCat -> Bool Source #

Does the category have a position stored in AST?

allEntryPoints :: CFG f -> List1 Cat Source #

Categories that are entry points to the parser.

These are either the declared entrypoints (in the original order), or, if no entrypoints were declared explicitly, all parsable categories (in the order of declaration in the grammar file).