📄 cpptree.g
字号:
tree grammar CppTree;options{ backtrack=true; tokenVocab=Cpp; ASTLabelType=CommonTree;}scope Macros { Map defines; }@header{ import java.util.Set; import java.lang.*; import java.util.HashSet; import java.util.Arrays;}@members{ protected String fileName; protected int lineNo=0; class ExpressionReturn { int value; String text; } class MacroParameter { int type; String text; } public CppTreeTreeParser(TreeNodeStream input,String filename) { this(input); this.fileName = filename; } protected static final String ONE = "1";// static global stack for macro definitions and macro call params.. public static Stack Macrox_stack = new Stack(); boolean isMacroDefined(String name) { for (int i = Macrox_stack.size()-1; i>=0; i--) { Macros_scope scope = (Macros_scope)Macrox_stack.get(i); if ( scope.defines.containsKey(name) ) { return true; } } return false; } Object getMacroObject(String name) { for (int i = Macrox_stack.size()-1; i>=0; i--) { Macros_scope scope = (Macros_scope)Macrox_stack.get(i); if ( scope.defines.containsKey(name) ) { return(scope.defines.get(name)); } } return null; } void putMacroObject(String name,Object Macro) { Macros_scope scope = (Macros_scope)Macrox_stack.get(0); if ( scope.defines.containsKey(name) ) { System.out.println("MACRO OBJECT " + name + "redefined "); } scope.defines.put(name,Macro); } void removeMacroObject(String name) { Macros_scope scope = (Macros_scope)Macrox_stack.get(0); if (!scope.defines.containsKey(name) ) { System.out.println("REMOVE undefined " + name ); } scope.defines.remove(name); } void putMacroParameter(String name,Object parameter) { ((Macros_scope)Macrox_stack.peek()).defines.put(name, parameter); } public int Mod(int a, int b) { int c; if(a<b) return a; c = (a/b); return (a - (c*b)); }// Functions form cdt.. protected String removedEscapedNewline(String macro_text) { int start, len; char text[] = macro_text.toCharArray(); start = 0; len = text.length; if (CharArrayUtils.indexOf('\n', text, start, start + len) == -1) return new String(text); char[] result = new char[len]; Arrays.fill(result, ' '); int counter = 0; for (int i = start; i < start + len; ++i) { if (text[i] == '\\' && i + 1 < text.length && text[i + 1] == '\n') ++i; else if (text[i] == '\\' && i + 1 < text.length && text[i + 1] == '\r' && i + 2 < text.length && text[i + 2] == '\n') i += 2; else result[counter++] = text[i]; } return new String(CharArrayUtils.trim(result)); } protected String removeComment(String macro_text) { char[] text = macro_text.toCharArray(); char[] result = new char[text.length]; Arrays.fill(result, ' '); int resultCount = 0; for (int i = 0; i < text.length; ++i) { if (text[i] == '/' && (i+1 <text.length) && text[i+1] == '/') { break; // Line Comment; } if (text[i] == '/' && (i + 1 < text.length) && text[i + 1] == '*') { i += 2; while (i < text.length && !(text[i] == '*' && i + 1 < text.length && text[i + 1] == '/')) ++i; ++i; } else result[resultCount++] = text[i]; } return new String(CharArrayUtils.trim(result)); } final static public String trim(String text) { if (text == null) return null; char[] chars =text.toCharArray(); int start = 0, length = chars.length, end = length - 1; while (end > start && (chars[end] == ' '||chars[end]== '\t')) { end--; } if (end == 0) return new String(""); if( end != length - 1) { return new String(CharArrayUtils.subarray(chars, start, end + 1)); } return new String(chars); } }preprocess @init { Macrox_stack.push(new Macros_scope()); ((Macros_scope)Macrox_stack.peek()).defines = new HashMap();} : procLine[true]+ { Macrox_stack.pop(); } ;procLine [boolean condition] : ( fileInclusion [condition] | macroDefine [condition] | macroUndef [condition] | conditionalCompilation [condition] | lineControl [condition] | diagnostics [condition] | macroExecution | txt=text_line [condition,true] ) ? e=End { if (condition == true) System.out.println(" // line " + e.getLine() + " " + fileName); } ;fileInclusion [ boolean condition]@init { String filename=null; } : ( s=INCLUDE { filename = s.toString(); } | inc=INCLUDE_EXPAND { if(isMacroDefined(inc.toString())) { Object expObject = getMacroObject(inc.toString()); if(expObject instanceof ObjectMacro) { filename = ((ObjectMacro)expObject).getExpansion(); } } else { filename = inc.toString(); } // modify filename which is local path... } ) { if(condition == true) { try { CppLexer iLexer = new CppLexer(new ANTLRFileStream(filename)); TokenRewriteStream itokens = new TokenRewriteStream(iLexer); itokens.LT(1); CppParser iParser = new CppParser(itokens); CppParser.preprocess_return iret = iParser.preprocess(); CommonTreeNodeStream inodes = new CommonTreeNodeStream((Tree)iret.tree); CppTreeTreeParser iwalker = new CppTreeTreeParser(inodes,filename); iwalker.preprocess(); } catch (Exception ex) { System.out.println(ex); } } } ;macroDefine [boolean condition]@init { String macro_text = null; List params = new ArrayList();} : ^(MAC_FUNCTION_OBJECT id=IDENTIFIER m=macroText) { if(condition == true) { FunctionMacro fncMac = new FunctionMacro(id.toString(),m.toString()); putMacroObject(id.toString(),fncMac); } } | ^(MAC_FUNCTION id=IDENTIFIER ( p=macroParam {params.add(p); })+ m=macroText) { if(condition == true) { FunctionMacro fncMac = new FunctionMacro(id.toString(),params,m.toString()); putMacroObject(id.toString(),fncMac); } } | ^(MAC_OBJECT id=IDENTIFIER m=macroText) { if(condition == true) { ObjectMacro objMac = new ObjectMacro($id.toString(),m.toString()); putMacroObject(id.toString(),objMac); } } ;macroParam returns [MacroParameter param]@init {param = new MacroParameter();} : ^(ELLIPSIS i=IDENTIFIER) { param.type = EXP_ARGS; param.text = i.getText(); } | e=ELLIPSIS { param.type = ELLIPSIS; param.text = new String("__VA_ARGS__"); } | i=IDENTIFIER { param.type = IDENTIFIER; param.text = i.getText(); } ;macroText returns [String mtext]@init { mtext = new String(""); } : ^(MACRO_DEFINE (src=source_text[false] { mtext += src; } )+) { mtext = removedEscapedNewline(mtext); mtext = trim(mtext); } | {$mtext = new String(" ");} ;macroUndef [boolean condition] : ^(UNDEF mac=IDENTIFIER) { if(condition == true) { removeMacroObject(mac.toString()); } } ;conditionalCompilation [boolean condition]@init { boolean ifMatched=false; boolean subCondition=false; } : ^(IF ( a=expression { if(ifMatched == true ) subCondition = false; else subCondition = ((a != 0 ) ? true: false); if(subCondition == true) ifMatched =true; } statement[condition && subCondition ] )+ (ELSE statement[condition && (!ifMatched)])?) ; lineControl [boolean condition] @init{ String file=null;} : ^( LINE n=DECIMAL_LITERAL ( s=STRING_LITERAL { file = s.toString(); } | { file = null; } ) ) { if(condition == true) { if(file == null) System.out.println("LINE : "+$n.toString()); else System.out.println("LINE : "+$n.toString() +" FILE: "+ file); } } ;diagnostics [boolean condition] : s=WARNING { if(condition) { System.out.println("warning: " + s); } } | s=ERROR { if(condition) { System.out.println("error: " + s); } } | s=PRAGMA { if(condition) { System.out.println(s); } } ;text_line[boolean condition,boolean out] returns [String text]@init { text = new String(""); } : ^(TEXT_LINE (src=source_text[true] { if(condition == true) { text += src; if(out) { System.out.print(src); } } } )+) ;statement[boolean condition] : procLine[condition]+ ;source_text[boolean expand] returns [String stext]@int { stext ="";} : ( m=macroExpansion[expand] { stext = m; } | m=primarySource[expand] { stext = m; } | m=concatenate[expand] { stext = m; } | t=SEMICOLON { stext = t.getText(); } | t=COMMA { stext = t.getText(); } | t=RPAREN { stext = t.getText(); } | t=LPAREN { stext = t.getText(); } | t=STRING_OP { stext = "#_#_" + t.getText(); } | t=WS { stext = t.getText(); } | TEXT_GROUP { stext = "()"; } | t=TEXT_END { if(expand) { stext = t.getText(); ++lineNo; } else stext =""; } | ^(TEXT_GROUP lst=macArgs[expand]) { stext = "("; for (int i=0; i<lst.size(); i++) { ExpressionReturn exp = (ExpressionReturn)lst.get(i); stext += exp.text; if( i != lst.size()-1) stext += ","; } stext += ")"; } | ^(STRINGIFICATION m=primarySource[expand]) { stext = "\"" + m + "\"";} // make String function to handle Escape sequence... ) ;concatenate[boolean expand] returns [String stext]@init { stext = ""; int i=0; } : ^(CONCATENATE (a=primarySource[expand] { if(expand == false) { if(i==0) stext = a; else stext = stext + " ## " + a; i++; } else stext += a; } )+ ) ;primarySource[boolean expand] returns [String stext]@int { stext ="";} : c=constant[false] { stext = c.text; } | t=COPERATOR { stext = t.getText(); } | t=CKEYWORD { stext = t.getText(); } | t=SIZEOF { stext = t.getText(); } | ^(SHARPSHARP id=IDENTIFIER) { if(expand) { if(isMacroDefined(id.toString())) { Object expObject = getMacroObject(id.toString()); if( expObject instanceof ExpressionReturn) { stext = ((ExpressionReturn)expObject).text; if(stext.equals("__VA_ARGS__")) stext = "__VA_ARGS_EMPTY__"; } else { System.out.println("Not Expected Argument Type : " + id.toString()); stext = "##" + id.toString() ; } } else { stext = "##" + id.toString() ; } } else { stext = "##" + id.toString() ; // make String function to handle Escape sequence... } } | id=IDENTIFIER { if(expand) { if(isMacroDefined(id.toString())) { Object expObject = getMacroObject(id.toString()); if(expObject instanceof ObjectMacro) { stext = ((ObjectMacro)expObject).getExpansion(); } else if( expObject instanceof ExpressionReturn) { stext = ((ExpressionReturn)expObject).text; } else if( expObject instanceof FunctionMacro) { stext = id.toString() ; } } else { stext = id.toString(); } } else { stext = id.toString(); } } ;macroExpansion[boolean expand] returns [String r] : ^(EXPAND id=IDENTIFIER ) { if(expand) { if(isMacroDefined(id.toString())) { Object expObject = getMacroObject(id.toString()); if(expObject instanceof FunctionMacro) { Macrox_stack.push(new Macros_scope()); ((Macros_scope)Macrox_stack.peek()).defines = new HashMap(); FunctionMacro expMacro = (FunctionMacro)expObject; if(expMacro.haveArgs()) { MacroParameter parm; List formalArgs = expMacro.getArgList(); int argSize = formalArgs.size(); parm=(MacroParameter)formalArgs.get(argSize-1); ExpressionReturn variadics = new ExpressionReturn(); variadics.text = "__VA_ARGS__"; if( argSize == 1 && (parm.type == ELLIPSIS || parm.type == EXP_ARGS)) { putMacroParameter(((parm.type == ELLIPSIS)? "__VA_ARGS__":parm.text), variadics); r = expMacro.getExpansion(); } else { System.out.println("Not Exist Parameter List"); r = id.toString() + "()"; } } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -