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

📄 actiontranslatorlexer.java

📁 ANTLR(ANother Tool for Language Recognition)它是这样的一种工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// $ANTLR 3.0b5 ActionTranslator.g 2006-11-08 09:57:01package org.antlr.codegen;import org.antlr.stringtemplate.StringTemplate;import org.antlr.runtime.*;import org.antlr.tool.*;import org.antlr.runtime.*;import java.util.Stack;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;public class ActionTranslatorLexer extends Lexer {    public static final int LOCAL_ATTR=17;    public static final int SET_DYNAMIC_SCOPE_ATTR=18;    public static final int ISOLATED_DYNAMIC_SCOPE=24;    public static final int WS=5;    public static final int UNKNOWN_SYNTAX=35;    public static final int DYNAMIC_ABSOLUTE_INDEXED_SCOPE_ATTR=23;    public static final int SCOPE_INDEX_EXPR=21;    public static final int DYNAMIC_SCOPE_ATTR=19;    public static final int ISOLATED_TOKEN_REF=14;    public static final int SET_ATTRIBUTE=30;    public static final int SET_EXPR_ATTRIBUTE=29;    public static final int ACTION=27;    public static final int ERROR_X=34;    public static final int TEMPLATE_INSTANCE=26;    public static final int TOKEN_SCOPE_ATTR=10;    public static final int ISOLATED_LEXER_RULE_REF=15;    public static final int ESC=32;    public static final int SET_ENCLOSING_RULE_SCOPE_ATTR=7;    public static final int ATTR_VALUE_EXPR=6;    public static final int RULE_SCOPE_ATTR=12;    public static final int LABEL_REF=13;    public static final int INT=37;    public static final int ARG=25;    public static final int EOF=-1;    public static final int SET_LOCAL_ATTR=16;    public static final int TEXT=36;    public static final int Tokens=38;    public static final int DYNAMIC_NEGATIVE_INDEXED_SCOPE_ATTR=22;    public static final int SET_TOKEN_SCOPE_ATTR=9;    public static final int ERROR_SCOPED_XY=20;    public static final int SET_RULE_SCOPE_ATTR=11;    public static final int ENCLOSING_RULE_SCOPE_ATTR=8;    public static final int ERROR_XY=33;    public static final int TEMPLATE_EXPR=31;    public static final int INDIRECT_TEMPLATE_INSTANCE=28;    public static final int ID=4;    public List chunks = new ArrayList();    Rule enclosingRule;    int outerAltNum;    Grammar grammar;    CodeGenerator generator;    antlr.Token actionToken;    	public ActionTranslatorLexer(CodeGenerator generator,    								 String ruleName,    								 GrammarAST actionAST)    	{    		this(new ANTLRStringStream(actionAST.token.getText()));    		this.generator = generator;    		this.grammar = generator.grammar;    	    this.enclosingRule = grammar.getRule(ruleName);    	    this.actionToken = actionAST.token;    	    this.outerAltNum = actionAST.outerAltNum;    	}    	public ActionTranslatorLexer(CodeGenerator generator,    								 String ruleName,    								 antlr.Token actionToken,    								 int outerAltNum)    	{    		this(new ANTLRStringStream(actionToken.getText()));    		this.generator = generator;    		grammar = generator.grammar;    	    this.enclosingRule = grammar.getRule(ruleName);    	    this.actionToken = actionToken;    		this.outerAltNum = outerAltNum;    	}    /*    public ActionTranslatorLexer(CharStream input, CodeGenerator generator,                                 Grammar grammar, Rule enclosingRule,                                 antlr.Token actionToken, int outerAltNum)    {        this(input);        this.grammar = grammar;        this.generator = generator;        this.enclosingRule = enclosingRule;        this.actionToken = actionToken;        this.outerAltNum = outerAltNum;    }    */    /** Return a list of strings and StringTemplate objects that     *  represent the translated action.     */    public List translateToChunks() {    	// System.out.println("###\naction="+action);    	Token t;    	do {    		t = nextToken();    	} while ( t.getType()!= Token.EOF );    	return chunks;    }    public String translate() {    	List theChunks = translateToChunks();    	//System.out.println("chunks="+a.chunks);    	StringBuffer buf = new StringBuffer();    	for (int i = 0; i < theChunks.size(); i++) {    		Object o = (Object) theChunks.get(i);    		buf.append(o);    	}    	//System.out.println("translated: "+buf.toString());    	return buf.toString();    }    public List translateAction(String action) {        ActionTranslatorLexer translator =            new ActionTranslatorLexer(generator,                                      enclosingRule.name,                                      new antlr.CommonToken(ANTLRParser.ACTION,action),1);        return translator.translateToChunks();    }    public boolean isTokenRefInAlt(String id) {        return enclosingRule.getTokenRefsInAlt(id, outerAltNum)!=null;    }    public boolean isRuleRefInAlt(String id) {        return enclosingRule.getRuleRefsInAlt(id, outerAltNum)!=null;    }    public Grammar.LabelElementPair getElementLabel(String id) {        return enclosingRule.getLabel(id);    }    public void checkElementRefUniqueness(String ref, boolean isToken) {    		List refs = null;    		if ( isToken ) {    		    refs = enclosingRule.getTokenRefsInAlt(ref, outerAltNum);    		}    		else {    		    refs = enclosingRule.getRuleRefsInAlt(ref, outerAltNum);    		}    		if ( refs!=null && refs.size()>1 ) {    			ErrorManager.grammarError(ErrorManager.MSG_NONUNIQUE_REF,    									  grammar,    									  actionToken,    									  ref);    		}    }    /** For $rulelabel.name, return the Attribute found for name.  It     *  will be a predefined property or a return value.     */    public Attribute getRuleLabelAttribute(String ruleName, String attrName) {    	Rule r = grammar.getRule(ruleName);    	AttributeScope scope = null;    	// is it a return value?    	if ( r.returnScope!=null && r.returnScope.getAttribute(attrName)!=null ) {    		scope = r.returnScope;    	}    	else if ( grammar.type != Grammar.LEXER &&    		 RuleLabelScope.predefinedRulePropertiesScope.getAttribute(attrName)!=null )    	{    		scope = RuleLabelScope.predefinedRulePropertiesScope;    	}    	else if ( grammar.type == Grammar.LEXER &&    		 RuleLabelScope.predefinedLexerRulePropertiesScope.getAttribute(attrName)!=null )    	{    		scope = RuleLabelScope.predefinedLexerRulePropertiesScope;    	}    	if ( scope!=null ) {    		return scope.getAttribute(attrName);    	}    	return null;    }    AttributeScope resolveDynamicScope(String scopeName) {    	if ( grammar.getGlobalScope(scopeName)!=null ) {    		return grammar.getGlobalScope(scopeName);    	}    	Rule scopeRule = grammar.getRule(scopeName);    	if ( scopeRule!=null ) {    		return scopeRule.ruleScope;    	}    	return null; // not a valid dynamic scope    }    protected StringTemplate template(String name) {    	StringTemplate st = generator.getTemplates().getInstanceOf(name);    	chunks.add(st);    	return st;    }    public ActionTranslatorLexer() {;}     public ActionTranslatorLexer(CharStream input) {        super(input);        ruleMemo = new HashMap[62+1];     }    public String getGrammarFileName() { return "ActionTranslator.g"; }    public Token nextToken() {        while (true) {            if ( input.LA(1)==CharStream.EOF ) {                return Token.EOF_TOKEN;            }            token = null;            tokenStartCharIndex = getCharIndex();    	text = null;            try {                int m = input.mark();                backtracking=1;                 failed=false;                mTokens();                backtracking=0;                if ( failed ) {                    input.rewind(m);                    input.consume();                 }                else {                    return token;                }            }            catch (RecognitionException re) {                // shouldn't happen in backtracking mode, but...                reportError(re);                recover(re);            }        }    }    public void memoize(IntStream input,    		int ruleIndex,    		int ruleStartIndex)    {    if ( backtracking>1 ) super.memoize(input, ruleIndex, ruleStartIndex);    }    public boolean alreadyParsedRule(IntStream input, int ruleIndex) {    if ( backtracking>1 ) return super.alreadyParsedRule(input, ruleIndex);    return false;    }// $ANTLR start SET_ENCLOSING_RULE_SCOPE_ATTR    public void mSET_ENCLOSING_RULE_SCOPE_ATTR() throws RecognitionException {        try {            ruleNestingLevel++;            int _type = SET_ENCLOSING_RULE_SCOPE_ATTR;            int _start = getCharIndex();            int _line = getLine();            int _charPosition = getCharPositionInLine();            int _channel = Token.DEFAULT_CHANNEL;            // ActionTranslator.g:201:4: ( '$' x= ID '.' y= ID ( WS )? '=' expr= ATTR_VALUE_EXPR ';' {...}?)            // ActionTranslator.g:201:4: '$' x= ID '.' y= ID ( WS )? '=' expr= ATTR_VALUE_EXPR ';' {...}?            {            match('$'); if (failed) return ;            int xStart = getCharIndex();            mID(); if (failed) return ;            Token x = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, xStart, getCharIndex()-1);            match('.'); if (failed) return ;            int yStart = getCharIndex();            mID(); if (failed) return ;            Token y = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, yStart, getCharIndex()-1);            // ActionTranslator.g:201:22: ( WS )?            int alt1=2;            int LA1_0 = input.LA(1);            if ( ((LA1_0>='\t' && LA1_0<='\n')||LA1_0==' ') ) {                alt1=1;            }            switch (alt1) {                case 1 :                    // ActionTranslator.g:201:22: WS                    {                    mWS(); if (failed) return ;                    }                    break;            }            match('='); if (failed) return ;            int exprStart = getCharIndex();            mATTR_VALUE_EXPR(); if (failed) return ;            Token expr = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, exprStart, getCharIndex()-1);            match(';'); if (failed) return ;            if ( !(enclosingRule!=null &&            	                         x.getText().equals(enclosingRule.name) &&            	                         enclosingRule.getLocalAttributeScope(y.getText())!=null) ) {                if (backtracking>0) {failed=true; return ;}                throw new FailedPredicateException(input, "SET_ENCLOSING_RULE_SCOPE_ATTR", "enclosingRule!=null &&\n\t                         $x.text.equals(enclosingRule.name) &&\n\t                         enclosingRule.getLocalAttributeScope($y.text)!=null");            }            if ( backtracking==1 ) {              		StringTemplate st = null;              		AttributeScope scope = enclosingRule.getLocalAttributeScope(y.getText());              		if ( scope.isPredefinedRuleScope ) {              			if ( y.getText().equals("st") || y.getText().equals("tree") ) {              				st = template("ruleSetPropertyRef_"+y.getText());              				grammar.referenceRuleLabelPredefinedAttribute(x.getText());              				st.setAttribute("scope", x.getText());              				st.setAttribute("attr", y.getText());              				st.setAttribute("expr", translateAction(expr.getText()));              			} else {              				ErrorManager.grammarError(ErrorManager.MSG_WRITE_TO_READONLY_ATTR,              										  grammar,              										  actionToken,              										  x.getText(),              										  y.getText());              			}              		}              	    else if ( scope.isPredefinedLexerRuleScope ) {              	    	// this is a better message to emit than the previous one...              			ErrorManager.grammarError(ErrorManager.MSG_WRITE_TO_READONLY_ATTR,              									  grammar,              									  actionToken,              									  x.getText(),              									  y.getText());              	    }              		else if ( scope.isParameterScope ) {              			st = template("parameterSetAttributeRef");

⌨️ 快捷键说明

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