mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-06 09:16:02 +02:00
Optional support for block-comments like /% comment %/, bug 130235.
This commit is contained in:
parent
4790fbf198
commit
c4f2d97cf1
6 changed files with 87 additions and 18 deletions
|
@ -12,6 +12,7 @@ package org.eclipse.cdt.core.parser.tests.ast2;
|
|||
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
|
||||
|
@ -47,6 +48,7 @@ public class LanguageExtensionsTest extends AST2BaseTest {
|
|||
|
||||
protected static final int SIZEOF_EXTENSION = 0x1;
|
||||
protected static final int FUNCTION_STYLE_ASM = 0x2;
|
||||
protected static final int SLASH_PERCENT_COMMENT = 0x4;
|
||||
|
||||
public static TestSuite suite() {
|
||||
return suite(LanguageExtensionsTest.class);
|
||||
|
@ -87,7 +89,13 @@ public class LanguageExtensionsTest extends AST2BaseTest {
|
|||
}
|
||||
|
||||
protected IASTTranslationUnit parseCPPWithExtension(String code, final int extensions) throws Exception {
|
||||
return parse(code, GPPScannerExtensionConfiguration.getInstance(),
|
||||
return parse(code,
|
||||
new GPPScannerExtensionConfiguration() {
|
||||
@Override
|
||||
public boolean supportSlashPercentComments() {
|
||||
return (extensions & SLASH_PERCENT_COMMENT) != 0;
|
||||
}
|
||||
},
|
||||
new GPPParserExtensionConfiguration() {
|
||||
@Override
|
||||
public boolean supportExtendedSizeofOperator() {
|
||||
|
@ -102,7 +110,13 @@ public class LanguageExtensionsTest extends AST2BaseTest {
|
|||
}
|
||||
|
||||
protected IASTTranslationUnit parseCWithExtension(String code, final int extensions) throws Exception {
|
||||
return parse(code, GCCScannerExtensionConfiguration.getInstance(),
|
||||
return parse(code,
|
||||
new GCCScannerExtensionConfiguration() {
|
||||
@Override
|
||||
public boolean supportSlashPercentComments() {
|
||||
return (extensions & SLASH_PERCENT_COMMENT) != 0;
|
||||
}
|
||||
},
|
||||
new GCCParserExtensionConfiguration() {
|
||||
@Override
|
||||
public boolean supportExtendedSizeofOperator() {
|
||||
|
@ -217,4 +231,13 @@ public class LanguageExtensionsTest extends AST2BaseTest {
|
|||
fdef= getDeclaration(tu, 2);
|
||||
fdef= getDeclaration(tu, 3);
|
||||
}
|
||||
|
||||
// /% a comment %/
|
||||
// int a;
|
||||
public void testSlashPercentComment() throws Exception {
|
||||
IASTTranslationUnit tu= parseCWithExtension(getAboveComment(), SLASH_PERCENT_COMMENT);
|
||||
|
||||
IASTDeclaration d= getDeclaration(tu, 0);
|
||||
assertEquals("int a;", d.getRawSignature());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,16 @@ import org.eclipse.cdt.internal.core.parser.scanner.Lexer.LexerOptions;
|
|||
|
||||
|
||||
public class LexerTests extends BaseTestCase {
|
||||
private static final LexerOptions DEFAULT_OPTIONS = new LexerOptions();
|
||||
private static final LexerOptions NO_DOLLAR = new LexerOptions();
|
||||
private static final LexerOptions NO_MINMAX = new LexerOptions();
|
||||
private static final LexerOptions SLASH_PERCENT = new LexerOptions();
|
||||
static {
|
||||
NO_DOLLAR.fSupportDollarInIdentifiers= false;
|
||||
NO_MINMAX.fSupportMinAndMax= false;
|
||||
SLASH_PERCENT.fSupportSlashPercentComments= true;
|
||||
}
|
||||
|
||||
static String TRIGRAPH_REPLACES_CHARS= "#^[]|{}~\\";
|
||||
static String TRIGRAPH_CHARS= "='()!<>-/";
|
||||
|
||||
|
@ -43,19 +53,13 @@ public class LexerTests extends BaseTestCase {
|
|||
}
|
||||
|
||||
private void init(String input) throws Exception {
|
||||
fLog.clear();
|
||||
fLexer= new Lexer(input.toCharArray(), new LexerOptions(), fLog, null);
|
||||
fLog.setInput(input);
|
||||
fLexer.nextToken();
|
||||
fLastEndOffset= 0;
|
||||
init(input, DEFAULT_OPTIONS);
|
||||
}
|
||||
|
||||
private void init(String input, boolean dollar, boolean minmax) throws Exception {
|
||||
private void init(String input, LexerOptions options) throws Exception {
|
||||
fLog.clear();
|
||||
final LexerOptions lexerOptions = new LexerOptions();
|
||||
lexerOptions.fSupportDollarInIdentifiers= dollar;
|
||||
lexerOptions.fSupportMinAndMax= minmax;
|
||||
fLexer= new Lexer(input.toCharArray(), lexerOptions, fLog, null);
|
||||
fLexer= new Lexer(input.toCharArray(), options, fLog, null);
|
||||
fLog.setInput(input);
|
||||
fLexer.nextToken();
|
||||
fLastEndOffset= 0;
|
||||
}
|
||||
|
@ -278,6 +282,19 @@ public class LexerTests extends BaseTestCase {
|
|||
eof();
|
||||
}
|
||||
|
||||
public void testSlashPercentComments() throws Exception {
|
||||
init("// /%\na", SLASH_PERCENT);
|
||||
comment("// /%");
|
||||
nl();
|
||||
id("a");
|
||||
eof();
|
||||
|
||||
init("/% // /% \n xxx%/a", SLASH_PERCENT);
|
||||
comment("/% // /% \n xxx%/");
|
||||
id("a");
|
||||
eof();
|
||||
}
|
||||
|
||||
public void testMinimalComment() throws Exception {
|
||||
init("a/**/b/**/");
|
||||
id("a");
|
||||
|
@ -331,7 +348,7 @@ public class LexerTests extends BaseTestCase {
|
|||
eof();
|
||||
}
|
||||
|
||||
init(ident, false, true);
|
||||
init(ident, NO_DOLLAR);
|
||||
final int idxDollar = ident.indexOf('$');
|
||||
id(ident.substring(0, idxDollar));
|
||||
token(Lexer.tOTHER_CHARACTER, "$");
|
||||
|
@ -470,7 +487,7 @@ public class LexerTests extends BaseTestCase {
|
|||
eof();
|
||||
assertEquals(ops, buf.toString()); // check token image
|
||||
|
||||
init(input, true, false);
|
||||
init(input, NO_MINMAX);
|
||||
for (int i = 0; i < tokens.length; i++) {
|
||||
switch (tokens[i]) {
|
||||
case IGCCToken.tMIN:
|
||||
|
|
|
@ -62,6 +62,14 @@ public abstract class AbstractScannerExtensionConfiguration implements IScannerE
|
|||
public boolean supportAtSignInIdentifiers() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @since 5.1
|
||||
*/
|
||||
public boolean supportSlashPercentComments() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.cdt.core.dom.parser.IScannerExtensionConfiguration#supportAdditionalNumericLiteralSuffixes()
|
||||
|
|
|
@ -48,6 +48,14 @@ public interface IScannerExtensionConfiguration {
|
|||
*/
|
||||
public boolean supportAtSignInIdentifiers();
|
||||
|
||||
/**
|
||||
* Support for block-comments comments using /% %/.
|
||||
* @return <code>true</code>, if /% should be interpreted as the start of a block-comment which is
|
||||
* ended by %/
|
||||
* @since 5.1
|
||||
*/
|
||||
public boolean supportSlashPercentComments();
|
||||
|
||||
/**
|
||||
* Support for (deprecated) GNU minimum and maximum operators (<code><?</code>
|
||||
* and <code>>?</code>).
|
||||
|
|
|
@ -150,6 +150,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
|
|||
fLexOptions.fSupportDollarInIdentifiers= configuration.support$InIdentifiers();
|
||||
fLexOptions.fSupportAtSignInIdentifiers= configuration.supportAtSignInIdentifiers();
|
||||
fLexOptions.fSupportMinAndMax = configuration.supportMinAndMaxOperators();
|
||||
fLexOptions.fSupportSlashPercentComments= configuration.supportSlashPercentComments();
|
||||
fKeywords= new CharArrayIntMap(40, -1);
|
||||
fPPKeywords= new CharArrayIntMap(40, -1);
|
||||
configureKeywords(language, configuration);
|
||||
|
|
|
@ -50,6 +50,7 @@ final public class Lexer {
|
|||
public boolean fSupportAtSignInIdentifiers= true;
|
||||
public boolean fSupportMinAndMax= true;
|
||||
public boolean fCreateImageLocations= true;
|
||||
public boolean fSupportSlashPercentComments= false;
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
|
@ -267,7 +268,12 @@ final public class Lexer {
|
|||
lineComment(start);
|
||||
continue;
|
||||
case '*':
|
||||
blockComment(start);
|
||||
blockComment(start, '*');
|
||||
continue;
|
||||
case '%':
|
||||
if (fOptions.fSupportSlashPercentComments) {
|
||||
blockComment(start, '%');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
|
@ -485,8 +491,14 @@ final public class Lexer {
|
|||
lineComment(start);
|
||||
continue;
|
||||
case '*':
|
||||
blockComment(start);
|
||||
blockComment(start, '*');
|
||||
continue;
|
||||
case '%':
|
||||
if (fOptions.fSupportSlashPercentComments) {
|
||||
blockComment(start, '%');
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return newToken(IToken.tDIV, start);
|
||||
|
||||
|
@ -681,11 +693,11 @@ final public class Lexer {
|
|||
return newToken((expectQuotes ? tQUOTE_HEADER_NAME : tSYSTEM_HEADER_NAME), start, length);
|
||||
}
|
||||
|
||||
private void blockComment(final int start) {
|
||||
private void blockComment(final int start, final char trigger) {
|
||||
// we can ignore line-splices, trigraphs and windows newlines when searching for the '*'
|
||||
int pos= fEndOffset;
|
||||
while(pos < fLimit) {
|
||||
if (fInput[pos++] == '*') {
|
||||
if (fInput[pos++] == trigger) {
|
||||
fEndOffset= pos;
|
||||
if (nextCharPhase3() == '/') {
|
||||
nextCharPhase3();
|
||||
|
|
Loading…
Add table
Reference in a new issue