Code Monkey home page Code Monkey logo

jcpp's Introduction

Introduction

The C Preprocessor is an interesting standard. It appears to be derived from the de-facto behaviour of the first preprocessors, and has evolved over the years. Implementation is therefore difficult.

JCPP is a complete, compliant, standalone, pure Java implementation of the C preprocessor. It is intended to be of use to people writing C-style compilers in Java using tools like sablecc, antlr, JLex, CUP and so forth (although if you aren't using sablecc, you need your head examined).

This project has has been used to successfully preprocess much of the source code of the GNU C library. As of version 1.2.5, it can also preprocess the Apple Objective C library.

Documentation

jcpp's People

Contributors

shevek avatar timepath avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jcpp's Issues

Handle framework includes properly

Will this work?

             }
             if (include(quoteincludepath, name))
                 return;
+        } else {
+            int idx = name.indexOf('/');
+            if (idx != -1) {
+                String frameworkName = name.substring(0, idx);
+                String headerName = name.substring(idx + 1);
+                String headerPath = frameworkName + ".framework/Headers/" + headerName;
+                if (include(frameworkspath, headerPath))
+                    return;
+            }
         }

         if (include(sysincludepath, name))

Column numbers seem to be wrong

The Input Stream:

aaa,,

Parses as:

Returning fresh token [IDENTIFIER@1,0]:"aaa"
pp: Returning [IDENTIFIER@1,0]:"aaa"
Returning fresh token [,@1,3]:","
pp: Returning [,@1,3]:","
Returning fresh token [,@1,3]:","
pp: Returning [,@1,3]:","
pp: Returning [EOF]:<265>

The column information of the seconds comma should be 4.

The JCPP version is 1.2.6

Minus sign and number constant recognized as a single token

Since this commit ca42036 the occurrence of minus sign followed by a number constant is returned by preprocessor as a single token. I wonder why the minus sign is not considered to be a separate token (as it was previously)? Is it required by some preprocessor specification?

Stripping line comments

Hello!

Is jcpp capable of lexing and removing C++ style line comments?

// This is a comment

I can't see anything in the API to do this, and there doesn't seem to be a Feature value that would enable it. I'm seeing line comments in the output of the preprocessor, so presumably it doesn't do it by default, either.

Problems using ANT build task under Windows

There were two problems using the ANT build task for JCPP under windows:

1- "etc/global.xml", line 15
there is an absolute path reference to /usr/share/ant/lib
It is better to use the ANT property ${ant.home} instead, so the script will also work in different installations:
<fileset dir="${ant.home}/lib">

2- "etc/targets/global-javadoc.xml", line 51:
the javadoc task makes a reference to directory "${global.dir.etc}/javadoc" ()
When I use a fresh copy from the sources, this directory does not exists, so the javadoc task fails (aborting the build process). A good workaround is to create this directory if it does not exists, before the javadoc task is called. So I've inserted the following two lines at line 6 of "etc/targets/global-javadoc.xml":
<!-- create javadoc etc directory if it does not exist-->
<mkdir dir="${global.dir.etc}/javadoc" />

NullPointerException when #endif is missing

NullPointerException is thrown for source.setActive(true) in Preprocessor._token() when #endif is missing and #ifdef is satisfied.
Here is a simple test case:

#define D
#ifdef D
    int a = 1;
#else
    int a = 2;
<EOF>

Similarly for:

#ifndef D
    int a = 1;
#else
    int a = 2;
<EOF>

Bad Token's line and column when code line is broken with backslash

When we define a multi-line macro, such as:

6: #define THREE 1 \
7:    + \
8:    2

we could expect that calling token.getLine() for Token representing number "2" would return line 8, but surprisingly the entire define definition is regarded as one-line preprocessor directive, so the result is 6.

The tokens list representing macro THREE:

[HASH@6,0]:"#"
[IDENTIFIER@6,1]:"define"
[IDENTIFIER@6,8]:"A"
[(@6,9]:"("
[IDENTIFIER@6,10]:"a"
[,@6,11]:","
[IDENTIFIER@6,13]:"b"
[)@6,14]:")"
[IDENTIFIER@6,16]:"a"
[WHITESPACE@6,17]:"    "
[+@6,21]:"+"
[WHITESPACE@6,22]:"    "
[IDENTIFIER@6,26]:"b"
[NL@6,27]:"

I'm aware that tokens list in Macro is not public, but still line and column numbers should be correct.

Whitespace used in token pasting

Input (groovy):

def pp = new Preprocessor();
pp.addInput(new StringLexerSource("""
#define ONE(arg) one_##arg
#define TWO(arg) ONE(two_##arg)

TWO(good)
TWO(     /* evil newline */
    bad)

ONE(good)
ONE(     /* evil newline */
    bad)
""", true))
println new CppReader(pp).text.trim()

Output:

one_two_good
    one_two_ bad

    one_good
    one_
 bad

Expected output:

one_two_good
one_two_bad

one_good
one_bad

False float number parsing

Float number "0.1" parsed as three tokens: octal numeric "0", dot ".", numeric "0" intead of float numeric "0.1".

Absolute paths in includes

JCPP cannot handle absolute paths in include directives. They are treated as relative. For example,

include "/home/me/test.h"

will not work. On the other hand

include "/test.h"

will work looking for the file in the current directory.

Unable to run Main class via Gradle

Just wanted to try out this project, but ran into some issues. After patching #40, I run into this:

$ ./gradlew run --args="--help"

> Task :compileJava
[...]
> Task :run FAILED
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NullPointerException
        at org.anarres.cpp.BuildMetadata.<init>(BuildMetadata.java:40)
        at org.anarres.cpp.BuildMetadata.getInstance(BuildMetadata.java:29)
        at org.anarres.cpp.Main.run(Main.java:61)
        at org.anarres.cpp.Main.main(Main.java:51)

FAILURE: Build failed with an exception.
[...]

It looks like it's failing to load /META-INF/jcpp.properties from the built resources. I don't see any file with that name, though.

Support extension of preprocessor

Hello there!

I thought about extending jcpp to support preprocessing of GLSL (OpenGL Shading Language). This would require to add new preprocessor directives such as "#version" and "#extension", which can be basically ignored in my case. I'm currently modifying the classes as I need them but you might be interested in adding the ability to just extend your preprocessor in a more reasonable fashion for such use cases. Such as adding hooks or just subclassing Preprocessor or something similar.

Regards
Holger

Empty variadic arguments

  1. Empty (missing) variadic arguments should be accepted.
  2. ``, ## VA_ARGS'' should remove the comma, if the variadic argument is empty.

Re-check multiline comment whitespace preservation

Hi!

When comments are not passed through (KEEPCOMMENTS or KEEPALLCOMMENTS
are not set), it is better to generate #line after /* ... */ or to pull
new lines form the comment to preserve in-to-out line
numbering correspondence. Of course, whoever wants the correspondence
has a way around: enable comments.

Thanks,
Igor

Line number mismatch after pop_source()

Hi Shevek,

In pop_source() you have:

return line_token(t.getLine() + 1, t.getName(), "2");

It seems like there should be no "+ 1" there since it creates
line number mismatch.

Thanks,
Igor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.