inline-c-0.9.1.0: Write Haskell source files including C code inline. No FFI required.

Safe HaskellNone
LanguageHaskell2010

Language.C.Types.Parse

Contents

Description

A parser for C99 declarations. Currently, the parser has the following limitations:

  • Array sizes can only be *, n (where n is a positive integer), x (where x is a C identifier). In C99 they can be arbitrary expressions. See the ArrayType data type.
  • _Bool, _Complex, and _Imaginary are not present.
  • Untyped parameter lists (pre-K&R C) are not allowed.

The parser is incremental and generic (see CParser). Pretty and Arbitrary instances are provided for all the data types.

The entry point if you want to parse C declarations is parameter_declaration.

Synopsis

Parser configuration

type TypeNames = HashSet CIdentifier Source #

A collection of named types (typedefs)

data CParserContext i Source #

Constructors

CParserContext 

Fields

Default configuration

Parser type

type CParser i m = (Monad m, Functor m, Applicative m, MonadPlus m, Parsing m, CharParsing m, TokenParsing m, LookAheadParsing m, MonadReader (CParserContext i) m, Hashable i) Source #

All the parsing is done using the type classes provided by the parsers package. You can use the parsing routines with any of the parsers that implement the classes, such as parsec or trifecta.

We parametrize the parsing by the type of the variable identifiers, i. We do so because we use this parser to implement anti-quoters referring to Haskell variables, and thus we need to parse Haskell identifiers in certain positions.

runCParser Source #

Arguments

:: Stream s Identity Char 
=> CParserContext i 
-> String

Source name.

-> s

String to parse.

-> ReaderT (CParserContext i) (Parsec s ()) a

Parser. Anything with type forall m. CParser i m => m a is a valid argument.

-> Either ParseError a 

Runs a CParser using parsec.

quickCParser Source #

Arguments

:: CParserContext i 
-> String

String to parse.

-> ReaderT (CParserContext i) (Parsec String ()) a

Parser. Anything with type forall m. CParser i m => m a is a valid argument.

-> a 

Useful for quick testing. Uses "quickCParser" as source name, and throws an error if parsing fails.

quickCParser_ Source #

Arguments

:: Bool 
-> String

String to parse.

-> ReaderT (CParserContext CIdentifier) (Parsec String ()) a

Parser. Anything with type forall m. CParser i m => m a is a valid argument.

-> a 

Types and parsing

data Declarator i Source #

Instances
Functor Declarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

Foldable Declarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

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

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

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

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

toList :: Declarator a -> [a] #

null :: Declarator a -> Bool #

length :: Declarator a -> Int #

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

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

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

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

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

Traversable Declarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

