gnucemitter.java
来自「plugin for eclipse」· Java 代码 · 共 2,964 行 · 第 1/5 页
JAVA
2,964 行
// $ANTLR : "expandedGnuCEmitter.g" -> "GnuCEmitter.java"$
package isis.anp.gnuc;
import antlr.TreeParser;
import antlr.Token;
import antlr.collections.AST;
import antlr.RecognitionException;
import antlr.ANTLRException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.collections.impl.BitSet;
import antlr.ASTPair;
import antlr.collections.impl.ASTArray;
import isis.anp.common.LineObject;
import isis.anp.common.PreprocessorInfoChannel;
import isis.anp.common.TNode;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Stack;
import java.util.Vector;
import antlr.MismatchedTokenException;
import antlr.NoViableAltException;
import antlr.RecognitionException;
import antlr.collections.AST;
import antlr.collections.impl.BitSet;
public class GnuCEmitter extends antlr.TreeParser implements GnuCEmitterTokenTypes
{
int tabs = 0;
PrintStream currentOutput = System.out;
int lineNum = 1;
String currentSource = "";
LineObject trueSourceFile;
final int lineDirectiveThreshold = Integer.MAX_VALUE;
PreprocessorInfoChannel preprocessorInfoChannel = null;
Stack sourceFiles = new Stack();
public GnuCEmitter( PreprocessorInfoChannel preprocChannel )
{
preprocessorInfoChannel = preprocChannel;
}
void initializePrinting()
{
Vector preprocs = preprocessorInfoChannel.extractLinesPrecedingTokenNumber( new Integer(1) );
printPreprocs(preprocs);
/* if ( currentSource.equals("") ) {
trueSourceFile = new LineObject(currentSource);
currentOutput.println("# 1 \"" + currentSource + "\"\n");
sourceFiles.push(trueSourceFile);
}
*/
}
void finalizePrinting() {
// flush any leftover preprocessing instructions to the stream
printPreprocs(
preprocessorInfoChannel.extractLinesPrecedingTokenNumber(
new Integer( preprocessorInfoChannel.getMaxTokenNumber() + 1 ) ));
//print a newline so file ends at a new line
currentOutput.println();
}
void printPreprocs( Vector preprocs )
{
// if there was a preprocessingDirective previous to this token then
// print a newline and the directive, line numbers handled later
if ( preprocs.size() > 0 ) {
if ( trueSourceFile != null ) {
currentOutput.println(); //make sure we're starting a new line unless this is the first line directive
}
lineNum++;
Enumeration e = preprocs.elements();
while (e.hasMoreElements())
{
Object o = e.nextElement();
if ( o.getClass().getName().equals("LineObject") ) {
LineObject l = (LineObject) o;
// we always return to the trueSourceFile, we never enter it from another file
// force it to be returning if in fact we aren't currently in trueSourceFile
if (( trueSourceFile != null ) //trueSource exists
&& ( !currentSource.equals(trueSourceFile.getSource()) ) //currently not in trueSource
&& ( trueSourceFile.getSource().equals(l.getSource()) ) ) { //returning to trueSource
l.setEnteringFile( false );
l.setReturningToFile( true );
}
// print the line directive
currentOutput.println(l);
lineNum = l.getLine();
currentSource = l.getSource();
// the very first line directive always represents the true sourcefile
if ( trueSourceFile == null ) {
trueSourceFile = new LineObject(currentSource);
sourceFiles.push(trueSourceFile);
}
// keep our own stack of files entered
if ( l.getEnteringFile() ) {
sourceFiles.push(l);
}
// if returning to a file, pop the exited files off the stack
if ( l.getReturningToFile() ) {
LineObject top = (LineObject) sourceFiles.peek();
while (( top != trueSourceFile ) && (! l.getSource().equals(top.getSource()) )) {
sourceFiles.pop();
top = (LineObject) sourceFiles.peek();
}
}
}
else { // it was a #pragma or such
currentOutput.println(o);
lineNum++;
}
}
}
}
void print( TNode t ) {
int tLineNum = t.getLocalLineNum();
if ( tLineNum == 0 ) tLineNum = lineNum;
Vector preprocs = preprocessorInfoChannel.extractLinesPrecedingTokenNumber((Integer)t.getAttribute("tokenNumber"));
printPreprocs(preprocs);
if ( (lineNum != tLineNum) ) {
// we know we'll be newlines or a line directive or it probably
// is just the case that this token is on the next line
// either way start a new line and indent it
currentOutput.println();
lineNum++;
printTabs();
}
if ( lineNum == tLineNum ){
// do nothing special, we're at the right place
}
else {
int diff = tLineNum - lineNum;
if ( lineNum < tLineNum ) {
// print out the blank lines to bring us up to right line number
for ( ; lineNum < tLineNum ; lineNum++ ) {
currentOutput.println();
}
printTabs();
}
else { // just reset lineNum
lineNum = tLineNum;
}
}
currentOutput.print( t.getText() + " " );
}
/* This was my attempt at being smart about line numbers
It didn't work quite right but I don't know why, I didn't
have enough test cases. Worked ok compiling rcs and ghostscript
*/
void printAddingLineDirectives( TNode t ) {
int tLineNum = t.getLocalLineNum();
String tSource = (String) t.getAttribute("source");
if ( tSource == null ) tSource = currentSource;
if ( tLineNum == 0 ) tLineNum = lineNum;
Vector preprocs = preprocessorInfoChannel.extractLinesPrecedingTokenNumber((Integer)t.getAttribute("tokenNumber"));
printPreprocs(preprocs);
if ( (lineNum != tLineNum) || !currentSource.equals(tSource) ) {
// we know we'll be newlines or a line directive or it probably
// is just the case that this token is on the next line
// either way start a new line and indent it
currentOutput.println();
lineNum++;
printTabs();
}
if ( ( lineNum == tLineNum ) && ( currentSource.equals(tSource) ) ){
// do nothing special, we're at the right place
}
else if ( currentSource.equals(tSource) ) {
int diff = tLineNum - lineNum;
if (diff > 0 && diff < lineDirectiveThreshold) {
// print out the blank lines to bring us up to right line number
for ( ; lineNum < tLineNum ; lineNum++ ) {
currentOutput.println();
}
}
else { // print line directive to get us to right line number
// preserve flags 3 and 4 if present in current file
if ( ! sourceFiles.empty() ) {
LineObject l = (LineObject) sourceFiles.peek();
StringBuffer tFlags = new StringBuffer("");
if (l.getSystemHeader()) {
tFlags.append(" 3");
}
if (l.getTreatAsC()) {
tFlags.append(" 4");
}
currentOutput.println("# " + tLineNum + " \"" + tSource + "\"" + tFlags.toString());
lineNum = tLineNum;
}
}
printTabs();
}
else { // different source
Enumeration sources = sourceFiles.elements();
// see if we're returning to a file we entered earlier
boolean returningToEarlierFile = false;
while (sources.hasMoreElements()) {
LineObject l = (LineObject) sources.nextElement();
if (l.getSource().equals(tSource)) {
returningToEarlierFile = true;
break;
}
}
if (returningToEarlierFile) {
// pop off the files we're exiting, but never pop the trueSourceFile
LineObject l = (LineObject) sourceFiles.peek();
while ( ( l != trueSourceFile ) &&(! l.getSource().equals(tSource) ) ) {
sourceFiles.pop();
l = (LineObject) sourceFiles.peek();
}
// put in the return flag, plus others as needed
StringBuffer tFlags = new StringBuffer(" 2");
if (l.getSystemHeader()) {
tFlags.append(" 3");
}
if (l.getTreatAsC()) {
tFlags.append(" 4");
}
currentOutput.println("# " + tLineNum + " \"" + tSource + "\"" + tFlags);
lineNum = tLineNum;
currentSource = tSource;
printTabs();
}
else { // entering a file that wasn't in the original source
// pretend we're entering it from top of stack
currentOutput.println("# " + tLineNum + " \"" + tSource + "\"" + " 1");
lineNum = tLineNum;
currentSource = tSource;
printTabs();
}
}
currentOutput.print( t.getText() + " " );
}
/** It is not ok to print newlines from the String passed in as
it will screw up the line number handling **/
void print( String s ) {
currentOutput.print( s + " " );
}
void printTabs() {
for ( int i = 0; i< tabs; i++ ) {
currentOutput.print( "\t" );
}
}
void commaSep( TNode t ) {
print( t );
if ( t.getNextSibling() != null ) {
print( "," );
}
}
int traceDepth = 0;
public void reportError(RecognitionException ex) {
if ( ex != null) {
System.err.println("ANTLR Tree Parsing RecognitionException Error: " + ex.getClass().getName() + " " + ex );
ex.printStackTrace(System.err);
}
}
public void reportError(NoViableAltException ex) {
System.err.println("ANTLR Tree Parsing NoViableAltException Error: " + ex.toString());
TNode.printTree( ex.node );
ex.printStackTrace(System.err);
}
public void reportError(MismatchedTokenException ex) {
if ( ex != null) {
TNode.printTree( ex.node );
System.err.println("ANTLR Tree Parsing MismatchedTokenException Error: " + ex );
ex.printStackTrace(System.err);
}
}
public void reportError(String s) {
System.err.println("ANTLR Error from String: " + s);
}
public void reportWarning(String s) {
System.err.println("ANTLR Warning from String: " + s);
}
protected void match(AST t, int ttype) throws MismatchedTokenException {
//System.out.println("match("+ttype+"); cursor is "+t);
super.match(t, ttype);
}
public void match(AST t, BitSet b) throws MismatchedTokenException {
//System.out.println("match("+b+"); cursor is "+t);
super.match(t, b);
}
protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
//System.out.println("matchNot("+ttype+"); cursor is "+t);
super.matchNot(t, ttype);
}
public void traceIn(String rname, AST t) {
traceDepth += 1;
for (int x=0; x<traceDepth; x++) System.out.print(" ");
super.traceIn(rname, t);
}
public void traceOut(String rname, AST t) {
for (int x=0; x<traceDepth; x++) System.out.print(" ");
super.traceOut(rname, t);
traceDepth -= 1;
}
public GnuCEmitter() {
tokenNames = _tokenNames;
}
public final void translationUnit(AST _t) throws RecognitionException {
TNode translationUnit_AST_in = (_t == ASTNULL) ? null : (TNode)_t;
if ( inputState.guessing==0 ) {
initializePrinting();
}
{
if (_t==null) _t=ASTNULL;
switch ( _t.getType()) {
case LITERAL_asm:
case SEMI:
case NDeclaration:
case NFunctionDef:
case NTypeMissing:
{
externalList(_t);
_t = _retTree;
break;
}
case 3:
{
break;
}
default:
{
throw new NoViableAltException(_t);
}
}
}
if ( inputState.guessing==0 ) {
finalizePrinting();
}
_retTree = _t;
}
public final void externalList(AST _t) throws RecognitionException {
TNode externalList_AST_in = (_t == ASTNULL) ? null : (TNode)_t;
try { // for error handling
{
int _cnt2562=0;
_loop2562:
do {
if (_t==null) _t=ASTNULL;
if ((_tokenSet_0.member(_t.getType()))) {
externalDef(_t);
_t = _retTree;
}
else {
if ( _cnt2562>=1 ) { break _loop2562; } else {throw new NoViableAltException(_t);}
}
_cnt2562++;
} while (true);
}
}
catch (RecognitionException ex) {
if (inputState.guessing==0) {
reportError(ex);
if (_t!=null) {_t = _t.getNextSibling();}
} else {
throw ex;
}
}
_retTree = _t;
}
public final void externalDef(AST _t) throws RecognitionException {
TNode externalDef_AST_in = (_t == ASTNULL) ? null : (TNode)_t;
TNode s = null;
try { // for error handling
if (_t==null) _t=ASTNULL;
switch ( _t.getType()) {
case NDeclaration:
{
declaration(_t);
_t = _retTree;
break;
}
case NFunctionDef:
{
functionDef(_t);
_t = _retTree;
break;
}
case LITERAL_asm:
{
asm_expr(_t);
_t = _retTree;
break;
}
case NTypeMissing:
{
typelessDeclaration(_t);
_t = _retTree;
break;
}
case SEMI:
{
s = (TNode)_t;
match(_t,SEMI);
_t = _t.getNextSibling();
if ( inputState.guessing==0 ) {
print( s );
}
break;
}
default:
{
throw new NoViableAltException(_t);
}
}
}
catch (RecognitionException ex) {
if (inputState.guessing==0) {
reportError(ex);
if (_t!=null) {_t = _t.getNextSibling();}
} else {
throw ex;
}
}
_retTree = _t;
}
public final void declaration(AST _t) throws RecognitionException {
TNode declaration_AST_in = (_t == ASTNULL) ? null : (TNode)_t;
TNode s = null;
try { // for error handling
AST __t2570 = _t;
TNode tmp1_AST_in = (TNode)_t;
match(_t,NDeclaration);
_t = _t.getFirstChild();
declSpecifiers(_t);
_t = _retTree;
{
if (_t==null) _t=ASTNULL;
switch ( _t.getType()) {
case NInitDecl:
{
initDeclList(_t);
_t = _retTree;
break;
}
case SEMI:
{
break;
}
default:
{
throw new NoViableAltException(_t);
}
}
}
{
int _cnt2573=0;
_loop2573:
do {
if (_t==null) _t=ASTNULL;
if ((_t.getType()==SEMI)) {
s = (TNode)_t;
match(_t,SEMI);
_t = _t.getNextSibling();
if ( inputState.guessing==0 ) {
print( s );
}
}
else {
if ( _cnt2573>=1 ) { break _loop2573; } else {throw new NoViableAltException(_t);}
}
_cnt2573++;
} while (true);
}
_t = __t2570;
_t = _t.getNextSibling();
}
catch (RecognitionException ex) {
if (inputState.guessing==0) {
reportError(ex);
if (_t!=null) {_t = _t.getNextSibling();}
} else {
throw ex;
}
}
_retTree = _t;
}
public final void functionDef(AST _t) throws RecognitionException {
TNode functionDef_AST_in = (_t == ASTNULL) ? null : (TNode)_t;
TNode v = null;
try { // for error handling
AST __t2680 = _t;
TNode tmp2_AST_in = (TNode)_t;
match(_t,NFunctionDef);
_t = _t.getFirstChild();
{
if (_t==null) _t=ASTNULL;
switch ( _t.getType()) {
case LITERAL_volatile:
case LITERAL_struct:
case LITERAL_union:
case LITERAL_enum:
case LITERAL_extern:
case LITERAL_static:
case LITERAL_const:
case LITERAL_void:
case LITERAL_char:
case LITERAL_short:
case LITERAL_int:
case LITERAL_long:
case LITERAL_float:
case LITERAL_double:
case LITERAL_signed:
case LITERAL_unsigned:
case NTypedefName:
case LITERAL_inline:
case LITERAL_typeof:
case LITERAL___complex:
{
functionDeclSpecifiers(_t);
_t = _retTree;
break;
}
case NDeclarator:
{
break;
}
default:
{
throw new NoViableAltException(_t);
}
}
}
declarator(_t);
_t = _retTree;
{
_loop2683:
do {
if (_t==null) _t=ASTNULL;
switch ( _t.getType()) {
case NDeclaration:
{
declaration(_t);
_t = _retTree;
break;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?