token_stream_selector.sa
上传用户:afrynkmhm
上传日期:2007-01-06
资源大小:1262k
文件大小:3k
- (*
- ANTLR Translator Generator
- Project led by Terence Parr at http://www.jGuru.com
- Software rights: http://www.antlr.org/RIGHTS.html
-
- $Id: //depot/code/org.antlr/release/antlr-2.7.0/lib/sather/Antlr/token_stream_selector.sa#1 $
- *)
- -- 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;