1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008 Henrik Tidefelt
21 #include "pdfstructure.h"
22 #include "pdfscanner.h"
23 #include "exitcodes.h"
28 unsigned char hexToChar( char c1, char c2 );
29 unsigned char octalToChar( char c1, char c2, char c3 );
31 size_t stringParenDepth;
33 #define YY_EXIT_FAILURE Shapes::Interaction::EXIT_INTERNAL_ERROR
35 using namespace SimplePDF;
40 Delimiter [\[\]\(\)\{\}\<\>\/\%]
41 Regular [^ \t\n\r\[\]\(\)\{\}\<\>\/\%]
44 DecInteger [+-]?[0-9]+
46 Float [+-]?[0-9]*[.][0-9]*
50 HexString [<]({WhiteSpace}|[0-9A-Fa-f])*[>]
51 ButParentheses ([^()]|(\\(.|\n)))*
53 IndirectRef {PlainInteger}{WhiteSpace}+{PlainInteger}{WhiteSpace}*"R"
54 IndirectDef {PlainInteger}{WhiteSpace}+{PlainInteger}{WhiteSpace}*"obj"
60 %option yyclass="PdfScanner"
68 yylval.pdfObj = new PDF_Int( strtol( yytext, & end, 10 ) );
76 i = strtol( yytext, & end, 10 );
77 v = strtol( end, & end, 10 );
78 yylval.pdfObj = new PDF_Indirect( i, v );
86 i = strtol( yytext, & end, 10 );
87 v = strtol( end, & end, 10 );
88 yylval.pdfR = new PDF_Indirect_in( i, v );
94 yylval.pdfObj = new PDF_Float( strtod( yytext, & end ) );
99 /* Note that we don't parse the internal meaning of the bracketed contents, since this
100 * would only require more work when writing back to a pdf file.
103 const char * src( yytext + 1 );
108 for( ; isblank( *src ); ++src )
114 for( ; isblank( *src ); ++src )
120 str += hexToChar( c1, 0 );
123 str += hexToChar( c1, c2 );
124 for( ; isblank( *src ); ++src )
130 yylval.pdfObj = new PDF_String( str );
132 yytext[ yyleng - 1 ] = '\0';
133 yylval.pdfObj = new PDF_HexString( yytext + 1 );
138 stringParenDepth = 1;
139 BEGIN( StringState );
144 yylval.str = strdup( yytext );
149 if( stringParenDepth > 0 )
151 yylval.str = strdup( yytext );
161 <StringState>{ButParentheses} {
162 /* Note that we don't parse the internal meaning of escape sequences within the name, since this
163 * would only require more work when writing back to a pdf file.
166 char * res( new char[ pdfleng + 1 ] );
167 char * dst( res.getPtr( ) );
168 const char * src( yytext );
213 *(dst++) = octalToChar( c1, c2, c3 );
217 *(dst++) = octalToChar( 0, c1, c2 );
223 *(dst++) = octalToChar( 0, 0, c1 );
244 yylval.str = strdup( yytext );
248 true { yylval.pdfObj = new PDF_Boolean( true ); return T_Constant; }
249 false { yylval.pdfObj = new PDF_Boolean( false ); return T_Constant; }
250 null { yylval.pdfObj = new PDF_Null( ); return T_Constant; }
251 endobj { return T_endobj; }
252 stream("\r\n"|"\n") { return T_stream; }
253 endstream { return T_endstream; }
255 "<<" { return T_OpenDic; }
256 ">>" { return T_CloseDic; }
263 /* Note that we don't parse the internal meaning of #-sequences within the name, since this
264 * would only require more work when writing back to a pdf file.
267 const char * src( yytext + 1 );
268 RefCountPtr< char > dstMem( new char[ pdflength + 1 ] );
269 char * dst( dstMem.getPtr( ) );
271 for( c = *(src++); ! isblank( c ); c = *(src++) )
279 (dst++) = hexToChar( c1, c2 );
287 yylval.pdfObj = new PDF_Name( dstMem.getPtr( ) );
289 yylval.str = strdup( yytext + 1 );
293 <INITIAL>[ \t\n\r]+ ;
295 . { throw( std::string( "PDF scanner found unrecognized token: " ) + yytext ); }
298 /* The closing %% above marks the end of the Rules section and the beginning
299 * of the User Subroutines section. All text from here to the end of the
300 * file is copied verbatim to the end of the generated lex.pdf.c file.
301 * This section is where you put definitions of helper functions.
305 unsigned char hexToChar( char c1, char c2 )
307 unsigned char res( 0 );
310 res += 16 * static_cast< unsigned char >( c1 - '0' );
314 res += 16 * static_cast< unsigned char >( c1 - 'A' + 10 );
318 res += 16 * static_cast< unsigned char >( c1 - 'a' + 10 );
323 res += static_cast< unsigned char >( c2 - '0' );
327 res += static_cast< unsigned char >( c2 - 'A' + 10 );
331 res += static_cast< unsigned char >( c2 - 'a' + 10 );
337 unsigned char octalToChar( char c1, char c2, char c3 )
339 return 64 * static_cast< unsigned char >( c1 - '0' ) + 8 * static_cast< unsigned char >( c2 - '0' ) + static_cast< unsigned char >( c3 - '0' );