Eq i => Eq (Declarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

(==) :: Declarator i -> Declarator i -> Bool #

(/=) :: Declarator i -> Declarator i -> Bool #

Show i => Show (Declarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty i => Pretty (Declarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

pretty :: Declarator i -> Doc #

prettyList :: [Declarator i] -> Doc #

data DirectDeclarator i Source #

Instances
Functor DirectDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

Foldable DirectDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

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

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

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

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

toList :: DirectDeclarator a -> [a] #

null :: DirectDeclarator a -> Bool #

length :: DirectDeclarator a -> Int #

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

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

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

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

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

Traversable DirectDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

Eq i => Eq (DirectDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Show i => Show (DirectDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty i => Pretty (DirectDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

data ArrayOrProto i Source #

Constructors

Array (ArrayType i) 
Proto [ParameterDeclaration i] 
Instances
Functor ArrayOrProto Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

Foldable ArrayOrProto Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

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

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

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

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

toList :: ArrayOrProto a -> [a] #

null :: ArrayOrProto a -> Bool #

length :: ArrayOrProto a -> Int #

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

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

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

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

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

Traversable ArrayOrProto Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

Eq i => Eq (ArrayOrProto i) Source # 
Instance details

Defined in Language.C.Types.Parse

Show i => Show (ArrayOrProto i) Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty i => Pretty (ArrayOrProto i) Source # 
Instance details

Defined in Language.C.Types.Parse

data ArrayType i Source #

Instances
Functor ArrayType Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

Foldable ArrayType Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

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

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

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

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

toList :: ArrayType a -> [a] #

null :: ArrayType a -> Bool #

length :: ArrayType a -> Int #

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

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

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

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

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

Traversable ArrayType Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

Eq i => Eq (ArrayType i) Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

(==) :: ArrayType i -> ArrayType i -> Bool #

(/=) :: ArrayType i -> ArrayType i -> Bool #

Show i => Show (ArrayType i) Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty i => Pretty (ArrayType i) Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

pretty :: ArrayType i -> Doc #

prettyList :: [ArrayType i] -> Doc #

data Pointer Source #

Constructors

Pointer [TypeQualifier] 
Instances
Eq Pointer Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

Show Pointer Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty Pointer Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

pretty :: Pointer -> Doc #

prettyList :: [Pointer] -> Doc #

data ParameterDeclaration i Source #

Instances
Functor ParameterDeclaration Source # 
Instance details

Defined in Language.C.Types.Parse

Foldable ParameterDeclaration Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

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

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

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

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

toList :: ParameterDeclaration a -> [a] #

null :: ParameterDeclaration a -> Bool #

length :: ParameterDeclaration a -> Int #

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

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

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

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

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

Traversable ParameterDeclaration Source # 
Instance details

Defined in Language.C.Types.Parse

Eq i => Eq (ParameterDeclaration i) Source # 
Instance details

Defined in Language.C.Types.Parse

Show i => Show (ParameterDeclaration i) Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty i => Pretty (ParameterDeclaration i) Source # 
Instance details

Defined in Language.C.Types.Parse

data DeclaratorOrAbstractDeclarator i Source #

Instances
Functor DeclaratorOrAbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Foldable DeclaratorOrAbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Traversable DeclaratorOrAbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Eq i => Eq (DeclaratorOrAbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Show i => Show (DeclaratorOrAbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

data AbstractDeclarator i Source #

Instances
Functor AbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Foldable AbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

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

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

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

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

toList :: AbstractDeclarator a -> [a] #

null :: AbstractDeclarator a -> Bool #

length :: AbstractDeclarator a -> Int #

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

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

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

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

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

Traversable AbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Eq i => Eq (AbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Show i => Show (AbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty i => Pretty (AbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

data DirectAbstractDeclarator i Source #

Instances
Functor DirectAbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Foldable DirectAbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Methods

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

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

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

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

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

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

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

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

toList :: DirectAbstractDeclarator a -> [a] #

null :: DirectAbstractDeclarator a -> Bool #

length :: DirectAbstractDeclarator a -> Int #

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

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

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

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

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

Traversable DirectAbstractDeclarator Source # 
Instance details

Defined in Language.C.Types.Parse

Eq i => Eq (DirectAbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Show i => Show (DirectAbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

Pretty i => Pretty (DirectAbstractDeclarator i) Source # 
Instance details

Defined in Language.C.Types.Parse

YACC grammar

The parser above is derived from a modification of the YACC grammar for C99 found at http://www.quut.com/c/ANSI-C-grammar-y-1999.html, reproduced below.

%token IDENTIFIER TYPE_NAME INTEGER

%token TYPEDEF EXTERN STATIC AUTO REGISTER INLINE RESTRICT
%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
%token BOOL COMPLEX IMAGINARY
%token STRUCT UNION ENUM

%start parameter_list
%%

declaration_specifiers
	: storage_class_specifier
	| storage_class_specifier declaration_specifiers
	| type_specifier
	| type_specifier declaration_specifiers
	| type_qualifier
	| type_qualifier declaration_specifiers
	| function_specifier
	| function_specifier declaration_specifiers
	;

storage_class_specifier
	: TYPEDEF
	| EXTERN
	| STATIC
	| AUTO
	| REGISTER
	;

type_specifier
	: VOID
	| CHAR
	| SHORT
	| INT
	| LONG
	| FLOAT
	| DOUBLE
	| SIGNED
	| UNSIGNED
	| BOOL
	| COMPLEX
	| IMAGINARY
 	| STRUCT IDENTIFIER
	| UNION IDENTIFIER
	| ENUM IDENTIFIER
	| TYPE_NAME
	;

type_qualifier
	: CONST
	| RESTRICT
	| VOLATILE
	;

function_specifier
	: INLINE
	;

declarator
	: pointer direct_declarator
	| direct_declarator
	;

direct_declarator
	: IDENTIFIER
	| '(' declarator ')'
	| direct_declarator '[' type_qualifier_list ']'
	| direct_declarator '[' type_qualifier_list * ']'
	| direct_declarator '[' * ']'
 	| direct_declarator '[' IDENTIFIER ']'
	| direct_declarator '[' INTEGER ']'
	| direct_declarator '[' ']'
	| direct_declarator '(' parameter_list ')'
	| direct_declarator '(' ')'
	;

pointer
	: *
	| * type_qualifier_list
	| * pointer
	| * type_qualifier_list pointer
	;

type_qualifier_list
	: type_qualifier
	| type_qualifier_list type_qualifier
	;

parameter_list
	: parameter_declaration
	| parameter_list ',' parameter_declaration
	;

parameter_declaration
	: declaration_specifiers declarator
	| declaration_specifiers abstract_declarator
	| declaration_specifiers
	;

abstract_declarator
	: pointer
	| direct_abstract_declarator
	| pointer direct_abstract_declarator
	;

direct_abstract_declarator
	: '(' abstract_declarator ')'
	| '[' ']'
	| direct_abstract_declarator '[' ']'
	| '[' * ']'
	| direct_abstract_declarator '[' * ']'
	| '[' IDENTIFIER ']'
	| direct_abstract_declarator '[' IDENTIFIER ']'
	| '[' INTEGER ']'
	| direct_abstract_declarator '[' INTEGER ']'
	| '(' ')'
	| '(' parameter_list ')'
	| direct_abstract_declarator '(' ')'
	| direct_abstract_declarator '(' parameter_list ')'
	;

%%
#include <stdio.h>

extern char yytext[];
extern int column;

void yyerror(char const *s)
{
	fflush(stdout);
	printf("n%*sn%*sn", column, "^", column, s);
}

Testing utilities