diff options
author | Doug Schaefer | 2003-01-14 16:21:02 +0000 |
---|---|---|
committer | Doug Schaefer | 2003-01-14 16:21:02 +0000 |
commit | b57ec3302ebf6ddfed67742290d4ea4eb2787ee4 (patch) | |
tree | da8639e8d69c2739c9c74f027758a8804aed290f | |
parent | 443220eb36104f8d86e0a808796a4c96803df59f (diff) | |
download | org.eclipse.cdt-b57ec3302ebf6ddfed67742290d4ea4eb2787ee4.tar.gz org.eclipse.cdt-b57ec3302ebf6ddfed67742290d4ea4eb2787ee4.tar.xz org.eclipse.cdt-b57ec3302ebf6ddfed67742290d4ea4eb2787ee4.zip |
Integrated clean parser as well as a few minor bug fixes.
3 files changed, 204 insertions, 21 deletions
diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnitInfo.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnitInfo.java index 7bde8e2520e..6e32af3eaf4 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnitInfo.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/TranslationUnitInfo.java @@ -10,6 +10,7 @@ import java.io.InputStream; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ISourceRange; +import org.eclipse.cdt.internal.core.newparser.Parser2; import org.eclipse.cdt.internal.core.newparser.Parser; import org.eclipse.cdt.internal.parser.CStructurizer; import org.eclipse.core.resources.IFile; @@ -58,6 +59,11 @@ class TranslationUnitInfo extends CFileInfo { // cdt 1.0 parser ModelBuilder modelBuilder= new ModelBuilder((TranslationUnit)getElement()); CStructurizer.getCStructurizer().parse(modelBuilder, in); + } else if (false) { + // new parser + NewModelBuilder modelBuilder = new NewModelBuilder((TranslationUnit)getElement()); + Parser2 parser = new Parser2(in, modelBuilder, true); + parser.parse(); } else { // new parser NewModelBuilder modelBuilder = new NewModelBuilder((TranslationUnit)getElement()); diff --git a/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Parser2.java b/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Parser2.java index e431f5b5250..64edcae8712 100644 --- a/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Parser2.java +++ b/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Parser2.java @@ -1,19 +1,68 @@ package org.eclipse.cdt.internal.core.newparser; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; + /** * This is an attempt at a copyright clean parser. The grammar is based * on the ISO C++ standard */ public class Parser2 { + private IParserCallback callback; + private boolean quickParse = false; private boolean parsePassed = true; + public Parser2(Scanner s, IParserCallback c, boolean quick) throws Exception { + callback = c; + scanner = s; + quickParse = quick; + scanner.setQuickScan(quick); + scanner.setCallback(c); + //fetchToken(); + } + + public Parser2(Scanner s, IParserCallback c) throws Exception { + this(s, c, false); + } + + public Parser2(Scanner s) throws Exception { + this(s, new NullParserCallback(), false); + } + + public Parser2(String code) throws Exception { + this(new Scanner(new StringReader(code))); + } + + public Parser2(String code, IParserCallback c) throws Exception { + this(new Scanner(new StringReader(code)), c, false); + } + + public Parser2(InputStream stream, IParserCallback c, boolean quick) throws Exception { + this(new Scanner(new InputStreamReader(stream)), c, quick); + } + + private static int parseCount = 0; + + public boolean parse() throws Exception { + long startTime = System.currentTimeMillis(); + translationUnit(); + System.out.println("Parse " + (++parseCount) + ": " + + ( System.currentTimeMillis() - startTime ) + "ms" + + ( parsePassed ? "" : " - parse failure" )); + + return parsePassed; + } + + + /** * translationUnit * : (declaration)* * */ - public void translationUnit() throws Exception { + public void translationUnit() { Token lastBacktrack = null; while (LT(1) != Token.tEOF) { if (!declaration()) { @@ -55,7 +104,7 @@ public class Parser2 { * - explicitInstantiation and explicitSpecialization into * templateDeclaration */ - public boolean declaration() throws Exception { + public boolean declaration() { switch (LT(1)) { case Token.t_asm: return true; // asmDefinition(); @@ -84,7 +133,7 @@ public class Parser2 { * To do: * - work in ctorInitializer and functionTryBlock */ - public boolean simpleDeclaration() throws Exception { + public boolean simpleDeclaration() { while (declSpecifier()); if (initDeclarator()) { @@ -98,12 +147,15 @@ public class Parser2 { switch (LT(1)) { case Token.tSEMI: + consume(); return true; case Token.tLBRACE: return true; // functionBody(); default: return false; } + + } /** @@ -123,7 +175,7 @@ public class Parser2 { * - folded elaboratedTypeSpecifier into classSpecifier and enumSpecifier * - find template names in name */ - public boolean declSpecifier() throws Exception { + public boolean declSpecifier() { switch (LT(1)) { case Token.t_auto: case Token.t_register: @@ -155,12 +207,13 @@ public class Parser2 { return name(); case Token.tCOLON: case Token.tIDENTIFIER: - return name(); + // To do: need a symbol table to look up this name and see if it is + // a type. If not backtrack. For now, backtrack. + return false; // name(); case Token.t_class: case Token.t_struct: case Token.t_union: - // classSpecifier(); - return true; + return classSpecifier(); case Token.t_enum: // enumSpecifier(); return true; @@ -178,26 +231,137 @@ public class Parser2 { * * To Do: * - Handle template ids + * - Handle unqualifiedId */ - public boolean name() throws Exception { + public boolean name() { if (LT(1) == Token.tCOLONCOLON) consume(); - while (LT(1) == Token.tIDENTIFIER) { + if (!consume(Token.tIDENTIFIER)) + return false; + + while (LT(1) == Token.tCOLONCOLON) { consume(); - if (LT(1) == Token.tCOLONCOLON) - consume(); + if (!consume(Token.tIDENTIFIER)) + return false; } return true; } /** + * cvQualifier + * : "const" | "volatile" + */ + public boolean cvQualifier() { + switch (LT(1)) { + case Token.t_const: + case Token.t_volatile: + consume(); + return true; + default: + return false; + } + } + + /** * initDeclarator - * : declarator ("=" initializerClause | "(" expressionList ")")? + * : declarator ("=" initializerClause | "(" expressionList ")")? + * + * To Do: + * - handle initializers */ public boolean initDeclarator() { + if (!declarator()) + return false; + + return true; + } + + /** + * declarator + * : (ptrOperator)* directDeclarator + * + * directDeclarator + * : declaratorId + * | directDeclarator "(" parameterDeclarationClause ")" (cvQualifier)* + * (exceptionSpecification)* + * | directDeclarator "[" (constantExpression)? "]" + * | "(" declarator")" + * + * declaratorId + * : name + */ + public boolean declarator() { + while (ptrOperator()); + + if (LT(1) == Token.tLPAREN) { + consume(); + if (!declarator()) + return false; + + return consume(Token.tRPAREN); + } + + if (!name()) + return false; + + for (;;) { + switch (LT(1)) { + case Token.tLPAREN: + consume(); + // parameterDeclarationClause(); + if (!consume(Token.tRPAREN)) + return false; + + continue; + case Token.tLBRACKET: + consume(); + // constantExpression(); + if (!consume(Token.tRBRACKET)) + return false; + + continue; + } + break; + } + + return true; + } + + /** + * ptrOperator + * : "*" (cvQualifier)* + * | "&" + * | name "*" (cvQualifier)* + */ + public boolean ptrOperator() { + int t = LT(1); + + if (t == Token.tAMPER) { + consume(); + return true; + } + + Token mark = mark(); + if (t == Token.tIDENTIFIER || t == Token.tCOLONCOLON) + if (!name()) + return false; + + if (t == Token.tSTAR) { + consume(); + + while (cvQualifier()); + + return true; + } + + backup(mark); + return false; + } + + public boolean classSpecifier() { return false; } @@ -205,41 +369,53 @@ public class Parser2 { private Scanner scanner; private Token currToken; - private void fetchToken() throws Exception { - scanner.nextToken(); + private Token fetchToken() { + try { + return scanner.nextToken(); + } catch (Exception e) { + return null; + } } - protected Token LA(int i) throws Exception { + protected Token LA(int i) { if (i < 1) // can't go backwards return null; if (currToken == null) - currToken = scanner.nextToken(); + currToken = fetchToken(); Token retToken = currToken; for (; i > 1; --i) { if (retToken.getNext() == null) - scanner.nextToken(); + fetchToken(); retToken = retToken.getNext(); } return retToken; } - protected int LT(int i) throws Exception { + protected int LT(int i) { return LA(i).type; } - protected Token consume() throws Exception { + protected Token consume() { if (currToken.getNext() == null) - scanner.nextToken(); + fetchToken(); Token retToken = currToken; currToken = currToken.getNext(); return retToken; } + protected boolean consume(int type) { + if (LT(1) == type) { + consume(); + return true; + } else + return false; + } + protected Token mark() { return currToken; } diff --git a/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Scanner.java b/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Scanner.java index 585cec3d459..25be1b5e8f2 100644 --- a/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Scanner.java +++ b/core/org.eclipse.cdt.core/newparser/org/eclipse/cdt/internal/core/newparser/Scanner.java @@ -1050,7 +1050,8 @@ public class Scanner { throw new ScannerException("End of file encountered without terminating #endif"); // we're done - currentToken.setNext(Token.EOF); + if (currentToken != null) + currentToken.setNext(Token.EOF); return Token.EOF; } |