[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SableCC event driven?



Good work. I also looked into it and turned that patch into something clearer
for comparison. See attachment. Should be mostly the same thing.
I ignored some spacing to get a clearer diff.

How does everyone feel about this? Worth pursuing and adding into
the java backen as it is? Or should we change tha API, by splitting into
different classes?

Regards,
Indrek

On Monday 02 February 2004 16:50, Jochen Wiedmann wrote:
> Hi,
>
> Etienne Gagnon wrote:
> > I would think that
> >   svn://svn.sablecc.org/developers/indrek/tags/sablecc-indrek-20040124/
> > is the appropriate tag.
>
> attached you find a patch, which demonstrates what I am attempting to do.
> I have currently implemented it in a separate directory java/events, but
> as you will note from the patch: If I did it right, then it is quite
> easy to add the event support to the current parser.xss in a 100%
> upwards compatible fashion. In other words, I would suggest to drop
> the separate template directory in favour of my changes.
>
>
> Regards,
>
> Jochen
Index: src/org/sablecc/sablecc/xss2/data/java/parser.xss
===================================================================
--- src/org/sablecc/sablecc/xss2/data/java/parser.xss	(revision 1478)
+++ src/org/sablecc/sablecc/xss2/data/java/parser.xss	(working copy)
@@ -111,8 +111,11 @@
     public Parser(Lexer lexer)
     {
         this.lexer = lexer;
+        push(0, null);
     }
 
+    private List ign;
+
 $ if {not(@inlined)}
     protected void filter() throws ParserException, LexerException, IOException
     {
@@ -148,7 +151,7 @@
         return value;
     }
 
-    private void push(int numstate, ArrayList listNode) throws ParserException, LexerException, IOException
+    private void push(int numstate, ArrayList listNode)
     {
         if(!stack.hasNext())
         {
@@ -182,32 +185,43 @@
 
     public Start parse() throws ParserException, LexerException, IOException
     {
-        push(0, null);
+        if ( lexer == null )
+            throw new RuntimeException ("parse() called on event driven parser!");
 
-        List ign = null;
         while(true)
         {
-            while(index(lexer.peek()) == -1)
+            Start start = addToken (lexer.next());
+            if ( start != null ) return start;
+        }
+    }
+
+    public Start addToken(Token tok) throws ParserException, LexerException, IOException
+    {
+            if(index(tok) == -1)
             {
                 if(ign == null)
                 {
                     ign = new TypedLinkedList(NodeCast.instance);
                 }
 
-                ign.add(lexer.next());
+                ign.add(tok);
+                return null;
             }
 
             if(ign != null)
             {
-                ignoredTokens.setIn(lexer.peek(), ign);
+                ignoredTokens.setIn(tok, ign);
                 ign = null;
             }
 
-            last_pos = lexer.peek().getPos();
-            last_line = lexer.peek().getLine();
-            last_token = lexer.peek();
+            last_pos = tok.getPos();
+            last_line = tok.getLine();
+            last_token = tok;
 
-            int index = index(lexer.peek());
+            int index = index(tok);
+
+        while(true)
+        {
             action[0] = actionTable[state()][0][1];
             action[1] = actionTable[state()][0][2];
 
@@ -243,13 +257,13 @@
 $ else
                 nodeList = new ArrayList(1);
 $ end if
-                nodeList.add(lexer.next());
+                nodeList.add(tok);
 $ if {not(@inlined)}
                 filter();
 $ end if
                 push(action[1], nodeList);
                 last_shift = action[1];
-                break;
+                return null;
               }
               case REDUCE:
               {
@@ -275,7 +289,7 @@
               }
               case ACCEPT:
               {
-                EOF node2 = (EOF) lexer.next();
+                EOF node2 = (EOF) tok;
                 ${/parser/prods/prod/@ename} node1 = (${/parser/prods/prod/@ename}) ((ArrayList)pop()).get(0);
                 Start node = new Start(node1, node2);
                 return node;