Bug 505594 - [HTML5] Actually place java translator files under Mercurial revision control. r+sr=jst

This commit is contained in:
Ben Newman 2009-07-27 16:42:45 -07:00
Родитель 74c7f0621e
Коммит d1541c50fe
17 изменённых файлов: 4215 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,121 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.util.List;
import japa.parser.ast.expr.AnnotationExpr;
import japa.parser.ast.expr.MarkerAnnotationExpr;
import japa.parser.ast.type.ReferenceType;
import japa.parser.ast.visitor.VoidVisitorAdapter;
public class AnnotationHelperVisitor<T> extends VoidVisitorAdapter<T> {
protected List<AnnotationExpr> currentAnnotations;
protected boolean nsUri() {
return hasAnnotation("NsUri");
}
protected boolean prefix() {
return hasAnnotation("Prefix");
}
protected boolean local() {
return hasAnnotation("Local");
}
protected boolean literal() {
return hasAnnotation("Literal");
}
protected boolean inline() {
return hasAnnotation("Inline");
}
protected boolean noLength() {
return hasAnnotation("NoLength");
}
protected boolean virtual() {
return hasAnnotation("Virtual");
}
private boolean hasAnnotation(String anno) {
if (currentAnnotations == null) {
return false;
}
for (AnnotationExpr ann : currentAnnotations) {
if (ann instanceof MarkerAnnotationExpr) {
MarkerAnnotationExpr marker = (MarkerAnnotationExpr) ann;
if (marker.getName().getName().equals(anno)) {
return true;
}
}
}
return false;
}
protected Type convertType(japa.parser.ast.type.Type type, int modifiers) {
if (type instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) type;
return new Type(convertTypeName(referenceType.getType().toString()), referenceType.getArrayCount(), noLength(), modifiers);
} else {
return new Type(convertTypeName(type.toString()), 0, false, modifiers);
}
}
private String convertTypeName(String name) {
if ("String".equals(name)) {
if (local()) {
return "@Local";
}
if (nsUri()) {
return "@NsUri";
}
if (prefix()) {
return "@Prefix";
}
if (literal()) {
return "@Literal";
}
}
return name;
}
}

Просмотреть файл

