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

📄 actionlexer.java

📁 Java写的词法/语法分析器。可生成JAVA语言或者是C++的词法和语法分析器。并可产生语法分析树和对该树进行遍历
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
// $ANTLR 2.7.0a11: action.g -> ActionLexer.java$package antlr.actions.sather;import java.io.InputStream;import antlr.TokenStreamException;import antlr.TokenStreamIOException;import antlr.TokenStreamRecognitionException;import antlr.CharStreamException;import antlr.CharStreamIOException;import antlr.ANTLRException;import java.io.Reader;import java.util.Hashtable;import antlr.CharScanner;import antlr.InputBuffer;import antlr.ByteBuffer;import antlr.CharBuffer;import antlr.Token;import antlr.CommonToken;import antlr.RecognitionException;import antlr.NoViableAltForCharException;import antlr.MismatchedCharException;import antlr.TokenStream;import antlr.ANTLRHashString;import antlr.LexerSharedInputState;import antlr.collections.impl.BitSet;import java.io.StringReader;import antlr.collections.impl.Vector;import antlr.*;/** Perform the following translations:    AST related translations	@@			-> currentRule_AST	@(x,y,z)		-> codeGenerator.getASTCreateString(vector-of(x,y,z))	@[x]			-> codeGenerator.getASTCreateString(x)	@x			-> codeGenerator.mapTreeId(x)	Inside context of @(...), you can ref (x,y,z), [x], and x as shortcuts.    Text related translations	%append(x)	-> text.append(x)	%setText(x)	-> text.setLength(sa_begin); text.append(x)	%getText	-> new String(text.getBuffer(),sa_begin,text.length()-sa_begin)	%setToken(x)	-> sa_token = x	%setType(x)	-> sa_ttype = x */public class ActionLexer extends antlr.CharScanner implements ActionLexerTokenTypes, TokenStream {	protected RuleBlock currentRule;	protected CodeGenerator generator;	protected int lineOffset = 0;	private Tool tool;	// The ANTLR tool	ActionTransInfo transInfo; 	public ActionLexer( String s,						RuleBlock currentRule,						CodeGenerator generator,						ActionTransInfo transInfo) {		this(new StringReader(s));		this.currentRule = currentRule;		this.generator = generator;		this.transInfo = transInfo;	}	public void setLineOffset(int lineOffset) {		// this.lineOffset = lineOffset;		setLine(lineOffset);	}	public void setTool(Tool tool) {		this.tool = tool;	}	// Override of error-reporting for syntax	public void reportError(RecognitionException e) {		System.err.print("Syntax error in action: ");		super.reportError(e);	}public ActionLexer(InputStream in) {	this(new ByteBuffer(in));}public ActionLexer(Reader in) {	this(new CharBuffer(in));}public ActionLexer(InputBuffer ib) {	this(new LexerSharedInputState(ib));}public ActionLexer(LexerSharedInputState state) {	super(state);	literals = new Hashtable();caseSensitiveLiterals = true;setCaseSensitive(true);}public Token nextToken() throws TokenStreamException {	Token theRetToken=null;tryAgain:	for (;;) {		Token _token = null;		int _ttype = Token.INVALID_TYPE;		resetText();		try {   // for char stream error handling			try {   // for lexical error handling				if (((LA(1) >= '\3' && LA(1) <= '~'))) {					mACTION(true);					theRetToken=_returnToken;				}				else {					if (LA(1)==EOF_CHAR) {uponEOF(); _returnToken = makeToken(Token.EOF_TYPE);}				else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}				}								if ( _returnToken==null ) continue tryAgain; // found SKIP token				_ttype = _returnToken.getType();				_returnToken.setType(_ttype);				return _returnToken;			}			catch (RecognitionException e) {				reportError(e);				consume();			}		}		catch (CharStreamException cse) {			if ( cse instanceof CharStreamIOException ) {				throw new TokenStreamIOException(((CharStreamIOException)cse).io);			}			else {				throw new TokenStreamException(cse.getMessage());			}		}	}}	public final void mACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {		int _ttype; Token _token=null; int _begin=text.length();		_ttype = ACTION;		int _saveIndex;				{		int _cnt3=0;		_loop3:		do {			switch ( LA(1)) {			case '\3':  case '\4':  case '\5':  case '\6':			case '\7':  case '\10':  case '\t':  case '\n':			case '\13':  case '\14':  case '\r':  case '\16':			case '\17':  case '\20':  case '\21':  case '\22':			case '\23':  case '\24':  case '\25':  case '\26':			case '\27':  case '\30':  case '\31':  case '\32':			case '\33':  case '\34':  case '\35':  case '\36':			case '\37':  case ' ':  case '!':  case '"':			case '#':  case '$':  case '&':  case '\'':			case '(':  case ')':  case '*':  case '+':			case ',':  case '-':  case '.':  case '/':			case '0':  case '1':  case '2':  case '3':			case '4':  case '5':  case '6':  case '7':			case '8':  case '9':  case ':':  case ';':			case '<':  case '=':  case '>':  case '?':			case 'A':  case 'B':  case 'C':  case 'D':			case 'E':  case 'F':  case 'G':  case 'H':			case 'I':  case 'J':  case 'K':  case 'L':			case 'M':  case 'N':  case 'O':  case 'P':			case 'Q':  case 'R':  case 'S':  case 'T':			case 'U':  case 'V':  case 'W':  case 'X':			case 'Y':  case 'Z':  case '[':  case '\\':			case ']':  case '^':  case '_':  case '`':			case 'a':  case 'b':  case 'c':  case 'd':			case 'e':  case 'f':  case 'g':  case 'h':			case 'i':  case 'j':  case 'k':  case 'l':			case 'm':  case 'n':  case 'o':  case 'p':			case 'q':  case 'r':  case 's':  case 't':			case 'u':  case 'v':  case 'w':  case 'x':			case 'y':  case 'z':  case '{':  case '|':			case '}':  case '~':			{				mSTUFF(false);				break;			}			case '@':			{				mAST_ITEM(false);				break;			}			case '%':			{				mTEXT_ITEM(false);				break;			}			default:			{				if ( _cnt3>=1 ) { break _loop3; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}			}			}			_cnt3++;		} while (true);		}		if ( _createToken && _token==null && _ttype!=Token.SKIP ) {			_token = makeToken(_ttype);			_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));		}		_returnToken = _token;	}		protected final void mSTUFF(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {		int _ttype; Token _token=null; int _begin=text.length();		_ttype = STUFF;		int _saveIndex;				switch ( LA(1)) {		case '(':		{			match('(');			{			if ((LA(1)=='*') && ((LA(2) >= '\3' && LA(2) <= '~'))) {				match('*');				{				_loop7:				do {					// nongreedy exit test					if ((LA(1)=='*') && (LA(2)==')')) break _loop7;					if ((LA(1)=='\r') && (LA(2)=='\n')) {						match('\r');						match('\n');						newline();					}					else if ((LA(1)=='\r') && ((LA(2) >= '\3' && LA(2) <= '~'))) {						match('\r');						newline();					}					else if ((LA(1)=='\n') && ((LA(2) >= '\3' && LA(2) <= '~'))) {						match('\n');						newline();					}					else if (((LA(1) >= '\3' && LA(1) <= '~')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {						matchNot(EOF_CHAR);					}					else {						break _loop7;					}									} while (true);				}				match("*)");			}			else {			}						}			break;		}		case '-':		{			match('-');			{			if ((LA(1)=='-') && ((LA(2) >= '\3' && LA(2) <= '~'))) {				match('-');				{				_loop10:				do {					// nongreedy exit test					if ((LA(1)=='\n'||LA(1)=='\r') && (true)) break _loop10;					if (((LA(1) >= '\3' && LA(1) <= '~')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {						matchNot(EOF_CHAR);					}					else {						break _loop10;					}									} while (true);				}				{				if ((LA(1)=='\r') && (LA(2)=='\n')) {					match("\r\n");				}				else if ((LA(1)=='\n')) {					match('\n');				}				else if ((LA(1)=='\r') && (true)) {					match('\r');				}				else {					throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());				}								}				newline();			}			else {			}						}			break;		}		case '"':		{			mSTRING(false);			break;		}		case '\'':		{			mCHAR(false);			break;		}		case '\n':		{			match('\n');			newline();			break;		}		case '\3':  case '\4':  case '\5':  case '\6':		case '\7':  case '\10':  case '\t':  case '\13':		case '\14':  case '\16':  case '\17':  case '\20':		case '\21':  case '\22':  case '\23':  case '\24':		case '\25':  case '\26':  case '\27':  case '\30':		case '\31':  case '\32':  case '\33':  case '\34':		case '\35':  case '\36':  case '\37':  case ' ':		case '!':  case '#':  case '$':  case '&':		case ')':  case '*':  case '+':  case ',':		case '.':  case '/':  case '0':  case '1':		case '2':  case '3':  case '4':  case '5':		case '6':  case '7':  case '8':  case '9':		case ':':  case ';':  case '<':  case '=':		case '>':  case '?':  case 'A':  case 'B':		case 'C':  case 'D':  case 'E':  case 'F':		case 'G':  case 'H':  case 'I':  case 'J':		case 'K':  case 'L':  case 'M':  case 'N':		case 'O':  case 'P':  case 'Q':  case 'R':		case 'S':  case 'T':  case 'U':  case 'V':		case 'W':  case 'X':  case 'Y':  case 'Z':		case '[':  case '\\':  case ']':  case '^':		case '_':  case '`':  case 'a':  case 'b':		case 'c':  case 'd':  case 'e':  case 'f':		case 'g':  case 'h':  case 'i':  case 'j':		case 'k':  case 'l':  case 'm':  case 'n':		case 'o':  case 'p':  case 'q':  case 'r':		case 's':  case 't':  case 'u':  case 'v':		case 'w':  case 'x':  case 'y':  case 'z':		case '{':  case '|':  case '}':  case '~':		{			{			match(_tokenSet_0);			}			break;		}		default:			if ((LA(1)=='\r') && (LA(2)=='\n')) {				match("\r\n");				newline();			}			else if ((LA(1)=='\r') && (true)) {				match('\r');				newline();			}		else {			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());		}		}		if ( _createToken && _token==null && _ttype!=Token.SKIP ) {			_token = makeToken(_ttype);			_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));		}		_returnToken = _token;	}		protected final void mAST_ITEM(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {		int _ttype; Token _token=null; int _begin=text.length();		_ttype = AST_ITEM;		int _saveIndex;		Token t=null;		Token id=null;		Token ctor=null;				if ((LA(1)=='@') && (LA(2)=='(')) {			_saveIndex=text.length();			match('@');			text.setLength(_saveIndex);			mTREE(true);			t=_returnToken;		}		else if ((LA(1)=='@') && (_tokenSet_1.member(LA(2)))) {			_saveIndex=text.length();			match('@');			text.setLength(_saveIndex);			mID(true);			id=_returnToken;								String idt = id.getText();					text.setLength(_begin); text.append(generator.mapTreeId(idt,transInfo));								{			if ((_tokenSet_2.member(LA(1))) && (_tokenSet_3.member(LA(2)))) {				{				switch ( LA(1)) {				case '\t':  case '\n':  case '\r':  case ' ':				{					mWS(false);					break;				}				case ':':				{					break;				}				default:				{					throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());				}				}				}				mVAR_ASSIGN(false);			}			else {			}						}		}		else if ((LA(1)=='@') && (LA(2)=='[')) {			_saveIndex=text.length();			match('@');			text.setLength(_saveIndex);			mAST_CONSTRUCTOR(true);			ctor=_returnToken;		}		else if ((LA(1)=='@') && (LA(2)=='@')) {			match("@@");								String r=currentRule.getRuleName()+"_AST"; text.setLength(_begin); text.append(r);					if ( transInfo!=null ) {						transInfo.refRuleRoot=r;	// we ref root of tree					}								{			if ((_tokenSet_2.member(LA(1))) && (_tokenSet_3.member(LA(2)))) {				{				switch ( LA(1)) {				case '\t':  case '\n':  case '\r':  case ' ':				{					mWS(false);					break;				}				case ':':				{					break;				}				default:				{					throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());				}				}				}				mVAR_ASSIGN(false);			}			else {			}						}		}		else {			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());		}				if ( _createToken && _token==null && _ttype!=Token.SKIP ) {			_token = makeToken(_ttype);			_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));		}		_returnToken = _token;	}		protected final void mTEXT_ITEM(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {		int _ttype; Token _token=null; int _begin=text.length();		_ttype = TEXT_ITEM;		int _saveIndex;		Token a1=null;		Token a2=null;		Token a3=null;		Token a4=null;				if ((LA(1)=='%') && (LA(2)=='a')) {			match("%append(");			mTEXT_ARG(true);			a1=_returnToken;			match(')');			String t = "text := text.append("+a1.getText()+")"; text.setLength(_begin); text.append(t);		}		else if ((LA(1)=='%') && (LA(2)=='s')) {			match("%set");			{			if ((LA(1)=='T') && (LA(2)=='e')) {				match("Text(");				mTEXT_ARG(true);				a2=_returnToken;				match(')');											String t;							t = "text := text.substring( 0, sa_begin ) + " + a2.getText();							text.setLength(_begin); text.append(t);										}			else if ((LA(1)=='T') && (LA(2)=='o')) {				match("Token(");				mTEXT_ARG(true);				a3=_returnToken;				match(')');											String t="_token = "+a3.getText();							text.setLength(_begin); text.append(t);										}			else if ((LA(1)=='T') && (LA(2)=='y')) {				match("Type(");				mTEXT_ARG(true);				a4=_returnToken;				match(')');											String t="sa_ttype := "+a4.getText();							text.setLength(_begin); text.append(t);										}			else {				throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());			}						}		}		else if ((LA(1)=='%') && (LA(2)=='g')) {			match("%getText");									text.setLength(_begin); text.append("text.substring( sa_begin, text.length - sa_begin )");							}		else {			throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());		}				if ( _createToken && _token==null && _ttype!=Token.SKIP ) {			_token = makeToken(_ttype);			_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));		}		_returnToken = _token;	}		protected final void mSTRING(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {		int _ttype; Token _token=null; int _begin=text.length();		_ttype = STRING;		int _saveIndex;				match('"');		{		_loop85:		do {

⌨️ 快捷键说明

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