RTF Parser

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5543 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Adam Fedor 1999-12-16 22:56:45 +00:00
parent ac6f996208
commit 16eefe44b2
18 changed files with 3132 additions and 7 deletions

View file

@ -1,3 +1,13 @@
1999-12-16 Adam Fedor <fedor@gnu.org>
* New RTF parser from Stefan Bðhringer
<stefan.boehringer@uni-bochum.de>. May not work because
of incomplete implementation of NSFontManager and others.
* Source/Parsers/attributedStringConsumer.m New file.
* Source/Parsers/rtfScanner.c: Likewise.
* Source/Parsers/rtfGrammer.y: Likewise.
* Source/NSFontManager.m: Stubs for some unimplemented functions.
Thu Dec 16 04:09:20 1999 Nicola Pero <n.pero@mi.flashnet.it>
* Headers/AppKit/NSControl.h: Prefixed ivars with underscore;

View file

@ -35,6 +35,11 @@ include ../Version
# The library to be compiled
LIBRARY_NAME=libgnustep-gui
# The Objective-C source files to be compiled
libgnustep-gui_C_FILES = \
Parsers/rtfGrammer.tab.c \
Parsers/rtfScanner.c
# The Objective-C source files to be compiled
libgnustep-gui_OBJC_FILES = Functions.m \
NSActionCell.m \
@ -139,7 +144,8 @@ tiff.m \
externs.m \
GSTable.m \
GSHbox.m \
GSVbox.m
GSVbox.m \
Parsers/attributedStringConsumer.m
ifneq ($(FOUNDATION_LIB), fd)
libgnustep-gui_OBJC_FILES += NSPasteboard.m

View file

@ -36,7 +36,8 @@
#
# Things to do before compiling
#before-all::
before-all::
$(MKDIRS) ./$(GNUSTEP_OBJ_DIR)/Parsers
# Things to do after compiling
# after-all::
@ -73,3 +74,6 @@
#
# GNUstep GUI Library specific targets
Parsers/rtfGrammer.tab.c: Parsers/rtfGrammer.y
$(BISON) $(BISON_FLAGS) $<

View file

@ -52,6 +52,9 @@ ADDITIONAL_CPPFLAGS = -DGNUSTEP_INSTALL_LIBDIR=\"$(GNUSTEP_INSTALL_LIBDIR)\" \
-DGNUSTEP_TARGET_OS=\"$(GNUSTEP_TARGET_OS)\" \
-DLIBRARY_COMBO=\"$(LIBRARY_COMBO)\"
BISON_FLAGS = -d -p GSRTF
BISON = BISON_SIMPLE=Parsers/bison.simple bison
# Additional flags to pass to the Objective-C compiler
ADDITIONAL_OBJCFLAGS = -Wall

View file

@ -4,6 +4,7 @@
#include <AppKit/NSFont.h>
#include <Foundation/NSString.h>
#include <Foundation/NSRange.h>
#include "Parsers/rtfConsumer.h"
/*
* function to return a character set containing characters that
@ -277,11 +278,6 @@ paraBreakCSet()
* With love from Michael, hehe.
*/
- (id) initWithRTF: (NSData*)data
documentAttributes: (NSDictionary**)dict
{
return self;
}
- (id) initWithRTFD: (NSData*)data
documentAttributes: (NSDictionary**)dict

View file

@ -254,6 +254,30 @@ static Class fontPanelClass = Nil;
return nil;
}
}
#else
- (NSFont *)convertFont:(NSFont *)fontObject
toFamily:(NSString *)family
{
return fontObject;
}
- (NSFont *)convertFont:(NSFont *)fontObject
toFace:(NSString *)typeface
{
return fontObject;
}
- (NSFont *)convertFont:(NSFont *)fontObject
toHaveTrait:(NSFontTraitMask)trait
{
return fontObject;
}
- (NSFont *)convertFont:(NSFont *)fontObject
toSize:(float)size
{
return fontObject;
}
#endif
//

View file

