Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and additions #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main/java/org/anarres/cpp/LexerSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,14 @@ private Token _number_suffix(StringBuilder text, NumericValue value, int d)
d = read();
} // This should probably be isPunct() || isWhite().
else if (Character.isLetter(d) || d == '_') {
// We've encountered something initially identified as a number.
// Read in the rest of this token as an identifer but return it as an invalid.
while(Character.isLetterOrDigit(d) || d == '_') {
text.append((char)d);
d = read();
}
unread(d);
value.setFlags(flags);
return invalid(text,
"Invalid suffix \"" + (char) d
+ "\" on numeric constant");
return new Token(INVALID,text.toString());
} else {
unread(d);
value.setFlags(flags);
Expand Down
64 changes: 53 additions & 11 deletions src/main/java/org/anarres/cpp/Preprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public String getName() {
private VirtualFileSystem filesystem;
private PreprocessorListener listener;

HashSet<String> quotedImports=new HashSet<String>();
HashSet<String> sysImports=new HashSet<String>();

public Preprocessor() {
this.inputs = new ArrayList<Source>();

Expand Down Expand Up @@ -737,6 +740,7 @@ private boolean macro(Macro m, Token orig)
case WHITESPACE:
case CCOMMENT:
case CPPCOMMENT:
case NL:
/* Avoid duplicating spaces. */
space = true;
break;
Expand Down Expand Up @@ -1127,6 +1131,25 @@ private void include(
}
if (include(quoteincludepath, name))
return;
} else {
int forwardSlashPos=name.indexOf('/');
if (forwardSlashPos!=-1) {
String frameworkName=name.substring(0,forwardSlashPos);
String includeName=name.substring(forwardSlashPos+1);
for (String frameworkPath:frameworkspath) {
File frameworkFile=new File(frameworkPath,frameworkName+".framework");
if (!frameworkFile.exists() || !frameworkFile.isDirectory())
continue;
File frameworkHeadersFile=new File(frameworkFile,"Headers");
if (!frameworkHeadersFile.exists() || !frameworkHeadersFile.isDirectory())
continue;
File includeFile=new File(frameworkHeadersFile,includeName);
if (!includeFile.exists() || !includeFile.isFile())
continue;
if (include(filesystem.getFile(includeFile.getCanonicalPath())))
return;
}
}
}

if (include(sysincludepath, name))
Expand All @@ -1146,7 +1169,7 @@ private void include(
}

@Nonnull
private Token include(boolean next)
private Token include(boolean next,boolean isImport)
throws IOException,
LexerException {
LexerSource lexer = (LexerSource) source;
Expand Down Expand Up @@ -1196,13 +1219,20 @@ private Token include(boolean next)
}
}

/* Do the inclusion. */
include(source.getPath(), tok.getLine(), name, quoted, next);
HashSet<String> importMap=quoted?quotedImports:sysImports;
if (!isImport || !importMap.contains(name)) {
/* Do the inclusion. */
include(source.getPath(), tok.getLine(), name, quoted, next);

importMap.add(name);

/* 'tok' is the 'nl' after the include. We use it after the
* #line directive. */
if (getFeature(Feature.LINEMARKERS))
return line_token(1, source.getName(), " 1");
/* 'tok' is the 'nl' after the include. We use it after the
* #line directive. */
if (getFeature(Feature.LINEMARKERS))
return line_token(1, source.getName(), " 1");
} else {
warning(tok,"Already imported:"+name);
}
return tok;
} finally {
lexer.setInclude(false);
Expand Down Expand Up @@ -1494,7 +1524,7 @@ private long expr(int priority)
tok = expr_token();
if (tok.getType() != ')') {
expr_untoken(tok);
error(tok, "missing ) in expression");
error(tok, "missing ) in expression at "+tok);
return 0;
}
break;
Expand Down Expand Up @@ -1609,7 +1639,17 @@ private long expr(int priority)
break;

case '?':
/* XXX Handle this? */
{
tok = expr_token();
if (tok.getType() != ':') {
expr_untoken(tok);
error(tok, "missing : in conditional expression");
return 0;
}
long falseResult=expr(0);
lhs = (lhs!=0) ? rhs : falseResult;
}
break;

default:
error(op,
Expand Down Expand Up @@ -1781,6 +1821,7 @@ private Token _token()
case RSH:
case RSH_EQ:
case STRING:
case SQSTRING :
case XOR_EQ:
return tok;

Expand Down Expand Up @@ -1851,11 +1892,12 @@ private Token _token()
return undef();
// break;

case PP_IMPORT :
case PP_INCLUDE:
if (!isActive())
return source_skipline(false);
else
return include(false);
return include(false,ppcmd==PP_IMPORT);
// break;
case PP_INCLUDE_NEXT:
if (!isActive())
Expand All @@ -1866,7 +1908,7 @@ private Token _token()
);
return source_skipline(false);
}
return include(true);
return include(true,false);
// break;

case PP_WARNING:
Expand Down