⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 token_stream_selector.sa

📁 SRI international 发布的OAA框架软件
💻 SA
字号:
(* 

  ANTLR Translator Generator
  Project led by Terence Parr at http://www.jGuru.com
  Software rights: http://www.antlr.org/RIGHTS.html
 
  $Id: token_stream_selector.sa,v 1.1.1.1 2002/01/02 19:59:09 agno Exp $

*)

-- A token stream MUX (multiplexor) knows about n token streams
-- and can multiplex them onto the same channel for use by token
-- stream consumer like a parser.  This is a way to have multiple
-- lexers break up the same input stream for a single parser.
-- Or, you can have multiple instances of the same lexer handle
-- multiple input streams; this works great for includes.

class ANTLR_TOKEN_STREAM_SELECTOR{TOKEN < $ANTLR_TOKEN} < $ANTLR_TOKEN_STREAM{TOKEN} is

   -- The set of inputs to the MUX 
   private attr name_to_stream : MAP{STR,$ANTLR_TOKEN_STREAM{TOKEN}};

   -- The currently-selected token stream input 
   attr current_stream : $ANTLR_TOKEN_STREAM{TOKEN};

   -- Used to track stack of input streams 
   private attr stream_stack : NR_A_STACK{$ANTLR_TOKEN_STREAM{TOKEN}};

   create : SAME is
      res ::= new;
      res.name_to_stream := #MAP{STR,$ANTLR_TOKEN_STREAM{TOKEN}};
      res.stream_stack := #NR_A_STACK{$ANTLR_TOKEN_STREAM{TOKEN}};
      return res;
   end;

   add_input_stream( stream : $ANTLR_TOKEN_STREAM{TOKEN}, key : STR ) is
      name_to_stream[key] := stream;
   end;

   stream( sname : STR ) : $ANTLR_TOKEN_STREAM{TOKEN} is
      stream_ ::= name_to_stream[sname];
      if ( void(stream_) ) then
	 raise "$ANTLR_TOKEN_STREAM " + sname + " not found";
      end;
      return stream_;
   end;

   -- keep looking for a token until you don't
   -- get a retry exception.
   next_token : TOKEN is
      loop 
	 protect
	    return current_stream.next_token;
	 when ANTLR_TOKEN_STREAM_RETRY_EXCEPTION then
	    -- just retry forever
	 end;
      end;
   end;

   pop : $ANTLR_TOKEN_STREAM{TOKEN} is
      stream_ ::= stream_stack.pop;
      select(stream_);
      return stream_;
   end;
   
   pop is
      stream_ ::= stream_stack.pop;
      select(stream_);
   end;

   push( stream_ : $ANTLR_TOKEN_STREAM{TOKEN} ) is
      stream_stack.push(current_stream); -- save current stream
      select(stream_);
   end;

   push( name : STR ) is
      stream_stack.push(current_stream);
      select(name);
   end;

   -- Abort recognition of current Token and try again.
   -- A stream can push a new stream (for include files
   -- for example, and then retry, which will cause
   -- the current stream to abort back to self.next_token.
   -- self.next_token then asks for a token from the
   -- current stream, which is the new "substream."
   retry is
      raise #ANTLR_TOKEN_STREAM_RETRY_EXCEPTION;
   end;
   
   -- Set the stream without pushing old stream 
   select( stream_ : $ANTLR_TOKEN_STREAM{TOKEN} ) is
      current_stream := stream_;
   end;

   select( name : STR ) is
      current_stream := stream( name );
   end;
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -