Hi,

>>Hmm, I think there is no preprocessor directive for jay. And those
>>files are compiled at a time in shape of .cs files.
> 
> By that I mean, maybe we could add support for such a directive. I would
> think that it would not be too hard of an addition.

Okay. So I made tiny patch for jay (am so lazy to make big changes).
Attached patch for jay adds "-d BlahBlah" to insert #define BlahBlah
on top of the output. (It is somewhat C# specific, but users could
just ignore this option when they don't want to use it.)

With this jay + simple sed solution, now my patch became compact.

If no further suggestions and problem on my patch, I'll checkin
them later.

Atsushi Eno
Index: output.c
===================================================================
--- output.c    (revision 41902)
+++ output.c    (working copy)
@@ -57,6 +57,7 @@
 static int lowzero;
 static int high;
 extern int csharp;
+extern char* cmdline_def;
 
 output () {
   int lno = 0;
@@ -66,6 +67,9 @@
   free_shifts();
   free_reductions();
 
+  if (cmdline_def)
+       output_cmdline_def();
+
   while (fgets(buf, sizeof buf, stdin) != NULL) {
     char * cp;
     ++ lno;
@@ -98,6 +102,11 @@
   free_parser();
 }
 
+output_cmdline_def()
+{
+       printf ("#define %s\n", cmdline_def);
+}
+
 output_rule_data()
 {
     register int i;
Index: main.c
===================================================================
--- main.c      (revision 41902)
+++ main.c      (working copy)
@@ -50,6 +50,7 @@
 char tflag;
 char vflag;
 int csharp = 0;
+char *cmdline_def;
 
 char *file_prefix = "y";
 char *myname = "yacc";
@@ -135,7 +136,7 @@
 
 usage()
 {
-    fprintf(stderr, "usage: %s [-tvcp] [-b file_prefix] filename\n", myname);
+    fprintf(stderr, "usage: %s [-tvcp] [-b file_prefix] [-d condition] 
filename\n", myname);
     exit(1);
 }
 
@@ -178,6 +179,15 @@
                usage();
            continue;
 
+       case 'd':
+           if (*++s)
+               cmdline_def = s;
+           else if (++i < argc)
+               cmdline_def = argv[i];
+           else
+               usage();
+           continue;
+
         case 't':
             tflag = 1;
             break;
Index: Mono.Xml.XPath/Pattern.cs
===================================================================
--- Mono.Xml.XPath/Pattern.cs   (revision 41902)
+++ Mono.Xml.XPath/Pattern.cs   (working copy)
@@ -43,7 +43,7 @@
        {
                internal static Pattern Compile (string s, Compiler comp)
                {               
-                       return Compile (comp.parser.Compile (s));
+                       return Compile (comp.patternParser.Compile (s));
                }
                
                internal static Pattern Compile (Expression e)
Index: System.Xml.XPath/Parser.jay
===================================================================
--- System.Xml.XPath/Parser.jay (revision 41921)
+++ System.Xml.XPath/Parser.jay (working copy)
@@ -1,23 +1,41 @@
 %{
-// XPath parser
+// XPath/XSLT Pattern parser
 //
-// Author - Piers Haken <[EMAIL PROTECTED]>
+// Author: Piers Haken <[EMAIL PROTECTED]>
+//         Atsushi Enomoto  <[EMAIL PROTECTED]>
 //
+// IMPORTANT:
+// Do not edit "PatternParser.jay". It is autogenerated from
+// Parser.jay. It will be overwritten!
+//
 
 using System;
 using System.Collections;
 using System.Xml;
 using System.Xml.XPath;
 
-namespace Mono.Xml.XPath
+#if XSLT_PATTERN
+namespace System.Xml.Xsl
+#else
+namespace System.Xml.XPath
+#endif
 {
+#if XSLT_PATTERN
+       internal class XsltPatternParser
+#else
        internal class XPathParser
+#endif
        {
        
                internal System.Xml.Xsl.IStaticXsltContext Context;
                
+#if XSLT_PATTERN
+               public XsltPatternParser () : this (null) {}
+               internal XsltPatternParser (System.Xml.Xsl.IStaticXsltContext 
context)
+#else
                public XPathParser () : this (null) {}
                internal XPathParser (System.Xml.Xsl.IStaticXsltContext context)
+#endif
                {
                        Context = context;
                        ErrorOutput = System.IO.TextWriter.Null;
@@ -29,7 +47,7 @@
                        try {
                                Tokenizer tokenizer = new Tokenizer (xpath);
                                return (Expression) yyparse (tokenizer);
-                       } catch (XPathException e) {
+                       } catch (XPathException) {
                                throw;
                        } catch (Exception e) {
                                throw new XPathException ("Error during parse 
of " + xpath, e);
@@ -125,7 +143,7 @@
 %token QName
 
 
-%start Expr
+%start Pattern
 
 
 %left AND
@@ -144,8 +162,117 @@
 
 %%
 
+/* XSLT Pattern */
 
+Pattern
+       : LocationPathPattern
+       | Pattern BAR LocationPathPattern
+       {
+               $$ = new ExprUNION ((NodeSet) $1, (NodeSet) $3);
+       }
+       ;
 
+LocationPathPattern
+       : SLASH
+       {
+               $$ = new ExprRoot ();
+       }
+       | SLASH RelativePathPattern
+       {
+               $$ = new ExprSLASH (new ExprRoot (), (NodeSet) $2);
+       }
+       | IdKeyPattern
+       | IdKeyPattern SLASH RelativePathPattern
+       {
+               $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
+       }
+       | IdKeyPattern SLASH2 RelativePathPattern
+       {
+               $$ = new ExprSLASH2 ((Expression) $1, (NodeSet) $3);
+       }
+       | SLASH2 RelativePathPattern
+       {
+               $$ = new ExprSLASH2 (new ExprRoot (), (NodeSet) $2);
+       }
+       | RelativePathPattern
+       ;
+
+// to avoid context-sensitive tokenizer, I just reuse FUNCTION_NAME
+IdKeyPattern
+       : FUNCTION_NAME PAREN_OPEN LITERAL PAREN_CLOSE
+       {
+               XmlQualifiedName name = (XmlQualifiedName) $1;
+               if (name.Name != "id" || name.Namespace != String.Empty)
+                       throw new XPathException (String.Format ("Expected 'id' 
but got '{0}'", name));
+               $$ = ExprFunctionCall.Factory (name,
+                       new FunctionArguments (
+                               new ExprLiteral ((string) $3),
+                               null),
+                       Context);
+       }
+       | FUNCTION_NAME PAREN_OPEN LITERAL COMMA LITERAL PAREN_CLOSE
+       {
+               XmlQualifiedName name = (XmlQualifiedName) $1;
+               if (name.Name != "key" || name.Namespace != String.Empty)
+                       throw new XPathException (String.Format ("Expected 
'key' but got '{0}'", name));
+               $$ = Context.TryGetFunction (name,
+                       new FunctionArguments (
+                               new ExprLiteral ((string) $3),
+                               new FunctionArguments (
+                                       new ExprLiteral ((string) $5),
+                                       null)));
+       }
+       ;
+
+RelativePathPattern
+       : StepPattern
+       | RelativePathPattern SLASH StepPattern
+       {
+               $$ = new ExprSLASH ((Expression) $1, (NodeSet) $3);
+       }
+       | RelativePathPattern SLASH2 StepPattern
+       {
+               $$ = new ExprSLASH2 ((Expression) $1, (NodeSet) $3);
+       }
+       ;
+
+StepPattern
+       : ChildOrAttributeAxisSpecifier NodeTest Predicates
+       {
+               $$ = CreateNodeTest ((Axes) $1, $2, (ArrayList) $3);
+       }
+       ;
+
+ChildOrAttributeAxisSpecifier
+       : AbbreviatedAxisSpecifier
+       | CHILD COLON2
+       {
+               $$ = Axes.Child;
+       }
+       | ATTRIBUTE COLON2
+       {
+               $$ = Axes.Attribute;
+       }
+       ;
+
+Predicates
+       : // empty
+       {
+               $$ = null;
+       }
+       | Predicates Predicate
+       {
+               ArrayList al = (ArrayList) $1;
+               if (al == null)
+                       al = new ArrayList ();
+               al.Add ((Expression) $2);
+               $$ = al;
+       }
+       ;
+
+/* ---- end of XSLT Pattern ---- */
+
+
 Expr
        : OrExpr
        ;
Index: System.Xml.XPath/Tokenizer.cs
===================================================================
--- System.Xml.XPath/Tokenizer.cs       (revision 41902)
+++ System.Xml.XPath/Tokenizer.cs       (working copy)
@@ -1,11 +1,17 @@
 //
-// System.Xml.XPath.Tokenizer
+// System.Xml.XPath.Tokenizer.cs / Mono.Xml.Xsl/PatternTokenizer.cs
 //
 // Author:
 //   Piers Haken ([EMAIL PROTECTED])
+//   Atsushi Enomoto ([EMAIL PROTECTED])
 //
 // (C) 2002 Piers Haken
+// (C) 2005 Novell Inc,
 //
+// IMPORTANT:
+//
+// Do not edit PatternTokenizer.cs. It is autogenerated.
+//
 
 //
 // Permission is hereby granted, free of charge, to any person obtaining
@@ -32,12 +38,16 @@
 using System.IO;
 using System.Text;
 using System.Collections;
+using System.Xml.XPath;
 using Mono.Xml.XPath;
-using Mono.Xml.XPath.yyParser;
 
+#if XSLT_PATTERN
+namespace System.Xml.Xsl
+#else
 namespace System.Xml.XPath
+#endif
 {
-       internal class Tokenizer : Mono.Xml.XPath.yyParser.yyInput
+       internal class Tokenizer : yyParser.yyInput
        {
                private string m_rgchInput;
                private int m_ich;
Index: Mono.Xml.Xsl/Compiler.cs
===================================================================
--- Mono.Xml.Xsl/Compiler.cs    (revision 41902)
+++ Mono.Xml.Xsl/Compiler.cs    (working copy)
@@ -127,7 +127,8 @@
        
                public CompiledStylesheet Compile (XPathNavigator nav, 
XmlResolver res, Evidence evidence)
                {
-                       this.parser = new XPathParser (this);
+                       this.xpathParser = new XPathParser (this);
+                       this.patternParser = new XsltPatternParser (this);
                        this.res = res;
                        if (res == null)
                                this.res = new XmlUrlResolver ();
@@ -350,7 +351,8 @@
                        return p;
                }
 
-               internal XPathParser parser;
+               internal XPathParser xpathParser;
+               internal XsltPatternParser patternParser;
                internal CompiledExpression CompileExpression (string 
expression)
                {
                        return CompileExpression (expression, false);
@@ -360,7 +362,7 @@
                {
                        if (expression == null || expression == "") return null;
 
-                       Expression expr = parser.Compile (expression);
+                       Expression expr = xpathParser.Compile (expression);
                        if (isKey)
                                expr = new ExprKeyContainer (expr);
                        CompiledExpression e = new CompiledExpression 
(expression, expr);
Index: Makefile
===================================================================
--- Makefile    (revision 41902)
+++ Makefile    (working copy)
@@ -40,7 +40,6 @@
        $(wildcard System.Xml.Serialization/standalone_tests/*.cs) \
        $(wildcard System.Xml.Serialization/standalone_tests/*.output) \
        System.Xml.XPath/Parser.jay     \
-       System.Xml.Query/XQueryParser.jay       \
        System.Xml.Query/skeleton-2.0.cs        \
        Test/Microsoft.Test.csproj      \
        Test/Mono.Test.csproj           \
@@ -50,14 +49,32 @@
        Test/XmlFiles/xsl/empty.xsl     \
        Test/XmlFiles/xsl/ChangeLog     \
        Test/XmlFiles/ChangeLog
+#      System.Xml.Query/XQueryParser.jay       \
 
 System.Xml.XPath/Parser.cs: System.Xml.XPath/Parser.jay 
$(topdir)/jay/skeleton.cs
        $(topdir)/jay/jay -ct < $(topdir)/jay/skeleton.cs $< >$@
 
-System.Xml.Query/XQueryParser.cs: System.Xml.Query/XQueryParser.jay 
System.Xml.Query/skeleton-2.0.cs
-       $(topdir)/jay/jay -ct < System.Xml.Query/skeleton-2.0.cs $< >$@
+Mono.Xml.Xsl/PatternParser.jay: System.Xml.XPath/Parser.jay 
$(topdir)/jay/skeleton.cs
+       sed "s/\%start Expr/\%start Pattern/" $< >$@
 
-BUILT_SOURCES = System.Xml.XPath/Parser.cs #System.Xml.Query/XQueryParser.cs
-CLEAN_FILES = Test/XmlFiles/xsl/result.xml System.Xml.Query/XQueryParser.cs
+Mono.Xml.Xsl/PatternParser.cs: Mono.Xml.Xsl/PatternParser.jay 
$(topdir)/jay/skeleton.cs
+       $(topdir)/jay/jay -ct -d XSLT_PATTERN < $(topdir)/jay/skeleton.cs $< >$@
 
+Mono.Xml.Xsl/PatternTokenizer.cs: System.Xml.XPath/Tokenizer.cs
+       echo "#define XSLT_PATTERN" > $@
+       cat $< >>$@
+
+#System.Xml.Query/XQueryParser.cs: System.Xml.Query/XQueryParser.jay 
System.Xml.Query/skeleton-2.0.cs
+#      $(topdir)/jay/jay -ct < System.Xml.Query/skeleton-2.0.cs $< >$@
+
+BUILT_SOURCES = System.Xml.XPath/Parser.cs \
+       Mono.Xml.Xsl/PatternParser.cs \
+       Mono.Xml.Xsl/PatternTokenizer.cs
+       #System.Xml.Query/XQueryParser.cs
+CLEAN_FILES = Test/XmlFiles/xsl/result.xml \
+       System.Xml.XPath/Parser.cs \
+       Mono.Xml.Xsl/PatternParser.cs \
+       Mono.Xml.Xsl/PatternTokenizer.cs
+       #System.Xml.Query/XQueryParser.cs
+
 include ../../build/library.make

Reply via email to