@ -0,0 +1,309 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008-2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CppTypes {
private static Set<String> reservedWords = new HashSet<String>();
static {
reservedWords.add("small");
reservedWords.add("for");
reservedWords.add("false");
reservedWords.add("true");
reservedWords.add("default");
reservedWords.add("class");
reservedWords.add("switch");
reservedWords.add("union");
reservedWords.add("template");
reservedWords.add("int");
reservedWords.add("char");
reservedWords.add("operator");
reservedWords.add("or");
reservedWords.add("and");
reservedWords.add("not");
reservedWords.add("xor");
reservedWords.add("unicode");
}
private static final String[] TREE_BUILDER_INCLUDES = { "prtypes",
"nsIAtom", "nsITimer", "nsString", "nsINameSpaceManager", "nsIContent",
"nsIDocument", "nsTraceRefcnt", "jArray", "nsHtml5DocumentMode",
"nsHtml5ArrayCopy", "nsHtml5NamedCharacters", "nsHtml5Parser",
"nsHtml5Atoms", "nsHtml5ByteReadable", "nsHtml5TreeOperation",
"nsHtml5PendingNotification", "nsHtml5StateSnapshot", "nsHtml5StackNode" };
private static final String[] INCLUDES = { "prtypes", "nsIAtom",
"nsString", "nsINameSpaceManager", "nsIContent", "nsIDocument",
"nsTraceRefcnt", "jArray", "nsHtml5DocumentMode",
"nsHtml5ArrayCopy", "nsHtml5NamedCharacters",
"nsHtml5Atoms", "nsHtml5ByteReadable", };
private static final String[] OTHER_DECLATIONS = {};
private static final String[] TREE_BUILDER_OTHER_DECLATIONS = { "typedef nsIContent* nsIContentPtr;" };
private static final String[] NAMED_CHARACTERS_INCLUDES = { "prtypes",
"jArray", "nscore" };
private static final String[] FORWARD_DECLARATIONS = { "nsHtml5Parser", };
private static final String[] CLASSES_THAT_NEED_SUPPLEMENT = {
"MetaScanner",
"StackNode",
"TreeBuilder",
"UTF16Buffer",
};
private final Map<String, String> atomMap = new HashMap<String, String>();
private final Writer atomWriter;
public CppTypes(File atomList) {
if (atomList == null) {
atomWriter = null;
} else {
try {
atomWriter = new OutputStreamWriter(new FileOutputStream(
atomList), "utf-8");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public void finished() {
try {
if (atomWriter != null) {
atomWriter.flush();
atomWriter.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String classPrefix() {
return "nsHtml5";
}
public String booleanType() {
return "PRBool";
}
public String charType() {
return "PRUnichar";
}
public String intType() {
return "PRInt32";
}
public String stringType() {
return "nsString*";
}
public String localType() {
return "nsIAtom*";
}
public String prefixType() {
return "nsIAtom*";
}
public String nsUriType() {
return "PRInt32";
}
public String falseLiteral() {
return "PR_FALSE";
}
public String trueLiteral() {
return "PR_TRUE";
}
public String nullLiteral() {
return "nsnull";
}
public String encodingDeclarationHandlerType() {
return "nsHtml5Parser*";
}
public String nodeType() {
return "nsIContent*";
}
public String xhtmlNamespaceLiteral() {
return "kNameSpaceID_XHTML";
}
public String svgNamespaceLiteral() {
return "kNameSpaceID_SVG";
}
public String xmlnsNamespaceLiteral() {
return "kNameSpaceID_XMLNS";
}
public String xmlNamespaceLiteral() {
return "kNameSpaceID_XML";
}
public String noNamespaceLiteral() {
return "kNameSpaceID_None";
}
public String xlinkNamespaceLiteral() {
return "kNameSpaceID_XLink";
}
public String mathmlNamespaceLiteral() {
return "kNameSpaceID_MathML";
}
public String arrayTemplate() {
return "jArray";
}
public String localForLiteral(String literal) {
String atom = atomMap.get(literal);
if (atom == null) {
atom = createAtomName(literal);
atomMap.put(literal, atom);
if (atomWriter != null) {
try {
atomWriter.write("HTML5_ATOM(" + atom + ", \"" + literal
+ "\")\n");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return "nsHtml5Atoms::" + atom;
}
private String createAtomName(String literal) {
String candidate = literal.replaceAll("[^a-zA-Z0-9_]", "_");
if ("".equals(candidate)) {
candidate = "emptystring";
}
while (atomMap.values().contains(candidate)
|| reservedWords.contains(candidate)) {
candidate = candidate + '_';
}
return candidate;
}
public String stringForLiteral(String literal) {
return '"' + literal + '"';
}
public String staticArrayMacro() {
return "J_ARRAY_STATIC";
}
public String[] boilerplateIncludes(String javaClass) {
if ("TreeBuilder".equals(javaClass)) {
return TREE_BUILDER_INCLUDES;
} else {
return INCLUDES;
}
}
public String[] boilerplateDeclarations(String javaClass) {
if ("TreeBuilder".equals(javaClass)) {
return TREE_BUILDER_OTHER_DECLATIONS;
} else {
return OTHER_DECLATIONS;
}
}
public String[] namedCharactersIncludes() {
return NAMED_CHARACTERS_INCLUDES;
}
public String[] boilerplateForwardDeclarations() {
return FORWARD_DECLARATIONS;
}
public String documentModeHandlerType() {
return "nsHtml5Parser*";
}
public String documentModeType() {
return "nsHtml5DocumentMode";
}
public String arrayCopy() {
return "nsHtml5ArrayCopy::arraycopy";
}
public String maxInteger() {
return "PR_INT32_MAX";
}
public String constructorBoilerplate(String className) {
return "MOZ_COUNT_CTOR(" + className + ");";
}
public String destructorBoilderplate(String className) {
return "MOZ_COUNT_DTOR(" + className + ");";
}
public String literalType() {
return "const char*";
}
public boolean hasSupplement(String javaClass) {
return Arrays.binarySearch(CLASSES_THAT_NEED_SUPPLEMENT, javaClass) > -1;
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,70 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GkAtomParser {
private static final Pattern ATOM = Pattern.compile("^GK_ATOM\\(([^,]+),\\s*\"([^\"]*)\"\\).*$");
private final BufferedReader reader;
public GkAtomParser(Reader reader) {
this.reader = new BufferedReader(reader);
}
public Map<String, String> parse() throws IOException {
Map<String, String> map = new HashMap<String, String>();
String line;
while((line = reader.readLine()) != null) {
Matcher m = ATOM.matcher(line);
if (m.matches()) {
map.put(m.group(2), m.group(1));
}
}
return map;
}
}

Просмотреть файл

@ -0,0 +1,352 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.util.LinkedList;
import java.util.List;
import japa.parser.ast.body.FieldDeclaration;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.body.ModifierSet;
import japa.parser.ast.body.VariableDeclarator;
import japa.parser.ast.expr.IntegerLiteralExpr;
import japa.parser.ast.expr.MethodCallExpr;
import japa.parser.ast.stmt.BlockStmt;
import japa.parser.ast.type.PrimitiveType;
import japa.parser.ast.type.ReferenceType;
import japa.parser.ast.type.Type;
public class HVisitor extends CppVisitor {
private enum Visibility {
NONE, PRIVATE, PUBLIC, PROTECTED,
}
private Visibility previousVisibility = Visibility.NONE;
private List<String> defines = new LinkedList<String>();
private SourcePrinter arrayInitPrinter = new SourcePrinter();
private SourcePrinter mainPrinterHolder;
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#printMethodNamespace()
*/
@Override protected void printMethodNamespace() {
}
public HVisitor(CppTypes cppTypes, SymbolTable symbolTable) {
super(cppTypes, symbolTable);
}
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#startClassDeclaration()
*/
@Override protected void startClassDeclaration() {
printer.print("#ifndef ");
printer.print(className);
printer.printLn("_h__");
printer.print("#define ");
printer.print(className);
printer.printLn("_h__");
printer.printLn();
String[] incs = cppTypes.boilerplateIncludes(javaClassName);
for (int i = 0; i < incs.length; i++) {
String inc = incs[i];
if (className.equals(inc)) {
continue;
}
printer.print("#include \"");
printer.print(inc);
printer.printLn(".h\"");
}
printer.printLn();
String[] forwDecls = cppTypes.boilerplateForwardDeclarations();
for (int i = 0; i < forwDecls.length; i++) {
String decl = forwDecls[i];
printer.print("class ");
printer.print(decl);
printer.printLn(";");
}
printer.printLn();
for (int i = 0; i < Main.H_LIST.length; i++) {
String klazz = Main.H_LIST[i];
if (!(klazz.equals(javaClassName) || klazz.equals("StackNode"))) {
printer.print("class ");
printer.print(cppTypes.classPrefix());
printer.print(klazz);
printer.printLn(";");
}
}
printer.printLn();
String[] otherDecls = cppTypes.boilerplateDeclarations(javaClassName);
for (int i = 0; i < otherDecls.length; i++) {
String decl = otherDecls[i];
printer.printLn(decl);
}
printer.printLn();
printer.print("class ");
printer.printLn(className);
printer.printLn("{");
printer.indent();
printer.indent();
}
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#endClassDeclaration()
*/
@Override protected void endClassDeclaration() {
printModifiers(ModifierSet.PUBLIC | ModifierSet.STATIC);
printer.printLn("void initializeStatics();");
printModifiers(ModifierSet.PUBLIC | ModifierSet.STATIC);
printer.printLn("void releaseStatics();");
printer.unindent();
printer.unindent();
if (cppTypes.hasSupplement(javaClassName)) {
printer.printLn();
printer.print("#include \"");
printer.print(className);
printer.printLn("HSupplement.h\"");
}
printer.printLn("};");
printer.printLn();
// This stuff should probably go into the .cpp anyway. sigh.
printer.print("#ifdef ");
printer.print(className);
printer.printLn("_cpp__");
printer.print(arrayInitPrinter.getSource());
printer.printLn("#endif");
printer.printLn();
for (String define : defines) {
printer.printLn(define);
}
printer.printLn();
printer.printLn();
printer.printLn("#endif");
}
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#printModifiers(int)
*/
@Override protected void printModifiers(int modifiers) {
if (ModifierSet.isPrivate(modifiers)) {
if (previousVisibility != Visibility.PRIVATE) {
printer.unindent();
printer.printLn("private:");
printer.indent();
previousVisibility = Visibility.PRIVATE;
}
} else if (ModifierSet.isProtected(modifiers)) {
if (previousVisibility != Visibility.PROTECTED) {
printer.unindent();
printer.printLn("protected:");
printer.indent();
previousVisibility = Visibility.PROTECTED;
}
} else {
if (previousVisibility != Visibility.PUBLIC) {
printer.unindent();
printer.printLn("public:");
printer.indent();
previousVisibility = Visibility.PUBLIC;
}
}
if (inline()) {
printer.print("inline ");
}
if (virtual()) {
printer.print("virtual ");
}
if (ModifierSet.isStatic(modifiers)) {
printer.print("static ");
}
}
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#fieldDeclaration(japa.parser.ast.body.FieldDeclaration, java.lang.LocalSymbolTable)
*/
@Override protected void fieldDeclaration(FieldDeclaration n, LocalSymbolTable arg) {
int modifiers = n.getModifiers();
List<VariableDeclarator> variables = n.getVariables();
VariableDeclarator declarator = variables.get(0);
if (ModifierSet.isStatic(modifiers) && ModifierSet.isFinal(modifiers)
&& n.getType() instanceof PrimitiveType) {
PrimitiveType type = (PrimitiveType) n.getType();
if (type.getType() != PrimitiveType.Primitive.Int) {
throw new IllegalStateException(
"Only int constant #defines supported.");
}
if (variables.size() != 1) {
throw new IllegalStateException(
"More than one variable declared by one declarator.");
}
String name = javaClassName + "." + declarator.getId().getName();
String value = declarator.getInit().toString();
if ("Integer.MAX_VALUE".equals(value)) {
value = cppTypes.maxInteger();
}
String longName = definePrefix + declarator.getId().getName();
if (symbolTable.cppDefinesByJavaNames.containsKey(name)) {
throw new IllegalStateException(
"Duplicate #define constant local name: " + name);
}
symbolTable.cppDefinesByJavaNames.put(name, longName);
defines.add("#define " + longName + " " + value);
} else {
if (n.getType() instanceof ReferenceType) {
ReferenceType rt = (ReferenceType) n.getType();
currentArrayCount = rt.getArrayCount();
if (currentArrayCount > 0
&& (rt.getType() instanceof PrimitiveType) && declarator.getInit() != null) {
if (!ModifierSet.isStatic(modifiers)) {
throw new IllegalStateException(
"Non-static array case not supported here." + declarator);
}
if (noLength()) {
inPrimitiveNoLengthFieldDeclarator = true;
mainPrinterHolder = printer;
printer = arrayInitPrinter;
n.getType().accept(this, arg);
printer.print(" ");
printer.print(className);
printer.print("::");
declarator.getId().accept(this, arg);
printer.print(" = ");
declarator.getInit().accept(this, arg);
printer.printLn(";");
printer = mainPrinterHolder;
} else if (!isNonToCharArrayMethodCall(declarator.getInit())) {
mainPrinterHolder = printer;
printer = arrayInitPrinter;
printer.print(cppTypes.arrayTemplate());
printer.print("<");
suppressPointer = true;
rt.getType().accept(this, arg);
suppressPointer = false;
printer.print(",");
printer.print(cppTypes.intType());
printer.print("> ");
printer.print(className);
printer.print("::");
declarator.getId().accept(this, arg);
printer.printLn(" = 0;");
printer = mainPrinterHolder;
}
} else if (ModifierSet.isStatic(modifiers)) {
mainPrinterHolder = printer;
printer = arrayInitPrinter;
n.getType().accept(this, arg);
printer.print(" ");
printer.print(className);
printer.print("::");
if ("AttributeName".equals(n.getType().toString())) {
printer.print("ATTR_");
} else if ("ElementName".equals(n.getType().toString())) {
printer.print("ELT_");
}
declarator.getId().accept(this, arg);
printer.print(" = ");
printer.print(cppTypes.nullLiteral());
printer.printLn(";");
printer = mainPrinterHolder;
}
}
printModifiers(modifiers);
n.getType().accept(this, arg);
printer.print(" ");
if (ModifierSet.isStatic(modifiers)) {
if ("AttributeName".equals(n.getType().toString())) {
printer.print("ATTR_");
} else if ("ElementName".equals(n.getType().toString())) {
printer.print("ELT_");
}
}
declarator.getId().accept(this, arg);
printer.printLn(";");
currentArrayCount = 0;
inPrimitiveNoLengthFieldDeclarator = false;
}
}
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#printConstructorBody(japa.parser.ast.stmt.BlockStmt, java.lang.LocalSymbolTable)
*/
@Override protected void printConstructorBody(BlockStmt block, LocalSymbolTable arg) {
printer.printLn(";");
}
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#visit(japa.parser.ast.body.MethodDeclaration, java.lang.LocalSymbolTable)
*/
@Override public void visit(MethodDeclaration n, LocalSymbolTable arg) {
arg = new LocalSymbolTable(javaClassName, symbolTable);
printMethodDeclaration(n, arg);
}
/**
* @see nu.validator.htmlparser.cpptranslate.CppVisitor#inHeader()
*/
@Override protected boolean inHeader() {
return true;
}
}

Просмотреть файл

@ -0,0 +1,84 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import japa.parser.ast.stmt.BreakStmt;
import japa.parser.ast.stmt.ContinueStmt;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import java.util.HashSet;
import java.util.Set;
public class LabelVisitor extends VoidVisitorAdapter<Object> {
private final Set<String> labels = new HashSet<String>();
public LabelVisitor() {
}
/**
* @see japa.parser.ast.visitor.VoidVisitorAdapter#visit(japa.parser.ast.stmt.BreakStmt, java.lang.Object)
*/
@Override
public void visit(BreakStmt n, Object arg) {
String label = n.getId();
if (label != null) {
labels.add(label + "_end");
}
}
/**
* @see japa.parser.ast.visitor.VoidVisitorAdapter#visit(japa.parser.ast.stmt.ContinueStmt, java.lang.Object)
*/
@Override
public void visit(ContinueStmt n, Object arg) {
String label = n.getId();
if (label != null) {
labels.add(label);
}
}
/**
* Returns the labels.
*
* @return the labels
*/
public Set<String> getLabels() {
return labels;
}
}

Просмотреть файл

@ -0,0 +1,75 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class LicenseExtractor {
private final Reader reader;
public LicenseExtractor(File file) throws IOException {
this.reader = new InputStreamReader(new FileInputStream(file), "utf-8");
}
public String extract() throws IOException {
boolean prevWasAsterisk = false;
StringBuilder sb = new StringBuilder();
int c;
while ((c = reader.read()) != -1) {
sb.append((char)c);
switch (c) {
case '*':
prevWasAsterisk = true;
continue;
case '/':
if (prevWasAsterisk) {
return sb.toString();
}
default:
prevWasAsterisk = false;
continue;
}
}
return "";
}
}

Просмотреть файл

@ -0,0 +1,89 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.util.HashMap;
import java.util.Map;
public class LocalSymbolTable {
private final Map<String, Type> locals = new HashMap<String, Type>();
private final String javaClassName;
private final SymbolTable delegate;
/**
* @param javaClassName
* @param delegate
*/
public LocalSymbolTable(String javaClassName, SymbolTable delegate) {
this.javaClassName = javaClassName;
this.delegate = delegate;
}
public void putLocalType(String name, Type type) {
locals.put(name, type);
}
/**
* @param klazz
* @param variable
* @return
* @see nu.validator.htmlparser.cpptranslate.SymbolTable#getFieldType(java.lang.String, java.lang.String)
*/
public Type getVariableType(String klazz, String variable) {
if (klazz == null) {
Type type = locals.get(variable);
if (type != null) {
return type;
}
}
return delegate.getFieldType(((klazz == null || "this".equals(klazz)) ? javaClassName : klazz), variable);
}
/**
* @param klazz may be <code>null</code> or "this"
* @param method
* @return
* @see nu.validator.htmlparser.cpptranslate.SymbolTable#getMethodReturnType(java.lang.String, java.lang.String)
*/
public Type getMethodReturnType(String klazz, String method) {
return delegate.getMethodReturnType(((klazz == null || "this".equals(klazz)) ? javaClassName : klazz), method);
}
}

Просмотреть файл

@ -0,0 +1,137 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import japa.parser.JavaParser;
import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Main {
static final String[] H_LIST = {
"Tokenizer",
"TreeBuilder",
"MetaScanner",
"AttributeName",
"ElementName",
"HtmlAttributes",
"StackNode",
"UTF16Buffer",
"StateSnapshot",
"Portability",
};
private static final String[] CPP_LIST = {
"Tokenizer",
"TreeBuilder",
"MetaScanner",
"AttributeName",
"ElementName",
"HtmlAttributes",
"StackNode",
"UTF16Buffer",
"StateSnapshot",
};
/**
* @param args
* @throws ParseException
* @throws IOException
*/
public static void main(String[] args) throws ParseException, IOException {
CppTypes cppTypes = new CppTypes(new File(args[2]));
SymbolTable symbolTable = new SymbolTable();
File javaDirectory = new File(args[0]);
File targetDirectory = new File(args[1]);
File cppDirectory = targetDirectory;
File javaCopyDirectory = new File(targetDirectory, "javasrc");
for (int i = 0; i < H_LIST.length; i++) {
parseFile(cppTypes, javaDirectory, cppDirectory, H_LIST[i], ".h", new HVisitor(cppTypes, symbolTable));
// copyFile(new File(javaDirectory, H_LIST[i] + ".java"), new File(javaCopyDirectory, H_LIST[i] + ".java"));
}
for (int i = 0; i < CPP_LIST.length; i++) {
parseFile(cppTypes, javaDirectory, cppDirectory, CPP_LIST[i], ".cpp", new CppVisitor(cppTypes, symbolTable));
}
cppTypes.finished();
}
private static void copyFile(File input, File output) throws IOException {
if (input.getCanonicalFile().equals(output.getCanonicalFile())) {
return; // files are the same!
}
// This is horribly inefficient, but perf is not really much of a concern here.
FileInputStream in = new FileInputStream(input);
FileOutputStream out = new FileOutputStream(output);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
out.flush();
out.close();
in.close();
}
private static void parseFile(CppTypes cppTypes, File javaDirectory, File cppDirectory, String className, String fne, CppVisitor visitor) throws ParseException,
FileNotFoundException, UnsupportedEncodingException, IOException {
File file = new File(javaDirectory, className + ".java");
String license = new LicenseExtractor(file).extract();
CompilationUnit cu = JavaParser.parse(new NoCppInputStream(new FileInputStream(file)), "utf-8");
LabelVisitor labelVisitor = new LabelVisitor();
cu.accept(labelVisitor, null);
visitor.setLabels(labelVisitor.getLabels());
cu.accept(visitor, null);
FileOutputStream out = new FileOutputStream(new File(cppDirectory, cppTypes.classPrefix() + className + fne));
OutputStreamWriter w = new OutputStreamWriter(out, "utf-8");
w.write(license);
w.write("\n\n/*\n * THIS IS A GENERATED FILE. PLEASE DO NOT EDIT.\n * Please edit " + className + ".java instead and regenerate.\n */\n\n");
w.write(visitor.getSource());
w.close();
}
}

Просмотреть файл

@ -0,0 +1,86 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.io.IOException;
import java.io.InputStream;
public class NoCppInputStream extends InputStream {
private final static char[] START = "[NOCPP[".toCharArray();
private final static char[] END = "]NOCPP]".toCharArray();
private int state;
private final InputStream delegate;
/**
* @param delegate
*/
public NoCppInputStream(InputStream delegate) {
this.delegate = delegate;
this.state = 0;
}
@Override public int read() throws IOException {
int c;
if (state == START.length) {
int endState = 0;
while (endState != END.length) {
c = delegate.read();
if (END[endState] == c) {
endState++;
} else {
endState = 0;
}
}
state = 0;
}
c = delegate.read();
if (START[state] == c) {
state++;
} else {
state = 0;
}
return c;
}
}

Просмотреть файл

@ -0,0 +1,70 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringLiteralParser {
private static final Pattern STRING_DECL = Pattern.compile("^.*\\(([^ ]+) = new nsString\\(\\)\\)->Assign\\(NS_LITERAL_STRING\\(\"([^\"]*)\"\\)\\);.*$");
private final BufferedReader reader;
public StringLiteralParser(Reader reader) {
this.reader = new BufferedReader(reader);
}
public Map<String, String> parse() throws IOException {
Map<String, String> map = new HashMap<String, String>();
String line;
while((line = reader.readLine()) != null) {
Matcher m = STRING_DECL.matcher(line);
if (m.matches()) {
map.put(m.group(2), m.group(1));
}
}
return map;
}
}

Просмотреть файл

@ -0,0 +1,73 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
public class StringPair {
/**
* @param first
* @param second
*/
public StringPair(String first, String second) {
this.first = first;
this.second = second;
}
private final String first;
private final String second;
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override public boolean equals(Object o) {
if (o instanceof StringPair) {
StringPair other = (StringPair) o;
return first.equals(other.first) && second.equals(other.second);
}
return false;
}
/**
* @see java.lang.Object#hashCode()
*/
@Override public int hashCode() {
return first.hashCode() ^ second.hashCode();
}
}

Просмотреть файл

@ -0,0 +1,80 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import java.util.HashMap;
import java.util.Map;
public class SymbolTable {
public final Map<String, String> cppDefinesByJavaNames = new HashMap<String, String>();
private final Map<StringPair, Type> fields = new HashMap<StringPair, Type>();
private final Map<StringPair, Type> methodReturns = new HashMap<StringPair, Type>();
/**
* This is a sad hack to work around the fact the there's no real symbol
* table yet.
*
* @param name
* @return
*/
public boolean isNotAnAttributeOrElementName(String name) {
return !("ATTRIBUTE_HASHES".equals(name)
|| "ATTRIBUTE_NAMES".equals(name)
|| "ELEMENT_HASHES".equals(name)
|| "ELEMENT_NAMES".equals(name) || "ALL_NO_NS".equals(name));
}
public void putFieldType(String klazz, String field, Type type) {
fields.put(new StringPair(klazz, field), type);
}
public void putMethodReturnType(String klazz, String method, Type type) {
methodReturns.put(new StringPair(klazz, method), type);
}
public Type getFieldType(String klazz, String field) {
return fields.get(new StringPair(klazz, field));
}
public Type getMethodReturnType(String klazz, String method) {
return methodReturns.get(new StringPair(klazz, method));
}
}

Просмотреть файл

@ -0,0 +1,71 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.body.FieldDeclaration;
import japa.parser.ast.body.MethodDeclaration;
public class SymbolTableVisitor extends AnnotationHelperVisitor<SymbolTable> {
private String javaClassName;
/**
* @see japa.parser.ast.visitor.VoidVisitorAdapter#visit(japa.parser.ast.body.FieldDeclaration, java.lang.Object)
*/
@Override public void visit(FieldDeclaration n, SymbolTable arg) {
currentAnnotations = n.getAnnotations();
arg.putFieldType(javaClassName, n.getVariables().get(0).getId().getName(), convertType(n.getType(), n.getModifiers()));
}
/**
* @see japa.parser.ast.visitor.VoidVisitorAdapter#visit(japa.parser.ast.body.MethodDeclaration, java.lang.Object)
*/
@Override public void visit(MethodDeclaration n, SymbolTable arg) {
currentAnnotations = n.getAnnotations();
arg.putMethodReturnType(javaClassName, n.getName(), convertType(n.getType(), n.getModifiers()));
}
/**
* @see japa.parser.ast.visitor.VoidVisitorAdapter#visit(japa.parser.ast.body.ClassOrInterfaceDeclaration, java.lang.Object)
*/
@Override public void visit(ClassOrInterfaceDeclaration n, SymbolTable arg) {
javaClassName = n.getName();
}
}

Просмотреть файл

@ -0,0 +1,99 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.cpptranslate;
public class Type {
/**
* @param type
* @param arrayCount
* @param noLength
* @param modifiers
*/
public Type(String type, int arrayCount, boolean noLength, int modifiers) {
this.type = type;
this.arrayCount = arrayCount;
this.noLength = noLength;
this.modifiers = modifiers;
}
private final String type;
private final int arrayCount;
private final boolean noLength;
private final int modifiers;
/**
* Returns the type.
*
* @return the type
*/
public String getType() {
return type;
}
/**
* Returns the arrayCount.
*
* @return the arrayCount
*/
public int getArrayCount() {
return arrayCount;
}
/**
* Returns the noLength.
*
* @return the noLength
*/
public boolean isNoLength() {
return noLength;
}
/**
* Returns the modifiers.
*
* @return the modifiers
*/
public int getModifiers() {
return modifiers;
}
}

Просмотреть файл

@ -0,0 +1,109 @@
/*
* Copyright (c) 2008 Mozilla Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package nu.validator.htmlparser.generator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GenerateNamedCharacters {
private static final int LEAD_OFFSET = 0xD800 - (0x10000 >> 10);
private static final Pattern LINE_PATTERN = Pattern.compile("^\\s*<tr> <td> <code title=\"\">([^<]*)</code> </td> <td> U\\+(\\S*) </td> </tr>.*$");
private static String toUString(int c) {
String hexString = Integer.toHexString(c);
switch (hexString.length()) {
case 1:
return "\\u000" + hexString;
case 2:
return "\\u00" + hexString;
case 3:
return "\\u0" + hexString;
case 4:
return "\\u" + hexString;
default:
throw new RuntimeException("Unreachable.");
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
TreeMap<String, String> entities = new TreeMap<String, String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
Matcher m = LINE_PATTERN.matcher(line);
if (m.matches()) {
entities.put(m.group(1), m.group(2));
}
}
System.out.println("static final char[][] NAMES = {");
for (Map.Entry<String, String> entity : entities.entrySet()) {
String name = entity.getKey();
System.out.print("\"");
System.out.print(name);
System.out.println("\".toCharArray(),");
}
System.out.println("};");
System.out.println("static final @NoLength char[][] VALUES = {");
for (Map.Entry<String, String> entity : entities.entrySet()) {
String value = entity.getValue();
int intVal = Integer.parseInt(value, 16);
System.out.print("{");
if (intVal == '\'') {
System.out.print("\'\\\'\'");
} else if (intVal == '\n') {
System.out.print("\'\\n\'");
} else if (intVal == '\\') {
System.out.print("\'\\\\\'");
} else if (intVal <= 0xFFFF) {
System.out.print("\'");
System.out.print(toUString(intVal));
System.out.print("\'");
} else {
int hi = (LEAD_OFFSET + (intVal >> 10));
int lo = (0xDC00 + (intVal & 0x3FF));
System.out.print("\'");
System.out.print(toUString(hi));
System.out.print("\', \'");
System.out.print(toUString(lo));
System.out.print("\'");
}
System.out.println("},");
}
System.out.println("};");
}
}

Просмотреть файл

@ -0,0 +1,295 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is HTML Parser C++ Translator code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package nu.validator.htmlparser.generator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nu.validator.htmlparser.cpptranslate.CppTypes;
public class GenerateNamedCharactersCpp {
private static final int LEAD_OFFSET = 0xD800 - (0x10000 >> 10);
private static final Pattern LINE_PATTERN = Pattern.compile("^\\s*<tr> <td> <code title=\"\">([^<]*)</code> </td> <td> U\\+(\\S*) </td> </tr>.*$");
private static String toHexString(int c) {
String hexString = Integer.toHexString(c);
switch (hexString.length()) {
case 1:
return "0x000" + hexString;
case 2:
return "0x00" + hexString;
case 3:
return "0x0" + hexString;
case 4:
return "0x" + hexString;
default:
throw new RuntimeException("Unreachable.");
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
TreeMap<String, String> entities = new TreeMap<String, String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(args[0]), "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
Matcher m = LINE_PATTERN.matcher(line);
if (m.matches()) {
entities.put(m.group(1), m.group(2));
}
}
CppTypes cppTypes = new CppTypes(null);
File targetDirectory = new File(args[1]);
generateH(targetDirectory, cppTypes, entities);
generateCpp(targetDirectory, cppTypes, entities);
}
private static void generateH(File targetDirectory, CppTypes cppTypes,
Map<String, String> entities) throws IOException {
File hFile = new File(targetDirectory, cppTypes.classPrefix()
+ "NamedCharacters.h");
Writer out = new OutputStreamWriter(new FileOutputStream(hFile),
"utf-8");
out.write("#ifndef " + cppTypes.classPrefix() + "NamedCharacters_h__\n");
out.write("#define " + cppTypes.classPrefix() + "NamedCharacters_h__\n");
out.write('\n');
String[] includes = cppTypes.namedCharactersIncludes();
for (int i = 0; i < includes.length; i++) {
String include = includes[i];
out.write("#include \"" + include + ".h\"\n");
}
out.write('\n');
out.write("class " + cppTypes.classPrefix() + "NamedCharacters\n");
out.write("{\n");
out.write(" public:\n");
out.write(" static " + cppTypes.arrayTemplate() + "<"
+ cppTypes.arrayTemplate() + "<" + cppTypes.charType() + ","
+ cppTypes.intType() + ">," + cppTypes.intType() + "> NAMES;\n");
out.write(" static " + cppTypes.arrayTemplate() + "<"
+ cppTypes.charType() + "," + cppTypes.intType()
+ ">* VALUES;\n");
out.write(" static " + cppTypes.charType() + "** WINDOWS_1252;\n");
out.write(" static void initializeStatics();\n");
out.write(" static void releaseStatics();\n");
out.write("};\n");
out.write("\n#endif // " + cppTypes.classPrefix()
+ "NamedCharacters_h__\n");
out.flush();
out.close();
}
private static void generateCpp(File targetDirectory, CppTypes cppTypes,
Map<String, String> entities) throws IOException {
File hFile = new File(targetDirectory, cppTypes.classPrefix()
+ "NamedCharacters.cpp");
Writer out = new OutputStreamWriter(new FileOutputStream(hFile),
"utf-8");
out.write("#define " + cppTypes.classPrefix()
+ "NamedCharacters_cpp__\n");
String[] includes = cppTypes.namedCharactersIncludes();
for (int i = 0; i < includes.length; i++) {
String include = includes[i];
out.write("#include \"" + include + ".h\"\n");
}
out.write('\n');
out.write("#include \"" + cppTypes.classPrefix()
+ "NamedCharacters.h\"\n");
out.write("\n");
out.write("" + cppTypes.arrayTemplate() + "<"
+ cppTypes.arrayTemplate() + "<" + cppTypes.charType() + ","
+ cppTypes.intType() + ">," + cppTypes.intType() + "> "
+ cppTypes.classPrefix() + "NamedCharacters::NAMES;\n");
out.write("static " + cppTypes.charType() + " const WINDOWS_1252_DATA[] = {\n");
out.write(" 0x20AC,\n");
out.write(" 0xFFFD,\n");
out.write(" 0x201A,\n");
out.write(" 0x0192,\n");
out.write(" 0x201E,\n");
out.write(" 0x2026,\n");
out.write(" 0x2020,\n");
out.write(" 0x2021,\n");
out.write(" 0x02C6,\n");
out.write(" 0x2030,\n");
out.write(" 0x0160,\n");
out.write(" 0x2039,\n");
out.write(" 0x0152,\n");
out.write(" 0xFFFD,\n");
out.write(" 0x017D,\n");
out.write(" 0xFFFD,\n");
out.write(" 0xFFFD,\n");
out.write(" 0x2018,\n");
out.write(" 0x2019,\n");
out.write(" 0x201C,\n");
out.write(" 0x201D,\n");
out.write(" 0x2022,\n");
out.write(" 0x2013,\n");
out.write(" 0x2014,\n");
out.write(" 0x02DC,\n");
out.write(" 0x2122,\n");
out.write(" 0x0161,\n");
out.write(" 0x203A,\n");
out.write(" 0x0153,\n");
out.write(" 0xFFFD,\n");
out.write(" 0x017E,\n");
out.write(" 0x0178\n");
out.write("};\n");
int k = 0;
for (Map.Entry<String, String> entity : entities.entrySet()) {
String name = entity.getKey();
int value = Integer.parseInt(entity.getValue(), 16);
out.write("static " + cppTypes.charType() + " const NAME_" + k
+ "[] = {\n");
out.write(" ");
for (int j = 0; j < name.length(); j++) {
char c = name.charAt(j);
if (j != 0) {
out.write(", ");
}
out.write('\'');
out.write(c);
out.write('\'');
}
out.write("\n};\n");
out.write("static " + cppTypes.charType() + " const VALUE_" + k
+ "[] = {\n");
out.write(" ");
if (value <= 0xFFFF) {
out.write(toHexString(value));
} else {
int hi = (LEAD_OFFSET + (value >> 10));
int lo = (0xDC00 + (value & 0x3FF));
out.write(toHexString(hi));
out.write(", ");
out.write(toHexString(lo));
}
out.write("\n};\n");
k++;
}
out.write("\n// XXX bug 501082: for some reason, msvc takes forever to optimize this function\n");
out.write("#ifdef _MSC_VER\n");
out.write("#pragma optimize(\"\", off)\n");
out.write("#endif\n\n");
out.write("void\n");
out.write(cppTypes.classPrefix()
+ "NamedCharacters::initializeStatics()\n");
out.write("{\n");
out.write(" NAMES = " + cppTypes.arrayTemplate() + "<"
+ cppTypes.arrayTemplate() + "<" + cppTypes.charType() + ","
+ cppTypes.intType() + ">," + cppTypes.intType() + ">("
+ entities.size() + ");\n");
int i = 0;
for (Map.Entry<String, String> entity : entities.entrySet()) {
out.write(" NAMES[" + i + "] = " + cppTypes.arrayTemplate() + "<"
+ cppTypes.charType() + "," + cppTypes.intType() + ">(("
+ cppTypes.charType() + "*)NAME_" + i + ", "
+ entity.getKey().length() + ");\n");
i++;
}
out.write(" VALUES = new " + cppTypes.arrayTemplate() + "<"
+ cppTypes.charType() + "," + cppTypes.intType() + ">["
+ entities.size() + "];\n");
i = 0;
for (Map.Entry<String, String> entity : entities.entrySet()) {
int value = Integer.parseInt(entity.getValue(), 16);
out.write(" VALUES[" + i + "] = " + cppTypes.arrayTemplate() + "<"
+ cppTypes.charType() + "," + cppTypes.intType() + ">(("
+ cppTypes.charType() + "*)VALUE_" + i + ", "
+ ((value <= 0xFFFF) ? "1" : "2") + ");\n");
i++;
}
out.write("\n");
out.write(" WINDOWS_1252 = new " + cppTypes.charType() + "*[32];\n");
out.write(" for (" + cppTypes.intType() + " i = 0; i < 32; ++i) {\n");
out.write(" WINDOWS_1252[i] = (" + cppTypes.charType() + "*)&(WINDOWS_1252_DATA[i]);\n");
out.write(" }\n");
out.write("}\n");
out.write("\n");
out.write("#ifdef _MSC_VER\n");
out.write("#pragma optimize(\"\", on)\n");
out.write("#endif\n\n");
out.write("void\n");
out.write(cppTypes.classPrefix()
+ "NamedCharacters::releaseStatics()\n");
out.write("{\n");
out.write(" NAMES.release();\n");
out.write(" delete[] VALUES;\n");
out.write(" delete[] WINDOWS_1252;\n");
out.write("}\n");
out.flush();
out.close();
}
}