xslpattern.l 5.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 *    XSLPattern lexer
 *
 * Copyright 2010 Adam Martinson for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

%{
#include "config.h"
#include "wine/port.h"

#ifdef HAVE_LIBXML2

#include "xslpattern.h"
#include "xslpattern.tab.h"
29
#include "wine/debug.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

WINE_DEFAULT_DEBUG_CHANNEL(msxml);

#define SCAN    xslpattern_get_extra(yyscanner)

#define YY_INPUT(tok_buf, tok_len, max) \
        do { \
            if (SCAN->pos <= SCAN->len) \
            { \
                tok_len = SCAN->len - SCAN->pos; \
                if (tok_len > max) tok_len = max; \
                memcpy(tok_buf, SCAN->in + SCAN->pos, tok_len); \
                SCAN->pos += tok_len; \
            } \
            else \
            { \
                tok_len = YY_NULL; \
            } \
        } while (0);

#define TOK(tok)    TRACE("token: %s : %s\n", #tok, yytext); return tok
#define OP(tok)     *yylval=NULL; TOK(tok)
#define SYM(tok)    *yylval=NULL; TOK(tok)
#define STR(tok)    *yylval=xmlStrdup(BAD_CAST yytext); TOK(tok)


%}

%option reentrant bison-bridge
%option noyywrap
%option prefix="xslpattern_"
61
%option noinput nounput never-interactive
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

/* From the w3c XML standard
 * <http://www.w3.org/TR/REC-xml/> */

    /* [2.3] Common Syntactic Constructs */
WSpace          ([[:space:]])

NCNameStartChar ([A-Za-z_]|[\xc0-\xd6\xd8-\xf6\xf8-\xff])

NameCharEx      ([0-9]|[-._\xb7])

NCNameChar      ({NCNameStartChar}|{NameCharEx})

/* From the w3c XML Namespace standard
 * <http://www.w3.org/TR/REC-xml-names/> */

    /* [3] Declaring Namespaces*/
NCName          ({NCNameStartChar}{NCNameChar}*)

/* Mostly verbatim from the w3c XPath standard.
 * <http://www.w3.org/TR/xpath/> */


    /* [3.4] Booleans
     * ||, &&, $foo$ are XSLPattern only */

OP_Or           ("or"|"||"|"$or$")
OP_And          ("and"|"&&"|"$and$")
OP_Eq           ("="|"$eq$")
OP_IEq          ("$ieq$")
OP_NEq          ("!="|"$ne$")
OP_INEq         ("$ine$")
OP_Lt           ("<"|"$lt$")
OP_ILt          ("$ilt$")
OP_Gt           (">"|"$gt$")
OP_IGt          ("$igt$")
OP_LEq          ("<="|"$le$")
OP_ILEq         ("$ile$")
OP_GEq          (">="|"$ge$")
OP_IGEq         ("$ige$")
OP_Not          ("$not$")
OP_All          ("$all$")
OP_Any          ("$any$")

    /* [3.7] Lexical Structure */
Literal             (([\x22]([^\x22]*)[\x22])|([\x27]([^\x27]*)[\x27]))
Number              ({Digits}("."{Digits}?)?|"."{Digits})
Digits              ([0-9]+)

ANY                 (.)

%%

{WSpace}+                   { /* ignored */ }
{Literal}                   { STR(TOK_Literal); }
"//"                        { SYM(TOK_DblFSlash); }
"/"                         { SYM(TOK_FSlash); }
".."                        { SYM(TOK_Parent); }
"."                         { SYM(TOK_Self); }
"::"                        { SYM(TOK_Axis); }
":"                         { SYM(TOK_Colon); }
"("                         { SYM('('); }
")"                         { SYM(')'); }
"["                         { SYM('['); }
"]"                         { SYM(']'); }
"@"                         { SYM('@'); }
","                         { SYM(','); }
"*"                         { SYM('*'); }
{OP_And}                    { OP(TOK_OpAnd); }
{OP_Or}                     { OP(TOK_OpOr); }
{OP_Not}                    { OP(TOK_OpNot); }
{OP_Eq}                     { OP(TOK_OpEq); }
{OP_IEq}                    { OP(TOK_OpIEq); }
{OP_NEq}                    { OP(TOK_OpNEq); }
{OP_INEq}                   { OP(TOK_OpINEq); }
{OP_Lt}                     { OP(TOK_OpLt); }
{OP_ILt}                    { OP(TOK_OpILt); }
{OP_Gt}                     { OP(TOK_OpGt); }
{OP_IGt}                    { OP(TOK_OpIGt); }
{OP_LEq}                    { OP(TOK_OpLEq); }
{OP_ILEq}                   { OP(TOK_OpILEq); }
{OP_GEq}                    { OP(TOK_OpGEq); }
{OP_IGEq}                   { OP(TOK_OpIGEq); }
{OP_All}                    { OP(TOK_OpAll); }
{OP_Any}                    { OP(TOK_OpAny); }
"|"                         { SYM('|'); }
"!"                         { SYM('!'); }
{NCName}                    { STR(TOK_NCName); }
{Number}                    { STR(TOK_Number); }
151
{ANY}                       { FIXME("Unexpected character '%s'.\n",yytext); }
152 153 154

%%

155
xmlChar* XSLPattern_to_XPath(xmlXPathContextPtr ctxt, xmlChar const* xslpat_str)
156 157
{
    parser_param p;
158
    TRACE("(%s)\n", debugstr_a((char const*)xslpat_str));
159
    memset(&p, 0, sizeof(parser_param));
160
    p.ctx = ctxt;
161 162 163 164 165 166 167 168
    p.in = xslpat_str;
    p.len = xmlStrlen(xslpat_str);

    xslpattern_lex_init(&p.yyscanner);
    xslpattern_set_extra(&p, p.yyscanner);

    xslpattern_parse(&p, p.yyscanner);

169
    TRACE("=> %s\n", debugstr_a((char const*)p.out));
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    xslpattern_lex_destroy(p.yyscanner);

    if (p.err)
    {
        xmlFree(p.out);
        return xmlStrdup(xslpat_str);
    }
    else
    {
        return p.out;
    }

}

#endif /* HAVE_LIBXML2 */