/* * Copyright (c) 2002-2003 Jesper K. Pedersen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * 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, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. **/ %{ #ifdef QT_ONLY #include "compat.h" #else #include #include #endif #include #include #include "regexp.h" #include "textregexp.h" #include "textrangeregexp.h" #include "repeatregexp.h" #include "lookaheadregexp.h" #include "concregexp.h" #include "altnregexp.h" #include "positionregexp.h" #include "dotregexp.h" #include "compoundregexp.h" extern int yylex(); extern void setParseData( QString str ); int yyerror (const char *); void setParseResult( RegExp* ); RegExp* parseQtRegExp( QString qstr, bool* ok ); static RegExp* parseResult; static int _index; %} %union { struct { int min; int max; } range; int backRef; RegExp* regexp; char ch; } %token TOK_Dot %token TOK_Dollar %token TOK_Carat %token TOK_MagicLeftParent %token TOK_PosLookAhead %token TOK_NegLookAhead %token TOK_LeftParen %token TOK_RightParent %token TOK_Bar %token TOK_Quantifier %token TOK_BackRef %token TOK_CharClass %token TOK_Char %token TOK_EscapeChar %token TOK_PosWordChar %token TOK_PosNonWordChar %start regexp %% empty : /* nothing */ ; regexp : expression { setParseResult( $1) ; } | empty { setParseResult( new ConcRegExp( false ) ); } ; expression : expression TOK_Bar term { if ( dynamic_cast( $1 ) ) { $$ = $1; } else { $$ = new AltnRegExp( false ); dynamic_cast( $$ )->addRegExp( $1 ); } dynamic_cast( $$ )->addRegExp( $3 ); } | term { $$ = $1; } | expression TOK_Bar { if ( dynamic_cast( $1 ) ) { $$ = $1; } else { $$ = new AltnRegExp( false ); dynamic_cast( $$ )->addRegExp( $1 ); } dynamic_cast( $$ )->addRegExp( new TextRegExp( false, QString::tqfromLatin1("") ) ); } | TOK_Bar term { $$ = new AltnRegExp( false ); dynamic_cast( $$ )->addRegExp( new TextRegExp( false, QString::tqfromLatin1("") ) ); dynamic_cast( $$ )->addRegExp( $2 ); } | TOK_Bar { $$ = new AltnRegExp( false ); } ; term : term factor { RegExp* last = dynamic_cast( $1 )->lastRegExp(); TextRegExp *reg1, *reg2; if ( last && ( reg1 = dynamic_cast( last ) ) && ( reg2 = dynamic_cast( $2 ) ) ) { reg1->append( reg2->text() ); delete reg2; } else { dynamic_cast($$)->addRegExp( $2 ); } $$ = $1; } | factor { ConcRegExp* reg = new ConcRegExp( false ); reg->addRegExp( $1 ); $$ = reg; } ; factor : atom TOK_Quantifier { $$ = new RepeatRegExp( false, $2.min, $2.max, $1 ); } | atom { $$ = $1; } ; atom : TOK_LeftParen expression TOK_RightParent { $$ = $2; } | TOK_MagicLeftParent expression TOK_RightParent { $$ = $2; } | TOK_PosLookAhead expression TOK_RightParent { $$ = new LookAheadRegExp( false, LookAheadRegExp::POSITIVE, $2 ); } | TOK_NegLookAhead expression TOK_RightParent { $$ = new LookAheadRegExp( false, LookAheadRegExp::NEGATIVE, $2 ); } | TOK_CharClass { $$ = $1; } | char { $$ = $1; } | TOK_Dollar { $$ = new PositionRegExp( false, PositionRegExp::ENDLINE ); } | TOK_Carat { $$ = new PositionRegExp( false, PositionRegExp::BEGLINE ); } | TOK_Dot { $$ = new DotRegExp( false ); } | TOK_BackRef { QString match = TQString(TQString::fromLocal8Bit("\\%1")).tqarg( $1 ); $$ = new TextRegExp( false, match ); KMessageBox::information(0,i18n("Back reference regular expressions are not supported.

" "\\1, \\2, ... are back references, meaning they refer to " "previous matches. " "Unfortunately this is not supported in the current version of this editor.

" "In the graphical area the text %1 has been inserted. This is however " "just a workaround to ensure that the application handles the regexp at all. " "Therefore, as soon as you edit the regular expression in the graphical area, " "the back reference will be replaced by matching the text %2 literally.") .tqarg( match ).tqarg( match ), i18n("Back reference regular expressions not supported"), QString::fromLocal8Bit("backReferenceNotSupported") ); } | TOK_PosWordChar { $$ = new PositionRegExp( false, PositionRegExp::WORDBOUNDARY ); } | TOK_PosNonWordChar { $$ = new PositionRegExp( false, PositionRegExp::NONWORDBOUNDARY ); } ; char : TOK_Char { if ( $1 == '{' || $1 == '}' || $1 == '[' || $1 == ']' || $1 == '\\' ) { yyerror( "illigal character - needs escaping" ); } $$ = new TextRegExp( false, TQString(TQString::fromLocal8Bit("%1")).tqarg($1)); } | TOK_EscapeChar { $$ = new TextRegExp( false, TQString(TQString::fromLocal8Bit("%1")).tqarg($1)); } ; %% RegExp* parseQtRegExp( QString qstr, bool* ok ) { _index = 0; parseResult = 0; setParseData( qstr ); yyparse(); *ok = ( yynerrs == 0 ); return parseResult; } void setParseResult( RegExp* regexp ) { parseResult = regexp; } int yyerror(const char *) { yynerrs++; return 0; }