@ -0,0 +1,267 @@
/* attributedStringConsumer.m
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "Parsers/rtfConsumer.h"
/* we have to satisfy the scanner with a stream reading function */
typedef struct {
NSString *string;
int position;
int length;
} StringContext;
static void
initStringContext(StringContext *ctxt, NSString *string)
{
ctxt->string = string;
ctxt->position = 0;
ctxt->length = [string length];
}
static int
readNSString(StringContext *ctxt)
{
return (ctxt->position < ctxt->length )
? [ctxt->string characterAtIndex:ctxt->position++]: EOF;
}
/*
we must implement from the rtfConsumerSkeleton.h file (Supporting files)
this includes the yacc error handling and output
*/
#define CTXT ((NSMutableDictionary *)ctxt)
#define FONTS [CTXT objectForKey:GSRTFfontDictName]
#define RESULT [CTXT objectForKey:GSRTFresultName]
#define GSRTFfontDictName @"fonts"
#define GSRTFcurrentTextPosition @"textPosition"
#define GSRTFresultName @"result"
#define GSRTFboldRange @"boldOn"
#define GSRTFitalicRange @"italicOn"
#define GSRTFunderlineRange @"underlineOn"
#define GSRTFcurrentFont @"currentFont"
#define GSRTFdocumentAttributes @"documentAttributes"
static NSRange MakeRangeFromAbs(int a1,int a2)
{
if(a1< a2) return NSMakeRange(a1,a2-a1);
else return NSMakeRange(a2,a1-a2);
}
/* handle errors (this is the yacc error mech) */
void GSRTFerror(const char *msg)
{
[NSException raise:NSInvalidArgumentException
format:@"Syntax error in RTF:%s", msg];
}
void GSRTFgenericRTFcommand(void *ctxt, RTFcmd cmd)
{
fprintf(stderr, "encountered rtf cmd:%s", cmd.name);
if (cmd.isEmpty) fprintf(stderr, " argument is empty\n");
else fprintf(stderr, " argument is %d\n", cmd.parameter);
}
//Start: we're doing some initialization
void GSRTFstart(void *ctxt)
{
[CTXT setObject:[NSNumber numberWithInt:0] forKey: GSRTFcurrentTextPosition];
[CTXT setObject:[NSFont userFontOfSize:12] forKey: GSRTFcurrentFont];
[CTXT setObject:[NSMutableDictionary dictionary] forKey: GSRTFfontDictName];
[RESULT beginEditing];
}
// Finished to parse one piece of RTF.
void GSRTFstop(void *ctxt)
{
//<!> close all open bolds et al.
[RESULT beginEditing];
}
void GSRTFopenBlock(void *ctxt)
{
}
void GSRTFcloseBlock(void *ctxt)
{
}
void GSRTFmangleText(void *ctxt, const char *text)
{
int oldPosition=[[CTXT objectForKey: GSRTFcurrentTextPosition] intValue],
textlen=strlen(text),
newPosition=oldPosition + textlen;
NSRange insertionRange=NSMakeRange(oldPosition,0);
NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:
[CTXT objectForKey:GSRTFcurrentFont],
NSFontAttributeName,nil];
[CTXT setObject:[NSNumber numberWithInt:newPosition]
forKey: GSRTFcurrentTextPosition];
[RESULT replaceCharactersInRange:insertionRange
withString:[NSString stringWithCString:text]];
[RESULT setAttributes:attributes range:NSMakeRange(oldPosition,textlen)];
}
void GSRTFregisterFont(void *ctxt, const char *fontName,
RTFfontFamily family, int fontNumber)
{
NSMutableDictionary *fonts = FONTS;
NSString *fontNameString;
NSNumber *fontId = [NSNumber numberWithInt:fontNumber];
if (!fontName || !*fontName || !fontId) // <?> fontId ist nie null
{
[NSException raise:NSInvalidArgumentException
format:@"Error in RTF (font omitted?), position:%d",
[[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]];
}
// exclude trailing ';' from fontName
fontNameString = [NSString stringWithCString:fontName length:strlen(fontName)-1];
[fonts setObject:fontNameString forKey:fontId];
}
void GSRTFchangeFontTo(void *ctxt, int fontNumber)
{
NSDictionary *fonts = FONTS;
NSNumber *fontId = [NSNumber numberWithInt:fontNumber];
NSFont *font=[NSFont fontWithName:[fonts objectForKey:fontId]
size:[[CTXT objectForKey:GSRTFcurrentFont] pointSize]];
if (!font) /* we're about to set an unknown font */
{
[NSException raise:NSInvalidArgumentException
format:@"Error in RTF (referring to undefined font \\f%d), position:%d",
fontNumber,
[[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]];
} else {
font=[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toFamily:[font familyName]];
[CTXT setObject:font forKey: GSRTFcurrentFont];
}
}
// <N> fontSize is in halfpoints according to spec
#define fs2points(a) ((a)/2.0)
void GSRTFchangeFontSizeTo(void *ctxt, int fontSize)
{
[CTXT setObject:[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toSize:fs2points(fontSize)]
forKey:GSRTFcurrentFont];
}
static NSRange rangeForContextAndAttribute(void *ctxt, NSString *attrib)
{
NSString *attribStartString=[CTXT objectForKey:GSRTFboldRange];
if(!attribStartString)
{
NSLog(@"RTF anomality (attribute:%@ off statement unpaired with on statement), position:%d",
attrib, [[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]);
return NSMakeRange(0, 0);
}
return MakeRangeFromAbs([attribStartString intValue],
[[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]);
}
void GSRTFhandleItalicAttribute(void *ctxt, BOOL state)
{
if(!state) // this indicates a bold off
{
[RESULT addAttribute:NSFontAttributeName
value:[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toHaveTrait:NSItalicFontMask]
range:rangeForContextAndAttribute(ctxt,GSRTFboldRange)];
} else {
[CTXT setObject:[CTXT objectForKey:GSRTFcurrentTextPosition] forKey:GSRTFitalicRange];
}
}
void GSRTFhandleBoldAttribute(void *ctxt, BOOL state)
{
if(!state) // this indicates a bold off
{
[RESULT addAttribute:NSFontAttributeName
value:[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toHaveTrait:NSBoldFontMask]
range:rangeForContextAndAttribute(ctxt,GSRTFboldRange)];
} else {
[CTXT setObject:[CTXT objectForKey:GSRTFcurrentTextPosition]
forKey:GSRTFboldRange];
}
}
void GSRTFhandleDocumentAttribute(void *ctxt, int attrib)
{
}
NSMutableAttributedString *attributedStringFromRTF(NSString *rtfString)
{
RTFscannerCtxt scanner;
StringContext stringCtxt;
NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
NSMutableAttributedString *result=[[NSMutableAttributedString alloc] init];
[myDict setObject:result forKey: GSRTFresultName];
initStringContext(&stringCtxt, rtfString);
lexInitContext(&scanner, &stringCtxt, (int (*)(void*))readNSString);
GSRTFparse(myDict, &scanner);
return [result autorelease];
}
@implementation NSAttributedString (RTFParser)
- (id) initWithRTF: (NSData*)data
documentAttributes: (NSDictionary**)dict
{
RTFscannerCtxt scanner;
StringContext stringCtxt;
NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
NSString *parseString = [NSString stringWithCString:[data bytes]
length:[data length]];
NSMutableAttributedString *result =
[[[NSMutableAttributedString alloc] init] autorelease];
[myDict setObject:result forKey: GSRTFresultName];
initStringContext(&stringCtxt, parseString);
lexInitContext(&scanner, &stringCtxt, (int (*)(void*))readNSString);
GSRTFparse(myDict, &scanner);
if (dict && [myDict objectForKey:GSRTFdocumentAttributes])
(*dict)=[myDict objectForKey:GSRTFdocumentAttributes]; // document
[self autorelease];
return [[[self class] alloc] initWithAttributedString:result];
}
@end

View file

@ -0,0 +1,242 @@
/* attributedStringConsumer.m
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "Parsers/rtfConsumer.h"
/* we have to satisfy the scanner with a stream reading function */
typedef struct {
NSString *string;
int position;
int length;
} StringContext;
static void
initStringContext(StringContext *ctxt, NSString *string)
{
ctxt->string = string;
ctxt->position = 0;
ctxt->length = [string length];
}
static int
readNSString(StringContext *ctxt)
{
return (ctxt->position < ctxt->length )
? [ctxt->string characterAtIndex:ctxt->position++]: EOF;
}
/*
we must implement from the rtfConsumerSceleton_c file (Supporting files)
this includes the yacc error handling and output
*/
#define CTXT ((NSMutableDictionary *)ctxt)
#define FONTS [CTXT objectForKey:GSRTFfontDictName]
#define RESULT [CTXT objectForKey:GSRTFresultName]
#define GSRTFfontDictName @"fonts"
#define GSRTFcurrentTextPosition @"textPosition"
#define GSRTFresultName @"result"
#define GSRTFboldRange @"boldOn"
#define GSRTFitalicRange @"italicOn"
#define GSRTFunderlineRange @"underlineOn"
#define GSRTFcurrentFont @"currentFont"
#define GSRTFdocumentAttributes @"documentAttributes"
static NSRange MakeRangeFromAbs(int a1,int a2)
{
if(a1< a2) return NSMakeRange(a1,a2-a1);
else return NSMakeRange(a2,a1-a2);
}
/* handle errors (this is the yacc error mech) */
void GSRTFerror(const char *msg)
{
[NSException raise:NSInvalidArgumentException
format:@"Syntax error in RTF:%s", msg];
}
void GSRTFgenericRTFcommand(void *ctxt, RTFcmd cmd)
{
fprintf(stderr, "encountered rtf cmd:%s", cmd.name);
if (cmd.isEmpty) fprintf(stderr, " argument is empty\n");
else fprintf(stderr, " argument is %d\n", cmd.parameter);
}
//Start: we're doing some initialization
void GSRTFstart(void *ctxt)
{
[CTXT setObject:[NSNumber numberWithInt:0] forKey: GSRTFcurrentTextPosition];
[CTXT setObject:[NSFont userFontOfSize:12] forKey: GSRTFcurrentFont];
[CTXT setObject:[NSMutableDictionary dictionary] forKey: GSRTFfontDictName];
[RESULT beginEditing];
}
// Finished to parse one piece of RTF.
void GSRTFstop(void *ctxt)
{
//<!> close all open bolds et al.
[RESULT beginEditing];
}
void GSRTFopenBlock(void *ctxt)
{
}
void GSRTFcloseBlock(void *ctxt)
{
}
void GSRTFmangleText(void *ctxt, const char *text)
{
int oldPosition=[[CTXT objectForKey: GSRTFcurrentTextPosition] intValue],
textlen=strlen(text),
newPosition=oldPosition + textlen;
NSRange insertionRange=NSMakeRange(oldPosition,0);
NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:
[CTXT objectForKey:GSRTFcurrentFont],
NSFontAttributeName,nil];
[CTXT setObject:[NSNumber numberWithInt:newPosition]
forKey: GSRTFcurrentTextPosition];
[RESULT replaceCharactersInRange:insertionRange
withString:[NSString stringWithCString:text]];
[RESULT setAttributes:attributes range:NSMakeRange(oldPosition,textlen)];
}
void GSRTFregisterFont(void *ctxt, const char *fontName,
RTFfontFamily family, int fontNumber)
{
NSMutableDictionary *fonts = FONTS;
NSString *fontNameString;
NSNumber *fontId = [NSNumber numberWithInt:fontNumber];
if (!fontName || !*fontName || !fontId) // <?> fontId ist nie null
{
[NSException raise:NSInvalidArgumentException
format:@"Error in RTF (font omitted?), position:%d",
[[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]];
}
// exclude trailing ';' from fontName
fontNameString = [NSString stringWithCString:fontName length:strlen(fontName)-1];
[fonts setObject:fontNameString forKey:fontId];
}
void GSRTFchangeFontTo(void *ctxt, int fontNumber)
{
NSDictionary *fonts = FONTS;
NSNumber *fontId = [NSNumber numberWithInt:fontNumber];
NSFont *font=[NSFont fontWithName:[fonts objectForKey:fontId]
size:[[CTXT objectForKey:GSRTFcurrentFont] pointSize]];
if (!font) /* we're about to set an unknown font */
{
[NSException raise:NSInvalidArgumentException
format:@"Error in RTF (referring to undefined font \\f%d), position:%d",
fontNumber,
[[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]];
} else {
font=[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toFamily:[font familyName]];
[CTXT setObject:font forKey: GSRTFcurrentFont];
}
}
// <N> fontSize is in halfpoints according to spec
#define fs2points(a) ((a)/2.0)
void GSRTFchangeFontSizeTo(void *ctxt, int fontSize)
{
[CTXT setObject:[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toSize:fs2points(fontSize)]
forKey:GSRTFcurrentFont];
}
static NSRange rangeForContextAndAttribute(void *ctxt, NSString *attrib)
{
NSString *attribStartString=[CTXT objectForKey:GSRTFboldRange];
if(!attribStartString)
{
NSLog(@"RTF anomality (attribute:%@ off statement unpaired with on statement), position:%d",
attrib, [[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]);
return NSMakeRange(0, 0);
}
return MakeRangeFromAbs([attribStartString intValue],
[[CTXT objectForKey:GSRTFcurrentTextPosition] intValue]);
}
void GSRTFhandleItalicAttribute(void *ctxt, BOOL state)
{
if(!state) // this indicates a bold off
{
[RESULT addAttribute:NSFontAttributeName
value:[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toHaveTrait:NSItalicFontMask]
range:rangeForContextAndAttribute(ctxt,GSRTFboldRange)];
} else {
[CTXT setObject:[CTXT objectForKey:GSRTFcurrentTextPosition] forKey:GSRTFitalicRange];
}
}
void GSRTFhandleBoldAttribute(void *ctxt, BOOL state)
{
if(!state) // this indicates a bold off
{
[RESULT addAttribute:NSFontAttributeName
value:[[NSFontManager sharedFontManager]
convertFont:[CTXT objectForKey:GSRTFcurrentFont]
toHaveTrait:NSBoldFontMask]
range:rangeForContextAndAttribute(ctxt,GSRTFboldRange)];
} else {
[CTXT setObject:[CTXT objectForKey:GSRTFcurrentTextPosition]
forKey:GSRTFboldRange];
}
}
void GSRTFhandleDocumentAttribute(void *ctxt, int attrib)
{
}
NSMutableAttributedString *attributedStringFromRTF(NSString *rtfString)
{
RTFscannerCtxt scanner;
StringContext stringCtxt;
NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
NSMutableAttributedString *result=[[NSMutableAttributedString alloc] init];
[myDict setObject:result forKey: GSRTFresultName];
initStringContext(&stringCtxt, rtfString);
lexInitContext(&scanner, &stringCtxt, (int (*)(void*))readNSString);
GSRTFparse(myDict, &scanner);
return [result autorelease];
}

693
Source/Parsers/bison.simple Normal file
View file

@ -0,0 +1,693 @@
/* -*-C-*- Note some compilers choke on comments on `#line' lines. */
#line 3 "/usr/local/share/bison.simple"
/* Skeleton output parser for bison,
Copyright (C) 1984, 1989, 1990 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* As a special exception, when this file is copied by Bison into a
Bison output file, you may use that output file without restriction.
This special exception was added by the Free Software Foundation
in version 1.24 of Bison. */
#ifndef alloca
#ifdef __GNUC__
#define alloca __builtin_alloca
#else /* not GNU C. */
#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi)
#include <alloca.h>
#else /* not sparc */
#if defined (MSDOS) && !defined (__TURBOC__)
#include <malloc.h>
#else /* not MSDOS, or __TURBOC__ */
#if defined(_AIX)
#include <malloc.h>
#pragma alloca
#else /* not MSDOS, __TURBOC__, or _AIX */
#ifdef __hpux
#ifdef __cplusplus
extern "C" {
void *alloca (unsigned int);
};
#else /* not __cplusplus */
void *alloca ();
#endif /* not __cplusplus */
#endif /* __hpux */
#endif /* not _AIX */
#endif /* not MSDOS, or __TURBOC__ */
#endif /* not sparc. */
#endif /* not GNU C. */
#endif /* alloca not defined. */
/* This is the parser code that is written into each bison parser
when the %semantic_parser declaration is not specified in the grammar.
It was written by Richard Stallman by simplifying the hairy parser
used when %semantic_parser is specified. */
/* Note: there must be only one dollar sign in this file.
It is replaced by the list of actions, each action
as one case of the switch. */
#define yyerrok (yyerrstatus = 0)
#define yyclearin (yychar = YYEMPTY)
#define YYEMPTY -2
#define YYEOF 0
#define YYACCEPT return(0)
#define YYABORT return(1)
#define YYERROR goto yyerrlab1
/* Like YYERROR except do call yyerror.
This remains here temporarily to ease the
transition to the new meaning of YYERROR, for GCC.
Once GCC version 2 has supplanted version 1, this can go. */
#define YYFAIL goto yyerrlab
#define YYRECOVERING() (!!yyerrstatus)
#define YYBACKUP() \
do \
if (yychar == YYEMPTY && yylen == 1) \
{ yychar = (token), yylval = (value); \
yychar1 = YYTRANSLATE (yychar); \
YYPOPSTACK; \
goto yybackup; \
} \
else \
{ yyerror ("syntax error: cannot back up"); YYERROR; } \
while (0)
#define YYTERROR 1
#define YYERRCODE 256
#ifndef YYPURE
#define YYLEX yylex()
#endif
#ifdef YYPURE
#ifdef YYLSP_NEEDED
#ifdef YYLEX_PARAM
#define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)
#else
#define YYLEX yylex(&yylval, &yylloc)
#endif
#else /* not YYLSP_NEEDED */
#ifdef YYLEX_PARAM
#define YYLEX yylex(&yylval, YYLEX_PARAM)
#else
#define YYLEX yylex(&yylval)
#endif
#endif /* not YYLSP_NEEDED */
#endif
/* If nonreentrant, generate the variables here */
#ifndef YYPURE
int yychar; /* the lookahead symbol */
YYSTYPE yylval; /* the semantic value of the */
/* lookahead symbol */
#ifdef YYLSP_NEEDED
YYLTYPE yylloc; /* location data for the lookahead */
/* symbol */
#endif
int yynerrs; /* number of parse errors so far */
#endif /* not YYPURE */
#if YYDEBUG != 0
int yydebug; /* nonzero means print parse trace */
/* Since this is uninitialized, it does not stop multiple parsers
from coexisting. */
#endif
/* YYINITDEPTH indicates the initial size of the parser's stacks */
#ifndef YYINITDEPTH
#define YYINITDEPTH 200
#endif
/* YYMAXDEPTH is the maximum size the stacks can grow to
(effective only if the built-in stack extension method is used). */
#if YYMAXDEPTH == 0
#undef YYMAXDEPTH
#endif
#ifndef YYMAXDEPTH
#define YYMAXDEPTH 10000
#endif
/* Prevent warning if -Wstrict-prototypes. */
#ifdef __GNUC__
/*int yyparse ();*/
#endif
#if __GNUC__ > 1 /* GNU C and GNU C++ define this. */
#define __yy_memcpy(TO,FROM,COUNT) __builtin_memcpy(TO,FROM,COUNT)
#else /* not GNU C or C++ */
#ifndef __cplusplus
/* This is the most reliable way to avoid incompatibilities
in available built-in functions on various systems. */
static void
__yy_memcpy (to, from, count)
char *to;
char *from;
int count;
{
register char *f = from;
register char *t = to;
register int i = count;
while (i-- > 0)
*t++ = *f++;
}
#else /* __cplusplus */
/* This is the most reliable way to avoid incompatibilities
in available built-in functions on various systems. */
static void
__yy_memcpy (char *to, char *from, int count)
{
register char *f = from;
register char *t = to;
register int i = count;
while (i-- > 0)
*t++ = *f++;
}
#endif
#endif
#line 196 "/usr/local/share/bison.simple"
/* The user can define YYPARSE_PARAM as the name of an argument to be passed
into yyparse. The argument should have type void *.
It should actually point to an object.
Grammar actions can access the variable by casting it
to the proper pointer type. */
typedef void *VOIDP;
#ifdef YYPARSE_PARAM
#ifdef __cplusplus
#define YYPARSE_PARAM_ARG void *YYPARSE_PARAM
#define YYPARSE_PARAM_DECL
#else /* not __cplusplus */
#define YYPARSE_PARAM_ARG YYPARSE_PARAM
#define YYPARSE_PARAM_DECL VOIDP YYPARSE_PARAM;
#endif /* not __cplusplus */
#else /* not YYPARSE_PARAM */
#define YYPARSE_PARAM_ARG
#define YYPARSE_PARAM_DECL
#endif /* not YYPARSE_PARAM */
int
yyparse(YYPARSE_PARAM_ARG)
YYPARSE_PARAM_DECL
{
register int yystate;
register int yyn;
register short *yyssp;
register YYSTYPE *yyvsp;
int yyerrstatus; /* number of tokens to shift before error messages enabled */
int yychar1 = 0; /* lookahead token as an internal (translated) token number */
short yyssa[YYINITDEPTH]; /* the state stack */
YYSTYPE yyvsa[YYINITDEPTH]; /* the semantic value stack */
short *yyss = yyssa; /* refer to the stacks thru separate pointers */
YYSTYPE *yyvs = yyvsa; /* to allow yyoverflow to reallocate them elsewhere */
#ifdef YYLSP_NEEDED
YYLTYPE yylsa[YYINITDEPTH]; /* the location stack */
YYLTYPE *yyls = yylsa;
YYLTYPE *yylsp;
#define YYPOPSTACK (yyvsp--, yyssp--, yylsp--)
#else
#define YYPOPSTACK (yyvsp--, yyssp--)
#endif
int yystacksize = YYINITDEPTH;
#ifdef YYPURE
int yychar;
YYSTYPE yylval;
int yynerrs;
#ifdef YYLSP_NEEDED
YYLTYPE yylloc;
#endif
#endif
YYSTYPE yyval; /* the variable used to return */
/* semantic values from the action */
/* routines */
int yylen;
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Starting parse\n");
#endif
yystate = 0;
yyerrstatus = 0;
yynerrs = 0;
yychar = YYEMPTY; /* Cause a token to be read. */
/* Initialize stack pointers.
Waste one element of value and location stack
so that they stay on the same level as the state stack.
The wasted elements are never initialized. */
yyssp = yyss - 1;
yyvsp = yyvs;
#ifdef YYLSP_NEEDED
yylsp = yyls;
#endif
/* Push a new state, which is found in yystate . */
/* In all cases, when you get here, the value and location stacks
have just been pushed. so pushing a state here evens the stacks. */
yynewstate:
*++yyssp = yystate;
if (yyssp >= yyss + yystacksize - 1)
{
/* Give user a chance to reallocate the stack */
/* Use copies of these so that the &'s don't force the real ones into memory. */
YYSTYPE *yyvs1 = yyvs;
short *yyss1 = yyss;
#ifdef YYLSP_NEEDED
YYLTYPE *yyls1 = yyls;
#endif
/* Get the current used size of the three stacks, in elements. */
int size = yyssp - yyss + 1;
#ifdef yyoverflow
/* Each stack pointer address is followed by the size of
the data in use in that stack, in bytes. */
#ifdef YYLSP_NEEDED
/* This used to be a conditional around just the two extra args,
but that might be undefined if yyoverflow is a macro. */
yyoverflow("parser stack overflow",
&yyss1, size * sizeof (*yyssp),
&yyvs1, size * sizeof (*yyvsp),
&yyls1, size * sizeof (*yylsp),
&yystacksize);
#else
yyoverflow("parser stack overflow",
&yyss1, size * sizeof (*yyssp),
&yyvs1, size * sizeof (*yyvsp),
&yystacksize);
#endif
yyss = yyss1; yyvs = yyvs1;
#ifdef YYLSP_NEEDED
yyls = yyls1;
#endif
#else /* no yyoverflow */
/* Extend the stack our own way. */
if (yystacksize >= YYMAXDEPTH)
{
yyerror("parser stack overflow");
return 2;
}
yystacksize *= 2;
if (yystacksize > YYMAXDEPTH)
yystacksize = YYMAXDEPTH;
yyss = (short *) alloca (yystacksize * sizeof (*yyssp));
__yy_memcpy ((char *)yyss, (char *)yyss1, size * sizeof (*yyssp));
yyvs = (YYSTYPE *) alloca (yystacksize * sizeof (*yyvsp));
__yy_memcpy ((char *)yyvs, (char *)yyvs1, size * sizeof (*yyvsp));
#ifdef YYLSP_NEEDED
yyls = (YYLTYPE *) alloca (yystacksize * sizeof (*yylsp));
__yy_memcpy ((char *)yyls, (char *)yyls1, size * sizeof (*yylsp));
#endif
#endif /* no yyoverflow */
yyssp = yyss + size - 1;
yyvsp = yyvs + size - 1;
#ifdef YYLSP_NEEDED
yylsp = yyls + size - 1;
#endif
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Stack size increased to %d\n", yystacksize);
#endif
if (yyssp >= yyss + yystacksize - 1)
YYABORT;
}
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Entering state %d\n", yystate);
#endif
goto yybackup;
yybackup:
/* Do appropriate processing given the current state. */
/* Read a lookahead token if we need one and don't already have one. */
/* yyresume: */
/* First try to decide what to do without reference to lookahead token. */
yyn = yypact[yystate];
if (yyn == YYFLAG)
goto yydefault;
/* Not known => get a lookahead token if don't already have one. */
/* yychar is either YYEMPTY or YYEOF
or a valid token in external form. */
if (yychar == YYEMPTY)
{
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Reading a token: ");
#endif
yychar = YYLEX;
}
/* Convert token to internal form (in yychar1) for indexing tables with */
if (yychar <= 0) /* This means end of input. */
{
yychar1 = 0;
yychar = YYEOF; /* Don't call YYLEX any more */
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Now at end of input.\n");
#endif
}
else
{
yychar1 = YYTRANSLATE(yychar);
#if YYDEBUG != 0
if (yydebug)
{
fprintf (stderr, "Next token is %d (%s", yychar, yytname[yychar1]);
/* Give the individual parser a way to print the precise meaning
of a token, for further debugging info. */
#ifdef YYPRINT
YYPRINT (stderr, yychar, yylval);
#endif
fprintf (stderr, ")\n");
}
#endif
}
yyn += yychar1;
if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
goto yydefault;
yyn = yytable[yyn];
/* yyn is what to do for this token type in this state.
Negative => reduce, -yyn is rule number.
Positive => shift, yyn is new state.
New state is final state => don't bother to shift,
just return success.
0, or most negative number => error. */
if (yyn < 0)
{
if (yyn == YYFLAG)
goto yyerrlab;
yyn = -yyn;
goto yyreduce;
}
else if (yyn == 0)
goto yyerrlab;
if (yyn == YYFINAL)
YYACCEPT;
/* Shift the lookahead token. */
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
#endif
/* Discard the token being shifted unless it is eof. */
if (yychar != YYEOF)
yychar = YYEMPTY;
*++yyvsp = yylval;
#ifdef YYLSP_NEEDED
*++yylsp = yylloc;
#endif
/* count tokens shifted since error; after three, turn off error status. */
if (yyerrstatus) yyerrstatus--;
yystate = yyn;
goto yynewstate;
/* Do the default action for the current state. */
yydefault:
yyn = yydefact[yystate];
if (yyn == 0)
goto yyerrlab;
/* Do a reduction. yyn is the number of a rule to reduce with. */
yyreduce:
yylen = yyr2[yyn];
if (yylen > 0)
yyval = yyvsp[1-yylen]; /* implement default value of the action */
#if YYDEBUG != 0
if (yydebug)
{
int i;
fprintf (stderr, "Reducing via rule %d (line %d), ",
yyn, yyrline[yyn]);
/* Print the symbols being reduced, and their result. */
for (i = yyprhs[yyn]; yyrhs[i] > 0; i++)
fprintf (stderr, "%s ", yytname[yyrhs[i]]);
fprintf (stderr, " -> %s\n", yytname[yyr1[yyn]]);
}
#endif
$ /* the action file gets copied in in place of this dollarsign */
#line 498 "/usr/local/share/bison.simple"
yyvsp -= yylen;
yyssp -= yylen;
#ifdef YYLSP_NEEDED
yylsp -= yylen;
#endif
#if YYDEBUG != 0
if (yydebug)
{
short *ssp1 = yyss - 1;
fprintf (stderr, "state stack now");
while (ssp1 != yyssp)
fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n");
}
#endif
*++yyvsp = yyval;
#ifdef YYLSP_NEEDED
yylsp++;
if (yylen == 0)
{
yylsp->first_line = yylloc.first_line;
yylsp->first_column = yylloc.first_column;
yylsp->last_line = (yylsp-1)->last_line;
yylsp->last_column = (yylsp-1)->last_column;
yylsp->text = 0;
}
else
{
yylsp->last_line = (yylsp+yylen-1)->last_line;
yylsp->last_column = (yylsp+yylen-1)->last_column;
}
#endif
/* Now "shift" the result of the reduction.
Determine what state that goes to,
based on the state we popped back to
and the rule number reduced by. */
yyn = yyr1[yyn];
yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
yystate = yytable[yystate];
else
yystate = yydefgoto[yyn - YYNTBASE];
goto yynewstate;
yyerrlab: /* here on detecting error */
if (! yyerrstatus)
/* If not already recovering from an error, report this error. */
{
++yynerrs;
#ifdef YYERROR_VERBOSE
yyn = yypact[yystate];
if (yyn > YYFLAG && yyn < YYLAST)
{
int size = 0;
char *msg;
int x, count;
count = 0;
/* Start X at -yyn if nec to avoid negative indexes in yycheck. */
for (x = (yyn < 0 ? -yyn : 0);
x < (sizeof(yytname) / sizeof(char *)); x++)
if (yycheck[x + yyn] == x)
size += strlen(yytname[x]) + 15, count++;
msg = (char *) malloc(size + 15);
if (msg != 0)
{
strcpy(msg, "parse error");
if (count < 5)
{
count = 0;
for (x = (yyn < 0 ? -yyn : 0);
x < (sizeof(yytname) / sizeof(char *)); x++)
if (yycheck[x + yyn] == x)
{
strcat(msg, count == 0 ? ", expecting `" : " or `");
strcat(msg, yytname[x]);
strcat(msg, "'");
count++;
}
}
yyerror(msg);
free(msg);
}
else
yyerror ("parse error; also virtual memory exceeded");
}
else
#endif /* YYERROR_VERBOSE */
yyerror("parse error");
}
goto yyerrlab1;
yyerrlab1: /* here on error raised explicitly by an action */
if (yyerrstatus == 3)
{
/* if just tried and failed to reuse lookahead token after an error, discard it. */
/* return failure if at end of input */
if (yychar == YYEOF)
YYABORT;
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);
#endif
yychar = YYEMPTY;
}
/* Else will try to reuse lookahead token
after shifting the error token. */
yyerrstatus = 3; /* Each real token shifted decrements this */
goto yyerrhandle;
yyerrdefault: /* current state does not do anything special for the error token. */
#if 0
/* This is wrong; only states that explicitly want error tokens
should shift them. */
yyn = yydefact[yystate]; /* If its default is to accept any token, ok. Otherwise pop it.*/
if (yyn) goto yydefault;
#endif
yyerrpop: /* pop the current state because it cannot handle the error token */
if (yyssp == yyss) YYABORT;
yyvsp--;
yystate = *--yyssp;
#ifdef YYLSP_NEEDED
yylsp--;
#endif
#if YYDEBUG != 0
if (yydebug)
{
short *ssp1 = yyss - 1;
fprintf (stderr, "Error: state stack now");
while (ssp1 != yyssp)
fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n");
}
#endif
yyerrhandle:
yyn = yypact[yystate];
if (yyn == YYFLAG)
goto yyerrdefault;
yyn += YYTERROR;
if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
goto yyerrdefault;
yyn = yytable[yyn];
if (yyn < 0)
{
if (yyn == YYFLAG)
goto yyerrpop;
yyn = -yyn;
goto yyreduce;
}
else if (yyn == 0)
goto yyerrpop;
if (yyn == YYFINAL)
YYACCEPT;
#if YYDEBUG != 0
if (yydebug)
fprintf(stderr, "Shifting error token, ");
#endif
*++yyvsp = yylval;
#ifdef YYLSP_NEEDED
*++yylsp = yylloc;
#endif
yystate = yyn;
goto yynewstate;
}

View file

@ -0,0 +1,35 @@
/* rtfConsumer.h created by pingu on Fri 12-Nov-1999
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _rtfConsumer_h_INCLUDE
#define _rtfConsumer_h_INCLUDE
#include "Parsers/rtfScanner.h"
/* external symbols from the grammer */
int GSRTFparse(void *ctxt, RTFscannerCtxt *lctxt);
#endif

View file

@ -0,0 +1,35 @@
/* rtfConsumer.h created by pingu on Fri 12-Nov-1999
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _rtfConsumer_h_INCLUDE
#define _rtfConsumer_h_INCLUDE
#include "Parsers/rtfScanner.h"
/* external symbols from the grammer */
int GSRTFparse(void *ctxt, RTFscannerCtxt *lctxt);
#endif

View file

@ -0,0 +1,76 @@
/* rtfConsumerFunctions.h created by pingu on Wed 17-Nov-1999
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* here we define the interface functions to grammer consumers */
#ifndef rtfConsumerFunctions_h_INCLUDE
#define rtfConsumerFunctions_h_INCLUDE
#include "Parsers/rtfScanner.h"
/* general statements:
measurement is usually in twips: one twentieth of a point (this is about 0.01764 mm)
a tabstop of 540 twips (as it occurs on NeXT) is therefore about 0.95 cm
*/
/* prepare the ctxt, or whatever you want */
void GSRTFstart(void *ctxt);
/* seal the parsing process, the context or whatever you want */
void GSRTFstop(void *ctxt);
/* those pairing functions enclose RTFBlocks. Use it to capture the hierarchical attribute changes of blocks.
i.e. attributes of a block are forgotten once a block is closed
*/
void GSRTFopenBlock(void *ctxt);
void GSRTFcloseBlock(void *ctxt);
/* handle errors */
void GSRTFerror(const char *msg);
/* handle rtf commands not expicated in the grammer */
void GSRTFgenericRTFcommand(void *ctxt, RTFcmd cmd);
/* go, handle text */
void GSRTFmangleText(void *ctxt, const char *text);
/*
font functions
*/
/* get noticed that a particular font is introduced the font number is
introduced by an prededing GSRTFchangeFontTo call this state
can be recognized by the fact that the fontNumber in question
is unseen by then */
void GSRTFregisterFont(void *ctxt, const char *fontName,
RTFfontFamily family, int fontNumber);
/* this function is twofold: change font in character stream [you must
maintain stream info in ctxt]; introduce fonts in the first place */
void GSRTFchangeFontTo(void *ctxt, int fontNumber);
/* subject says it all */
void GSRTFchangeFontSizeTo(void *ctxt, int fontSize);
#endif

View file

@ -0,0 +1,76 @@
/* rtfConsumerFunctions.h created by pingu on Wed 17-Nov-1999
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* here we define the interface functions to grammer consumers */
#ifndef rtfConsumerFunctions_h_INCLUDE
#define rtfConsumerFunctions_h_INCLUDE
#include "Parsers/rtfScanner.h"
/* general statements:
measurement is usually in twips: one twentieth of a point (this is about 0.01764 mm)
a tabstop of 540 twips (as it occurs on NeXT) is therefore about 0.95 cm
*/
/* prepare the ctxt, or whatever you want */
void GSRTFstart(void *ctxt);
/* seal the parsing process, the context or whatever you want */
void GSRTFstop(void *ctxt);
/* those pairing functions enclose RTFBlocks. Use it to capture the hierarchical attribute changes of blocks.
i.e. attributes of a block are forgotten once a block is closed
*/
void GSRTFopenBlock(void *ctxt);
void GSRTFcloseBlock(void *ctxt);
/* handle errors */
void GSRTFerror(const char *msg);
/* handle rtf commands not expicated in the grammer */
void GSRTFgenericRTFcommand(void *ctxt, RTFcmd cmd);
/* go, handle text */
void GSRTFmangleText(void *ctxt, const char *text);
/*
font functions
*/
/* get noticed that a particular font is introduced the font number is
introduced by an prededing GSRTFchangeFontTo call this state
can be recognized by the fact that the fontNumber in question
is unseen by then */
void GSRTFregisterFont(void *ctxt, const char *fontName,
RTFfontFamily family, int fontNumber);
/* this function is twofold: change font in character stream [you must
maintain stream info in ctxt]; introduce fonts in the first place */
void GSRTFchangeFontTo(void *ctxt, int fontNumber);
/* subject says it all */
void GSRTFchangeFontSizeTo(void *ctxt, int fontSize);
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,44 @@
typedef union {
int number;
const char *text;
RTFcmd cmd;
} YYSTYPE;
#ifndef YYLTYPE
typedef
struct yyltype
{
int timestamp;
int first_line;
int first_column;
int last_line;
int last_column;
char *text;
}
yyltype;
#define YYLTYPE yyltype
#endif
#define RTFtext 257
#define RTFstart 258
#define RTFfont 259
#define RTFfontSize 260
#define RTFpaperWidth 261
#define RTFpaperHeight 262
#define RTFmarginLeft 263
#define RTFmarginRight 264
#define RTFbold 265
#define RTFitalic 266
#define RTFunderline 267
#define RTFunderlineStop 268
#define RTFOtherStatement 269
#define RTFfontListStart 270
#define RTFfamilyNil 271
#define RTFfamilyRoman 272
#define RTFfamilySwiss 273
#define RTFfamilyModern 274
#define RTFfamilyScript 275
#define RTFfamilyDecor 276
#define RTFfamilyTech 277

192
Source/Parsers/rtfGrammer.y Normal file
View file

@ -0,0 +1,192 @@
/* rtfGrammer.y
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
if processed using -p GSRTFP (as recommended) it will introduce the following global symbols:
'GSRTFPparse', `GSRTFPlex', `GSRTFPerror', `GSRTFPnerrs', `GSRTFPlval',
`GSRTFPchar', `GSRTFPdebug
*/
/* we request for a reentrant parser */
%pure_parser
%{
/*
The overall plan is to make this grammer universal in usage.
Intrested buddies can implement plain C functions to consume what
the grammer is producing. this way the rtf-grammer-tree can be
converted to what is needed: GNUstep attributed strings, tex files,
...
The plan is laid out by defining a set of C functions which cover
all what is needed to mangle rtf information (it is NeXT centric
however and may even lack some features). Be aware that some
functions are called at specific times when some information may or
may not be available. The first argument of all functions is a
context, which is asked to be maintained by the consumer at
whichever purpose seems appropriate. This context must be passed to
the parser by issuing 'value = GSRTFparse(ctxt, lctxt);' in the
first place.
*/
#include <stdlib.h>
#include <string.h>
#include "Parsers/rtfScanner.h"
/* this context is passed to the interface functions */
typedef void * GSRTFctxt;
#define YYPARSE_PARAM ctxt, lctxt
#define YYLEX_PARAM lctxt
#define YYERROR_VERBOSE
#include "rtfConsumerFunctions.h"
%}
%union {
int number;
const char *text;
RTFcmd cmd;
}
/* <!><p> RTFtext values have to be freed */
%token <text> RTFtext
%token RTFstart
%token <cmd> RTFfont
%token <cmd> RTFfontSize
%token <cmd> RTFpaperWidth
%token <cmd> RTFpaperHeight
%token <cmd> RTFmarginLeft
%token <cmd> RTFmarginRight
%token <cmd> RTFbold
%token <cmd> RTFitalic
%token <cmd> RTFunderline
%token <cmd> RTFunderlineStop
%token <cmd> RTFOtherStatement
%token RTFfontListStart
// <!> we assume token numbers to be sequential
// \fnil | \froman | \fswiss | \fmodern | \fscript | \fdecor | \ftech
// look at rtfScanner.h for enum definition
%token RTFfamilyNil
%token RTFfamilyRoman
%token RTFfamilySwiss
%token RTFfamilyModern
%token RTFfamilyScript
%token RTFfamilyDecor
%token RTFfamilyTech
%type <number> rtfFontFamily rtfStatement rtfGeneralStatement rtfBlockStatement rtfFontStatement
/* let's go */
%%
rtfText: { GSRTFstart(ctxt); } rtfBlock { GSRTFstop(ctxt); }
;
rtfBlock: '{' { GSRTFopenBlock(ctxt); } rtfIngredients '}' { GSRTFcloseBlock(ctxt); }
;
rtfIngredients: /* empty */
| rtfIngredients rtfStatement
| rtfIngredients RTFtext { GSRTFmangleText(ctxt, $2); free((void *)$2); }
| rtfIngredients rtfBlock
;
/*
RTF statements start with a '\', have a alpha name and a number argument
*/
rtfGeneralStatement: rtfBlockStatement
| rtfStatement
;
rtfBlockStatement: '{' rtfStatement '}' { $$=0; }
;
rtfStatement: RTFstart { $$=0; }
| rtfFontList { $$=0; }
| RTFfont { $$=0; GSRTFchangeFontTo(ctxt, $1.parameter); }
| RTFfontSize { $$=0; GSRTFchangeFontSizeTo(ctxt, $1.parameter); }
| RTFpaperWidth { $$=0; }
| RTFpaperHeight { $$=0; }
| RTFmarginLeft { $$=0; }
| RTFmarginRight { $$=0; }
| RTFbold { $$=0; GSRTFhandleBoldAttribute(ctxt, $1.isEmpty || !!$1.parameter); }
| RTFitalic { $$=0; GSRTFhandleItalicAttribute(ctxt, $1.isEmpty || !!$1.parameter); }
| RTFunderline { $$=0; }
| RTFunderlineStop { $$=0; }
| RTFOtherStatement { $$=0; GSRTFgenericRTFcommand(ctxt, $1); }
;
/*
Font description
*/
rtfFontList: RTFfontListStart rtfFonts
;
rtfFonts:
| rtfFonts rtfFontStatement
| rtfFonts '{' rtfFontStatement '}'
;
/* the first RTFfont tags the font with a number */
/* RTFtext introduces the fontName */
rtfFontStatement: RTFfont rtfFontFamily RTFtext { $$=0; GSRTFregisterFont(ctxt, $3, $2, $1.parameter);
free((void *)$3);
}
;
rtfFontFamily:
RTFfamilyNil { $$ = RTFfamilyNil - RTFfamilyNil; }
| RTFfamilyRoman { $$ = RTFfamilyRoman - RTFfamilyNil; }
| RTFfamilySwiss { $$ = RTFfamilySwiss - RTFfamilyNil; }
| RTFfamilyModern { $$ = RTFfamilyModern - RTFfamilyNil; }
| RTFfamilyScript { $$ = RTFfamilyScript - RTFfamilyNil; }
| RTFfamilyDecor { $$ = RTFfamilyDecor - RTFfamilyNil; }
| RTFfamilyTech { $$ = RTFfamilyTech - RTFfamilyNil; }
;
/*
Font description end
*/
/*
some cludgy trailer
*/
dummyNonTerminal: '\\' { @1.first_line; } /* we introduce a @n to fix the lex attributes */
;
%%
/* some C code here */

294
Source/Parsers/rtfScanner.c Normal file
View file

@ -0,0 +1,294 @@
/* rtcScanner
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "Parsers/rtfScanner.h"
#include "Parsers/rtfGrammer.tab.h"
// <§> scanner types and helpers
#define CArraySize(a) (sizeof(a)/sizeof((a)[0])-1)
typedef struct {
char *bf;
int length, position, chunkSize;
} DynamicString;
typedef struct {
const char *string;
int token;
} LexKeyword;
GSLexError initDynamicString(DynamicString *string)
{
string->length = 0, string->position = 0, string->chunkSize = 128;
string->bf = calloc(1, string->length = string->chunkSize);
if (!string->bf)
return LEXoutOfMemory;
return NoError;
}
GSLexError appendChar(DynamicString *string, int c)
{
if (string->position == string->length)
{
if (!(string->bf = realloc(string->bf,
string->length += string->chunkSize)))
return LEXoutOfMemory;
else
string->chunkSize <<= 1;
}
string->bf[string->position++] = c;
return NoError;
}
void lexInitContext(RTFscannerCtxt *lctxt, void *customContext,
int (*getcharFunction)(void *))
{
lctxt->streamLineNumber = 1;
lctxt->streamPosition = lctxt->pushbackCount = 0;
lctxt->lgetchar = getcharFunction;
lctxt->customContext = customContext;
}
int lexGetchar(RTFscannerCtxt *lctxt)
{
int c;
if (lctxt->pushbackCount)
{
lctxt->pushbackCount--;
return lctxt->pushbackBuffer[lctxt->pushbackCount];
}
lctxt->streamPosition++;
c = lctxt->lgetchar(lctxt->customContext);
if (c == '\n')
lctxt->streamLineNumber++;
return c;
}
void lexUngetchar(RTFscannerCtxt *lctxt, int c)
{
if (c == '\n')
lctxt->streamLineNumber--;
lctxt->pushbackBuffer[lctxt->pushbackCount++] = c; //<!> no checking here
}
int lexStreamPosition(RTFscannerCtxt *lctxt)
{
return lctxt->streamPosition - lctxt->pushbackCount;
}
char *my_strdup(const char *str)
{
char *copy = str? malloc(strlen(str) + 1): 0;
return !copy? 0: strcpy(copy, str);
}
int findStringFromKeywordArray(const char *string, const LexKeyword *array,
int arrayCount)
{
int min, max, mid, cmp;
const LexKeyword *currentKeyword;
for (min=0, max=arrayCount; min<=max; )
{
mid = (min+max)>>1;
currentKeyword = array + mid;
if (!(cmp = strcmp(string, currentKeyword->string)))
{
return currentKeyword->token;
}
else if (cmp>0)
min=mid+1;
else
max=mid-1;
}
return 0; // couldn't find
}
// end <§> scanner types and helpers
// <§> core scanner functions
#define token(a) (a)
// <!> must be sorted
LexKeyword RTFcommands[]={
"b", token(RTFbold),
"f", token(RTFfont),
"fdecor", token(RTFfamilyDecor),
"fmodern", token(RTFfamilyModern),
"fnil", token(RTFfamilyNil),
"fonttbl", token(RTFfontListStart),
"froman", token(RTFfamilyRoman),
"fs", token(RTFfontSize),
"fscript", token(RTFfamilyScript),
"fswiss", token(RTFfamilySwiss),
"ftech", token(RTFfamilyTech),
"i", token(RTFitalic),
"margl", token(RTFmarginLeft),
"margr", token(RTFmarginRight),
"paperh", token(RTFpaperHeight),
"paperw", token(RTFpaperWidth),
"rtf", token(RTFstart),
"ul", token(RTFunderline),
"ulnone", token(RTFunderlineStop)
};
BOOL probeCommand(RTFscannerCtxt *lctxt)
{
int c = lexGetchar(lctxt);
lexUngetchar(lctxt, c);
return isalpha(c);
}
// <N> According to spec a cmdLength of 32 is respected
#define RTFMaxCmdLength 32
#define RTFMaxArgumentLength 64
GSLexError readCommand(RTFscannerCtxt *lctxt, YYSTYPE *lvalp, int *token) // the '\\' is already read
{
char cmdNameBf[RTFMaxCmdLength+1], *cmdName = cmdNameBf;
char argumentBf[RTFMaxArgumentLength+1], *argument = argumentBf;
int c, foundToken;
lvalp->cmd.name = 0; // initialize
while (isalpha( c = lexGetchar(lctxt) ))
{
*cmdName++ = c;
if (cmdName >= cmdNameBf + RTFMaxCmdLength)
return LEXsyntaxError;
}
*cmdName = 0;
if (!(foundToken = findStringFromKeywordArray(cmdNameBf, RTFcommands,
CArraySize(RTFcommands))))
{
if (!(lvalp->cmd.name = my_strdup(cmdNameBf)))
return LEXoutOfMemory;
*token = RTFOtherStatement;
}
else
{
*token = foundToken;
}
if (c == ' ') // this is an empty argument
{
lvalp->cmd.isEmpty = YES;
}
else if (isdigit(c) || c == '-') // we've found a numerical argument
{
do
{
*argument++ = c;
if (argument >= argumentBf + RTFMaxArgumentLength)
return LEXsyntaxError;
} while (isdigit(c = lexGetchar(lctxt)));
*argument = 0;
if (c != ' ')
lexUngetchar(lctxt, c); // <N> ungetc non-digit
// the consumption of the space seems necessary on NeXT but
// is not according to spec
lvalp->cmd.isEmpty = NO, lvalp->cmd.parameter = atoi(argumentBf);
}
else
{
lvalp->cmd.isEmpty = YES;
lexUngetchar(lctxt, c); // ungetc non-whitespace delimiter
}
return NoError;
}
GSLexError readText(RTFscannerCtxt *lctxt, YYSTYPE *lvalp)
{
int c;
DynamicString text;
GSLexError error;
if ((error = initDynamicString(&text)))
return error;
for (;;)
{
c = lexGetchar(lctxt);
if (c == EOF || c == '{' || c == '}')
{
lexUngetchar(lctxt, c);
break;
}
if (c == '\\') // see <p>
{
if (probeCommand(lctxt))
{
lexUngetchar(lctxt, c);
break;
}
appendChar(&text, lexGetchar(lctxt));
}
else
{
if (c != '\n' && c != '\r') // <N> newline and cr are ignored if not quoted
appendChar(&text, c);
}
}
appendChar(&text, 0);
lvalp->text = text.bf; // release is up to the consumer
return NoError;
}
int GSRTFlex(YYSTYPE *lvalp, YYLTYPE *llocp, RTFscannerCtxt *lctxt) /* provide value and position in the params */
{
int c;
int token = 0;
do
c = lexGetchar(lctxt);
while ( c == '\n' || c == '\r' ); // <A> the listed characters are to be ignored
switch (c)
{
case EOF: token = 0;
break;
case '{': token = '{';
break;
case '}': token = '}';
break;
case '\\':
if (probeCommand(lctxt))
{
readCommand(lctxt, lvalp, &token);
break;
}
// else fall through to default: read text <A>
// no break <A>
default:
lexUngetchar(lctxt, c);
readText(lctxt, lvalp);
token = RTFtext;
break;
}
//*llocp = lctxt->position();
return token;
}

View file

@ -0,0 +1,68 @@
/* rtcScanner.h
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Stefan Bðhringer (stefan.boehringer@uni-bochum.de)
Date: Dec 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef rtfScanner_h_INCLUDE
#define rtfScanner_h_INCLUDE
#include <objc/objc.h>
#if !defined(YES)
typedef char BOOL;
enum { NO, YES };
#endif
typedef enum { NoError, LEXoutOfMemory, LEXsyntaxError } GSLexError;
typedef struct _RTFscannerCtxt {
int (*lgetchar)(void *);
char pushbackBuffer[2]; // gaurantee 2 chars of pushback
int pushbackCount;
int streamPosition;
int streamLineNumber;
void *customContext;
} RTFscannerCtxt;
typedef struct {
BOOL isEmpty;
int number;
} RTFparameter;
typedef struct {
BOOL isEmpty;
int parameter;
int token;
const char *name;
} RTFcmd;
typedef enum {
GSRTFfamilyNil, GSRTFfamilyRoman, GSRTFfamilySwiss,
GSRTFfamilyModern, GSRTFfamilyScript, GSRTFfamilyDecor,
GSRTFfamilyTech
} RTFfontFamily;
void lexInitContext(RTFscannerCtxt *lctxt, void *customContext, int (*getcharFunction)());
#endif