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

define inside struct fails #50

Closed
tmm1 opened this issue Sep 25, 2015 · 3 comments
Closed

define inside struct fails #50

tmm1 opened this issue Sep 25, 2015 · 3 comments

Comments

@tmm1
Copy link
Contributor

tmm1 commented Sep 25, 2015

struct test {
  int field;
#define SIZE 123
  int ary[SIZE];
}
c2nim/testsuite/tests/structdefine.h(3, 8) Error: identifier expected, but found '#'
@Araq
Copy link
Member

Araq commented Sep 29, 2015

Yes, that is not supported by c2nim's parser. It's not a bug. By design #define is parsed as a statement.

@Araq Araq closed this as completed Sep 29, 2015
@tmm1
Copy link
Contributor Author

tmm1 commented Sep 29, 2015

I started on a patch that produces the following for the example above:

type
  test* = object
    field*: cint
    const
      SIZE* = 123
    ary*: array[SIZE, cint]

Here's the patch for posterity:

diff --git a/cparse.nim b/cparse.nim
index 48c1305..8434e33 100644
--- a/cparse.nim
+++ b/cparse.nim
@@ -76,7 +76,9 @@ type

   ERetryParsing = object of Exception

+  SectionParser = proc(p: var Parser): PNode {.nimcall.}

+proc parseDir(p: var Parser; sectionParser: SectionParser): PNode
 proc addTypeDef(section, name, t, genericParams: PNode)
 proc parseStruct(p: var Parser, stmtList: PNode, isUnion: bool): PNode
 proc parseStructBody(p: var Parser, stmtList: PNode, isUnion: bool,
@@ -766,6 +768,10 @@ proc parseStructBody(p: var Parser, stmtList: PNode, isUnion: bool,
           addSon(result, def)
           getTok(p, nil)
           continue
+    elif p.tok.xkind == pxDirective or p.tok.xkind == pxDirectiveParLe:
+      var define = parseDir(p, statement)
+      addSon(result, define)
+      baseTyp = typeAtom(p)
     else:
       baseTyp = typeAtom(p)

diff --git a/cpp.nim b/cpp.nim
index 84f378d..c8bc90a 100644
--- a/cpp.nim
+++ b/cpp.nim
@@ -12,9 +12,6 @@
 const
   c2nimSymbol = "C2NIM"

-type
-  SectionParser = proc(p: var Parser): PNode {.nimcall.}
-
 proc eatNewLine(p: var Parser, n: PNode) =
   if p.tok.xkind == pxLineComment:
     skipCom(p, n)

I'm not going to pursue this, as making it actually compile would be a lot harder and it sounds like this is a WONTFIX.

@Araq Araq reopened this Sep 29, 2015
@Araq
Copy link
Member

Araq commented Sep 29, 2015

Nah, that's good. Moving the const out of the type is easy to do in a post-processing pass of the AST. We already have one, we can extend it or create a second one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants