A collection of named types (typedefs)
Moduleinline-c-0.9.1.10Haskell2010
Language.C.Types.Parse
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(wherexis a C identifier). In C99 they can be arbitrary expressions. See theArrayTypedata type._Bool,_Complex, and_Imaginaryare not present.Untyped parameter lists (pre-K&R C) are not allowed.
The parser is incremental and generic (see CParser). Pretty
and QC.Arbitrary instances are provided for all the data types.
The entry point if you want to parse C declarations is
parameter_declaration.
- 18 types
- 25 values
- Packageinline-c-0.9.1.10
- Exports43
- LanguageHaskell2010
- LicenceMIT
- SourceParse.hs
Parser configuration
2 declarationsConstructors
CParserContextcpcIdentName :: StringcpcTypeNames :: TypeNamesFunction used to determine whether an identifier is a type name.
cpcParseIdent :: forall (m :: Type -> Type). CParser i m => m iParses an identifier, *without consuming whitespace afterwards*.
cpcIdentToString :: i -> StringcpcEnableCpp :: Bool
Default configuration
A type for C identifiers.
Instances6Eq, Ord, Show, IsString, Hashable, Pretty
Eq CIdentifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseOrd CIdentifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseShow CIdentifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseIsString CIdentifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseHashable CIdentifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty CIdentifierDefined in inline-c-0.9.1.10 · Language.C.Types.Parse
Parser type
4 declarationstype CParser i (m :: Type -> Type) = (Monad m, Functor m, Applicative m, MonadPlus m, Parsing m, CharParsing m, TokenParsing m, LookAheadParsing m, MonadReader (CParserContext i) m, MonadFail m, Hashable i)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 :: Stream s Identity Char=> CParserContext i-> StringSource name.
-> sString to parse.
-> ReaderT (CParserContext i) (Parsec s ()) aParser. Anything with type
forall m. CParser i m => m ais a valid argument.-> Either ParseError a
Runs a CParser using parsec.
quickCParser :: CParserContext i-> StringString to parse.
-> ReaderT (CParserContext i) (Parsec String ()) aParser. Anything with type
forall m. CParser i m => m ais a valid argument.-> a
Useful for quick testing. Uses "quickCParser" as source name, and throws
an error if parsing fails.
quickCParser_ :: Bool-> StringString to parse.
-> ReaderT (CParserContext CIdentifier) (Parsec String ()) aParser. Anything with type
forall m. CParser i m => m ais a valid argument.-> a
Like quickCParser, but uses cCParserContext (const False) as
CParserContext.
Types and parsing
29 declarationsInstances3Eq, Show, Pretty
Eq DeclarationSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseShow DeclarationSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty DeclarationSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.Parse
Instances3Eq, Show, Pretty
Eq StorageClassSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseShow StorageClassSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty StorageClassSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.Parse
Instances3Eq, Show, Pretty
Eq TypeSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseShow TypeSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty TypeSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.Parse
Instances3Eq, Show, Pretty
Eq TypeQualifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseShow TypeQualifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty TypeQualifierDefined in inline-c-0.9.1.10 · Language.C.Types.Parse
Instances3Eq, Show, Pretty
Eq FunctionSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParseShow FunctionSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty FunctionSpecifierDefined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Instances6Functor, Foldable, Traversable, Eq, Show, Pretty
Functor DeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable DeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable DeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (Declarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (Declarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty i => Pretty (Declarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Instances6Functor, Foldable, Traversable, Eq, Show, Pretty
Functor DirectDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable DirectDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable DirectDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (DirectDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (DirectDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty i => Pretty (DirectDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Array (ArrayType i)Proto [ParameterDeclaration i]
Instances6Functor, Foldable, Traversable, Eq, Show, Pretty
Functor ArrayOrProtoDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable ArrayOrProtoDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable ArrayOrProtoDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (ArrayOrProto i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (ArrayOrProto i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty i => Pretty (ArrayOrProto i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Instances6Functor, Foldable, Traversable, Eq, Show, Pretty
Functor ArrayTypeDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable ArrayTypeDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable ArrayTypeDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (ArrayType i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (ArrayType i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty i => Pretty (ArrayType i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Instances6Functor, Foldable, Traversable, Eq, Show, Pretty
Functor ParameterDeclarationDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable ParameterDeclarationDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable ParameterDeclarationDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (ParameterDeclaration i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (ParameterDeclaration i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty i => Pretty (ParameterDeclaration i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Instances5Functor, Foldable, Traversable, Eq, Show
Functor DeclaratorOrAbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable DeclaratorOrAbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable DeclaratorOrAbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (DeclaratorOrAbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (DeclaratorOrAbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Instances6Functor, Foldable, Traversable, Eq, Show, Pretty
Functor AbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable AbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable AbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (AbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (AbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty i => Pretty (AbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
Constructors
Instances6Functor, Foldable, Traversable, Eq, Show, Pretty
Functor DirectAbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseFoldable DirectAbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseTraversable DirectAbstractDeclaratorDefined in inline-c-0.9.1.10 · Language.C.Types.ParseEq i => Eq (DirectAbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParseShow i => Show (DirectAbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.ParsePretty i => Pretty (DirectAbstractDeclarator i)Defined in inline-c-0.9.1.10 · Language.C.Types.Parse
YACC grammar
0 declarationsThe 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);
}