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

Re: SableCC event driven?




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

--- src\org\sablecc\sablecc\xss2\data\java\parser.xss	2004-02-02 09:20:09.884777600 +0100
+++ src\org\sablecc\sablecc\xss2\data\java\events\parser.xss	2004-02-02 15:36:40.398340800 +0100
@@ -94,7 +94,6 @@
      protected ArrayList nodeList;

  $ end if
-    private final Lexer lexer;
      private final ListIterator stack = new LinkedList().listIterator();
      private int last_shift;
      private int last_pos;
@@ -108,9 +107,7 @@
      private final static int ACCEPT = 2;
      private final static int ERROR = 3;

-    public Parser(Lexer lexer)
-    {
-        this.lexer = lexer;
+    public Parser() {
      }

  $ if {not(@inlined)}
@@ -180,62 +177,61 @@
          return converter.index;
      }

-    public Start parse() throws ParserException, LexerException, IOException
-    {
+    public Start parse(Lexer pLexer) throws ParserException, LexerException, IOException {
+    	parseStart();
+    	for (;;) {
+    		Start node = addToken(pLexer.next());
+    		if (node != null) {
+    			return null;
+    		}
+    	}
+    }
+
+    private List ign;
+    public void parseStart() throws ParserException, LexerException, IOException {
          push(0, null);
+        ign = null;
+    }

-        List ign = null;
-        while(true)
-        {
-            while(index(lexer.peek()) == -1)
-            {
-                if(ign == null)
-                {
+	public Start addToken(Token pToken) throws ParserException, LexerException, IOException {
+		int index = index(pToken);
+		if (index == -1) {
+		    if(ign == null) {
                      ign = new TypedLinkedList(NodeCast.instance);
                  }
-
-                ign.add(lexer.next());
+			ign.add(pToken);
+			return null;
              }

-            if(ign != null)
-            {
-                ignoredTokens.setIn(lexer.peek(), ign);
-                ign = null;
+        if (ign != null  &&  ign.size() > 0) {
+        	ignoredTokens.setIn(pToken, ign);
+            ign.clear();
              }

-            last_pos = lexer.peek().getPos();
-            last_line = lexer.peek().getLine();
-            last_token = lexer.peek();
+		last_pos = pToken.getPos();
+        last_line = pToken.getLine();
+        last_token = pToken;

-            int index = index(lexer.peek());
              action[0] = actionTable[state()][0][1];
              action[1] = actionTable[state()][0][2];

              int low = 1;
              int high = actionTable[state()].length - 1;

-            while(low <= high)
-            {
+        while (low <= high) {
                  int middle = (low + high) / 2;
-
-                if(index < actionTable[state()][middle][0])
-                {
+			if (index < actionTable[state()][middle][0]) {
                      high = middle - 1;
-                }
-                else if(index > actionTable[state()][middle][0])
-                {
+            } else if(index > actionTable[state()][middle][0]) {
                      low = middle + 1;
-                }
-                else
-                {
+            } else {
                      action[0] = actionTable[state()][middle][1];
                      action[1] = actionTable[state()][middle][2];
                      break;
                  }
              }

-            switch(action[0])
-            {
+        switch(action[0]) {
                case SHIFT:
                {
  $ if {@inlined}
@@ -243,18 +239,17 @@
  $ else
                  nodeList = new ArrayList(1);
  $ end if
-                nodeList.add(lexer.next());
+              nodeList.add(pToken);
  $ if {not(@inlined)}
                  filter();
  $ end if
                  push(action[1], nodeList);
                  last_shift = action[1];
-                break;
+              return null;
                }
                case REDUCE:
                {
-                switch(action[1])
-                {
+			  switch(action[1]) {
  $ foreach {rules/rule}
                    case @index:
                    {
@@ -271,22 +266,20 @@
                    }
  $ end foreach
                  }
-                break;
+              return addToken(pToken);
                }
                case ACCEPT:
                {
-                EOF node2 = (EOF) lexer.next();
+              EOF node2 = (EOF) pToken;
                  ${/parser/prods/prod/@ename} node1 = (${/parser/prods/prod/@ename}) ((ArrayList)pop()).get(0);
-                Start node = new Start(node1, node2);
-                return node;
+              return new Start(node1, node2);
                }
-              case ERROR:
+          default:
                  throw new ParserException(last_token,
                    "[" + last_line + "," + last_pos + "] " +
                    errorMessages[errors[action[1]]]);
              }
          }
-    }

  $ foreach {rules/rule}
      ArrayList new@index()