org.objectweb.fractal.mind.st.BackendFormatRenderer Maven / Gradle / Ivy
/**
* Copyright (C) 2009 STMicroelectronics
*
* This file is part of "Mind Compiler" is free software: you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* Contact: [email protected]
*
* Authors: Matthieu Leclercq
* Contributors:
*/
package org.objectweb.fractal.mind.st;
import java.io.File;
import org.antlr.stringtemplate.AttributeRenderer;
public class BackendFormatRenderer implements AttributeRenderer {
public static final String TO_UPPER = "toUpper";
public static final String TO_C_NAME = "toCName";
public static final String TO_UPPER_C_NAME = "toUpperCName";
public static final String NAME_TO_PATH = "nameToPath";
public static final String PATH_TO_C_NAME = "pathToCName";
public static final String PATH_TO_UPPER_C_NAME = "pathToUpperCName";
public static final String SOURCE_TO_LINE = "sourceTo#line";
public String toString(final Object o) {
return o.toString();
}
public String toString(final Object o, final String formatName) {
if (TO_UPPER.equals(formatName)) {
return o.toString().toUpperCase();
} else if (TO_C_NAME.equals(formatName)) {
return o.toString().replace('.', '_');
} else if (TO_UPPER_C_NAME.equals(formatName)) {
return o.toString().replace('.', '_').toUpperCase();
} else if (NAME_TO_PATH.equals(formatName)) {
return o.toString().replace('.', File.separatorChar);
} else if (PATH_TO_C_NAME.equals(formatName)) {
return o.toString().replace('/', '_').replace('.', '_');
} else if (PATH_TO_UPPER_C_NAME.equals(formatName)) {
return o.toString().replace('/', '_').replace('.', '_').toUpperCase();
} else if (SOURCE_TO_LINE.equals(formatName)) {
final String source = o.toString();
final int i = source.lastIndexOf(':');
if (i == -1) return "";
final String inputFilePath = source.substring(0, i);
final int j = source.indexOf('-', i);
int lineNumber;
if (j == -1) {
try {
lineNumber = Integer.parseInt(source.substring(i + 1));
} catch (final NumberFormatException e) {
return "";
}
} else {
try {
lineNumber = Integer.parseInt(source.substring(i + 1, j));
} catch (final NumberFormatException e) {
return "";
}
}
return "#line " + lineNumber + " \"" + inputFilePath + "\"";
}
return o.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy