[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[cdt-patch] Enable the new parser for the Comparator
|
Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/ChangeLog,v
retrieving revision 1.89
diff -u -r1.89 ChangeLog
--- ChangeLog 10 Apr 2003 19:26:23 -0000 1.89
+++ ChangeLog 14 Apr 2003 17:30:16 -0000
@@ -1,3 +1,13 @@
+2003-04-14 Alain Magloire
+
+ The problem: the old parser can still hangs and bring chaos, this
+ temporary code(since the new parser does not have yet a callback
+ mechanism) allow us to use the new parser when doing the visual diffs.
+ It is key on the preference setting, to enable/disable the old parser.
+
+ * src/org/eclipse/cdt/internal/ui/comparator/CStructureCreator.java:
+ * src/org/eclipse/cdt/internal/ui/comparator/ComparatorModelbuilder.java:
+
2003-04-10 Alain Magloire
* src/org/eclipse/cdt/internal/ui/editor/CEditorActionContributor.java:
Index: src/org/eclipse/cdt/internal/ui/compare/CStructureCreator.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/compare/CStructureCreator.java,v
retrieving revision 1.4
diff -u -r1.4 CStructureCreator.java
--- src/org/eclipse/cdt/internal/ui/compare/CStructureCreator.java 27 Mar 2003 16:12:17 -0000 1.4
+++ src/org/eclipse/cdt/internal/ui/compare/CStructureCreator.java 14 Apr 2003 17:30:16 -0000
@@ -26,6 +26,7 @@
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
+import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.parser.CStructurizer;
import org.eclipse.cdt.internal.parser.IStructurizerCallback;
@@ -69,14 +70,20 @@
DocumentInputStream is= new DocumentInputStream(doc);
IStructurizerCallback callback= new CNodeTreeConstructor(root, doc);
try {
- CStructurizer.getCStructurizer().parse(callback, is);
+ if (CCorePlugin.getDefault().useNewParser()) {
+ // new parser
+ ComparatorModelBuilder modelBuilder = new ComparatorModelBuilder(callback, (s != null ? s : ""));
+ modelBuilder.parse();
+ } else {
+ CStructurizer.getCStructurizer().parse(callback, is);
+ }
} catch (NodeConstructorError e) {
System.out.println("Parse error: " + e);
return null;
} catch (IOException e) {
return null;
- }
-
+ }
+
return root;
}
Index: src/org/eclipse/cdt/internal/ui/compare/ComparatorModelBuilder.java
===================================================================
RCS file: src/org/eclipse/cdt/internal/ui/compare/ComparatorModelBuilder.java
diff -N src/org/eclipse/cdt/internal/ui/compare/ComparatorModelBuilder.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/internal/ui/compare/ComparatorModelBuilder.java 14 Apr 2003 17:30:16 -0000
@@ -0,0 +1,260 @@
+/*******************************************************************************
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v0.5
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v05.html
+ *
+ * Contributors:
+ * QnX Software Systems - initial implementation base on code from rational/IBM
+ ******************************************************************************/
+
+package org.eclipse.cdt.internal.ui.compare;
+
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.cdt.core.model.ICElement;
+import org.eclipse.cdt.internal.core.dom.ClassKey;
+import org.eclipse.cdt.internal.core.dom.ClassSpecifier;
+import org.eclipse.cdt.internal.core.dom.DOMBuilder;
+import org.eclipse.cdt.internal.core.dom.Declaration;
+import org.eclipse.cdt.internal.core.dom.Declarator;
+import org.eclipse.cdt.internal.core.dom.EnumerationSpecifier;
+import org.eclipse.cdt.internal.core.dom.IOffsetable;
+import org.eclipse.cdt.internal.core.dom.Inclusion;
+import org.eclipse.cdt.internal.core.dom.Macro;
+import org.eclipse.cdt.internal.core.dom.NamespaceDefinition;
+import org.eclipse.cdt.internal.core.dom.ParameterDeclarationClause;
+import org.eclipse.cdt.internal.core.dom.SimpleDeclaration;
+import org.eclipse.cdt.internal.core.dom.TemplateDeclaration;
+import org.eclipse.cdt.internal.core.dom.TranslationUnit;
+import org.eclipse.cdt.internal.core.dom.TypeSpecifier;
+import org.eclipse.cdt.internal.core.parser.Parser;
+import org.eclipse.cdt.internal.parser.IStructurizerCallback;
+
+
+/**
+ * @author alain
+ * TODO: this should be remove when the new parser provides proper callbacks.
+ *
+ */
+public class ComparatorModelBuilder {
+
+ IStructurizerCallback callback;
+ String code;
+
+
+ /**
+ */
+ public ComparatorModelBuilder(IStructurizerCallback cb, String buffer) {
+ callback = cb;
+ code = buffer;
+ }
+
+ public void parse() {
+ DOMBuilder domBuilder = new DOMBuilder();
+ try {
+
+ Parser parser = new Parser(code, domBuilder, true);
+ parser.parse();
+ } catch (Exception e) {
+ callback.reportError(e);
+ }
+ generateModelElements(domBuilder.getTranslationUnit());
+ }
+
+ protected void generateModelElements(TranslationUnit tu){
+ Iterator i = tu.iterateOffsetableElements();
+ while (i.hasNext()){
+ IOffsetable offsetable = (IOffsetable)i.next();
+ if (offsetable instanceof Inclusion){
+ createInclusion((Inclusion) offsetable);
+ }
+ else if (offsetable instanceof Macro){
+ createMacro((Macro) offsetable);
+ }else if(offsetable instanceof Declaration){
+ generateModelElements ((Declaration) offsetable);
+ }
+ }
+ }
+
+ protected void generateModelElements (Declaration declaration){
+ // Namespace Definition
+ if (declaration instanceof NamespaceDefinition){
+ NamespaceDefinition nsDef = (NamespaceDefinition) declaration;
+ createNamespace(nsDef);
+ List nsDeclarations = nsDef.getDeclarations();
+ Iterator nsDecls = nsDeclarations.iterator();
+ while (nsDecls.hasNext()){
+ Declaration subNsDeclaration = (Declaration) nsDecls.next();
+ generateModelElements(subNsDeclaration);
+ }
+ }// end Namespace Definition
+
+ // Simple Declaration
+ if (declaration instanceof SimpleDeclaration){
+ SimpleDeclaration simpleDeclaration = (SimpleDeclaration) declaration;
+
+ /*-------------------------------------------
+ * Checking the type if it is a composite one
+ *-------------------------------------------*/
+ TypeSpecifier typeSpec = simpleDeclaration.getTypeSpecifier();
+ // Enumeration
+ if (typeSpec instanceof EnumerationSpecifier){
+ EnumerationSpecifier enumSpecifier = (EnumerationSpecifier) typeSpec;
+ createEnumeration (enumSpecifier);
+ } else if (typeSpec instanceof ClassSpecifier){ // Structure
+ ClassSpecifier classSpecifier = (ClassSpecifier) typeSpec;
+ createClass (simpleDeclaration, classSpecifier, false);
+ // create the sub declarations
+ List declarations = classSpecifier.getDeclarations();
+ Iterator j = declarations.iterator();
+ while (j.hasNext()){
+ Declaration subDeclaration = (Declaration)j.next();
+ generateModelElements(subDeclaration);
+ } // end while j
+ }
+ /*-----------------------------------------
+ * Create declarators of simple declaration
+ * ----------------------------------------*/
+ List declarators = simpleDeclaration.getDeclarators();
+ Iterator d = declarators.iterator();
+ while (d.hasNext()){
+ Declarator declarator = (Declarator)d.next();
+ createElement(simpleDeclaration, declarator);
+ } // end while d
+ } // end if SimpleDeclaration
+
+ // Template Declaration
+ if(declaration instanceof TemplateDeclaration){
+ TemplateDeclaration templateDeclaration = (TemplateDeclaration)declaration;
+ SimpleDeclaration simpleDeclaration = (SimpleDeclaration)templateDeclaration.getDeclarations().get(0);
+ TypeSpecifier typeSpec = simpleDeclaration.getTypeSpecifier();
+ if (typeSpec instanceof ClassSpecifier){
+ ClassSpecifier classSpecifier = (ClassSpecifier) typeSpec;
+ createClass(simpleDeclaration, classSpecifier, true);
+ // create the sub declarations
+ List declarations = classSpecifier.getDeclarations();
+ Iterator j = declarations.iterator();
+ while (j.hasNext()){
+ Declaration subDeclaration = (Declaration)j.next();
+ generateModelElements(subDeclaration);
+ } // end while j
+ }
+ List declarators = simpleDeclaration.getDeclarators();
+ Iterator d = declarators.iterator();
+ while (d.hasNext()){
+ Declarator declarator = (Declarator)d.next();
+ createTemplateElement(templateDeclaration, simpleDeclaration, declarator);
+ } // end while d
+
+ }// end Template Declaration
+
+ }
+
+ private void createElement(SimpleDeclaration simpleDeclaration, Declarator declarator){
+ // typedef
+ if (simpleDeclaration.getDeclSpecifier().isTypedef()){
+ createTypeDef(declarator, simpleDeclaration);
+ } else {
+ ParameterDeclarationClause pdc = declarator.getParms();
+ if (pdc == null){
+ createVariableSpecification(simpleDeclaration, declarator);
+ }
+ else{
+ createFunctionSpecification(simpleDeclaration, declarator, pdc, false);
+ }
+ }
+ }
+
+ private void createTemplateElement(TemplateDeclaration templateDeclaration, SimpleDeclaration simpleDeclaration, Declarator declarator){
+ // TODO: no template in the old parser
+ ParameterDeclarationClause pdc = declarator.getParms();
+ if (pdc != null){
+ createFunctionSpecification(simpleDeclaration, declarator, pdc, true);
+ }
+ }
+
+ private void createInclusion(Inclusion inclusion){
+ callback.includeDecl(inclusion.getName(), inclusion.getStartingOffset(), inclusion.getTotalLength(),0, 0);
+ }
+
+ private void createMacro(Macro macro){
+ callback.defineDecl(macro.getName(), macro.getStartingOffset(), macro.getTotalLength(), 0, 0);
+ }
+
+ private void createNamespace(NamespaceDefinition nsDef){
+ // TODO: the old parser callback does not know about namespace.
+ }
+
+ private void createEnumeration(EnumerationSpecifier enumSpecifier){
+ callback.structDeclBegin(enumSpecifier.getName().toString(), ICElement.C_ENUMERATION,
+ enumSpecifier.getName().getStartOffset(), enumSpecifier.getName().length(),
+ enumSpecifier.getStartingOffset(), 0, 0);
+ callback.structDeclEnd(enumSpecifier.getTotalLength(), 0);
+ }
+
+ private void createClass(SimpleDeclaration simpleDeclaration, ClassSpecifier classSpecifier, boolean isTemplate){
+ // create element
+ String className = (classSpecifier.getName() == null ) ? "" : classSpecifier.getName().toString();
+ int kind;
+ switch( classSpecifier.getClassKey() )
+ {
+ case ClassKey.t_class:
+ kind = ICElement.C_CLASS;
+ break;
+ case ClassKey.t_struct:
+ kind = ICElement.C_STRUCT;
+ break;
+ default:
+ kind = ICElement.C_UNION;
+ break;
+ }
+
+ // set element position
+ if( classSpecifier.getName() != null )
+ {
+ callback.structDeclBegin(className, kind,
+ classSpecifier.getName().getStartOffset(), classSpecifier.getName().length(),
+ classSpecifier.getStartingOffset(), 0, 0);
+ }
+ else
+ {
+ callback.structDeclBegin(className, kind,
+ classSpecifier.getClassKeyToken().getOffset(), classSpecifier.getClassKeyToken().getLength(),
+ classSpecifier.getStartingOffset(), 0, 0);
+
+ }
+ callback.structDeclBegin(className, kind,
+ classSpecifier.getClassKeyToken().getOffset(), classSpecifier.getClassKeyToken().getLength(),
+ classSpecifier.getStartingOffset(), 0, 0);
+ callback.structDeclEnd(classSpecifier.getTotalLength(), 0);
+ }
+
+ private void createTypeDef(Declarator declarator, SimpleDeclaration simpleDeclaration){
+ // TODO:No typedef in the old callback
+ String declaratorName = declarator.getName().toString();
+ callback.fieldDecl(declaratorName,
+ declarator.getName().getStartOffset(), declarator.getName().length(),
+ simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength(),
+ 0, 0, 0);
+ }
+
+ private void createVariableSpecification(SimpleDeclaration simpleDeclaration, Declarator declarator){
+ String declaratorName = declarator.getName().toString();
+ callback.fieldDecl(declaratorName, declarator.getName().getStartOffset(), declarator.getName().length(),
+ simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength(), 0, 0, 0);
+ }
+
+ private void createFunctionSpecification(SimpleDeclaration simpleDeclaration, Declarator declarator, ParameterDeclarationClause pdc, boolean isTemplate){
+ String declaratorName = declarator.getName().toString();
+ callback.functionDeclBegin(declaratorName,
+ declarator.getName().getStartOffset(), declarator.getName().length(),
+ simpleDeclaration.getStartingOffset(), 0, 0, 0);
+ callback.functionDeclEnd(simpleDeclaration.getTotalLength(), 0, simpleDeclaration.isFunctionDefinition());
+ }
+
+}
+