]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/lib/antlrplugin.cpp
2 * Copyright (c) 2007,2011-2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "antlrplugin.h"
26 #include "RequirementLexer.hpp"
27 #include "RequirementParser.hpp"
28 #include <antlr/TokenStreamException.hpp>
32 namespace CodeSigning
{
34 namespace Parser
= Security_CodeSigning
;
38 // Lexer input adapters
40 class StdioInputStream
: public antlr::InputBuffer
{
42 StdioInputStream(FILE *fp
) : mFile(fp
) { }
43 int getChar() { return fgetc(mFile
); }
49 class StringInputStream
: public antlr::InputBuffer
{
51 StringInputStream(const string
&s
) : mInput(s
), mPos(mInput
.begin()) { }
52 int getChar() { return (mPos
== mInput
.end()) ? EOF
: static_cast<unsigned char>(*mPos
++); }
56 string::const_iterator mPos
;
61 // Generic parser driver
63 template <class Input
, class Source
, class Result
>
64 const Result
*parse(Source source
, Result
*(Parser::RequirementParser::*rule
)(), std::string
&errors
)
67 Parser::RequirementLexer
lexer(input
);
68 Parser::RequirementParser
parser(lexer
);
70 const Result
*result
= (parser
.*rule
)();
71 errors
= parser
.errors
;
75 ::free((void *)result
);
76 } catch (const antlr::TokenStreamException
&ex
) {
77 errors
= ex
.toString() + "\n";
79 return NULL
; // signal failure
84 // Hook up each supported parsing action to the plugin interface
87 const Requirement
*fileRequirement(FILE *source
, string
&errors
)
88 { return parse
<StdioInputStream
>(source
, &Parser::RequirementParser::requirement
, errors
); }
91 const Requirement
*stringRequirement(string source
, string
&errors
)
92 { return parse
<StringInputStream
>(source
, &Parser::RequirementParser::requirement
, errors
); }
95 const Requirements
*fileRequirements(FILE *source
, string
&errors
)
96 { return parse
<StdioInputStream
>(source
, &Parser::RequirementParser::requirementSet
, errors
); }
99 const Requirements
*stringRequirements(string source
, string
&errors
)
100 { return parse
<StringInputStream
>(source
, &Parser::RequirementParser::requirementSet
, errors
); }
103 const BlobCore
*fileGeneric(FILE *source
, string
&errors
)
104 { return parse
<StdioInputStream
>(source
, &Parser::RequirementParser::autosense
, errors
); }
107 const BlobCore
*stringGeneric(string source
, string
&errors
)
108 { return parse
<StringInputStream
>(source
, &Parser::RequirementParser::autosense
, errors
); }
112 // Basic plugin hookup
114 static AntlrPlugin plugin
= {
123 AntlrPlugin
*findAntlrPlugin()
129 } // end namespace CodeSigning
130 } // end namespace Security